This commit is contained in:
Jack Halford 2017-03-25 20:26:51 +01:00
commit 75d9218bec
9 changed files with 41 additions and 163 deletions

View file

@ -6,7 +6,7 @@
# By: wescande <wescande@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2016/08/29 21:32:58 by wescande #+# #+# #
# Updated: 2017/03/25 20:15:36 by jhalford ### ########.fr #
# Updated: 2017/03/25 20:26:47 by jhalford ### ########.fr #
# #
# **************************************************************************** #
@ -14,6 +14,7 @@ NAME = 42sh
CC = gcc
FLAGS = -Wall -Wextra -Werror
FLAGS = -Wall -Wextra -Werror #-fsanitize=address#-fvisibility=hidden
D_FLAGS = -g
DELTA = $$(echo "$$(tput cols)-47"|bc)

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/25 03:18:18 by ariard #+# #+# */
/* Updated: 2017/03/25 15:12:34 by jhalford ### ########.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 *semi;
if (!path || !old_pathnames)
return (NULL);
new_pathnames = ft_strdup(old_pathnames);
temp = new_pathnames;
while (new_pathnames)

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/25 03:52:52 by ariard #+# #+# */
/* Updated: 2017/03/25 16:49:38 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 *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)))
ft_strdel(&target);

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/25 03:54:22 by ariard #+# #+# */
/* Updated: 2017/03/25 17:03:26 by ariard ### ########.fr */
/* Updated: 2017/03/25 20:03:28 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,54 +15,15 @@
#define CDERR_6 "cd : %s: Permission denied"
#define CDERR_7 "cd : %s: No such file or directory"
static int getsup(char *buf)
{
int len;
len = ft_strlen(buf);
while (len)
{
if (buf[len] == '/')
{
while (buf[len] == '/')
len--;
ft_strcat(buf, ft_strrchr(&buf[len], '/'));
break;
}
len--;
}
return (0);
}
int bt_cd_process_dotdot(char *target)
{
char *pwd;
char *buf;
pwd = getcwd(NULL, 0);
buf = ft_strnew(PATH_MAX + 1);
if (target[0] == '.' && target[1] == '/')
ft_strcat(buf, ft_strrchr(pwd, '/'));
if (target[0] == '.' && target[1] == '.' && target[2] == '/')
getsup(buf);
target += ft_strlenchr(target, '/');
while (target)
{
if (*target == '/' && *target++)
ft_strcat(buf, "/");
while (*target == '/')
target++;
if (target[0] == '.' && target[1] != '.')
ft_strcat(buf, ft_strrchr(target, '/'));
else if (target[0] == '.' && target[1] == '.')
getsup(buf);
else
ft_strncat(buf, target, ft_strlenchr(target, '/'));
target += ft_strlenchr(target, '/');
}
if (!access(target, R_OK))
DG();
if (!is_directory(target))
return (SH_ERR(CDERR_7, target));
if (access(target, R_OK) < 0)
return (SH_ERR(CDERR_6, target));
if (chdir(target))
return (SH_ERR(CDERR_7, target));
setwd("PWD");
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/25 15:14:55 by ariard #+# #+# */
/* Updated: 2017/03/25 17:07:16 by ariard ### ########.fr */
/* Updated: 2017/03/25 19:51:03 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,6 +21,8 @@ int bt_cd_process_symlink(char *target)
char buffer[PATH_MAX + 1];
ft_bzero(buffer, PATH_MAX + 1);
if (!is_directory(target))
return (SH_ERR(CDERR_4, target));
if (!access(target, R_OK))
return (SH_ERR(CDERR_3, target));
if (chdir(target))

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin_new_cd.c :+: :+: :+: */
/* builtin_cd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/25 02:00:40 by ariard #+# #+# */
/* Updated: 2017/03/25 17:08:31 by ariard ### ########.fr */
/* Created: 2017/03/25 18:20:42 by ariard #+# #+# */
/* Updated: 2017/03/25 20:02:35 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -27,17 +27,16 @@ static t_cliopts g_cdopts[] =
{0, NULL, 0, 0, NULL, 0},
};
static char *cd_operand_exist(char *arg)
static char *cd_operand_exist(char *arg)
{
char *target;
target = NULL;
if (!arg)
{
if (!(target = ft_getenv(data_singleton()->env, "HOME")))
SH_ERR(CDERR_0, "HOME");
if (!(target = ft_strdup(ft_getenv(data_singleton()->env, "HOME"))))
SH_ERR(CDERR_1, "HOME");
}
else
target = arg;
return (target);
}
@ -48,13 +47,15 @@ static char *cd_operand_begin(char *arg)
if (arg && arg[0])
{
if (arg[0] == '/')
target = arg;
else if (arg[0] == '.')
target = ft_strdup(arg);
else if (!ft_strncmp(arg, "./", 2) || !ft_strncmp(arg, "../", 3)
|| !ft_strcmp(arg, ".") || !ft_strcmp(arg, ".."))
target = ft_str3join(ft_getenv(data_singleton()->env,
"PWD"), "/", arg);
else if (ft_strcmp(arg, "-") == 0)
else if (!ft_strcmp(arg, "-"))
{
if (!(target = ft_getenv(data_singleton()->env, "OLDPWD")))
if (!(target = ft_strdup(ft_getenv(data_singleton()->env,
"OLDPWD"))))
SH_ERR(CDERR_1, "OLDPWD");
}
else
@ -77,29 +78,26 @@ void setwd(char *var)
int builtin_cd(const char *path, char *const av[],
char *const envp[])
{
char *oldpwd;
char *target;
t_data_template data;
(void)envp;
(void)path;
data.flag = CD_OPT_L;
DG();
if (cliopts_get((char **)av, g_cdopts, &data))
return (1);
DG("after parsing opt");
if (data.av_data[0] && data.av_data[1])
return (SH_ERR(CD_USAGE));
return (SH_ERR(CDERR_0) && SH_ERR(CD_USAGE));
if (!(target = cd_operand_exist(*data.av_data)))
return (1);
DG();
return (0);
setwd("OLDPWD");
if (!target)
target = cd_operand_begin(*data.av_data);
if (HAS_CDOPT_P(data.flag))
bt_cd_process_symlink(target);
else
bt_cd_process_dotdot(target);
oldpwd = getcwd(NULL, 0);
DG("target is %s", target);
if (HAS_CDOPT_P(data.flag) && !bt_cd_process_symlink(target))
builtin_setenv(NULL, (char*[]){"cd", "OLDPWD", oldpwd, NULL}, NULL);
else if (!bt_cd_process_dotdot(target))
builtin_setenv(NULL, (char*[]){"cd", "OLDPWD", oldpwd, NULL}, NULL);
free(target);
free(oldpwd);
return (0);
}

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/15 14:19:48 by gwojda #+# #+# */
/* Updated: 2017/03/25 17:18:41 by ariard ### ########.fr */
/* Updated: 2017/03/25 18:17:57 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -25,9 +25,7 @@ int readline(int has_prompt, char **input)
return (ret);
}
readline_init(has_prompt);
DG("readline stind");
ret = ft_read_stdin(input);
DG("after stdin");
if (ret < 0)
return (ret);
if (data_singleton()->line.input)

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/20 14:45:40 by gwojda #+# #+# */
/* Updated: 2017/03/25 17:14:28 by ariard ### ########.fr */
/* Updated: 2017/03/25 20:12:37 by ariard ### ########.fr */
/* */
/* ************************************************************************** */
@ -21,9 +21,7 @@ static int do_readline_routine(char **stream)
data = data_singleton();
has_prompt = !(get_lexer_stack(data->lexer)
|| data->parser.state == UNDEFINED || data->lexer.state == HEREDOC);
DG("before readline");
ret = readline(has_prompt, stream);
DG("after readline");
if (ret == -1)
{
ft_strdel(stream);
@ -44,13 +42,10 @@ static int handle_instruction(t_list **token, t_btree **ast)
data = data_singleton();
while (1)
{
DG();
if ((ret = do_readline_routine(&stream)) > 0)
return (ret);
DG();
if (do_lexer_routine(token, stream) > 0)
continue ;
DG();
if ((ret = do_parser_routine(token, ast)) == 1
&& SH_NO_INTERACTIVE(data->opts))
return (ret);

View file

@ -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);
}