Minishell 1.0
42 School Minishell Project - A simple shell implementation
Yüklüyor...
Arıyor...
Eşleşme Yok
builtin_cd_utils.c
Bu dosyanın dokümantasyonuna git.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* builtin_cd_utils.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: hgenc <hgenc@student.42kocaeli.com.tr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2026/03/09 18:10:00 by hgenc #+# #+# */
9/* Updated: 2026/03/09 18:10:00 by hgenc ### ########.fr */
10/* */
11/* ************************************************************************** */
12
14#include <stdlib.h>
15#include <unistd.h>
16
17static char *build_path(char **stack, int top)
18{
19 char *result;
20 char *tmp;
21 int i;
22
23 result = ft_strdup("");
24 i = 0;
25 while (i < top && result)
26 {
27 tmp = ft_strjoin(result, "/");
28 free(result);
29 if (!tmp)
30 result = ft_strjoin("", stack[i]);
31 else
32 result = ft_strjoin(tmp, stack[i]);
33 free(tmp);
34 i++;
35 }
36 if (!result || !result[0])
37 {
38 free(result);
39 return (ft_strdup("/"));
40 }
41 return (result);
42}
43
44char *normalize_logical_path(char *path)
45{
46 char **segs;
47 char *stack[512];
48 int top;
49 int i;
50 char *result;
51
52 segs = ft_split(path, '/');
53 if (!segs)
54 return (NULL);
55 top = 0;
56 i = 0;
57 while (segs[i])
58 {
59 if (ft_strcmp(segs[i], "..") == 0)
60 {
61 if (top > 0)
62 top--;
63 }
64 else if (segs[i][0] && ft_strcmp(segs[i], ".") != 0)
65 stack[top++] = segs[i];
66 i++;
67 }
68 result = build_path(stack, top);
69 free_str_array(segs);
70 return (result);
71}
72
73char *compute_logical_pwd(t_shell *shell, char *arg)
74{
75 char *base;
76 char *joined;
77 char *result;
78
79 if (!arg)
80 return (NULL);
81 if (arg[0] == '/')
82 return (normalize_logical_path(arg));
83 base = get_env_value(shell->env_list, "PWD");
84 if (!base)
85 base = "";
86 joined = ft_strjoin(base, "/");
87 if (!joined)
88 return (NULL);
89 result = ft_strjoin(joined, arg);
90 free(joined);
91 if (!result)
92 return (NULL);
93 joined = normalize_logical_path(result);
94 free(result);
95 return (joined);
96}
97
98void update_pwd_vars(t_shell *shell, char *old_pwd, char *new_pwd)
99{
100 char *old_dup;
101
102 if (!old_pwd)
103 old_dup = ft_strdup("");
104 else
105 old_dup = ft_strdup(old_pwd);
106 update_env_value(shell->env_list, "PWD", new_pwd);
107 update_env_value(shell->env_list, "OLDPWD", old_dup);
108 free(old_dup);
109}
110
111int change_dir(t_shell *shell, char *new_pwd)
112{
113 char *old_pwd;
114
115 old_pwd = get_env_value(shell->env_list, "PWD");
116 update_pwd_vars(shell, old_pwd, new_pwd);
117 free(new_pwd);
118 return (0);
119}
void update_pwd_vars(t_shell *shell, char *old_pwd, char *new_pwd)
int change_dir(t_shell *shell, char *new_pwd)
static char * build_path(char **stack, int top)
char * compute_logical_pwd(t_shell *shell, char *arg)
char * normalize_logical_path(char *path)
void free_str_array(char **arr)
NULL-terminated string dizisini serbest bırakır.
Definition cleanup.c:30
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
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
Minishell ana header dosyası
struct s_shell t_shell
t_list * env_list
Definition minishell.h:149
int ft_strcmp(const char *s1, const char *s2)
Definition utils.c:48