-
Notifications
You must be signed in to change notification settings - Fork 0
/
excmd.c
46 lines (45 loc) · 897 Bytes
/
excmd.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
#include "header.h"
/**
* execute_command - main function to execute cmd
* @command: command to execute
* @progname: name of program
* Return: returns -1 on failure
*/
int execute_command(char *command, char *progname)
{
char *args[8];
char *token;
int i, pid;
int status, exit_status;
token = strtok(command, " \n\t");
i = 0;
while (token != NULL && i < 8)
{
token[strcspn(token, "\n")] = '\0';
args[i] = token;
token = strtok(NULL, " \n\t");
i++;
}
args[i] = NULL;
if (args[0] == NULL)
return (0);
if (strcmp(args[0], "exit") == 0)
{
exit_status = 0;
if (args[1] != NULL)
{
exit_status = atoi(args[1]);
}
_exit(exit_status);
}
pid = execute_child_process(args, progname);
if (pid == -1)
return (-1);
status = wait_child_process(pid);
if (WIFEXITED(status))
{
exit_status = WEXITSTATUS(status);
return (exit_status);
}
return (-1);
}