rectif env. attente de validation
This commit is contained in:
parent
422c685417
commit
53244ed5a7
4 changed files with 116 additions and 49 deletions
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/03/14 22:59:57 by jhalford #+# #+# */
|
/* Created: 2017/03/14 22:59:57 by jhalford #+# #+# */
|
||||||
/* Updated: 2017/03/24 15:13:06 by wescande ### ########.fr */
|
/* Updated: 2017/03/25 01:08:44 by wescande ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -19,8 +19,6 @@
|
||||||
|
|
||||||
# define BT_EXPORT_LP (1 << 0)
|
# define BT_EXPORT_LP (1 << 0)
|
||||||
|
|
||||||
# define BT_ENV_LI (1 << 0)
|
|
||||||
# define BT_ENV_LU (1 << 1)
|
|
||||||
|
|
||||||
struct s_env_data
|
struct s_env_data
|
||||||
{
|
{
|
||||||
|
|
@ -36,6 +34,8 @@ int builtin_return_status(int ret, int status);
|
||||||
int builtin_export(const char *path, char *const av[], char *const envp[]);
|
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_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 bt_env_opt_u(char *opt_arg, t_env_data *data);
|
||||||
|
int bt_env_opt_i(char *opt_arg, t_env_data *data);
|
||||||
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[]);
|
||||||
int builtin_exit(const char *path, char *const argv[], char *const envp[]);
|
int builtin_exit(const char *path, char *const argv[], char *const envp[]);
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/03/14 20:22:56 by jhalford #+# #+# */
|
/* Created: 2017/03/14 20:22:56 by jhalford #+# #+# */
|
||||||
/* Updated: 2017/03/20 15:48:05 by jhalford ### ########.fr */
|
/* Updated: 2017/03/24 23:57:01 by wescande ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -27,6 +27,7 @@ struct s_cliopts
|
||||||
t_flag flag_on;
|
t_flag flag_on;
|
||||||
t_flag flag_off;
|
t_flag flag_off;
|
||||||
int (*get)();
|
int (*get)();
|
||||||
|
int arg_required:1;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct s_data_template
|
struct s_data_template
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/03/14 20:04:04 by jhalford #+# #+# */
|
/* Created: 2017/03/14 20:04:04 by jhalford #+# #+# */
|
||||||
/* Updated: 2017/03/24 15:02:26 by jhalford ### ########.fr */
|
/* Updated: 2017/03/25 01:17:12 by wescande ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -17,13 +17,25 @@
|
||||||
|
|
||||||
#include "cliopts.h"
|
#include "cliopts.h"
|
||||||
|
|
||||||
|
static char *check_required(char ***av, char *arg)
|
||||||
|
{
|
||||||
|
char *ret;
|
||||||
|
|
||||||
|
if (!av || !*av)
|
||||||
|
return (NULL);
|
||||||
|
if (!arg || !*arg)
|
||||||
|
return (*++(*av));
|
||||||
|
ret = arg + 1;
|
||||||
|
return (ret);
|
||||||
|
}
|
||||||
|
|
||||||
static t_cliopts *get_map_long(t_cliopts opt_map[], char *arg)
|
static t_cliopts *get_map_long(t_cliopts opt_map[], char *arg)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
i = -1;
|
i = -1;
|
||||||
while (opt_map[++i].c)
|
while (opt_map[++i].c)
|
||||||
if (ft_strcmp(opt_map[i].str, arg) == 0)
|
if (!ft_strcmp(opt_map[i].str, arg))
|
||||||
return (&opt_map[i]);
|
return (&opt_map[i]);
|
||||||
return (NULL);
|
return (NULL);
|
||||||
}
|
}
|
||||||
|
|
@ -45,27 +57,28 @@ static int cliopts_parse_short(
|
||||||
t_cliopts *map;
|
t_cliopts *map;
|
||||||
char *arg;
|
char *arg;
|
||||||
int i;
|
int i;
|
||||||
|
char *tmp;
|
||||||
|
|
||||||
arg = **av + 1;
|
arg = **av + 1;
|
||||||
i = 0;
|
i = -1;
|
||||||
while (arg[i])
|
while (arg[++i] && !(tmp = NULL))
|
||||||
{
|
{
|
||||||
if (!(map = get_map_short(opt_map, arg[i])))
|
if (!(map = get_map_short(opt_map, arg[i])))
|
||||||
return (ERR_SET(E_CO_INV, arg[i]));
|
return (ERR_SET(E_CO_INV, arg[i]));
|
||||||
if (map->get)
|
if (map->get)
|
||||||
{
|
{
|
||||||
if (!(arg[i - 1] == '-' && arg[i + 1] == 0))
|
if (map->arg_required && !(tmp = check_required(av, arg + i)))
|
||||||
return (ERR_SET(E_CO_MULT, *arg));
|
|
||||||
++(*av);
|
|
||||||
if ((map->get)(av, data))
|
|
||||||
return (ERR_SET(E_CO_MISS, *arg));
|
return (ERR_SET(E_CO_MISS, *arg));
|
||||||
|
tmp = tmp ? tmp : **av;
|
||||||
|
if ((map->get)(tmp, data))
|
||||||
|
return (ERR_SET(E_CO_MISS, *arg));
|
||||||
|
if (map->arg_required && !(*(arg += ft_strlen(arg))))
|
||||||
|
++(*av);
|
||||||
}
|
}
|
||||||
((t_data_template*)data)->flag |= map->flag_on;
|
((t_data_template*)data)->flag |= map->flag_on;
|
||||||
((t_data_template*)data)->flag &= ~map->flag_off;
|
((t_data_template*)data)->flag &= ~map->flag_off;
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
++(*av);
|
return (++(*av) ? 0 : 0);
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int cliopts_parse_long(
|
static int cliopts_parse_long(
|
||||||
|
|
@ -73,17 +86,21 @@ static int cliopts_parse_long(
|
||||||
{
|
{
|
||||||
t_cliopts *map;
|
t_cliopts *map;
|
||||||
char *arg;
|
char *arg;
|
||||||
|
char *tmp;
|
||||||
|
|
||||||
arg = **av + 2;
|
arg = **av + 2;
|
||||||
|
tmp = NULL;
|
||||||
if (!(map = get_map_long(opt_map, arg)))
|
if (!(map = get_map_long(opt_map, arg)))
|
||||||
return (ERR_SET(E_CO_INVL, arg));
|
return (ERR_SET(E_CO_INVL, arg));
|
||||||
((t_data_template*)data)->flag |= map->flag_on;
|
((t_data_template*)data)->flag |= map->flag_on;
|
||||||
((t_data_template*)data)->flag &= ~map->flag_off;
|
((t_data_template*)data)->flag &= ~map->flag_off;
|
||||||
if (map->get)
|
if (map->get)
|
||||||
{
|
{
|
||||||
++(*av);
|
if (map->arg_required && !(tmp = check_required(av, NULL)))
|
||||||
if ((map->get)(av, data))
|
return (ERR_SET(E_CO_MISS, *arg));
|
||||||
|
if ((map->get)(tmp, data))
|
||||||
return (ERR_SET(E_CO_MISSL, arg));
|
return (ERR_SET(E_CO_MISSL, arg));
|
||||||
|
++(*av);
|
||||||
}
|
}
|
||||||
++(*av);
|
++(*av);
|
||||||
return (0);
|
return (0);
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@
|
||||||
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2017/03/22 16:20:31 by gwojda #+# #+# */
|
/* Created: 2017/03/22 16:20:31 by gwojda #+# #+# */
|
||||||
/* Updated: 2017/03/24 17:59:00 by jhalford ### ########.fr */
|
/* Updated: 2017/03/25 01:15:38 by wescande ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -15,14 +15,75 @@
|
||||||
#define ENV_USAGE "env [-i] [name=value]... [utility [argument...]]"
|
#define ENV_USAGE "env [-i] [name=value]... [utility [argument...]]"
|
||||||
#define ENV_NOFILE "env: %s: No such file or directory"
|
#define ENV_NOFILE "env: %s: No such file or directory"
|
||||||
#define ENV_NOPERM "env: %s: Permission denied"
|
#define ENV_NOPERM "env: %s: Permission denied"
|
||||||
|
# define BT_ENV_LI (1 << 0)
|
||||||
|
# define BT_ENV_LU (1 << 1)
|
||||||
|
|
||||||
t_cliopts g_env_opts[] =
|
static t_cliopts g_env_opts[] =
|
||||||
{
|
{
|
||||||
{'i', NULL, BT_ENV_LI, 0, NULL},
|
{'i', NULL, 0, 0, &bt_env_opt_i, 0},
|
||||||
|
{'u', NULL, 0, 0, &bt_env_opt_u, 1},
|
||||||
{0, 0, 0, 0, 0},
|
{0, 0, 0, 0, 0},
|
||||||
};
|
};
|
||||||
|
|
||||||
int bt_env_getcustom(char ***av, t_env_data *data)
|
int bt_env_opt_i(char *opt_arg, t_env_data *data)
|
||||||
|
{
|
||||||
|
(void)opt_arg;
|
||||||
|
ft_tabdel(&data->custom_env);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void env_freeone(char **env, char *arg)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char *tmp;
|
||||||
|
|
||||||
|
while (env && *env && (i = -1))
|
||||||
|
{
|
||||||
|
if (ft_strcmp(*env, arg) == '='
|
||||||
|
&& ft_strlen(arg) == ft_strlenchr(*env, '='))
|
||||||
|
{
|
||||||
|
tmp = *env;
|
||||||
|
while (*env)
|
||||||
|
{
|
||||||
|
*env = *(env + 1);
|
||||||
|
++env;
|
||||||
|
}
|
||||||
|
ft_strdel(&tmp);
|
||||||
|
return ;
|
||||||
|
}
|
||||||
|
++env;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int bt_env_opt_u(char *opt_arg, t_env_data *data)
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
char *tmp;
|
||||||
|
char **env;
|
||||||
|
char **tmp_env;
|
||||||
|
|
||||||
|
env = data->custom_env;
|
||||||
|
while (env && *env && (i = -1))
|
||||||
|
{
|
||||||
|
if (ft_strcmp(*env, opt_arg) == '='
|
||||||
|
&& ft_strlen(opt_arg) == ft_strlenchr(*env, '='))
|
||||||
|
{
|
||||||
|
tmp = *env;
|
||||||
|
tmp_env = env + 1;
|
||||||
|
while (*env)
|
||||||
|
{
|
||||||
|
*env = *(env + 1);
|
||||||
|
++env;
|
||||||
|
}
|
||||||
|
env = tmp_env;
|
||||||
|
ft_strdel(&tmp);
|
||||||
|
}
|
||||||
|
++env;
|
||||||
|
}
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
static int bt_env_getcustom(char ***av, t_env_data *data)
|
||||||
{
|
{
|
||||||
if (!av || !*av || !data)
|
if (!av || !*av || !data)
|
||||||
return (1);
|
return (1);
|
||||||
|
|
@ -34,59 +95,47 @@ int bt_env_getcustom(char ***av, t_env_data *data)
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int bt_env_parse(t_env_data *data, char **av)
|
static int bt_env_parse(t_env_data *data, char **av)
|
||||||
{
|
{
|
||||||
data->flag = 0;
|
data->flag = 0;
|
||||||
data->av_data = NULL;
|
data->av_data = NULL;
|
||||||
DG();
|
data->custom_env = ft_sstrdup(data_singleton()->env);
|
||||||
if (cliopts_get(av, g_env_opts, data))
|
if (cliopts_get(av, g_env_opts, data))
|
||||||
return (1);
|
return (1);
|
||||||
DG();
|
|
||||||
data->custom_env = NULL;
|
|
||||||
bt_env_getcustom(&data->av_data, data);
|
bt_env_getcustom(&data->av_data, data);
|
||||||
DG();
|
|
||||||
if (!(data->flag & BT_ENV_LI))
|
if (!(data->flag & BT_ENV_LI))
|
||||||
{
|
{
|
||||||
DG("no -i");
|
|
||||||
data->custom_env = ft_sstrmerge(data_singleton()->env, data->custom_env);
|
data->custom_env = ft_sstrmerge(data_singleton()->env, data->custom_env);
|
||||||
}
|
}
|
||||||
DG();
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int builtin_env(const char *path,
|
int builtin_env(const char *path,
|
||||||
char *const argv[], char *const envp[])
|
char *const argv[], char *const envp[])
|
||||||
{
|
{
|
||||||
t_env_data data;
|
t_env_data dat;
|
||||||
int status;
|
int status;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
struct stat buf;
|
|
||||||
|
|
||||||
(void)envp;
|
(void)envp;
|
||||||
if (bt_env_parse(&data, (char**)argv))
|
if (bt_env_parse(&dat, (char**)argv))
|
||||||
return (ft_perror("env") && SH_ERR("usage: %s", ENV_USAGE));
|
return (ft_perror("env") && SH_ERR("usage: %s", ENV_USAGE));
|
||||||
DG();
|
if (!*dat.av_data)
|
||||||
if (!*data.av_data)
|
|
||||||
{
|
{
|
||||||
DG();
|
ft_sstrprint(dat.custom_env, '\n');
|
||||||
ft_sstrprint(data.custom_env, '\n');
|
return (ft_putchar('\n') * 0);
|
||||||
ft_putchar('\n');
|
|
||||||
return (0);
|
|
||||||
}
|
}
|
||||||
else if ((pid = fork()) == 0)
|
else if ((pid = fork()) == 0)
|
||||||
{
|
{
|
||||||
DG();
|
if (!(path = ft_strchr(dat.av_data[0], '/') ? ft_strdup(dat.av_data[0])
|
||||||
if (!(path = ft_strchr(data.av_data[0], '/') ?
|
: ft_hash(dat.av_data[0])) || access(path, F_OK) != 0)
|
||||||
ft_strdup(data.av_data[0]) : ft_hash(data.av_data[0]))
|
exit(SH_ERR(ENV_NOFILE, dat.av_data[0]));
|
||||||
|| access(path, F_OK) != 0)
|
if (is_directory(path) || access(path, X_OK) != 0)
|
||||||
exit(SH_ERR(ENV_NOFILE, data.av_data[0]));
|
exit(SH_ERR(ENV_NOPERM, dat.av_data[0]));
|
||||||
stat(path, &buf);
|
execve(path, dat.av_data, dat.custom_env);
|
||||||
if (S_ISDIR(buf.st_mode) || access(path, X_OK) != 0)
|
|
||||||
exit(SH_ERR(ENV_NOPERM, data.av_data[0]));
|
|
||||||
execve(path, data.av_data, data.custom_env);
|
|
||||||
}
|
}
|
||||||
waitpid(pid, &status, 0);
|
waitpid(pid, &status, 0);
|
||||||
ft_sstrfree(data.custom_env);
|
ft_sstrfree(dat.custom_env);
|
||||||
tcsetpgrp(STDIN, data_singleton()->jobc.shell_pgid);
|
tcsetpgrp(STDIN, data_singleton()->jobc.shell_pgid);
|
||||||
return (0);
|
return (WIFEXITED(status) ? WEXITSTATUS(status) : 128 + WTERMSIG(status));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue