forked from gutudanii/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
is_digit.c
99 lines (89 loc) · 1.61 KB
/
is_digit.c
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
#include "shell.h"
/**
* _isdigit - check for digit
* @c: int
* Return: int
*/
int _isdigit(int c)
{
if (c >= '0' && c <= '9')
{
return (1);
}
else
{
return (0);
}
}
/**
* check_word - evalute alpha chars in string
* @argv: arguments
* Return: 1 if no alpha chars, 0 if yes
*/
int check_word(char **argv)
{
int i;
for (i = 0; i < _strlen(argv[1]); i++)
if (!_isdigit(argv[1][i]))
return (0);
return (1);
}
/**
* exit_built_in - Handler of exit built-in command
* Description: exit stop the shell, exit(status)
* stops and 'return' status
* @p: struct with all vars inside
* Return: 1 if no alpha chars
*/
int exit_built_in(params *p)
{
if (p->argv[1])
{
if (check_word(p->argv))
{
int status = _atoi(p->argv[1]);
free(p->cmd);
free(p->buff);
free(p->argv);
exit(status);
}
else
{
int error_len;
char error_msg[64] = "";
char *cnt = _itoa(*(p->loop));
_strcpy(error_msg, p->name);
_strcat(error_msg, ": ");
_strcat(error_msg, cnt);
_strcat(error_msg, ": ");
_strcat(error_msg, p->argv[0]);
_strcat(error_msg, ": Illegal number: ");
_strcat(error_msg, p->argv[1]);
_strcat(error_msg, "\n");
free(cnt);
error_len = _strlen(error_msg);
write(2, error_msg, error_len);
p->exit_value = 2;
return (1);
}
}
free(p->cmd);
free(p->buff);
free(p->argv);
exit(p->exit_value);
}
/**
* env_built_in - Handler of exit built-in command
* @p: struct with all vars
*/
void env_built_in(params *p)
{
int i = 0;
while (environ[i] != NULL)
{
write(1, environ[i], _strlen(environ[i]));
write(1, "\n", 1);
i++;
}
free(p->cmd);
}