merged builtin_env
This commit is contained in:
commit
06b2ad21ba
10 changed files with 72 additions and 122 deletions
|
|
@ -19,6 +19,18 @@
|
|||
|
||||
# define BT_EXPORT_LP (1 << 0)
|
||||
|
||||
# define BT_ENV_LI (1 << 0)
|
||||
# define BT_ENV_LU (1 << 1)
|
||||
|
||||
struct s_env_data
|
||||
{
|
||||
t_flag flag;
|
||||
char **av_data;
|
||||
char **custom_env;
|
||||
};
|
||||
|
||||
typedef struct s_env_data t_env_data;
|
||||
|
||||
t_execf *is_builtin(t_process *p);
|
||||
int builtin_return_status(int ret, int status);
|
||||
int builtin_export(const char *path, char *const av[], char *const envp[]);
|
||||
|
|
@ -38,6 +50,7 @@ int builtin_history(const char *path, char *const av[], char *const envp[]);
|
|||
int builtin_hash(const char *path, char *const av[], char *const envp[]);
|
||||
int builtin_math(const char *path, char *const av[], char *const envp[]);
|
||||
|
||||
int bt_env_geti(char ***av, t_env_data *data);
|
||||
int error_msg(char *msg);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/11/03 18:04:07 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/14 21:09:17 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/03/21 17:52:10 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
@ -22,4 +22,5 @@ void ft_sstrdel(char **sstr, int index)
|
|||
sstr[i] = sstr[i + 1];
|
||||
i++;
|
||||
}
|
||||
ft_strdel(sstr + index);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,124 +1,64 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* builtin_env.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/21 01:14:41 by wescande ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
#define US_ENV "env [-i] [-u name] [name=value]... [utility [argument...]]"
|
||||
|
||||
static int env_usage(int arg_miss, char c)
|
||||
#define ENV_USAGE "env [-i] [name=value]... [utility [argument...]]"
|
||||
#define ENV_NOFILE "env: %s: No such file or directory"
|
||||
#define ENV_NOPERM "env: %s: Permission denied"
|
||||
|
||||
t_cliopts g_env_opts[] =
|
||||
{
|
||||
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: %s\n", US_ENV);
|
||||
{'i', NULL, BT_ENV_LI, 0, bt_env_geti},
|
||||
{0, 0, 0, 0, 0},
|
||||
};
|
||||
|
||||
int bt_env_geti(char ***av, t_env_data *data)
|
||||
{
|
||||
if (!av || !*av || !data)
|
||||
return (1);
|
||||
while (**av && ft_strchr(**av, '='))
|
||||
{
|
||||
data->custom_env = ft_sstradd(data->custom_env, **av);
|
||||
++(*av);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
static void env_replace(char ***custom_env, char *arg)
|
||||
{
|
||||
char **arg_split;
|
||||
|
||||
if ((arg_split = ft_strsplit(arg, '=')))
|
||||
{
|
||||
env_freeone(*custom_env, *arg_split);
|
||||
ft_tabdel(&arg_split);
|
||||
}
|
||||
*custom_env = ft_sstradd(*custom_env, arg);
|
||||
}
|
||||
|
||||
static int env_treat_flag(char ***custom_env, char *const *arg[])
|
||||
{
|
||||
char *tmp;
|
||||
|
||||
while (*(++*arg))
|
||||
{
|
||||
if (!ft_strcmp(**arg, "-i"))
|
||||
{
|
||||
tmp = ft_strjoin("PATH=", ft_getenv(*custom_env, "PATH"));
|
||||
ft_tabdel(custom_env);
|
||||
*custom_env = ft_sstradd(NULL, tmp);
|
||||
ft_strdel(&tmp);
|
||||
}
|
||||
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
|
||||
--(*av);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int bt_env_parse(t_env_data *data, char **av)
|
||||
{
|
||||
data->flag = 0;
|
||||
data->av_data = NULL;
|
||||
data->custom_env = NULL;
|
||||
if (cliopts_get(av, g_env_opts, data))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int builtin_env(const char *path,
|
||||
char *const argv[], char *const envp[])
|
||||
{
|
||||
char **env;
|
||||
t_env_data data;
|
||||
int status;
|
||||
pid_t pid;
|
||||
struct stat buf;
|
||||
|
||||
(void)path;
|
||||
pid = 0;
|
||||
if (!argv || ft_strcmp(*argv, "env"))
|
||||
return (builtin_return_status(0, env_usage(0, 0)));
|
||||
env = ft_sstrdup((char **)envp);
|
||||
if (env_treat_flag(&env, &argv))
|
||||
(void)envp;
|
||||
if (bt_env_parse(&data, (char**)argv))
|
||||
return (ft_perror() && SH_ERR("usage: %s", ENV_USAGE) ? 0 : 0);
|
||||
else if (!*data.av_data)
|
||||
return (builtin_setenv(NULL, (char*[]){"setenv", 0}, NULL));
|
||||
else if ((pid = fork()) == 0)
|
||||
{
|
||||
ft_sstrfree(env);
|
||||
return (builtin_return_status(0, 1));
|
||||
if (!(path = ft_strchr(data.av_data[0], '/') ?
|
||||
ft_strdup(data.av_data[0]) : ft_hash(data.av_data[0]))
|
||||
|| access(path, F_OK) != 0)
|
||||
exit (SH_ERR(ENV_NOFILE, data.av_data[0]));
|
||||
stat(path, &buf);
|
||||
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);
|
||||
}
|
||||
if (!*argv)
|
||||
{
|
||||
ft_sstrprint(env, '\n');
|
||||
if (env)
|
||||
ft_putchar('\n');
|
||||
}
|
||||
else
|
||||
pid = command_setoutput(argv, env);
|
||||
ft_tabdel(&env);
|
||||
DG("%d", pid);
|
||||
return (builtin_return_status(pid, 0));
|
||||
waitpid(pid, &status, 0);
|
||||
ft_sstrfree(data.custom_env);
|
||||
return (0);
|
||||
/* return (builtin_return_status(pid, 0)); */
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@
|
|||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
#define US_READ "read [-ers] [-u fd] [-t timeout] [-p prompt]"
|
||||
#define US_READ_1 "[-n nchars] [-d delim] [name ...]"
|
||||
|
||||
|
|
@ -26,11 +27,6 @@ t_cliopts g_read_opts[] =
|
|||
{0, 0, 0, 0, 0},
|
||||
};
|
||||
|
||||
void bt_read_usage(void)
|
||||
{
|
||||
SH_ERR("usage: read %s %s\n", US_READ, US_READ_1);
|
||||
}
|
||||
|
||||
int bt_read_init(t_read *data, char **av)
|
||||
{
|
||||
data->opts = 0;
|
||||
|
|
@ -115,7 +111,7 @@ int builtin_read(const char *path, char *const av[], char *const envp[])
|
|||
if (ret == -1)
|
||||
exit(1);
|
||||
if (ret != 0)
|
||||
bt_read_usage();
|
||||
SH_ERR("usage: read %s %s\n", US_READ, US_READ_1);
|
||||
if (ret != 2)
|
||||
bt_read_exit(&data);
|
||||
return (builtin_return_status(0, ret));
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2016/11/28 14:29:17 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/21 17:40:31 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/03/21 17:51:37 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/07 15:48:24 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/20 15:50:12 by gwojda ### ########.fr */
|
||||
/* Updated: 2017/03/21 18:10:21 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/07 17:26:53 by wescande #+# #+# */
|
||||
/* Updated: 2017/03/21 00:53:01 by wescande ### ########.fr */
|
||||
/* Updated: 2017/03/21 18:10:08 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/13 22:21:19 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/21 20:53:04 by ariard ### ########.fr */
|
||||
/* Updated: 2017/03/21 18:11:33 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/02/06 22:12:31 by jhalford #+# #+# */
|
||||
/* Updated: 2017/03/21 17:40:36 by jhalford ### ########.fr */
|
||||
/* Updated: 2017/03/21 17:41:36 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@
|
|||
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2017/03/14 19:44:25 by wescande #+# #+# */
|
||||
/* Updated: 2017/03/21 01:21:38 by wescande ### ########.fr */
|
||||
/* Updated: 2017/03/21 18:15:53 by jhalford ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue