-
Notifications
You must be signed in to change notification settings - Fork 3
/
funexe.c
168 lines (155 loc) · 2.94 KB
/
funexe.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include "holberton.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
/**
* funexc - call execvp or buitin functions.
* @h: pointer to the head of the linked list
* Return: no return
*/
void funexc(command_t *h)
{
command_t *copy = NULL;
int pos = 0;
static int cont;
while (h)
{
cont++;
h->cont = cont;
copy = h;
if (h->id == -1)
imprimir_error(h);
if (h->id == 0)
_extern(h);
else
_built(h);
h = h->next;
freecommand(copy);
pos++;
}
}
/**
* _extern - call execvp or buitin functions.
* @h: pointer to the head of the linked list
*
* Return: no return
*/
void _extern(command_t *h)
{
int status = 0, pid = 0;
int ex = 0;
char **env = _setenv(NULL, NULL);
if (!h->args)
printf("no sirve esta mierda\n");
pid = fork();
if (pid == 0)
{
status = execve(*h->args, h->args, env);
if (status == -1)
salir(h);
}
else
{
wait(&ex);
ex = ex % 255;
setstatus(&ex);
}
}
/**
* _built - call different builtins.
* @h: node tha has the builtin command
*
* Return: no return
*/
void _built(command_t *h)
{
int j = 0, entero = 0, i = 0;
char **argseach = NULL;
builtin commandsbuilt[] = {{"history", _history}, {"exit", salir},
{"env", _env}, {"help", _help},
{"cd", _cd}, {"setenv", _setenviron},
{"unsetenv", _unsetenv}, {"alias", _alias},
{NULL}};
entero = 0;
i = 0;
argseach = h->args;
while (commandsbuilt[i].built != NULL)
{
entero = 0;
for (j = 0; argseach[0][j] != '\0'; j++)
{
if (argseach[0][j] - commandsbuilt[i].built[j] != 0)
{
entero = argseach[0][j] - commandsbuilt[i].built[j];
break;
}
}
if (entero == 0)
{
commandsbuilt[i].f(h);
return;
}
i++;
}
if (entero == 0 && argseach[0][j] != '\0')
entero = argseach[0][j] - commandsbuilt[i].built[j];
}
/**
* print_err_exit - print error when command not found.
* @h: node tha has the builtin command
*
* Return: no return
*/
void print_err_exit(command_t *h)
{
char *err = h->name;
char *sp = ": ";
char *err1 = ": exit: Illegal number: ";
char *num = print_number(h->cont);
char sl = 10;
write(STDERR_FILENO, err, _strlen(err) - 1);
write(STDERR_FILENO, sp, _strlen(sp) - 1);
write(STDERR_FILENO, num, _strlen(num) - 1);
write(STDERR_FILENO, err1, _strlen(err1) - 1);
write(STDERR_FILENO, h->args[1], _strlen(h->args[1]) - 1);
write(STDERR_FILENO, &sl, 1);
free(num);
}
/**
* salir - exit from the function.
* @h: copy of head of linked list
*
* Return: no return
*/
int salir(command_t *h)
{
command_t *cpy = NULL;
char **env = NULL;
alias *al = NULL, *ali = NULL;
int i = 0;/*, res = 0;*/
char *buffer = NULL;
buffer = getpath();
while (h)
{
cpy = h->next;
freecommand(h);
h = cpy;
}
env = _setenv(NULL, NULL);
for (; env[i]; i++)
free(env[i]);
free(env[i]);
free(env);
al = setalias(NULL);
ali = al;
while (al)
{
free(al->name);
free(al->value);
ali = al->next;
free(al);
al = ali;
}
free(buffer);
exit(setstatus(NULL));
}