correctif cd
This commit is contained in:
parent
67f2bfa812
commit
865bb87f0b
6 changed files with 12 additions and 89 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/03/25 03:18:18 by ariard #+# #+# */
|
/* Created: 2017/03/25 03:18:18 by ariard #+# #+# */
|
||||||
/* Updated: 2017/03/25 18:43:18 by ariard ### ########.fr */
|
/* Updated: 2017/03/25 20:13:39 by ariard ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -19,6 +19,8 @@ char *create_directory(const char *path, const char *old_pathnames)
|
||||||
char *temp;
|
char *temp;
|
||||||
char *semi;
|
char *semi;
|
||||||
|
|
||||||
|
if (!path || !old_pathnames)
|
||||||
|
return (NULL);
|
||||||
new_pathnames = ft_strdup(old_pathnames);
|
new_pathnames = ft_strdup(old_pathnames);
|
||||||
temp = new_pathnames;
|
temp = new_pathnames;
|
||||||
while (new_pathnames)
|
while (new_pathnames)
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/03/25 03:52:52 by ariard #+# #+# */
|
/* Created: 2017/03/25 03:52:52 by ariard #+# #+# */
|
||||||
/* Updated: 2017/03/25 18:38:59 by ariard ### ########.fr */
|
/* Updated: 2017/03/25 20:15:18 by ariard ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -17,7 +17,8 @@ char *bt_cd_get_cdpath(char *arg)
|
||||||
char *cdpath;
|
char *cdpath;
|
||||||
char *target;
|
char *target;
|
||||||
|
|
||||||
if (!(cdpath = ft_getenv(data_singleton()->env, "CDPATH")))
|
if (!(cdpath = ft_getenv(data_singleton()->env, "CDPATH"))
|
||||||
|
|| ft_strlen(cdpath) == 0)
|
||||||
{
|
{
|
||||||
if (!is_directory(target = ft_str3join(".", "/", arg)))
|
if (!is_directory(target = ft_str3join(".", "/", arg)))
|
||||||
ft_strdel(&target);
|
ft_strdel(&target);
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/03/25 03:54:22 by ariard #+# #+# */
|
/* Created: 2017/03/25 03:54:22 by ariard #+# #+# */
|
||||||
/* Updated: 2017/03/25 19:44:50 by ariard ### ########.fr */
|
/* Updated: 2017/03/25 20:03:28 by ariard ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -17,6 +17,7 @@
|
||||||
|
|
||||||
int bt_cd_process_dotdot(char *target)
|
int bt_cd_process_dotdot(char *target)
|
||||||
{
|
{
|
||||||
|
DG();
|
||||||
if (!is_directory(target))
|
if (!is_directory(target))
|
||||||
return (SH_ERR(CDERR_7, target));
|
return (SH_ERR(CDERR_7, target));
|
||||||
if (access(target, R_OK) < 0)
|
if (access(target, R_OK) < 0)
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/03/25 18:20:42 by ariard #+# #+# */
|
/* Created: 2017/03/25 18:20:42 by ariard #+# #+# */
|
||||||
/* Updated: 2017/03/25 19:45:23 by ariard ### ########.fr */
|
/* Updated: 2017/03/25 20:02:35 by ariard ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -62,7 +62,7 @@ static char *cd_operand_begin(char *arg)
|
||||||
target = bt_cd_get_cdpath(arg);
|
target = bt_cd_get_cdpath(arg);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
target = NULL;
|
target = arg;
|
||||||
return (target);
|
return (target);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -91,8 +91,6 @@ int builtin_cd(const char *path, char *const av[],
|
||||||
return (SH_ERR(CDERR_0) && SH_ERR(CD_USAGE));
|
return (SH_ERR(CDERR_0) && SH_ERR(CD_USAGE));
|
||||||
if (!(target = cd_operand_exist(*data.av_data)))
|
if (!(target = cd_operand_exist(*data.av_data)))
|
||||||
target = cd_operand_begin(*data.av_data);
|
target = cd_operand_begin(*data.av_data);
|
||||||
if (!target)
|
|
||||||
target = ft_strdup(*data.av_data);
|
|
||||||
oldpwd = getcwd(NULL, 0);
|
oldpwd = getcwd(NULL, 0);
|
||||||
DG("target is %s", target);
|
DG("target is %s", target);
|
||||||
if (HAS_CDOPT_P(data.flag) && !bt_cd_process_symlink(target))
|
if (HAS_CDOPT_P(data.flag) && !bt_cd_process_symlink(target))
|
||||||
|
|
@ -100,5 +98,6 @@ int builtin_cd(const char *path, char *const av[],
|
||||||
else if (!bt_cd_process_dotdot(target))
|
else if (!bt_cd_process_dotdot(target))
|
||||||
builtin_setenv(NULL, (char*[]){"cd", "OLDPWD", oldpwd, NULL}, NULL);
|
builtin_setenv(NULL, (char*[]){"cd", "OLDPWD", oldpwd, NULL}, NULL);
|
||||||
free(target);
|
free(target);
|
||||||
|
free(oldpwd);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/03/20 14:45:40 by gwojda #+# #+# */
|
/* Created: 2017/03/20 14:45:40 by gwojda #+# #+# */
|
||||||
/* Updated: 2017/03/25 18:20:07 by ariard ### ########.fr */
|
/* Updated: 2017/03/25 20:12:37 by ariard ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
|
||||||
80
42sh/trash
80
42sh/trash
|
|
@ -1,80 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* builtin_cd.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2016/12/03 11:57:53 by jhalford #+# #+# */
|
|
||||||
/* Updated: 2017/03/25 04:09:23 by ariard ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "minishell.h"
|
|
||||||
|
|
||||||
#define BT_CD_L (1 << 0)
|
|
||||||
#define BT_CD_P (1 << 1)
|
|
||||||
#define CD_USAGE "usage: cd [-L|-P] [dir]"
|
|
||||||
#define CDERR_1 "cd: no such file or directory: %s"
|
|
||||||
#define CDERR_2 "cd: %s not set"
|
|
||||||
#define CDERR_3 "cd: too many arguments"
|
|
||||||
|
|
||||||
static t_cliopts g_cdopts[] =
|
|
||||||
{
|
|
||||||
{'P', NULL, BT_CD_P, BT_CD_L, NULL, 0},
|
|
||||||
{'L', NULL, BT_CD_L, BT_CD_P, NULL, 0},
|
|
||||||
{0, NULL, 0, 0, NULL, 0},
|
|
||||||
};
|
|
||||||
|
|
||||||
static char *bt_cd_target(char *arg)
|
|
||||||
{
|
|
||||||
char *target;
|
|
||||||
|
|
||||||
if (!arg)
|
|
||||||
{
|
|
||||||
if (!(target = ft_getenv(data_singleton()->env, "HOME")))
|
|
||||||
SH_ERR(CDERR_2, "HOME");
|
|
||||||
}
|
|
||||||
else if (ft_strcmp(arg, "-") == 0)
|
|
||||||
{
|
|
||||||
DG("doing -");
|
|
||||||
if (!(target = ft_getenv(data_singleton()->env, "OLDPWD")))
|
|
||||||
SH_ERR(CDERR_2, "OLDPWD");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
target = arg;
|
|
||||||
return (target);
|
|
||||||
}
|
|
||||||
|
|
||||||
void setwd(char *var)
|
|
||||||
{
|
|
||||||
char *cwd;
|
|
||||||
|
|
||||||
cwd = getcwd(NULL, 0);
|
|
||||||
builtin_setenv(NULL, (char*[]){"cd", var, cwd, NULL}, NULL);
|
|
||||||
free(cwd);
|
|
||||||
}
|
|
||||||
|
|
||||||
int builtin_cd(const char *path,
|
|
||||||
char *const av[], char *const envp[])
|
|
||||||
{
|
|
||||||
char *target;
|
|
||||||
t_data_template data;
|
|
||||||
|
|
||||||
(void)path;
|
|
||||||
(void)envp;
|
|
||||||
data.flag = BT_CD_L;
|
|
||||||
if (cliopts_get((char**)av, g_cdopts, &data))
|
|
||||||
return (ft_perror("cd") && SH_ERR(CD_USAGE));
|
|
||||||
if (data.av_data[0] && data.av_data[1])
|
|
||||||
return (SH_ERR(CDERR_3) && SH_ERR(CD_USAGE));
|
|
||||||
if (!(target = bt_cd_target(*data.av_data)))
|
|
||||||
return (1);
|
|
||||||
setwd("OLDPWD");
|
|
||||||
if (chdir(target))
|
|
||||||
return (SH_ERR(CDERR_1, target));
|
|
||||||
else if (target != *data.av_data)
|
|
||||||
ft_printf("%s\n", target);
|
|
||||||
setwd("PWD");
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
Loading…
Reference in a new issue