-
Notifications
You must be signed in to change notification settings - Fork 0
/
child.c
54 lines (45 loc) · 1.21 KB
/
child.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
/**
* @name : child.c
* @author : Thomas Gummerer
* @version : 0.1
* @copyright : Copyright (c) Thomas Gummerer 2010 | All rights reserved.
* @license : GPL
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include "child.h"
#include "def.h"
#include "commands.h"
char * com [MAX_COMMAND_LENGTH];
char * sep = " \t\n";
char * element;
// Executes a command, as a child process
int child (char command []) {
int count=0;
// Execute internal commands
if (isInternal(command)) {
internal (command);
exit (0);
}
element = strtok (command, sep);
while (element != NULL)
{
com[count] = element;
count++;
com[count] = NULL;
element = strtok (NULL, sep);
}
// Execute command
execvp (com[0], com);
perror("Command was not found");
return EXIT_FAILURE;
}
// Checks if the command is a internal command, which is executed by the shell directly
// TODO: rewrite to get the commands from a array, or a configuration file
int isInternal (char command[]) {
if (strncmp("jobs", command, 4) == 0 || strncmp("bg", command, 2) == 0 || strncmp("fg", command, 2) == 0 || strncmp("in", command, 2) == 0 || strncmp("out", command, 3) == 0)
return 1;
return 0;
}