Minishell 1.0
42 School Minishell Project - A simple shell implementation
Yüklüyor...
Arıyor...
Eşleşme Yok
env_initialization.c
Bu dosyanın dokümantasyonuna git.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* env_initialization.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: bkinali <bkinali@student.42.fr> #+# +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2025-11-19 13:08:31 by bkinali #+# #+# */
9/* Updated: 2025-11-19 13:08:31 by bkinali ### ########.tr */
10/* */
11/* ************************************************************************** */
12
13/**
14 * @file env_initialization.c
15 * @brief Ortam değişkenleri başlatma ve yönetimi
16 */
17
19#include <stdlib.h>
20#include <unistd.h>
21
22/**
23 * @brief Ortam değişkeni node'unu serbest bırakır
24 * @param content t_env yapısı (void* olarak)
25 */
26void free_env_node(void *content)
27{
28 t_env *env;
29
30 if (!content)
31 return ;
32 env = (t_env *)content;
33 if (env->key)
34 free(env->key);
35 if (env->value)
36 free(env->value);
37 free(env);
38}
39
40/**
41 * @brief Ortam değişkeni içeriği oluşturur
42 * @param key Değişken adı
43 * @param value Değişken değeri
44 * @return t_env* Yeni oluşturulan yapı veya NULL
45 */
46t_env *create_env_content(char *key, char *value)
47{
48 t_env *content;
49
50 content = ft_calloc(1, sizeof(t_env));
51 if (!content)
52 return (NULL);
53 content->key = ft_strdup(key);
54 if (!content->key)
55 {
56 free_env_node(content);
57 return (NULL);
58 }
59 if (!value)
60 content->value = NULL;
61 else
62 {
63 content->value = ft_strdup(value);
64 if (!content->value)
65 {
66 free_env_node(content);
67 return (NULL);
68 }
69 }
70 return (content);
71}
72
73/**
74 * @brief Ortam değişkeni node'u listeye ekler
75 */
76int append_env_node(t_list **env_list, char *key, char *value)
77{
78 t_list *node;
79 t_env *content;
80
81 content = create_env_content(key, value);
82 if (!content)
83 return (0);
84 node = ft_lstnew(content);
85 if (!node)
86 {
87 free_env_node(content);
88 return (0);
89 }
90 ft_lstadd_back(env_list, node);
91 return (1);
92}
93
94/**
95 * @brief SHLVL değişkenini günceller (iç içe shell için)
96 * @param shell Shell yapısı
97 */
98static void update_shlvl(t_shell *shell)
99{
100 t_env *env;
101 int level;
102 char *new_level;
103
104 env = find_env_by_key(shell->env_list, "SHLVL");
105 if (!env)
106 return ;
107 level = 1;
108 if (env->value)
109 level = ft_atoi(env->value) + 1;
110 if (level < 1)
111 level = 1;
112 new_level = ft_itoa(level);
113 if (new_level)
114 {
115 free(env->value);
116 env->value = new_level;
117 }
118}
119
120/**
121 * @brief Varsayılan ortam değişkenlerini oluşturur (envp boşsa)
122 */
124{
125 char *pwd;
126
127 pwd = getcwd(NULL, 0);
128 if (!pwd)
129 return (-1);
130 if (!append_env_node(&shell->env_list, "PWD", pwd))
131 {
132 free(pwd);
133 return (-1);
134 }
135 free(pwd);
136 if (!append_env_node(&shell->env_list, "SHLVL", "1"))
137 return (-1);
138 if (!append_env_node(&shell->env_list, "_", "./minishell"))
139 return (-1);
140 return (0);
141}
142
143/**
144 * @brief Ortam değişkenleri listesini başlatır
145 * @param shell Shell yapısı
146 * @return int Başarı: 0, Hata: -1
147 */
149{
150 if (!shell->envp || !shell->envp[0])
151 {
152 if (init_default_env(shell) == -1)
153 {
154 ft_lstclear(&shell->env_list, &free_env_node);
155 return (-1);
156 }
157 return (0);
158 }
159 if (!parse_env_variables(shell))
160 return (-1);
161 update_shlvl(shell);
162 return (0);
163}
int init_default_env(t_shell *shell)
Varsayılan ortam değişkenlerini oluşturur (envp boşsa).
int append_env_node(t_list **env_list, char *key, char *value)
Ortam değişkeni node'u listeye ekler.
int init_env_list(t_shell *shell)
Ortam değişkenleri listesini başlatır.
void free_env_node(void *content)
Ortam değişkeni node'unu serbest bırakır.
t_env * create_env_content(char *key, char *value)
Ortam değişkeni içeriği oluşturur.
static void update_shlvl(t_shell *shell)
SHLVL değişkenini günceller (iç içe shell için).
int parse_env_variables(t_shell *shell)
Tüm ortam değişkenlerini (envp) ayrıştırarak listeye ekler.
Definition env_parser.c:60
t_env * find_env_by_key(t_list *env_list, char *key)
Env listesinde key'e göre arama yapar.
Definition env_utils.c:29
Minishell ana header dosyası
struct s_env t_env
-----> EXPANDER <--—
struct s_shell t_shell
char * key
Definition minishell.h:94
char * value
Definition minishell.h:95
char ** envp
Definition minishell.h:151
t_list * env_list
Definition minishell.h:149