-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocess.c
More file actions
executable file
·68 lines (56 loc) · 1.01 KB
/
process.c
File metadata and controls
executable file
·68 lines (56 loc) · 1.01 KB
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
#include "process.h"
#include "shell.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <termios.h>
/**
* Executes the process p.
* If the shell is in interactive mode and the process is a foreground process,
* then p should take control of the terminal.
*/
void launch_process(process* proc){
int pid = fork();
if(pid == 0)
{
tok_t* path;
extern char** environ;
path = getToks(environ[9]);
int no_of_paths = 1, i = 1;
while(path[no_of_paths] != NULL)
{
no_of_paths++;
}
no_of_paths--;
dup2(proc->std_in, 0);
dup2(proc->std_out, 1);
char p[500];
do
{
strcpy(p, path[i]);
strcat(p, "/");
strcat(p, proc->argv[0]);
i++;
}
while(fopen(p, "r") == NULL && i <= no_of_paths);
if(i > no_of_paths)
{
execv(proc->argv[0], proc->argv);
}
else
{
execv(p, proc->argv);
}
exit(20);
}
else if(pid > 0)
{
proc->pid = pid;
waitpid(pid, &(proc->status), 0);
}
else
{
printf("Could not create process :(");
}
}