maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
exit_status.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* exit_status.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: dmdemirk <dmdemirk@student.42london.c +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/09/06 11:34:39 by dmdemirk #+# #+# */
9/* Updated: 2024/09/06 11:48:50 by dmdemirk ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "exit_status.h"
14#include "libft.h"
15#include <unistd.h>
16#include "shell.h"
17
18#include <stdio.h>
19
20void exit_status_handler(t_ms_data *data, int status_code, char *err_arg);
21void ft_puterror(char *err_arg, char *error_message);
22void set_exit_status(int *exit_status, int status_code);
24
25void exit_status_handler(t_ms_data *data, int status_code, char *err_arg)
26{
27 char *error_message;
28
29 error_message = NULL;
30 if (status_code == IS_DIRECTORY)
31 error_message = "No such file or directory";
32 else if (status_code == NUMERIC_REQUIRED)
33 error_message = "numeric argument required";
34 else if (status_code == TOO_MANY_ARGS)
35 error_message = "too many arguments";
36 else if (status_code == UNKNOWN_COMMAND)
37 error_message = "command not found";
38 else if (status_code == INVALID_ARGUMENT)
39 error_message = "invalid argument";
40 else if (status_code == PERMISSION_DENIED)
41 error_message = "permission denied";
42 else if (status_code == NOT_VALID_IDENTIFIER)
43 error_message = "unset: not a valid identifier";
44 else if (status_code == INVALID_OPTION)
45 error_message = "invalid option";
46 else
47 error_message = "error";
48 ft_puterror(err_arg, error_message);
49 set_exit_status(&data->exit_status, status_code);
51}
52
54{
55 char *tmp_var;
56
57 tmp_var = ft_itoa(data->exit_status);
58 set_shell_var(&data->shell_variables, "?", tmp_var);
59 free(tmp_var);
60}
61
62void ft_puterror(char *err_arg, char *error_message)
63{
64 if (err_arg)
65 {
66 ft_putstr_fd("bash: ", STDERR_FILENO);
67 ft_putstr_fd(err_arg, STDERR_FILENO);
68 ft_putstr_fd(": ", STDERR_FILENO);
69 ft_putendl_fd(error_message, STDERR_FILENO);
70 }
71 else
72 {
73 ft_putstr_fd("bash: ", STDERR_FILENO);
74 ft_putendl_fd(error_message, STDERR_FILENO);
75 }
76}
77
78void set_exit_status(int *exit_status, int status_code)
79{
80 *exit_status = status_code;
81}
void set_shell_var(t_env **shell_var, const char *key, const char *value)
void exit_status_handler(t_ms_data *data, int status_code, char *err_arg)
Definition exit_status.c:25
void ft_puterror(char *err_arg, char *error_message)
Definition exit_status.c:62
void set_shell_var_handler(t_ms_data *data)
Definition exit_status.c:53
void set_exit_status(int *exit_status, int status_code)
Definition exit_status.c:78
#define NUMERIC_REQUIRED
Definition exit_status.h:21
#define TOO_MANY_ARGS
Definition exit_status.h:22
#define NOT_VALID_IDENTIFIER
Definition exit_status.h:27
#define INVALID_OPTION
Definition exit_status.h:28
#define PERMISSION_DENIED
Definition exit_status.h:25
#define UNKNOWN_COMMAND
Definition exit_status.h:23
#define INVALID_ARGUMENT
Definition exit_status.h:24
#define IS_DIRECTORY
Definition exit_status.h:20
char * ft_itoa(int n)
Definition ft_itoa.c:40
void ft_putstr_fd(char *s, int fd)
void ft_putendl_fd(char *s, int fd)
int exit_status
Definition shell.h:30
t_env * shell_variables
Definition shell.h:25