Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
vmarchaud committed Mar 18, 2016
1 parent afc86cc commit c36fb6e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
1 change: 1 addition & 0 deletions minishell/auteur
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vmarchau
28 changes: 18 additions & 10 deletions minishell/src/builtin_cd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: vmarchau <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/02/22 13:24:57 by vmarchau #+# #+# */
/* Updated: 2016/03/14 13:49:22 by vmarchau ### ########.fr */
/* Updated: 2016/03/18 13:33:18 by vmarchau ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -18,16 +18,20 @@ int verify_access(char *path)

if ((stat = malloc(sizeof(struct stat))) == NULL)
return (0);
if (access(path, F_OK) != 0)
ft_putendl_fd("cd: directory cant be accessed", 2);
else if (lstat(path, stat) < 0)
if (lstat(path, stat) < 0)
ft_putendl_fd("cd: permission denied", 2);
else if (!S_ISDIR(stat->st_mode) && !S_ISLNK(stat->st_mode))
ft_putendl_fd("cd: cannot cd in this type", 2);
else if (access(path, F_OK) != 0)
ft_putendl_fd("cd: directory cant be accessed", 2);
else if (access(path, X_OK) != 0)
ft_putendl_fd("cd: permission denied", 2);
else
{
free(stat);
return (1);
}
free(stat);
return (0);
}

Expand All @@ -38,28 +42,32 @@ void builtin_cd_here(t_global *gbl, char *path)
if ((tab = malloc(sizeof(char*) * 3)) == NULL)
return ;
tab[0] = ft_strdup("cd");
tab[1] = path;
tab[1] = ft_strdup(path);
tab[2] = NULL;
builtin_cd(gbl, 2, tab);
free(tab[1]);
free(tab[0]);
free(tab);
}

void builtin_cd(t_global *gbl, int size, char **args)
{
t_env *entry;
char *del;

if (size >= 3)
return (ft_putendl_fd("cd: too much args", 2));
if (size == 1)
args[1] = ft_strdup("~");
entry = find_entry(gbl, "HOME");
if (ft_strchr(args[1], '~') && entry != NULL)
args[1] = strreplace_once(args[1], "~", entry->value);
if (size == 1 && entry != NULL)
return (builtin_cd_here(gbl, entry->value));
if (!verify_access(args[1]))
return ;
entry = find_entry(gbl, "PWD");
if (entry != NULL)
put_entry(gbl, "OLDPWD", entry->value);
chdir(args[1]);
put_entry(gbl, "PWD", getcwd(NULL, 0));
del = getcwd(NULL, 0);
put_entry(gbl, "PWD", del);
free(del);
update_tabenv(gbl);
}
9 changes: 7 additions & 2 deletions minishell/src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: vmarchau <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/02/15 15:06:11 by vmarchau #+# #+# */
/* Updated: 2016/03/14 14:40:30 by vmarchau ### ########.fr */
/* Updated: 2016/03/18 12:49:00 by vmarchau ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -37,12 +37,17 @@ char *strjoins(char *first, char *second, char *third)
void update_shell_lvl(t_global *gbl)
{
t_env *env;
char *tmp;

env = find_entry(gbl, "SHLVL");
if (env == NULL || !env->value)
if (env == NULL)
gbl->env = put_entry(gbl, ft_strdup("SHLVL"), ft_strdup("1"));
else
{
tmp = env->value;
env->value = ft_itoa(ft_atoi(env->value) + 1);
free(tmp);
}
}

int contains_char(char *str)
Expand Down

0 comments on commit c36fb6e

Please sign in to comment.