rectif builtin cd : ajout du -- pour stopper les args, ajout des messages d'erreurs pour trops d'arguments ou home not set

This commit is contained in:
wescande 2017-03-03 20:13:55 +01:00
parent 4c44407b65
commit ed0c3803b0

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 11:57:53 by jhalford #+# #+# */ /* Created: 2016/12/03 11:57:53 by jhalford #+# #+# */
/* Updated: 2017/02/21 11:13:47 by ariard ### ########.fr */ /* Updated: 2017/03/03 20:12:45 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,6 +17,8 @@
#define HAS_CDOPT_P(x) (x & CD_OPT_P) #define HAS_CDOPT_P(x) (x & CD_OPT_P)
#define HAS_CDOPT_L(x) (x & CD_OPT_L) #define HAS_CDOPT_L(x) (x & CD_OPT_L)
#define CDERR_1 "{red}cd: no such file or directory: %s{eoc}\n" #define CDERR_1 "{red}cd: no such file or directory: %s{eoc}\n"
#define CDERR_2 "{red}cd: HOME not set{eoc}\n"
#define CDERR_3 "{red}cd: too many arguments{eoc}\n"
static char *builtin_cd_special(char *const av[], char *const env[]) static char *builtin_cd_special(char *const av[], char *const env[])
{ {
@ -25,7 +27,15 @@ static char *builtin_cd_special(char *const av[], char *const env[])
if (!*av) if (!*av)
{ {
if (!(target = ft_getenv((char**)env, "HOME"))) if (!(target = ft_getenv((char**)env, "HOME")))
{
ft_dprintf(2, CDERR_2);
return (NULL); return (NULL);
}
}
else if (*av && *(av + 1))
{
ft_dprintf(2, CDERR_3);
return (NULL);
} }
else if (ft_strcmp(*av, "-") == 0) else if (ft_strcmp(*av, "-") == 0)
target = ft_strdup(ft_getenv((char**)env, "OLDPWD")); target = ft_strdup(ft_getenv((char**)env, "OLDPWD"));
@ -38,10 +48,8 @@ static int builtin_cd_opts(char *const av[], int *opts)
{ {
int i; int i;
int j; int j;
int tmp_opts;
i = 1; i = 1;
tmp_opts = 0;
if (av) if (av)
while (av[i] && av[i][0] == '-' && av[i][1]) while (av[i] && av[i][0] == '-' && av[i][1])
{ {
@ -49,35 +57,37 @@ static int builtin_cd_opts(char *const av[], int *opts)
while (av[i][++j]) while (av[i][++j])
{ {
if (av[i][j] == 'P') if (av[i][j] == 'P')
tmp_opts |= CDOPT_P; *opts = CDOPT_P;
else if (av[i][j] == 'L') else if (av[i][j] == 'L')
tmp_opts |= CDOPT_L; *opts = CDOPT_L;
else if (av[i][j] == '-')
return (i + 1);
else else
return (i); return (i);
} }
*opts |= tmp_opts; ++i;
i++;
} }
return (i); return (i);
} }
int builtin_cd(const char *path, char *const av[], char *const envp[]) int builtin_cd(const char *path,
char *const av[], char *const envp[])
{ {
int i; int i;
int opts; int opts;
char *target; char *target;
char *cwd; char *cwd;
opts = 0; opts = CDOPT_L;
i = builtin_cd_opts(av, &opts); i = builtin_cd_opts(av, &opts);
if (!(target = builtin_cd_special(av + i, envp))) if (!(target = builtin_cd_special(av + i, envp)))
return (0); return (1);
cwd = getcwd(NULL, 0); cwd = getcwd(NULL, 0);
builtin_setenv(path, (char*[3]){"OLDPWD", cwd, NULL}, envp); builtin_setenv(path, (char*[3]){"OLDPWD", cwd, NULL}, envp);
free(cwd); free(cwd);
if (chdir(target)) if (chdir(target))
{ {
ft_printf(CDERR_1, target); ft_dprintf(2, CDERR_1, target);
return (1); return (1);
} }
else if (target != av[i]) else if (target != av[i])