Minishell 1.0
42 School Minishell Project - A simple shell implementation
Yüklüyor...
Arıyor...
Eşleşme Yok
builtin_exit.c
Bu dosyanın dokümantasyonuna git.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* builtin_exit.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: hgenc <hgenc@student.42kocaeli.com.tr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2026/01/17 18:47:00 by hgenc #+# #+# */
9/* Updated: 2026/01/18 17:23:00 by hgenc ### ########.fr */
10/* */
11/* ************************************************************************** */
12
14#include <stdlib.h>
15#include <unistd.h>
16
17
18static int is_numeric(char *str)
19{
20 int i;
21
22 i = 0;
23 if (str[i] == '+' || str[i] == '-')
24 i++;
25 if (!str[i])
26 return (0);
27 while (str[i])
28 {
29 if (!ft_isdigit(str[i]))
30 return (0);
31 i++;
32 }
33 return (1);
34}
35
36static int check_overflow(unsigned long long num, char digit)
37{
38 if (num > LLONG_MAX_VAL / 10)
39 return (1);
40 if (num == LLONG_MAX_VAL / 10
41 && (unsigned long long)(digit - '0') > LLONG_MAX_VAL % 10)
42 return (1);
43 return (0);
44}
45
46static int get_exit_code(char *str)
47{
48 unsigned long long num;
49 int sign;
50 int i;
51
52 num = 0;
53 sign = 1;
54 i = 0;
55 if (str[i] == '-')
56 {
57 sign = -1;
58 i++;
59 }
60 else if (str[i] == '+')
61 i++;
62 while (str[i])
63 {
64 if (check_overflow(num, str[i]))
65 return (-1);
66 num = num * 10 + (str[i] - '0');
67 i++;
68 }
69 return ((int)(((long long)(num * sign) % 256 + 256) % 256));
70}
71
72static void exit_numeric_error(t_shell *shell, char *arg)
73{
74 shell_error("exit", arg, "numeric argument required");
75 shell_exit(shell, 2);
76}
77
78int builtin_exit(t_shell *shell, t_cmd *cmd)
79{
80 int exit_code;
81
82 ft_putendl_fd("exit", STDERR_FILENO);
83 if (!cmd->args[1])
84 shell_exit(shell, shell->exit_status);
85 if (cmd->args[2])
86 {
87 if (!is_numeric(cmd->args[1]))
88 exit_numeric_error(shell, cmd->args[1]);
89 shell_error("exit", NULL, "too many arguments");
90 return (2);
91 }
92 if (!is_numeric(cmd->args[1]))
93 exit_numeric_error(shell, cmd->args[1]);
94 exit_code = get_exit_code(cmd->args[1]);
95 if (exit_code == -1)
96 exit_numeric_error(shell, cmd->args[1]);
97 shell_exit(shell, exit_code);
98 return (0);
99}
static void exit_numeric_error(t_shell *shell, char *arg)
static int is_numeric(char *str)
static int get_exit_code(char *str)
static int check_overflow(unsigned long long num, char digit)
int builtin_exit(t_shell *shell, t_cmd *cmd)
void shell_exit(t_shell *shell, int exit_code)
Shell'i temizleyip programdan çıkar.
Definition cleanup.c:76
void shell_error(char *cmd, char *arg, char *msg)
Minishell ana header dosyası
#define LLONG_MAX_VAL
Definition minishell.h:46
struct s_cmd t_cmd
struct s_shell t_shell
char ** args
Definition minishell.h:129
int exit_status
Definition minishell.h:153