-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlaunch.c
More file actions
71 lines (65 loc) · 1.01 KB
/
launch.c
File metadata and controls
71 lines (65 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
68
69
70
71
#include <sys/wait.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <signal.h>
#include "helper.h"
#include "builtin.h"
extern int fg;
extern char fgname[1000];
int main_launch(char **args)
{
pid_t proc_id;
int status, flag = 0, i = 0;
proc_id = fork();
if (proc_id == 0)
{
while(args[i] != NULL)
{
if(strcmp(args[i], "&") == 0)
{
flag = 1;
args[i] = NULL;
break;
}
i++;
}
if (execvp(args[0], args) == -1)
{
perror("main");
}
exit(EXIT_FAILURE);
}
else if (proc_id < 0)
{
perror("main");
}
else
{
fg = proc_id;
while(args[i] != NULL)
{
if(strcmp(args[i], "&") == 0)
{
printf("%s [%d]\n", args[0], proc_id);
main_insert(proc_id, args[0]);
flag = 1;
args[i] = NULL;
break;
}
i++;
}
if(!flag)
{
fg = proc_id;
strcpy(fgname, args[0]);
waitpid(proc_id, &status, WCONTINUED);
}
}
return 1;
}