faut arreter de coder les builtin avec le cul
This commit is contained in:
parent
c81d00343d
commit
ccaa9bc159
7 changed files with 109 additions and 112 deletions
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 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_list(char c);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
|
||||
i = 0;
|
||||
if (!list || !*list)
|
||||
return ;
|
||||
while (list[i])
|
||||
{
|
||||
ft_putstr_fd(list[i++], fd);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
while (*env)
|
||||
{
|
||||
if (ft_strcmp(*env, key) == '=')
|
||||
if (ft_strcmp(*env, key) == '='
|
||||
&& ft_strlen(key) == ft_strlenchr(*env, '='))
|
||||
return (*env + ft_strlen(key) + 1);
|
||||
env++;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,98 +6,82 @@
|
|||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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"
|
||||
|
||||
/*
|
||||
** a ajouter dans la lib ?
|
||||
*/
|
||||
|
||||
int ft_sstr_found(char **sstr, char *name)
|
||||
static int env_usage(int arg_miss, char c)
|
||||
{
|
||||
int size;
|
||||
|
||||
size = 0;
|
||||
if (sstr)
|
||||
while (sstr[size] && ft_strncmp(name, sstr[size], ft_strlen(name)))
|
||||
++size;
|
||||
return (size);
|
||||
if (arg_miss)
|
||||
ft_dprintf(2, "{red}env: option requires an argument -- u{eoc}\n");
|
||||
else if (c)
|
||||
ft_dprintf(2, "{red}env: illegal option -- %c{eoc}\n", c);
|
||||
ft_dprintf(2, "usage: env [-i] [-u name] ... [name=value] ... cmd\n");
|
||||
return (1);
|
||||
}
|
||||
|
||||
void ft_sstr_freeone(char **sstr, int index)
|
||||
static void env_freeone(char **env, char *arg)
|
||||
{
|
||||
int i;
|
||||
char *tmp;
|
||||
|
||||
if (!sstr || !sstr[index])
|
||||
return ;
|
||||
tmp = sstr[index];
|
||||
while (sstr[index])
|
||||
while (env && *env && (i = -1))
|
||||
{
|
||||
sstr[index] = sstr[index + 1];
|
||||
index++;
|
||||
if (ft_strcmp(*env, arg) == '='
|
||||
&& ft_strlen(arg) == ft_strlenchr(*env, '='))
|
||||
{
|
||||
tmp = *env;
|
||||
while (*env)
|
||||
{
|
||||
*env = *(env + 1);
|
||||
++env;
|
||||
}
|
||||
ft_strdel(&tmp);
|
||||
return ;
|
||||
}
|
||||
++env;
|
||||
}
|
||||
free(tmp);
|
||||
}
|
||||
|
||||
/*
|
||||
**
|
||||
*/
|
||||
|
||||
static void ft_env_execute(char *const argv[], char **env)
|
||||
static void env_replace(char ***custom_env, char *arg)
|
||||
{
|
||||
pid_t soon;
|
||||
char *path;
|
||||
char *path_exe;
|
||||
char **arg_split;
|
||||
|
||||
path = ft_getenv(env, "PATH");
|
||||
path_exe = ft_findexec(path, *argv);
|
||||
if (!path || !path_exe)
|
||||
if ((arg_split = ft_strsplit(arg, '=')))
|
||||
{
|
||||
ft_dprintf(2, "{red}%s: no such file or directory: %s{eoc}\n",
|
||||
SHELL_NAME, *argv);
|
||||
return ;
|
||||
env_freeone(*custom_env, *arg_split);
|
||||
ft_tabdel(&arg_split);
|
||||
}
|
||||
if ((soon = fork()))
|
||||
wait(&soon);
|
||||
*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
|
||||
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);
|
||||
return (env_usage(1, 0));
|
||||
}
|
||||
else if (!ft_strcmp(**argv, "-u"))
|
||||
else if (ft_strchr(**arg, '='))
|
||||
env_replace(custom_env, **arg);
|
||||
else if (!ft_strcmp(**arg, "--"))
|
||||
{
|
||||
++(*argv);
|
||||
if (!**argv)
|
||||
{
|
||||
ft_illegal_opt_env('u');
|
||||
return (1);
|
||||
++*arg;
|
||||
return (0);
|
||||
}
|
||||
ft_sstr_freeone(*env, ft_sstr_found(*env, **argv));
|
||||
++(*argv);
|
||||
}
|
||||
else if (***argv == '-')
|
||||
{
|
||||
ft_illegal_opt_env(*(**argv + 1));
|
||||
return (1);
|
||||
else if ((**arg)[0] == '-')
|
||||
return (env_usage(0, (**arg)[1]));
|
||||
else
|
||||
return (0);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
|
@ -107,30 +91,22 @@ int builtin_env(const char *path, char *const argv[], char *const envp[])
|
|||
char **env;
|
||||
|
||||
(void)path;
|
||||
if (!argv || ft_strcmp(*argv, "env"))
|
||||
return (env_usage(0, 0));
|
||||
env = ft_sstrdup((char **)envp);
|
||||
while (*argv)
|
||||
if (env_treat_flag(&env, &argv))
|
||||
{
|
||||
if (ft_check_env_opt((char ***)&argv, (char ***)&env))
|
||||
break ;
|
||||
while (*argv && ft_strrchr(*argv, '='))
|
||||
{
|
||||
env = ft_sstradd(env, *argv);
|
||||
++argv;
|
||||
ft_sstrfree(env);
|
||||
return (1);
|
||||
}
|
||||
if (env && (!*argv || (!ft_strcmp(*argv, "env") && !*(argv + 1))))
|
||||
if (!*argv)
|
||||
{
|
||||
ft_sstrprint(env, '\n');
|
||||
if (env)
|
||||
ft_putchar('\n');
|
||||
break ;
|
||||
}
|
||||
if (*argv && ft_strcmp(*argv, "env"))
|
||||
{
|
||||
ft_env_execute(argv, env);
|
||||
break ;
|
||||
}
|
||||
if (*argv)
|
||||
++argv;
|
||||
}
|
||||
ft_sstrfree(env);
|
||||
else
|
||||
ft_putstr(command_getoutput(NULL, argv, env));
|
||||
ft_tabdel(&env);
|
||||
return (0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
i = 0;
|
||||
while (i < 10)
|
||||
{
|
||||
//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[1], STDOUT);
|
||||
dup2(exec->fd_save[2], STDERR);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
||||
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;
|
||||
int ret;
|
||||
|
|
@ -38,8 +52,6 @@ char *command_getoutput(char *command)
|
|||
int fds[2];
|
||||
char **av;
|
||||
|
||||
if (!command)
|
||||
return (NULL);
|
||||
pipe(fds);
|
||||
if (!(pid = fork()))
|
||||
{
|
||||
|
|
@ -47,8 +59,11 @@ char *command_getoutput(char *command)
|
|||
dup2_close(fds[PIPE_WRITE], STDOUT);
|
||||
av = ft_sstradd(NULL, data_singleton()->argv[0]);
|
||||
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);
|
||||
execve(data_singleton()->argv[0], av, data_singleton()->env);
|
||||
execve(data_singleton()->argv[0], av, env);
|
||||
exit(1);
|
||||
}
|
||||
waitpid(pid, &ret, WUNTRACED);
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
int len;
|
||||
|
||||
if (!(output = command_getoutput(command)))
|
||||
if (!(output = command_getoutput(command, NULL, data_singleton()->env)))
|
||||
return (NULL);
|
||||
len = ft_strlen(output);
|
||||
while (output[--len] == '\n')
|
||||
|
|
|
|||
Loading…
Reference in a new issue