ajout d'env avec opt -i -u / export / unset

This commit is contained in:
gwojda 2017-02-15 15:46:43 +01:00
parent 73f12e7e96
commit b67c7fd7d3
10 changed files with 190 additions and 92 deletions

View file

@ -36,9 +36,11 @@ builtin/builtin_cd.c\
builtin/builtin_echo.c\ builtin/builtin_echo.c\
builtin/builtin_env.c\ builtin/builtin_env.c\
builtin/builtin_exit.c\ builtin/builtin_exit.c\
builtin/builtin_export.c\
builtin/builtin_history.c\ builtin/builtin_history.c\
builtin/builtin_read.c\ builtin/builtin_read.c\
builtin/builtin_setenv.c\ builtin/builtin_setenv.c\
builtin/builtin_unset.c\
builtin/builtin_unsetenv.c\ builtin/builtin_unsetenv.c\
builtin/is_builtin.c\ builtin/is_builtin.c\
completion/c_binary.c\ completion/c_binary.c\

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 17:21:56 by jhalford #+# #+# */ /* Created: 2016/12/13 17:21:56 by jhalford #+# #+# */
/* Updated: 2017/02/02 19:07:01 by gwojda ### ########.fr */ /* Updated: 2017/02/15 11:45:15 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,6 +18,8 @@
# include "builtin_read.h" # include "builtin_read.h"
t_execf *is_builtin(t_process *p); t_execf *is_builtin(t_process *p);
int builtin_export(const char *path, char *const av[], char *const envp[]);
int builtin_unset(const char *path, char *const av[], char *const envp[]);
int builtin_env(const char *path, char *const argv[], char *const envp[]); int builtin_env(const char *path, char *const argv[], char *const envp[]);
int builtin_echo(const char *path, char *const argv[], char *const envp[]); int builtin_echo(const char *path, char *const argv[], char *const envp[]);
int builtin_cd(const char *path, char *const argv[], char *const envp[]); int builtin_cd(const char *path, char *const argv[], char *const envp[]);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */ /* Created: 2016/11/10 13:07:44 by jhalford #+# #+# */
/* Updated: 2017/02/10 00:26:10 by jhalford ### ########.fr */ /* Updated: 2017/02/15 14:20:59 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -3,43 +3,131 @@
/* ::: :::::::: */ /* ::: :::::::: */
/* builtin_env.c :+: :+: :+: */ /* builtin_env.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* 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/01/10 13:08:15 by jhalford ### ########.fr */ /* Updated: 2017/02/15 15:45:54 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "minishell.h" #include "minishell.h"
int builtin_env(const char *path, char *const argv[], char *const envp[]) /*
** a ajouter dans la lib ?
*/
int ft_sstr_found(char **sstr, char *name)
{ {
(void)argv; int size;
(void)envp;
(void)path; size = 0;
if (sstr)
while (sstr[size] && ft_strncmp(name, sstr[size], ft_strlen(name)))
++size;
return (size);
}
void ft_sstr_freeone(char **sstr, int index)
{
char *tmp;
tmp = sstr[index];
while (sstr[index])
{
sstr[index] = sstr[index + 1];
index++;
}
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');
return (1);
}
ft_sstr_freeone(*env, ft_sstr_found(*env, **argv));
++(*argv);
}
else if (***argv == '-')
{
ft_illegal_opt_env(*(**argv + 1));
return (1);
}
return (0); return (0);
} }
/* int builtin_env(char **av, t_data *data) */
/* { */
/* int i; */
/* char **env; */
/* i = 1; */ int builtin_env(const char *path, char *const argv[], char *const envp[])
/* env = NULL; */ {
/* if (!av[1]) */ char **env;
/* { */
/* ft_sstrprint(data->env, '\n'); */ (void)path;
/* ft_putchar('\n'); */ env = ft_sstrdup((char **)envp);
/* } */ while (*argv)
/* else */ {
/* { */ if (ft_check_env_opt((char ***)&argv, (char ***)&env))
/* while (av[i] && ft_strchr(av[i], '=')) */ break ;
/* { */ while (*argv && ft_strrchr(*argv, '='))
/* env = ft_sstradd(env, av[i]); */ {
/* i++; */ env = ft_sstradd(env, *argv);
/* } */ ++argv;
/* if (av[i]) */ }
/* ft_cmd_process(av + i); */ if (env && (!*argv || (!ft_strcmp(*argv, "env") && !*(argv + 1))))
/* } */ {
/* return (0); */ ft_sstrprint(env, '\n');
/* } */ ft_putchar('\n');
return (0);
}
if (*argv && ft_strcmp(*argv, "env"))
{
ft_env_execute(argv, env);
return (0);
}
if (*argv)
++argv;
}
return (0);
}

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin_export.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 11:39:37 by gwojda #+# #+# */
/* Updated: 2017/02/15 11:46:20 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int builtin_export(const char *path, char *const av[], char *const envp[])
{
if (ft_strcmp(av[0], "export") == 0)
av++;
return (builtin_setenv(path, av, envp));
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin_unset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 11:43:34 by gwojda #+# #+# */
/* Updated: 2017/02/15 11:58:22 by gwojda ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int builtin_unset(const char *path, char *const av[], char *const envp[])
{
return (builtin_unsetenv(path, av, envp));
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */ /* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 13:09:57 by jhalford #+# #+# */ /* Created: 2016/12/13 13:09:57 by jhalford #+# #+# */
/* Updated: 2017/01/26 14:58:02 by gwojda ### ########.fr */ /* Updated: 2017/02/15 11:44:31 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,6 +16,8 @@ t_stof g_builtin[] =
{ {
{"echo", &builtin_echo}, {"echo", &builtin_echo},
{"cd", &builtin_cd}, {"cd", &builtin_cd},
{"export", &builtin_export},
{"unset", &builtin_unset},
{"setenv", &builtin_setenv}, {"setenv", &builtin_setenv},
{"unsetenv", &builtin_unsetenv}, {"unsetenv", &builtin_unsetenv},
{"env", &builtin_env}, {"env", &builtin_env},

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */ /* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/13 13:51:33 by gwojda #+# #+# */ /* Created: 2016/12/13 13:51:33 by gwojda #+# #+# */
/* Updated: 2017/02/14 14:13:05 by gwojda ### ########.fr */ /* Updated: 2017/02/15 11:52:24 by gwojda ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -36,6 +36,12 @@ static int ft_git_status(void)
ft_printf("\x1b[38;5;47mgit:(\x1b[38;5;203m%s\x1b[38;5;47m)", line); ft_printf("\x1b[38;5;47mgit:(\x1b[38;5;203m%s\x1b[38;5;47m)", line);
free(tmp); free(tmp);
} }
else
{
line = ft_strdup(line + 3);
ft_printf("\x1b[38;5;47mgit:(\x1b[38;5;203m%s\x1b[38;5;47m)", line);
free(tmp);
}
if (!get_next_line(pip[0], &tmp)) if (!get_next_line(pip[0], &tmp))
printf("\x1b[38;5;83m %C ", L''); printf("\x1b[38;5;83m %C ", L'');
else else

View file

@ -1,62 +1,19 @@
 shell_init.c 28 interactive shell settings  shell_init.c 28 interactive shell settings
 main.c 90 start of shell pid=44206 pgrp=44206 job_control is ON  main.c 88 start of shell JOBC is ON
 lexer_end.c 7 check  main.c 64 [env -o] stack=[0] state=[4]
 main.c 65 [history] stack=[0] state=[4]  token_print.c 29 13:[env]
 token_print.c 29 13:[history]  token_print.c 29 13:[-o]
 token_print.c 29 13:[history]  main.c 64 [env -u] stack=[0] state=[4]
 lexer_end.c 7 check  token_print.c 29 13:[env]
 main.c 65 [history] stack=[0] state=[4]  token_print.c 29 13:[-u]
 token_print.c 29 13:[history]  main.c 64 [histor] stack=[0] state=[4]
 token_print.c 29 13:[history]  token_print.c 29 13:[histor]
 lexer_end.c 7 check  main.c 64 [history] stack=[0] state=[4]
 main.c 65 [aaaaaaa] stack=[0] state=[4]
 token_print.c 29 13:[aaaaaaa]
 token_print.c 29 13:[aaaaaaa]
 lexer_end.c 7 check
 main.c 65 [ssssss] stack=[0] state=[4]
 token_print.c 29 13:[ssssss]
 token_print.c 29 13:[ssssss]
 lexer_end.c 7 check
 main.c 65 [ddddddddd] stack=[0] state=[4]
 token_print.c 29 13:[ddddddddd]
 token_print.c 29 13:[ddddddddd]
 lexer_end.c 7 check
 main.c 65 [ffffffffffffff] stack=[0] state=[4]
 token_print.c 29 13:[ffffffffffffff]
 token_print.c 29 13:[ffffffffffffff]
 lexer_end.c 7 check
 main.c 65 [ffffffffffffff] stack=[0] state=[4]
 token_print.c 29 13:[ffffffffffffff]
 token_print.c 29 13:[ffffffffffffff]
 lexer_end.c 7 check
 main.c 65 [history] stack=[0] state=[4]
 token_print.c 29 13:[history]
 token_print.c 29 13:[history]
 lexer_end.c 7 check
 main.c 65 [history] stack=[0] state=[4]
 token_print.c 29 13:[history]
 token_print.c 29 13:[history]
 lexer_end.c 7 check
 main.c 65 [ls] stack=[0] state=[4]
 token_print.c 29 13:[ls]
 token_print.c 29 13:[ls]
 job_wait.c 20 gonna wait [1]
 lexer_end.c 7 check
 main.c 65 [ls -la] stack=[0] state=[4]
 token_print.c 29 13:[ls]
 token_print.c 29 13:[-la]
 token_print.c 29 13:[ls]
 token_print.c 29 13:[-la]
 job_wait.c 20 gonna wait [1]
 lexer_end.c 7 check
 main.c 65 [ls -la] stack=[0] state=[4]
 token_print.c 29 13:[ls]
 token_print.c 29 13:[-la]
 token_print.c 29 13:[ls]
 token_print.c 29 13:[-la]
 job_wait.c 20 gonna wait [1]
 lexer_end.c 7 check
 main.c 65 [history] stack=[0] state=[4]
 token_print.c 29 13:[history]
 token_print.c 29 13:[history]  token_print.c 29 13:[history]
 main.c 64 [env -u LESS] stack=[0] state=[4]
 token_print.c 29 13:[env]
 token_print.c 29 13:[-u]
 token_print.c 29 13:[LESS]
 main.c 64 [env ] stack=[0] state=[0]
 token_print.c 29 13:[env]
 shell_exit.c 17 shell_exit()  shell_exit.c 17 shell_exit()

3
42sh/testmake Normal file
View file

@ -0,0 +1,3 @@
 shell_init.c 28 interactive shell settings
 main.c 88 start of shell JOBC is ON
 shell_exit.c 17 shell_exit()