Skip to content

Commit 118f325

Browse files
author
Dillon Phang
committed
Fixed cd when either or both PWD and OLDPWD doesn't exist
1 parent f6b4610 commit 118f325

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
lines changed

inc/minishell.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,6 @@ typedef struct s_parsestr
9595

9696
extern int g_sig_received;
9797

98-
t_dls *parse_token(char *input, t_minishell **mnsh);
99-
t_dls *ft_dlsnew(char *content, t_type type);
100-
void ft_dlsadd_back(t_dls **lst, t_dls *new);
101-
int execute(t_dls *tokens, char **envp);
102-
t_ast *parse_ast(t_dls *tokens, t_minishell **mnsh);
103-
int execute_ast(t_minishell **mnst, int *opipe);
10498
// ========================= built-ins ===================================
10599
// builtins_utils
106100
int ft_strcmp(const char *s1, const char *s2);
@@ -127,6 +121,7 @@ int export(char **cmd, t_minishell **mnsh);
127121
// pwd
128122
int pwd(void);
129123
// unset
124+
void rm_envp(t_minishell **mnsh, t_envp **to_rm);
130125
int unset(char **cmd, t_minishell **mnsh);
131126
// ========================= execution ===================================
132127
// excu
@@ -190,11 +185,17 @@ int execute_tokens(t_dls *tokens, t_minishell **mnsh);
190185
// parsing
191186
char **process_av(t_dls *tokens);
192187
char **parse_to_arg(t_dls *tokens);
188+
int execute_ast(t_minishell **mnst, int *opipe);
193189
// token utils
194190
t_dls *parse_token(char *input, t_minishell **mnsh);
195191
char **parse_to_arg(t_dls *tokens);
196192
// redir utils
197193
void fork_heredoc(int pid, int fd[2], char *delim,
198194
t_minishell **mnsh);
195+
// dls
196+
t_dls *ft_dlsnew(char *content, t_type type);
197+
void ft_dlsadd_back(t_dls **lst, t_dls *new);
198+
// ast
199+
t_ast *parse_ast(t_dls *tokens, t_minishell **mnsh);
199200

200201
#endif

src/builtins/cd.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,12 @@ void replace_prewd(t_envp **wd, t_envp **old_wd)
4343
{
4444
char *new_wd;
4545

46-
free((*old_wd)->content);
47-
(*old_wd)->content = ft_strpathdup("OLDPWD=", (*wd)->content + 5);
48-
free((*wd)->content);
46+
if (old_wd)
47+
{
48+
free((*old_wd)->content);
49+
(*old_wd)->content = ft_strpathdup("OLDPWD=", (*wd)->content + 4);
50+
free((*wd)->content);
51+
}
4952
new_wd = (char *)malloc(PATH_MAX * sizeof(char));
5053
getcwd(new_wd, PATH_MAX);
5154
(*wd)->content = ft_strpathdup("PWD=", new_wd);
@@ -71,11 +74,10 @@ void update_wd(t_minishell **mnsh)
7174
}
7275
if (wd && old_wd)
7376
replace_prewd(&wd, &old_wd);
74-
else
75-
{
76-
ft_putstr_fd("Error: PWD or OLDPWD not found in envp\n", 2);
77-
(*mnsh)->exit_code = 1;
78-
}
77+
else if (wd && !old_wd)
78+
replace_prewd(&wd, &old_wd);
79+
else if (!wd && old_wd)
80+
rm_envp(mnsh, &old_wd);
7981
}
8082

8183
int cd(char **cmd, t_minishell **mnsh)

src/builtins/exit.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ void exit_child(t_minishell **mnsh, int exit_status)
5656
(*mnsh)->exit_code = WEXITSTATUS(exit_status);
5757
if (WIFSIGNALED(exit_status))
5858
{
59-
if (WTERMSIG(exit_status) == SIGINT)
59+
if (WTERMSIG(exit_status) == SIGINT
60+
|| WTERMSIG(exit_status) == SIGQUIT)
6061
{
6162
g_sig_received = 1;
6263
printf("\n");

src/lst/dls.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
t_dls *ft_dlsnew(char *content, t_type type)
1616
{
1717
t_dls *node;
18+
1819
if (!content)
1920
return (NULL);
2021
node = malloc(sizeof(t_dls));

0 commit comments

Comments
 (0)