forked from thecamo1509/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell_01.c
More file actions
81 lines (79 loc) · 3.17 KB
/
shell_01.c
File metadata and controls
81 lines (79 loc) · 3.17 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
#include "functions.h"
int main(int argc, char *argv[], char **env)
{
char *cont, *result;
char **tokenizado;
int status = 0, val_fd = 0, comp = 0, i = 0;
ssize_t c;
pid_t hijo;
while (1)
{
hijo = fork();
if (hijo == -1)
{
perror("Error: hijo");
return (-1);
}
val_fd = isatty(STDIN_FILENO);
if (hijo == 0)
{
if (argc == 1)
{
if (val_fd != 0)
write(1, "#cisfun$ ", 10);
cont = read_line(&c);
if (c == EOF)
{
free(cont);
write(1, "\n", 2);
exit(3);
}
if (*cont == '\n') /* enter */
{
free(cont);
return (0);
}
tokenizado = words(cont, " \t\r\n");
comp = _strcmp(tokenizado[0], "exit");
if (comp == 0 && tokenizado[1] == NULL)
{
free(tokenizado);
free(cont);
exit (3);
}
comp = _strcmp(tokenizado[0], "env");
if (comp == 0)
{
while (env[i] != NULL)
{
write(1, env[i], _strlen(env[i]));
write(1, "\n", 1);
i++;
}
}
result = l_path(tokenizado[0], env);
if (execve(result, tokenizado, NULL) == -1)
{
free(result);
free(tokenizado);
perror(argv[0]);
if (val_fd == 0)
exit (2);
return (1);
}
}
}
else
{
wait(&status);
status = WEXITSTATUS(status);
if (status == 2)
exit(127);
if (val_fd == 0 || status == 3)
{
break;
}
}
}
return (0);
}