shlvl_init(), set SHLVL
+ export builtin small change
This commit is contained in:
2
Makefile
2
Makefile
@@ -25,7 +25,7 @@ LIBS = -L $(LIBFT_D) -lft \
|
|||||||
LIBFT_D = ./libft
|
LIBFT_D = ./libft
|
||||||
LIBFT = $(LIBFT_D)/libft.a
|
LIBFT = $(LIBFT_D)/libft.a
|
||||||
|
|
||||||
SRCS = main.c init.c free.c generic.c error_wrappers.c \
|
SRCS = main.c init.c retrieve_path.c free.c generic.c error_wrappers.c \
|
||||||
signals.c \
|
signals.c \
|
||||||
shell_loop.c shell_script.c \
|
shell_loop.c shell_script.c \
|
||||||
lexing.c fill_token.c check_operators.c \
|
lexing.c fill_token.c check_operators.c \
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ int simple_command_builtin(t_cmd *cmd, t_all *c);
|
|||||||
int builtin_cd(int argc, char *argv[], t_all *c);
|
int builtin_cd(int argc, char *argv[], t_all *c);
|
||||||
int builtin_pwd(int argc, char *argv[], t_all *c);
|
int builtin_pwd(int argc, char *argv[], t_all *c);
|
||||||
int builtin_export(int argc, char *argv[], t_all *c);
|
int builtin_export(int argc, char *argv[], t_all *c);
|
||||||
|
int export_var(char *arg);
|
||||||
int builtin_unset(int argc, char *argv[], t_all *c);
|
int builtin_unset(int argc, char *argv[], t_all *c);
|
||||||
int builtin_exit(int argc, char *argv[], t_all *c);
|
int builtin_exit(int argc, char *argv[], t_all *c);
|
||||||
int builtin_env(int argc, char *argv[], t_all *c);
|
int builtin_env(int argc, char *argv[], t_all *c);
|
||||||
|
|||||||
@@ -12,7 +12,6 @@
|
|||||||
|
|
||||||
#include "minishell.h"
|
#include "minishell.h"
|
||||||
|
|
||||||
static int export_var(char *arg);
|
|
||||||
static int change_var_value(char *arg, char *var_name);
|
static int change_var_value(char *arg, char *var_name);
|
||||||
|
|
||||||
#define ERR_ID 1
|
#define ERR_ID 1
|
||||||
@@ -32,7 +31,7 @@ int builtin_export(int argc, char *argv[], t_all *c)
|
|||||||
{
|
{
|
||||||
ret = export_var(argv[i]);
|
ret = export_var(argv[i]);
|
||||||
if (ret == -1)
|
if (ret == -1)
|
||||||
return (EXIT_FAILURE);
|
return (ft_reti_perror(EXIT_FAILURE, CMD", export_var()"));
|
||||||
else if (ret == ERR_ID)
|
else if (ret == ERR_ID)
|
||||||
exit_value = EXIT_FAILURE;
|
exit_value = EXIT_FAILURE;
|
||||||
if (ft_strncmp(argv[i], "PATH=", 5) == 0)
|
if (ft_strncmp(argv[i], "PATH=", 5) == 0)
|
||||||
@@ -44,7 +43,9 @@ int builtin_export(int argc, char *argv[], t_all *c)
|
|||||||
|
|
||||||
// in complete shell, must mark arguments for export
|
// in complete shell, must mark arguments for export
|
||||||
// (Not implemented in minishell)
|
// (Not implemented in minishell)
|
||||||
static int export_var(char *arg)
|
// arg == var_name[=value]
|
||||||
|
// return "-1" on alloc error
|
||||||
|
int export_var(char *arg)
|
||||||
{
|
{
|
||||||
char **var_split;
|
char **var_split;
|
||||||
int ret;
|
int ret;
|
||||||
@@ -52,7 +53,7 @@ static int export_var(char *arg)
|
|||||||
ret = 0;
|
ret = 0;
|
||||||
var_split = ft_split(arg, '=');
|
var_split = ft_split(arg, '=');
|
||||||
if (!var_split)
|
if (!var_split)
|
||||||
return (ft_reti_perror(-1, CMD", ft_split()"));
|
return (-1);
|
||||||
if (!ft_is_posix_name(var_split[0]))
|
if (!ft_is_posix_name(var_split[0]))
|
||||||
{
|
{
|
||||||
shell_error("export: ", var_split[0], ERR_ID_STR, 0);
|
shell_error("export: ", var_split[0], ERR_ID_STR, 0);
|
||||||
|
|||||||
41
srcs/init.c
41
srcs/init.c
@@ -6,7 +6,7 @@
|
|||||||
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
|
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
|
||||||
/* Updated: 2021/12/04 15:11:20 by lperrey ### ########.fr */
|
/* Updated: 2021/12/08 06:27:51 by lperrey ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@@ -14,6 +14,7 @@
|
|||||||
|
|
||||||
static char *init_prompt_base(void);
|
static char *init_prompt_base(void);
|
||||||
static int init_readline_hook(void);
|
static int init_readline_hook(void);
|
||||||
|
static int shlvl_init(void);
|
||||||
|
|
||||||
int init(t_all *c)
|
int init(t_all *c)
|
||||||
{
|
{
|
||||||
@@ -24,30 +25,42 @@ int init(t_all *c)
|
|||||||
rl_startup_hook = NULL;
|
rl_startup_hook = NULL;
|
||||||
environ = ft_dup_2d_arr(environ, (t_dup_f)ft_strdup);
|
environ = ft_dup_2d_arr(environ, (t_dup_f)ft_strdup);
|
||||||
if (!environ)
|
if (!environ)
|
||||||
return (ft_reti_perror(0, "ft_dup_2d_arr(environ) error"));
|
return (ft_reti_perror(0, "ft_dup_2d_arr(environ)"));
|
||||||
c->path = retrieve_path();
|
c->path = retrieve_path();
|
||||||
|
if (!shlvl_init())
|
||||||
|
return (ft_reti_perror(0, "shlvl_init()"));
|
||||||
c->prompt_base = init_prompt_base();
|
c->prompt_base = init_prompt_base();
|
||||||
if (!c->prompt_base)
|
if (!c->prompt_base)
|
||||||
return (ft_reti_perror(0, "init_prompt_base() error"));
|
return (ft_reti_perror(0, "init_prompt_base()"));
|
||||||
c->prompt = init_prompt(c->prompt_base);
|
c->prompt = init_prompt(c->prompt_base);
|
||||||
if (!c->prompt)
|
if (!c->prompt)
|
||||||
return (ft_reti_perror(0, "init_prompt() error"));
|
return (ft_reti_perror(0, "init_prompt()"));
|
||||||
set_signals_behaviour();
|
set_signals_behaviour();
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
char **retrieve_path(void)
|
static int shlvl_init(void)
|
||||||
{
|
{
|
||||||
char *path;
|
char *tmp;
|
||||||
char **path_split;
|
|
||||||
|
|
||||||
path = getenv("PATH");
|
tmp = getenv("SHLVL");
|
||||||
if (!path)
|
if (tmp && ft_isdigit_str(tmp))
|
||||||
return (ft_retp_print(NULL, "minishell: Warning, $PATH not set\n", 2));
|
{
|
||||||
path_split = ft_split(path, ':');
|
tmp = ft_itoa(ft_atoi(tmp) + 1);
|
||||||
if (!path_split)
|
if (!tmp)
|
||||||
return (ft_retp_perror(NULL, "retrieve_path()"));
|
return (0);
|
||||||
return (path_split);
|
tmp = ft_strjoinfree_s2("SHLVL=", tmp);
|
||||||
|
if (!tmp)
|
||||||
|
return (0);
|
||||||
|
if (export_var(tmp) == -1)
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (export_var("SHLVL=1") == -1)
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
return (1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static char *init_prompt_base(void)
|
static char *init_prompt_base(void)
|
||||||
|
|||||||
27
srcs/retrieve_path.c
Normal file
27
srcs/retrieve_path.c
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* retrieve_path.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: lperrey <lperrey@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2021/10/08 09:22:12 by lperrey #+# #+# */
|
||||||
|
/* Updated: 2021/12/08 06:27:23 by lperrey ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
char **retrieve_path(void)
|
||||||
|
{
|
||||||
|
char *path;
|
||||||
|
char **path_split;
|
||||||
|
|
||||||
|
path = getenv("PATH");
|
||||||
|
if (!path)
|
||||||
|
return (ft_retp_print(NULL, "minishell: Warning, $PATH not set\n", 2));
|
||||||
|
path_split = ft_split(path, ':');
|
||||||
|
if (!path_split)
|
||||||
|
return (ft_retp_perror(NULL, "retrieve_path()"));
|
||||||
|
return (path_split);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user