new Makefile compile libft without relink. change on exec for forker. change on all builtin for return status & set exit status

This commit is contained in:
wescande 2017-03-20 14:42:49 +01:00
parent d2c65880aa
commit 229864750f
16 changed files with 136 additions and 109 deletions

View file

@ -6,14 +6,14 @@
# By: wescande <wescande@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2016/08/29 21:32:58 by wescande #+# #+# #
# Updated: 2017/03/20 11:37:21 by wescande ### ########.fr #
# Updated: 2017/03/20 12:04:31 by wescande ### ########.fr #
# #
# **************************************************************************** #
NAME = 42sh
CC = gcc
FLAGS = -Wall -Wextra -Werror -g
FLAGS = -Wall -Wextra -Werror -fvisibility=hidden
D_FLAGS = -g
DELTA = $$(echo "$$(tput cols)-47"|bc)
@ -40,6 +40,7 @@ builtin/builtin_hash.c\
builtin/builtin_history.c\
builtin/builtin_math.c\
builtin/builtin_read.c\
builtin/builtin_return_status.c\
builtin/builtin_setenv.c\
builtin/builtin_unset.c\
builtin/builtin_unsetenv.c\
@ -302,8 +303,8 @@ NB = $(words $(SRC_BASE))
INDEX = 0
all :
@make -j -C $(LIBFT_DIR)
@make $(NAME)
@make -C $(LIBFT_DIR)
@make -j $(NAME)
$(NAME): $(LIBFT_LIB) $(OBJ_DIR) $(OBJS)
@$(CC) $(OBJS) -o $(NAME) \

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 22:59:57 by jhalford #+# #+# */
/* Updated: 2017/03/17 18:59:55 by ariard ### ########.fr */
/* Updated: 2017/03/20 12:51:23 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,6 +20,7 @@
# define BT_EXPORT_LP (1 << 0)
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[]);
int builtin_unset(const char *path, char *const av[], char *const envp[]);
int builtin_env(const char *path, char *const argv[], char *const envp[]);

View file

@ -6,7 +6,7 @@
# By: jhalford <jack@crans.org> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2017/02/07 16:09:36 by jhalford #+# #+# #
# Updated: 2017/03/18 14:15:35 by gwojda ### ########.fr #
# Updated: 2017/03/20 12:05:06 by wescande ### ########.fr #
# #
# **************************************************************************** #
@ -207,7 +207,7 @@ all:
$(NAME): $(OBJ_DIR) $(OBJS)
@$(AR) $(NAME) $(OBJS)
@ranlib $(NAME)
@strip -x $(NAME)
@# @strip -x $(NAME)
@printf "\r\033[48;5;15;38;5;25m✅ MAKE $(NAME)\033[0m\033[K\n"
$(OBJ_DIR)%.o: $(SRC_DIR)%.c | $(OBJ_DIR)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/12/03 11:57:53 by jhalford #+# #+# */
/* Updated: 2017/03/16 14:17:57 by jhalford ### ########.fr */
/* Updated: 2017/03/20 12:54:02 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -81,14 +81,14 @@ int builtin_cd(const char *path,
opts = CDOPT_L;
i = builtin_cd_opts(av, &opts);
if (!(target = builtin_cd_special(av + i, envp)))
return (1);
return (builtin_return_status(0, 1));
cwd = getcwd(NULL, 0);
builtin_setenv(path, (char*[4]){"setenv", "OLDPWD", cwd, NULL}, envp);
free(cwd);
if (chdir(target))
{
ft_dprintf(2, CDERR_1, target);
return (1);
return (builtin_return_status(0, 1));
}
else if (target != av[i])
ft_printf("%s\n", target);
@ -97,5 +97,5 @@ int builtin_cd(const char *path,
free(cwd);
if (!ft_strcmp(*(av + i), "-"))
free(target);
return (0);
return (builtin_return_status(0, 0));
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:21:41 by jhalford #+# #+# */
/* Updated: 2017/01/20 00:10:31 by ariard ### ########.fr */
/* Updated: 2017/03/20 14:40:00 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -25,5 +25,5 @@ int builtin_echo(const char *path, char *const av[], char *const envp[])
ft_putchar(' ');
}
ft_putchar('\n');
return (0);
return (builtin_return_status(0, 0));
}

View file

@ -6,11 +6,12 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:14:20 by jhalford #+# #+# */
/* Updated: 2017/03/20 11:29:17 by wescande ### ########.fr */
/* Updated: 2017/03/20 12:52:05 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)
{
@ -18,9 +19,8 @@ static int env_usage(int arg_miss, char c)
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");
set_exitstatus(1, 1);
return (0);
ft_dprintf(2, "usage: %s\n", US_ENV);
return (1);
}
static void env_freeone(char **env, char *arg)
@ -87,7 +87,8 @@ static int env_treat_flag(char ***custom_env, char *const *arg[])
return (0);
}
int builtin_env(const char *path, char *const argv[], char *const envp[])
int builtin_env(const char *path,
char *const argv[], char *const envp[])
{
char **env;
pid_t pid;
@ -95,12 +96,12 @@ int builtin_env(const char *path, char *const argv[], char *const envp[])
(void)path;
pid = 0;
if (!argv || ft_strcmp(*argv, "env"))
return (env_usage(0, 0));
return (builtin_return_status(0, env_usage(0, 0)));
env = ft_sstrdup((char **)envp);
if (env_treat_flag(&env, &argv))
{
ft_sstrfree(env);
return (0);
return (builtin_return_status(0, 1));
}
if (!*argv)
{
@ -111,6 +112,5 @@ int builtin_env(const char *path, char *const argv[], char *const envp[])
else
pid = command_setoutput(NULL, argv, env, 0);
ft_tabdel(&env);
set_exitstatus(0, 1);
return (pid);
return (builtin_return_status(pid, 0));
}

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/15 11:39:37 by gwojda #+# #+# */
/* Updated: 2017/03/18 11:46:32 by gwojda ### ########.fr */
/* Updated: 2017/03/20 14:01:11 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -47,7 +47,7 @@ int builtin_export(
if (cliopts_get((char**)av, export_opts, &data))
ft_perror();
if (data.flag & BT_EXPORT_LP)
return (bt_export_print());
return (builtin_return_status(0, bt_export_print()));
av = data.av_data;
while (*av)
{
@ -64,5 +64,5 @@ int builtin_export(
builtin_unsetenv("internal", (char*[]){"local", *av, NULL}, NULL);
av++;
}
return (0);
return (builtin_return_status(0, 0));
}

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/18 16:37:43 by gwojda #+# #+# */
/* Updated: 2017/02/18 16:51:43 by gwojda ### ########.fr */
/* Updated: 2017/03/20 14:03:30 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -43,8 +43,8 @@ int builtin_hash(const char *path, char *const av[], char *const envp[])
(void)path;
(void)envp;
if (ft_hash_opt(av))
return (0);
if (!av || ft_hash_opt(av))
return (builtin_return_status(0, 1));
i = 0;
while (i < MAX_HASH)
{
@ -52,5 +52,5 @@ int builtin_hash(const char *path, char *const av[], char *const envp[])
ft_hash_lst_print(g_hash[i]);
++i;
}
return (0);
return (builtin_return_status(0, 0));
}

View file

@ -6,7 +6,7 @@
/* By: gwojda <gwojda@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/24 14:54:53 by gwojda #+# #+# */
/* Updated: 2017/02/20 20:30:18 by ariard ### ########.fr */
/* Updated: 2017/03/20 14:04:02 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -23,7 +23,7 @@ int builtin_history(const char *path, char *const av[], char *const envp[])
head = data_singleton()->line.list_end;
len = 1;
if (!head)
return (0);
return (builtin_return_status(0, 0));
if (head && !head->str)
head = head->next;
while (head && head->str)
@ -34,5 +34,5 @@ int builtin_history(const char *path, char *const av[], char *const envp[])
++len;
head = head->next;
}
return (0);
return (builtin_return_status(0, 0));
}

View file

@ -6,26 +6,26 @@
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/17 18:54:00 by ariard #+# #+# */
/* Updated: 2017/03/17 19:59:42 by ariard ### ########.fr */
/* Updated: 2017/03/20 14:14:59 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
# define MATHERR_0 "math : invalid number of arguments\n"
# define MATHERR_0 "math : invalid number of arguments\n"
# define MATHERR_1 "math : invalid variable name\n"
# define MATHERR_2 "math : invalid operator\n"
# define MATHERR_3 "math : invalid operand\n"
# define MATHERR_4 "math : division by 0\n"
# define MATHERR_4 "math : division by 0\n"
static int error_msg(char *msg)
{
ft_dprintf(2, "{red}%s{eoc}", msg);
return (-1);
return (1);
}
static int init_math(char **var, char **value, char **operator,
char **operand)
char **operand)
{
*var = NULL;
*value = NULL;
@ -45,71 +45,70 @@ static int get_value(char *var, char **value)
ft_strdel(&esc);
if (!ret)
return (error_msg(MATHERR_1));
temp = ft_sstrstr(data_singleton()->local_var, var);
if (temp)
{
temp += ft_strlenchr(temp, '=') + 1;
*value = ft_strdup(temp);
if (!(ft_stris(*value, &ft_isdigit)))
{
ft_strdel(value);
*value = ft_itoa(0);
}
}
else
*value = ft_itoa(0);
return (0);
temp = ft_sstrstr(data_singleton()->local_var, var);
if (temp)
{
temp += ft_strlenchr(temp, '=') + 1;
*value = ft_strdup(temp);
if (!(ft_stris(*value, &ft_isdigit)))
{
ft_strdel(value);
*value = ft_itoa(0);
}
}
else
*value = ft_itoa(0);
return (0);
}
static int do_math(char **value, char *operator, char *operand)
{
long ope1;
long ope2;
long ope1;
long ope2;
ope1 = ft_atoi(*value);
if (operand)
ope2 = ft_atoi(operand);
else
ope2 = 0;
if ((operator[0] == '/' || operator[0] == '%') && ope2 == 0)
return (error_msg(MATHERR_4));
else
{
ope1 = (operator[0] == '+') ? ope1 + ope2 : ope1;
ope1 = (operator[0] == '-') ? ope1 - ope2 : ope1;
ope1 = (operator[0] == '/') ? ope1 / ope2 : ope1;
ope1 = (operator[0] == '*') ? ope1 * ope2 : ope1;
ope1 = (operator[0] == '%') ? ope1 % ope2 : ope1;
}
ft_strdel(value);
*value = ft_itoa(ope1);
return (0);
ope1 = ft_atoi(*value);
if (operand)
ope2 = ft_atoi(operand);
else
ope2 = 0;
if ((operator[0] == '/' || operator[0] == '%') && ope2 == 0)
return (error_msg(MATHERR_4));
else
{
ope1 = (operator[0] == '+') ? ope1 + ope2 : ope1;
ope1 = (operator[0] == '-') ? ope1 - ope2 : ope1;
ope1 = (operator[0] == '/') ? ope1 / ope2 : ope1;
ope1 = (operator[0] == '*') ? ope1 * ope2 : ope1;
ope1 = (operator[0] == '%') ? ope1 % ope2 : ope1;
}
ft_strdel(value);
*value = ft_itoa(ope1);
return (0);
}
int builtin_math(const char *path, char *const av[], char *const envp[])
{
char *var;
char *value;
char *operator;
char *operand;
char *var;
char *value;
char *operator;
char *operand;
(void)path;
(void)envp;
if (!av || !av[1] || !av[2] || !av[3] || av[4])
return (error_msg(MATHERR_0));
init_math(&var, &value, &operator, &operand);
var = av[1];
if (get_value(var, &value) == -1)
return (-1);
operator = av[2];
if (!(ft_strlen(operator) == 1 && (operator[0] == '+' || operator[0] == '-'
|| operator[0] == '/' || operator[0] == '*' || operator[0] == '%')))
return (error_msg(MATHERR_2));
operand = av[3];
if (!ft_stris(operand, &ft_isdigit))
return (error_msg(MATHERR_3));
if (do_math(&value, operator, operand) == -1)
return (-1);
builtin_setenv("setenv", (char *[]){"local", var, value, 0}, data_singleton()->local_var);
return (0);
(void)path;
(void)envp;
if (!av || !av[1] || !av[2] || !av[3] || av[4])
return (builtin_return_status(0, error_msg(MATHERR_0)));
init_math(&var, &value, &operator, &operand);
var = av[1];
if (get_value(var, &value))
return (builtin_return_status(0, 1));
operator = av[2];
if (ft_strlen(operator) != 1 || !(ft_strchr("+-/*%", operator[0])))
return (builtin_return_status(0, error_msg(MATHERR_2)));
operand = av[3];
if (!ft_stris(operand, &ft_isdigit))
return (builtin_return_status(0, error_msg(MATHERR_3)));
if (do_math(&value, operator, operand))
return (builtin_return_status(0, 1));
builtin_setenv("setenv", (char *[]){"local", var, value, 0}, data_singleton()->local_var);
return (builtin_return_status(0, 0));
}

View file

@ -6,11 +6,13 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/01/20 15:01:45 by jhalford #+# #+# */
/* Updated: 2017/03/20 08:31:25 by jhalford ### ########.fr */
/* Updated: 2017/03/20 12:53:06 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#define US_READ "read [-ers] [-u fd] [-t timeout] [-p prompt]"
#define US_READ_1 "[-n nchars] [-d delim] [name ...]"
t_cliopts g_read_opts[] =
{
@ -26,8 +28,7 @@ t_cliopts g_read_opts[] =
void bt_read_usage(void)
{
ft_dprintf(2, "{red}read: usage: read [-ers] [-u fd] [-t timeout]");
ft_dprintf(2, " [-p prompt] [-n nchars] [-d delim] [name ...]{eoc}\n");
ft_dprintf(2, "usage: read %s %s\n", US_READ, US_READ_1);
}
int bt_read_init(t_read *data, char **av)
@ -117,5 +118,5 @@ int builtin_read(const char *path, char *const av[], char *const envp[])
bt_read_usage();
if (ret != 2)
bt_read_exit(&data);
return (ret);
return (builtin_return_status(0, ret));
}

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* builtin_return_status.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wescande <wescande@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/20 12:48:04 by wescande #+# #+# */
/* Updated: 2017/03/20 12:49:18 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int builtin_return_status(int ret, int status)
{
set_exitstatus(status, 1);
return (ret);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */
/* Updated: 2017/03/20 08:24:10 by jhalford ### ########.fr */
/* Updated: 2017/03/20 14:38:25 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -39,10 +39,9 @@ int builtin_setenv(const char *path,
{
char ***env;
(void)envp;
(void)path;
if (!av || !av[0])
return (1);
return (builtin_return_status(0, 1));
if (ft_strcmp(av[0], "local") == 0)
env = &data_singleton()->local_var;
else
@ -53,6 +52,6 @@ int builtin_setenv(const char *path,
ft_putchar('\n');
}
else
return (assign_var(av, env));
return (0);
assign_var(av, env);
return (envp ? builtin_return_status(0, 0) : 0);
}

View file

@ -6,7 +6,7 @@
/* 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 */
/* Updated: 2017/03/20 14:27:43 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,5 +14,13 @@
int builtin_unset(const char *path, char *const av[], char *const envp[])
{
char *tmp;
char **av_tmp;
tmp = av[0];
av_tmp = (char **)av;
av_tmp[0] = "local";
builtin_unsetenv(path, av, envp);
av_tmp[0] = tmp;
return (builtin_unsetenv(path, av, envp));
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jhalford@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:29:17 by jhalford #+# #+# */
/* Updated: 2017/03/18 11:47:53 by gwojda ### ########.fr */
/* Updated: 2017/03/20 14:40:05 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -35,5 +35,5 @@ int builtin_unsetenv(const char *path, char *const av[], char *const envp[])
}
i++;
}
return (0);
return (builtin_return_status(0, 0));
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 14:25:17 by jhalford #+# #+# */
/* Updated: 2017/03/14 00:54:47 by ariard ### ########.fr */
/* Updated: 2017/03/20 14:38:34 by wescande ### ########.fr */
/* */
/* ************************************************************************** */
@ -32,7 +32,6 @@ void set_exitstatus(int status, int override)
}
}
astatus = ft_itoa(exitval);
builtin_setenv("setenv", (char*[]){"setenv", "?", astatus, 0},
data_singleton()->env);
builtin_setenv("setenv", (char*[]){"setenv", "?", astatus, 0}, NULL);
ft_strdel(&astatus);
}