-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathback.c
More file actions
78 lines (76 loc) · 1.76 KB
/
back.c
File metadata and controls
78 lines (76 loc) · 1.76 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
72
73
74
75
76
77
78
#include "headers.h"
#include "back.h"
void handler(int sig)
{
pid_t pid;
int status;
pid = waitpid(-1, &status, WNOHANG);
struct Node *copy = ll;
copy = copy->next;
struct Node *previous = ll;
while (copy != NULL)
{
if (copy->data == pid)
{
break;
}
previous = copy;
copy = copy->next;
}
if (copy != NULL)
{
fprintf(stderr, "%s with pid %d exited with exit code %d\n", copy->name[0], copy->data, WEXITSTATUS(status));
previous->next = copy->next;
}
}
void back(char *inputs[], int args)
{
int tempss = args;
char *argv[args];
for (int i = 0; i < tempss - 1; i++)
{
argv[i] = malloc(1000 * sizeof(char));
for (int k = 0; k <= strlen(inputs[i]); k++)
{
argv[i][k] = inputs[i][k];
}
}
argv[tempss - 1] = NULL;
int forkreturn = fork();
if (forkreturn == 0)
{
setpgid(0, 0);
if (execvp(argv[0], argv) < 0)
{
fprintf(stderr, "ERROR : Execvp Failed\n");
_exit(EXIT_FAILURE);
}
_exit(0);
}
else if (forkreturn < 0)
{
fprintf(stderr, "ERROR : Fork Failed\n");
exit(0);
}
else
{
setpgid(forkreturn, 0);
tcsetpgrp(STDIN_FILENO, getpid());
struct Node *copy = ll;
while (copy->next != NULL)
{
copy = copy->next;
}
struct Node *temp;
temp = (struct Node *)malloc(sizeof(struct Node));
temp->data = forkreturn;
for (int k = 0; k < args; k++)
{
strcpy(temp->name[k], inputs[k]);
}
temp->next = NULL;
copy->next = temp;
signal(SIGCHLD, handler);
return;
}
}