-
Notifications
You must be signed in to change notification settings - Fork 0
/
simple_shell.c
86 lines (84 loc) · 1.62 KB
/
simple_shell.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
#include "main.h"
/**
* saverinos - main part
* @cmd: The full command
* @array: array of arguments
* @exiting: a pointer to exit
* @argv: name of the program
* Return: 0 or 1
*/
int saverinos(char **array, char *cmd, int *exiting, char *argv)
{
cmd = fullcmd(array[0]);
if (strcmp(array[0], "env") == 0 || access(cmd, X_OK) != -1)
{
saver(array, cmd);
return (1);
}
else
{
error_not_found(array[0], argv), *exiting = 127;
return (2);
}
return (0);
}
/**
* main - main function
* @argc: number of arguments
* @argv: command line arguments
* Return: number of arguments
*/
int main(int argc, char **argv)
{
char **array, *buffer = NULL, *cmd = NULL;
int num_of_char, exiting = 0, te = 0;
size_t buffer_size;
pid_t pid = 0;
for (;;)
{
if (isatty(0))
write(STDIN_FILENO, "<achux21> ", 10);
num_of_char = getline(&buffer, &buffer_size, stdin);
(num_of_char == -1) ? (free(buffer), exit(0)) : (void)0;
array = translate_array(buffer);
if (!array[0])
{
free(array);
continue;
}
if (!strcmp(array[0], "exit"))
{
exiting_advance(array, &exiting, argv[0]);
break;
}
pid = fork();
if (pid == 0)
{
te = saverinos(array, cmd, &exiting, argv[0]);
if (te == 1)
break;
else if (te == 2)
exit(exiting);
}
else
wait_child(&exiting);
free(array), (void)argc;
}
free(buffer);
if (exiting < 0)
exit_error(exiting, argv[0]), exiting = 2;
exit(exiting);
}
/**
* saver - Function That saved me from betty
* @array: array
* @cmd: cmd
*/
void saver(char **array, char *cmd)
{
if (strcmp(array[0], "env") == 0)
printenv();
else
execve(cmd, array, environ);
free(array);
}