faut arreter de coder les builtin avec le cul

This commit is contained in:
wescande 2017-03-18 04:16:17 +01:00
parent c81d00343d
commit ccaa9bc159
7 changed files with 109 additions and 112 deletions

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */ /* Created: 2016/12/01 12:15:50 by jhalford #+# #+# */
/* Updated: 2017/03/17 20:19:18 by jhalford ### ########.fr */ /* Updated: 2017/03/18 02:58:19 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -82,7 +82,7 @@ void token_print(t_list *lst);
int reduce_parens(t_list **alst, char *str); int reduce_parens(t_list **alst, char *str);
int bquotes_expand(t_list **alst); int bquotes_expand(t_list **alst);
char *command_getoutput(char *command); char *command_getoutput(char *command, char *const av_cmd[], char **env);
int ft_is_delim(char c); int ft_is_delim(char c);
int ft_is_delim_list(char c); int ft_is_delim_list(char c);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 15:17:29 by jhalford #+# #+# */ /* Created: 2016/11/28 15:17:29 by jhalford #+# #+# */
/* Updated: 2016/11/28 15:23:52 by jhalford ### ########.fr */ /* Updated: 2017/03/18 03:37:16 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -17,6 +17,8 @@ void ft_sstrprint_fd(int fd, char **list, char sep)
int i; int i;
i = 0; i = 0;
if (!list || !*list)
return ;
while (list[i]) while (list[i])
{ {
ft_putstr_fd(list[i++], fd); ft_putstr_fd(list[i++], fd);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/01 12:15:45 by jhalford #+# #+# */ /* Created: 2016/12/01 12:15:45 by jhalford #+# #+# */
/* Updated: 2016/12/01 12:29:25 by jhalford ### ########.fr */ /* Updated: 2017/03/18 03:09:57 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,7 +18,8 @@ char *ft_getenv(char **env, char *key)
return (NULL); return (NULL);
while (*env) while (*env)
{ {
if (ft_strcmp(*env, key) == '=') if (ft_strcmp(*env, key) == '='
&& ft_strlen(key) == ft_strlenchr(*env, '='))
return (*env + ft_strlen(key) + 1); return (*env + ft_strlen(key) + 1);
env++; env++;
} }

View file

@ -6,98 +6,82 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */ /* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */
/* Updated: 2017/03/07 11:29:18 by ariard ### ########.fr */ /* Updated: 2017/03/18 04:13:57 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
/* static int env_usage(int arg_miss, char c)
** a ajouter dans la lib ?
*/
int ft_sstr_found(char **sstr, char *name)
{ {
int size; if (arg_miss)
ft_dprintf(2, "{red}env: option requires an argument -- u{eoc}\n");
size = 0; else if (c)
if (sstr) ft_dprintf(2, "{red}env: illegal option -- %c{eoc}\n", c);
while (sstr[size] && ft_strncmp(name, sstr[size], ft_strlen(name))) ft_dprintf(2, "usage: env [-i] [-u name] ... [name=value] ... cmd\n");
++size; return (1);
return (size);
} }
void ft_sstr_freeone(char **sstr, int index) static void env_freeone(char **env, char *arg)
{ {
int i;
char *tmp; char *tmp;
if (!sstr || !sstr[index]) while (env && *env && (i = -1))
return ;
tmp = sstr[index];
while (sstr[index])
{ {
sstr[index] = sstr[index + 1]; if (ft_strcmp(*env, arg) == '='
index++; && ft_strlen(arg) == ft_strlenchr(*env, '='))
}
free(tmp);
}
/*
**
*/
static void ft_env_execute(char *const argv[], char **env)
{
pid_t soon;
char *path;
char *path_exe;
path = ft_getenv(env, "PATH");
path_exe = ft_findexec(path, *argv);
if (!path || !path_exe)
{
ft_dprintf(2, "{red}%s: no such file or directory: %s{eoc}\n",
SHELL_NAME, *argv);
return ;
}
if ((soon = fork()))
wait(&soon);
else
set_exitstatus(execve(path_exe, argv, env), 1);
free(path_exe);
}
static void ft_illegal_opt_env(char c)
{
ft_dprintf(2, "{red}env: option requires an argument -- %c\n", c);
ft_dprintf(2, "usage: env\t[-iv] [-P utilpath] [-S string]");
ft_dprintf(2, " [-u name]\n\t\t[name=value ...] ");
ft_dprintf(2, "[utility [argument ...]]{eoc}\n");
}
static int ft_check_env_opt(char ***argv, char ***env)
{
if (!ft_strcmp(**argv, "-i"))
{
ft_sstrfree(*env);
*env = NULL;
++(*argv);
}
else if (!ft_strcmp(**argv, "-u"))
{
++(*argv);
if (!**argv)
{ {
ft_illegal_opt_env('u'); tmp = *env;
return (1); while (*env)
{
*env = *(env + 1);
++env;
}
ft_strdel(&tmp);
return ;
} }
ft_sstr_freeone(*env, ft_sstr_found(*env, **argv)); ++env;
++(*argv);
} }
else if (***argv == '-') }
static void env_replace(char ***custom_env, char *arg)
{
char **arg_split;
if ((arg_split = ft_strsplit(arg, '=')))
{ {
ft_illegal_opt_env(*(**argv + 1)); env_freeone(*custom_env, *arg_split);
return (1); ft_tabdel(&arg_split);
}
*custom_env = ft_sstradd(*custom_env, arg);
}
static int env_treat_flag(char ***custom_env, char *const *arg[])
{
while (*(++*arg))
{
if (!ft_strcmp(**arg, "-i"))
ft_tabdel(custom_env);
else if (!ft_strcmp(**arg, "-u"))
{
++*arg;
if (**arg)
env_freeone(*custom_env, **arg);
else
return (env_usage(1, 0));
}
else if (ft_strchr(**arg, '='))
env_replace(custom_env, **arg);
else if (!ft_strcmp(**arg, "--"))
{
++*arg;
return (0);
}
else if ((**arg)[0] == '-')
return (env_usage(0, (**arg)[1]));
else
return (0);
} }
return (0); return (0);
} }
@ -107,30 +91,22 @@ int builtin_env(const char *path, char *const argv[], char *const envp[])
char **env; char **env;
(void)path; (void)path;
if (!argv || ft_strcmp(*argv, "env"))
return (env_usage(0, 0));
env = ft_sstrdup((char **)envp); env = ft_sstrdup((char **)envp);
while (*argv) if (env_treat_flag(&env, &argv))
{ {
if (ft_check_env_opt((char ***)&argv, (char ***)&env)) ft_sstrfree(env);
break ; return (1);
while (*argv && ft_strrchr(*argv, '='))
{
env = ft_sstradd(env, *argv);
++argv;
}
if (env && (!*argv || (!ft_strcmp(*argv, "env") && !*(argv + 1))))
{
ft_sstrprint(env, '\n');
ft_putchar('\n');
break ;
}
if (*argv && ft_strcmp(*argv, "env"))
{
ft_env_execute(argv, env);
break ;
}
if (*argv)
++argv;
} }
ft_sstrfree(env); if (!*argv)
{
ft_sstrprint(env, '\n');
if (env)
ft_putchar('\n');
}
else
ft_putstr(command_getoutput(NULL, argv, env));
ft_tabdel(&env);
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/09 14:51:23 by jhalford #+# #+# */ /* Created: 2017/03/09 14:51:23 by jhalford #+# #+# */
/* Updated: 2017/03/11 17:58:14 by jhalford ### ########.fr */ /* Updated: 2017/03/18 04:13:04 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,7 +21,10 @@ void process_resetfds(t_process *p)
exec = &data_singleton()->exec; exec = &data_singleton()->exec;
i = 0; i = 0;
while (i < 10) while (i < 10)
close(i++); {
//if (i!=3) //JACK SOME PB HERE on close la sortie debug en forcant le close sur tous les fd... ne risque-t-on pas autre chose ??!!!
close(i++);
}
dup2(exec->fd_save[0], STDIN); dup2(exec->fd_save[0], STDIN);
dup2(exec->fd_save[1], STDOUT); dup2(exec->fd_save[1], STDOUT);
dup2(exec->fd_save[2], STDERR); dup2(exec->fd_save[2], STDERR);

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 19:44:25 by wescande #+# #+# */ /* Created: 2017/03/14 19:44:25 by wescande #+# #+# */
/* Updated: 2017/03/15 23:17:21 by wescande ### ########.fr */ /* Updated: 2017/03/18 04:11:20 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -30,7 +30,21 @@ static char *manage_output(int *fds)
return (output); return (output);
} }
char *command_getoutput(char *command) static char *manage_command(char *const av_cmd[])
{
char *command;
int i;
if (!av_cmd)
return (NULL);
i = -1;
command = ft_strdup(av_cmd[++i]);
while (av_cmd[++i])
ft_strjoin(command, av_cmd[i]);
return (command);
}
char *command_getoutput(char *command, char *const av_cmd[], char **env)
{ {
char *output; char *output;
int ret; int ret;
@ -38,8 +52,6 @@ char *command_getoutput(char *command)
int fds[2]; int fds[2];
char **av; char **av;
if (!command)
return (NULL);
pipe(fds); pipe(fds);
if (!(pid = fork())) if (!(pid = fork()))
{ {
@ -47,8 +59,11 @@ char *command_getoutput(char *command)
dup2_close(fds[PIPE_WRITE], STDOUT); dup2_close(fds[PIPE_WRITE], STDOUT);
av = ft_sstradd(NULL, data_singleton()->argv[0]); av = ft_sstradd(NULL, data_singleton()->argv[0]);
av = ft_sstradd(av, "-c"); av = ft_sstradd(av, "-c");
if (!(command = command ? command : manage_command(av_cmd)))
exit(1);
DG("command is %s", command);
av = ft_sstradd(av, command); av = ft_sstradd(av, command);
execve(data_singleton()->argv[0], av, data_singleton()->env); execve(data_singleton()->argv[0], av, env);
exit(1); exit(1);
} }
waitpid(pid, &ret, WUNTRACED); waitpid(pid, &ret, WUNTRACED);

View file

@ -6,7 +6,7 @@
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */ /* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/17 17:47:53 by wescande #+# #+# */ /* Created: 2017/02/17 17:47:53 by wescande #+# #+# */
/* Updated: 2017/03/15 23:18:56 by wescande ### ########.fr */ /* Updated: 2017/03/18 02:59:06 by wescande ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -72,7 +72,7 @@ static char *get_output(char *command)
char *output; char *output;
int len; int len;
if (!(output = command_getoutput(command))) if (!(output = command_getoutput(command, NULL, data_singleton()->env)))
return (NULL); return (NULL);
len = ft_strlen(output); len = ft_strlen(output);
while (output[--len] == '\n') while (output[--len] == '\n')