maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
exit.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* exit.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/06/03 16:32:42 by dmdemirk #+# #+# */
9/* Updated: 2024/09/09 13:10:47 by dmdemirk ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "shell.h"
14#include "libft.h"
15#include <stdlib.h>
16#include <stdio.h>
17#include "env.h"
18#include "exit_status.h"
19#include "tokens.h"
20#include "signals.h"
21#include "builtins.h"
22
23/*
24Functionality:
25- Exit the shell
26- Free the environment
27- Exit with the exit status
28 */
29
36
37void handle_numeric_error(t_ms_data *data, const char *arg)
38{
39 char *temp_str;
40
41 temp_str = ft_strjoin("exit: ", arg);
43 free(temp_str);
44 handle_exit(data, 0);
45}
46
52
53void handle_exit(t_ms_data *data, int status)
54{
55 if (status != 0 && status != TOO_MANY_ARGS)
56 ft_putendl_fd("exit", STDOUT_FILENO);
58 exit(status);
59}
60
62{
63 int number;
64
65 number = 0;
66 if (!data->args || !data->args[1])
67 {
68 ft_free_2d_arr(data->args);
69 data->args = NULL;
70 ft_putendl_fd("exit", STDOUT_FILENO);
71 handle_exit(data, 0);
72 return (0);
73 }
74 if (ft_isnumber(data->args[1]) == 0)
75 handle_numeric_error(data, data->args[1]);
76 else if (data->args[2])
78 else
79 {
80 number = ft_atoi(data->args[1]);
81 ft_free_2d_arr(data->args);
82 data->args = NULL;
83 handle_exit(data, number);
84 }
85 return (EXIT_SUCCESS);
86}
void free_signal_context(void)
void ft_free_2d_arr(char **arr)
void handle_exit(t_ms_data *data, int status)
Definition exit.c:53
int builtin_exit(t_ms_data *data)
Definition exit.c:61
void handle_too_many_args_error(t_ms_data *data)
Definition exit.c:47
void cleanup_exit_resources(t_ms_data *data)
Definition exit.c:30
void handle_numeric_error(t_ms_data *data, const char *arg)
Definition exit.c:37
#define NUMERIC_REQUIRED
Definition exit_status.h:21
#define TOO_MANY_ARGS
Definition exit_status.h:22
#define EXIT_SUCCESS
Definition exit_status.h:16
int ft_isnumber(char *str)
void exit_status_handler(t_ms_data *data, int status_code, char *err_arg)
Definition exit_status.c:25
char * ft_strjoin(char const *s1, char const *s2)
Definition ft_strjoin.c:23
void ft_putendl_fd(char *s, int fd)
int ft_atoi(const char *str)
char ** args
Definition shell.h:23
void clear_history_file(void)
void free_ms_data(t_ms_data *data)
Definition clean.c:51