-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdLine.c
More file actions
35 lines (33 loc) · 711 Bytes
/
cmdLine.c
File metadata and controls
35 lines (33 loc) · 711 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "parsing1.h"
void execute(char* cmd ) {
pid_t pid;
pid = fork();
int status = 0;
char** split = stringSplit(cmd);
if (pid == -1) { /* fork a child process */
fprintf(stderr, "*** ERROR: forking child process failed\n");
exit(1);
}
else if ( pid > 0) {
wait(NULL);
}
else {
status = execvp(split[0], split);
if( status == -1){
fprintf(stderr, "Error executing %s\n", cmd);
exit(1);
}
}
int e = 0;
while(split[e] != NULL) {
free(split[e]);
e++;
}
free(split);
}