new cd to finish

This commit is contained in:
Antoine Riard 2017-03-25 04:08:25 +01:00
parent d19de18e07
commit d027576bbf
13 changed files with 156 additions and 40 deletions

View file

@ -1,23 +0,0 @@
$TOKEN201703241737_NAME
21sh_error
42ShellTester
42sh
42sh_error
Makefile
README.md
STDBUG
VAR
auteur
donovan_segaults_06-02
file
includes
libft
minishell_error
new_file
objs
pdf
sample
scriptheader.sh
src
update_makefile.sh
write_on_stdout_and_stderr

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 22:59:57 by jhalford #+# #+# */
/* Updated: 2017/03/24 23:17:32 by jhalford ### ########.fr */
/* Updated: 2017/03/25 04:05:40 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -51,6 +51,11 @@ int builtin_math(const char *path, char *const av[], char *const envp[]);
int builtin_func(const char *path, char *const av[], char *const envp[]);
int bt_env_geti(char ***av, t_env_data *data);
int get_cdpath(char *arg);
int process_dotdot(char *target);
int process_symlink(char *target);
int error_msg(char *msg);
#endif

View file

@ -192,6 +192,7 @@ str/ft_strstr.c\
str/ft_strsub.c\
str/ft_strtok.c\
str/ft_strtrim.c\
sys/create_directory.c\
sys/dup2_close.c\
sys/fd_replace.c\
sys/ft_getenv.c\

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 17:24:23 by jhalford #+# #+# */
/* Updated: 2017/03/25 01:42:01 by jhalford ### ########.fr */
/* Updated: 2017/03/25 04:07:20 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -35,6 +35,7 @@ char *ft_getenv(char **env, char *key);
int open_access(char *file, t_flag a_flag, t_flag o_flag, t_flag o_perm);
int is_directory(const char *path);
char *create_direcotry(const char *path, const char *old_pathnames);
int dup2_close(int fd1, int fd2);
int fd_replace(int fd1, int fd2);

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* create_directory.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/25 03:18:18 by ariard #+# #+# */
/* Updated: 2017/03/25 04:07:10 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *create_directory(const char *path, const char *old_pathnames)
{
char *new_pathnames;
char *newdir;
char *semi;
new_pathnames = ft_strdup(old_pathnames);
temp = new_pathnames;
while (new_pathnames)
{
if ((semi = ft_strchr(new_pathnames, ":")))
*semi = 0;
if (!is_directory(newdir = ft_str3join(new_pathnames, "/", path)))
ft_strdel(&newdir);
else
break;
new_pathnames += new_pathnames + 1;
}
ft_strdel(temp);
return (newdir);
}

View file

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* bt_cd_getpath.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/25 03:52:52 by ariard #+# #+# */
/* Updated: 2017/03/25 04:05:06 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int get_cdpath(char *arg)
{
char *cdpath;
char *target;
if (!(cdpath = ft_getenv(data_singleton()->env, "CDPATH")))
{
if (!is_directory(target = ft_str3join(".", "/", arg)))
ft_strdel(&target);
}
else
target = create_directory(arg, cdpath);
return (target);
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* bt_cd_process_dotdot.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/25 03:54:22 by ariard #+# #+# */
/* Updated: 2017/03/25 04:01:03 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int process_dotdot(char *target)
{
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 11:57:53 by jhalford #+# #+# */
/* Updated: 2017/03/25 00:52:32 by jhalford ### ########.fr */
/* Updated: 2017/03/25 02:05:50 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -40,7 +40,6 @@ static char *bt_cd_target(char *arg)
DG("doing -");
if (!(target = ft_getenv(data_singleton()->env, "OLDPWD")))
SH_ERR(CDERR_2, "OLDPWD");
DG("found OLDPWD %s", target);
}
else
target = arg;

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/25 02:00:40 by ariard #+# #+# */
/* Updated: 2017/03/25 02:04:47 by ariard ### ########.fr */
/* Updated: 2017/03/25 04:05:49 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,14 +16,61 @@
#define CD_OPT_P (1 << 1)
#define HAS_CDOPT_P(x) (x & CD_OPT_P)
#define HAS_CDOPT_L(x) (x & CD_OPT_L)
#define CDERR_0 "cd: too many arguments"
#define CDERR_1 "cd %s not set"
static t_cliopts g_cdopts[] =
{
{'P', NULL, CD_OPT_P, CD_OPT_L, NULL},
{'L', NULL, CD_OPT_L, CD_OPT_P, NULL},
{0, NULL, 0, 0, NULL},
{'P', NULL, CD_OPT_P, CD_OPT_L, NULL, 0},
{'L', NULL, CD_OPT_L, CD_OPT_P, NULL, 0},
{0, NULL, 0, 0, NULL, 0},
};
static char *cd_operand_exist(char *arg)
{
char *target;
if (!arg)
{
if (!(target = ft_getenv(data_singleton()->env, "HOME")))
SH_ERR(CDERR_1, "HOME");
}
else
target = arg;
return (target);
}
static char *cd_operand_begin(char *arg)
{
char *target;
if (arg && arg[0])
{
if (arg[0] = '/')
target = arg;
else if (arg[0] = '.')
target = ft_str3join(ft_getenv(&data_singleton()->env,
"PWD"), "/", arg);
else if (ft_strcmp(arg, "-") == 0)
{
if (!(target = ft_getenv(data_singleton()->env, "OLDPWD")))
SH_ERR(CDERR_2, "OLDPWD");
}
else
target = get_cdpath(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 av[], char *const envp[])
{
@ -35,4 +82,17 @@ int builtin_cd(const char *path, char *const av[],
data.flag = CD_OPT_L;
if (cliopts_get((char **av, g_cdopts, &data))
return (1);
if (data.av_data[0] && data.av_data[1])
return (SH_ERR(CDERR_0) && SH_ERR(CD_USAGE));
if (!(target = cd_operand_exist(*data.av_data)))
return (1);
setwd("OLDPWD");
if (!target)
target = cd_operand_begin(*data.av_data);
if (HAS_CDOPT_P(data.flag))
process_symlink(target);
else
process_dotdot(target);
setwd("PWD");
return (0);
}

View file

@ -1,7 +0,0 @@
#include "stdio.h"
int main(void)
{
printf("hello");
return (0);
}

View file

@ -1 +0,0 @@
father/son/litte_son