-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargs2.c
More file actions
executable file
·127 lines (115 loc) · 1.71 KB
/
args2.c
File metadata and controls
executable file
·127 lines (115 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include "shell.h"
/**
* exit_checker - handle arguments
* for the built-in exit
*
* @index: index
* @input: input
* @args: arguments
*/
void exit_checker(char ***args, char **input, int index)
{
int sts = 128;
if (args && *args)
{
if (_strcmp((*args)[0], "exit") == 0)
{
if (index > 1)
{
sts = 130;
}
if (!(*args)[1])
{
free(*input);
free_array(*args);
exit(sts % 128);
}
if (is_letter((*args)[1]) || atoi((*args)[1]) < 0)
{
sts = 130;
fprintf(stderr,
"./hsh: 1: exit: Illegal number: %s\n",
(*args)[1]);
}
if ((*args)[1] && is_digit((*args)[1]))
{
sts = atoi((*args)[1]);
if (sts == 1000)
{
free(*input);
free_array(*args);
exit(232);
}
}
free(*input);
free_array(*args);
exit(sts % 128);
}
}
}
/**
* exit_message - handle arguments
* for the built-in exit
*
* @index: index
* @input: input
* @args: arguments
*/
void exit_message(char ***args, char **input, int *index)
{
if (*args != NULL)
{
fprintf(stderr,
"./hsh: %d: %s: not found\n",
*index, *args[0]);
}
free(*input);
free_array(*args);
exit(127);
}
/**
* is_digit - Check if the str is digit
*
* @str: the string
*
* Return: 0 or 1
*/
int is_digit(char *str)
{
int i;
if (str == NULL)
{
return (0);
}
for (i = 0; str[i] != '\0'; i++)
{
if (!(str[i] >= '0' && str[i] <= '9'))
{
return (0);
}
}
return (1);
}
/**
* is_letter - Check if the str is a letter
*
* @str: the string
*
* Return: 0 if Null, 1 if not Null
*/
int is_letter(char *str)
{
int i;
if (str == NULL)
{
return (0);
}
for (i = 0; str[i] != '\0'; i++)
{
if ((str[i] < '0' || str[i] > '9'))
{
return (1);
}
}
return (0);
}