builtins/env.c \
builtins/export.c \
builtins/unset.c \
+ builtins/exit.c \
SOURCES := $(addprefix $(SRCDIR)/, $(SOURCES))
/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/26 11:39:54 by ljiriste #+# #+# */
-/* Updated: 2024/08/26 11:51:32 by ljiriste ### ########.fr */
+/* Updated: 2024/08/27 15:02:28 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
int ft_env(int argc, t_execution_env *env);
int export(int argc, char **argv, t_execution_env *env);
int unset(int argc, char **argv, t_execution_env *env);
+int execute_exit(int argc, char **argv, t_execution_env *env);
void unset_single(const char *var_name, t_execution_env *env);
/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/26 09:08:46 by ljiriste #+# #+# */
-/* Updated: 2024/08/27 13:37:07 by ljiriste ### ########.fr */
+/* Updated: 2024/08/27 15:03:52 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
int stdin_fd;
int stdout_fd;
int ret_val;
+ int may_exit;
+ int exit;
int last_was_builtin;
int last_builtin_ret_val;
t_vars *vars;
--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* exit.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/08/27 14:49:17 by ljiriste #+# #+# */
+/* Updated: 2024/08/27 15:05:25 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "builtins.h"
+#include "minishell_structs.h"
+#include <unistd.h>
+
+int is_numeric(const char *str)
+{
+ size_t i;
+
+ i = 0;
+ if (str[0] == '+' || str[0] == '-')
+ ++i;
+ while (str[i])
+ {
+ if (!ft_isdigit(str[i]))
+ return (0);
+ ++i;
+ }
+ return (1);
+}
+
+int execute_exit(int argc, char **argv, t_execution_env *env)
+{
+ if (env->may_exit)
+ env->exit = 1;
+ if (argc == 1)
+ return (0);
+ if (argc > 2)
+ {
+ ft_dprintf(STDERR_FILENO, "exit: too many arguments\n");
+ return (1);
+ }
+ if (!is_numeric(argv[1]))
+ {
+ ft_dprintf(STDERR_FILENO, "exit: %s: numeric argument required\n", argv[1]);
+ return (2);
+ }
+ return (ft_atoi(argv[1]));
+}
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/21 21:24:52 by ljiriste #+# #+# */
-/* Updated: 2024/08/27 10:34:14 by ljiriste ### ########.fr */
+/* Updated: 2024/08/27 15:02:02 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
env->stdin_fd = STDIN_FILENO;
env->stdout_fd = STDOUT_FILENO;
env->ret_val = 0;
+ env->exit = 0;
env->vars = malloc(sizeof(t_vars));
if (!env->vars)
return (1);
/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/21 08:57:54 by ljiriste #+# #+# */
-/* Updated: 2024/08/27 11:37:08 by ljiriste ### ########.fr */
+/* Updated: 2024/08/27 15:04:43 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
return (1);
if (!ft_strcmp(str, "unset"))
return (1);
+ if (!ft_strcmp(str, "exit"))
+ return (1);
return (0);
}
env->last_builtin_ret_val = export(count_fields(fields), fields, env);
else if (!ft_strcmp(fields[0], "unset"))
env->last_builtin_ret_val = unset(count_fields(fields), fields, env);
+ else if (!ft_strcmp(fields[0], "exit"))
+ env->last_builtin_ret_val = execute_exit(count_fields(fields), fields, env);
else
return (1);
env->last_was_builtin = 1;
if (pipeline->children.size == 1)
{
+ env->may_exit = (depth == 0);
if (ex_command(ft_vec_access(&pipeline->children, 0), env))
return (1);
+ env->may_exit = 0;
}
else
{
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/26 13:11:47 by ljiriste #+# #+# */
-/* Updated: 2024/08/27 14:31:19 by ljiriste ### ########.fr */
+/* Updated: 2024/08/27 15:08:06 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
int main(int argc, __attribute__((unused)) char **argv, char **envp)
{
+ int res;
char *line;
t_execution_env env;
clean_env(&env);
return (2);
}
- while (1)
+ while (env.exit == 0)
{
line = get_line();
- if (!line || !ft_strncmp(line, "exit", 4))
+ if (!line)
+ {
+ env.ret_val = 0;
break ;
+ }
handle_input(&line, &env);
free(line);
}
+ res = env.ret_val;
clean_env(&env);
parse(NULL, NULL);
terminate_input();
- free(line);
ft_printf("exit\n");
- return (0);
+ return (res);
}