-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecve.c
More file actions
51 lines (49 loc) · 1020 Bytes
/
execve.c
File metadata and controls
51 lines (49 loc) · 1020 Bytes
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
#include "shell.h"
/**
* _execv - executes a command
* @order_words: array of arguments
* @argv: previous pointer
* @n: old size of previous pointer
* @ix: new size for our pointer
* Return: array of pointers to words
*/
int _execv(char **order_words, int n, char **argv, int ix)
{
char *cmd;
pid_t pt;
int st;
if (string_cmp(order_words[0], "exit") == 0)
{
char *k;
int h;
h = 0;
if (n >= 2)
{
k = strdup(order_words[1]);
h = atoi(k);
free(k);
k = NULL; }
else if (ix > 1)
h = ix;
free_the_array(order_words);
exit(h); }
cmd = get_path(order_words[0]);
if (cmd == NULL)
{
_printerror(argv[0], order_words[0], ix);
free_the_array(order_words);
return (127); }
pt = fork();
if (pt == 0)
{
if (execve(cmd, order_words, environ) == -1)
{
free(cmd);
cmd = NULL;
free_the_array(order_words); } }
else
{
waitpid(pt, &st, 0);
free_the_array(order_words);
free(cmd), cmd = NULL; }
return (WEXITSTATUS(st)); }