Minishell 1.0
42 School Minishell Project - A simple shell implementation
Yüklüyor...
Arıyor...
Eşleşme Yok
env_utils.c
Bu dosyanın dokümantasyonuna git.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* env_utils.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: hgenc <hgenc@student.42kocaeli.com.tr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2026/02/16 20:30:00 by hgenc #+# #+# */
9/* Updated: 2026/02/16 20:30:00 by hgenc ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13/**
14 * @file env_utils.c
15 * @brief Ortam değişkenleri yardımcı fonksiyonları
16 *
17 * Env yapısı üzerinde güncelleme ve dönüştürme işlemleri.
18 */
19
21#include <stdlib.h>
22
23/**
24 * @brief Env listesinde key'e göre arama yapar
25 * @param env_list Ortam değişkenleri listesi
26 * @param key Aranacak key
27 * @return Bulunan t_env pointer, bulunamazsa NULL
28 */
29t_env *find_env_by_key(t_list *env_list, char *key)
30{
31 t_list *curr;
32 t_env *env;
33
34 curr = env_list;
35 while (curr)
36 {
37 env = (t_env *)curr->content;
38 if (ft_strcmp(env->key, key) == 0)
39 return (env);
40 curr = curr->next;
41 }
42 return (NULL);
43}
44
45/**
46 * @brief Mevcut env değişkeninin değerini günceller
47 * @param env_list Ortam değişkenleri listesi
48 * @param key Güncellenecek değişken adı
49 * @param new_value Yeni değer (NULL olabilir, strdup yapılır)
50 * @return 0 = başarılı, 1 = bulunamadı
51 */
52int update_env_value(t_list *env_list, char *key, char *new_value)
53{
54 t_env *env;
55
56 env = find_env_by_key(env_list, key);
57 if (!env)
58 return (1);
59 free(env->value);
60 if (new_value)
61 env->value = ft_strdup(new_value);
62 else
63 env->value = NULL;
64 return (0);
65}
66
67/**
68 * @brief Environment listesinde key'e göre değer döndürür
69 * @param env_list Ortam değişkenleri listesi
70 * @param key Aranacak key
71 * @return char* Bulunursa değerin orijinal pointer'ı, bulunamazsa NULL
72 */
73char *get_env_value(t_list *env_list, char *key)
74{
75 t_env *env;
76
77 env = find_env_by_key(env_list, key);
78 if (!env)
79 return (NULL);
80 return (env->value);
81}
82
83/**
84 * @brief t_env yapısını "KEY=VALUE" formatına çevir
85 * @param env Environment node
86 * @return char* malloc'lanmış string
87 */
88char *env_to_str(t_env *env)
89{
90 char *key_eq;
91 char *result;
92
93 if (!env || !env->key)
94 return (NULL);
95 key_eq = ft_strjoin(env->key, "=");
96 if (!key_eq)
97 return (NULL);
98 if (!env->value)
99 result = ft_strdup(key_eq);
100 else
101 result = ft_strjoin(key_eq, env->value);
102 free(key_eq);
103 return (result);
104}
105
106/**
107 * @brief shell->env_list'i char **envp formatına çevir
108 * @param shell Shell structure
109 * @return char** NULL-terminated array
110 */
111char **create_envp(t_shell *shell)
112{
113 int size;
114 char **envp;
115 t_list *current;
116 int i;
117
118 if (!shell || !shell->env_list)
119 return (NULL);
120 size = ft_lstsize(shell->env_list);
121 envp = (char **)malloc(sizeof(char *) * (size + 1));
122 if (!envp)
123 return (NULL);
124 current = shell->env_list;
125 i = 0;
126 while (current)
127 {
128 envp[i] = env_to_str((t_env *)current->content);
129 if (!envp[i])
130 {
131 envp[i] = NULL;
132 free_str_array(envp);
133 return (NULL);
134 }
135 current = current->next;
136 i++;
137 }
138 envp[i] = NULL;
139 return (envp);
140}
void free_str_array(char **arr)
NULL-terminated string dizisini serbest bırakır.
Definition cleanup.c:30
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
char * get_env_value(t_list *env_list, char *key)
Environment listesinde key'e göre değer döndürür.
Definition env_utils.c:73
char * env_to_str(t_env *env)
t_env yapısını "KEY=VALUE" formatına çevir
Definition env_utils.c:88
int update_env_value(t_list *env_list, char *key, char *new_value)
Mevcut env değişkeninin değerini günceller.
Definition env_utils.c:52
char ** create_envp(t_shell *shell)
shell->env_list'i char **envp formatına çevir
Definition env_utils.c:111
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
t_list * env_list
Definition minishell.h:149
int ft_strcmp(const char *s1, const char *s2)
Definition utils.c:48