Skip to content

Commit

Permalink
cwd buffer changed to cwd malloc
Browse files Browse the repository at this point in the history
  • Loading branch information
ggjulio committed Sep 16, 2020
1 parent d264c12 commit 2a5f7ec
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
4 changes: 2 additions & 2 deletions includes/shell.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: juligonz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/08/15 17:54:31 by juligonz #+# #+# */
/* Updated: 2020/09/14 18:08:37 by juligonz ### ########.fr */
/* Updated: 2020/09/16 16:31:03 by juligonz ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -44,7 +44,7 @@ typedef struct s_shell
{
char *name;
t_environment env;
char cwd[PATH_MAX + 3];
char *cwd;
int status;
int running;
} t_shell;
Expand Down
12 changes: 8 additions & 4 deletions srcs/builtins/internal_builtins/cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: juligonz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/08/23 13:40:40 by hwinston #+# #+# */
/* Updated: 2020/09/11 02:14:14 by juligonz ### ########.fr */
/* Updated: 2020/09/16 16:31:28 by juligonz ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -25,13 +25,17 @@ static int has_only_slashes(char *str)

static void do_cd(char *path)
{
char *tmp;

set_environment_variable_value("OLDPWD", g_sh.cwd);
getcwd(g_sh.cwd, sizeof(g_sh.cwd));
free(g_sh.cwd);
g_sh.cwd = getcwd(NULL, 0);
if ((!strncmp(path, "//", 2) && !has_only_slashes(path))
|| !strcmp(path, "//"))
{
ft_memmove(g_sh.cwd + 1, g_sh.cwd, ft_strlen(g_sh.cwd) + 1);
g_sh.cwd[0] = '/';
ft_asprintf(&tmp, "/%s", g_sh.cwd);
free(g_sh.cwd);
g_sh.cwd = tmp;
}
set_environment_variable_value("PWD", g_sh.cwd);
}
Expand Down
4 changes: 2 additions & 2 deletions srcs/environment/environment_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: juligonz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/08/19 15:10:55 by juligonz #+# #+# */
/* Updated: 2020/09/14 17:06:54 by juligonz ### ########.fr */
/* Updated: 2020/09/14 21:24:12 by juligonz ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -51,7 +51,7 @@ const char *get_environment_variable_value(char *name)
if (!variable)
return (NULL);
value = ft_strchr(variable, '=');
if (*value == '=')
if (*value == '=' && value[1])
return (value + 1);
return (NULL);
}
Expand Down
5 changes: 3 additions & 2 deletions srcs/shell/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: juligonz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/08/15 17:52:21 by juligonz #+# #+# */
/* Updated: 2020/09/09 02:30:53 by juligonz ### ########.fr */
/* Updated: 2020/09/16 16:19:05 by juligonz ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -24,7 +24,7 @@ t_shell create_shell(const char *name, t_environment envp)
result.running = 42;
signal(SIGINT, sigint_handler);
signal(SIGQUIT, sigquit_handler);
getcwd(result.cwd, sizeof(result.cwd));
result.cwd = getcwd(NULL, 0);
return (result);
}

Expand All @@ -42,6 +42,7 @@ void destroy_shell(t_shell to_destroy)
{
free_environment(to_destroy.env);
free(to_destroy.name);
free(to_destroy.cwd);
}

void free_shell(t_shell *to_free)
Expand Down

0 comments on commit 2a5f7ec

Please sign in to comment.