-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuiltin.c
141 lines (132 loc) · 2.5 KB
/
builtin.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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "shell.h"
/**
* checkBuiltin - check for built in and run associated command
* @cmd: global argtok0
* Return: integer
*/
int (*checkBuiltin(char *cmd))(void)
{
int i = 0;
builtins s_spec[] = {
{"exit", exitB},
{"env", envB},
/* {"cd", cdB}, */
/* {"setenv", setenvB}, */
/* {"unsetenv", unsetenvB}, */
/* {"help", helpB}, */
/* {"history", historyB}, */
{NULL, NULL}
};
while (s_spec[i].str != NULL)
{
if (_strcmp(s_spec[i].str, cmd) == 0)
return (s_spec[i].fptr);
i++;
}
return (NULL);
}
/**
* envB - if command 'env' is passed print environ
* Description: print out environment list
* Return: 1 for success 0 for fail
*/
int envB(void)
{
char **cpy = environ;
while (*cpy != NULL)
{
write(STDOUT_FILENO, (*cpy), _strlen(*cpy));
write(STDOUT_FILENO, "\n", 1);
cpy++;
}
return (0);
}
/**
* exitB - free allocated stuff and then exit
* Return: 2 on failure
*/
int exitB(void)
{
long int manual_exit, i = 1;
if (globes.argTokes[1] != NULL)
{
if (globes.argTokes[1][0] < '0' || globes.argTokes[1][0] > '9')
if (globes.argTokes[1][0] != '+')
{
errno = EILLEGALNUMB, _error();
return (2);
}
for (; globes.argTokes[1][i]; i++)
if (globes.argTokes[1][i] < '0' && globes.argTokes[1][i] > '9')
{
errno = EILLEGALNUMB, _error();
return (2);
}
if (_strlen(globes.argTokes[1]) > 11)
{
errno = EILLEGALNUMB, _error();
return (2);
}
if (globes.argTokes[1][0] == '+')
manual_exit = _atoi(&globes.argTokes[1][1]);
else
manual_exit = _atoi(globes.argTokes[1]);
if (manual_exit > 2147483647l)
{
errno = EILLEGALNUMB, _error();
return (2);
}
sillyFree();
exit(manual_exit & 0377);
}
else
{
sillyFree();
exit(globes.last_exit_status);
}
}
/**
* sillyFree - only purpose is to reduce 4 lines to make code betty compliant
*
* Return: void
*/
void sillyFree(void)
{
free(globes.line);
freeTokes(globes.argTokes);
freeTokes(globes.pathTokes);
}
/**
* _atoi - convert a char array into an int
* @str: char string to convert
*
* Return: integer rep of char str
*/
long int _atoi(char *str)
{
long int exit_status = 0;
int i = 0;
char *cpy = str;
for (; cpy[i]; i++)
;
i--;
for (; *cpy; cpy++, i--)
exit_status += (*cpy - '0') * _pow(10, i);
return (exit_status);
}
/**
* _pow - returns val of x raised to y
* @x: base int
* @y: power int
*
* Return: val of x raised to y
*/
long int _pow(int x, int y)
{
if (y < 0)
return (-1);
else if (y == 0)
return (1);
else
return (x * _pow(x, y - 1));
}