-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfg.c
More file actions
70 lines (69 loc) · 1.7 KB
/
fg.c
File metadata and controls
70 lines (69 loc) · 1.7 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
#include "headers.h"
#include "fg.h"
void fg(char *inputs[], int args)
{
if (args != 2)
{
fprintf(stderr, "ERROR: Incorrect number of arguments\n");
}
else
{
int jobno = atoi(inputs[1]);
struct Node *copy = ll;
copy = copy->next;
struct Node *previous = ll;
int counter = 0;
while (copy != NULL)
{
counter++;
previous = copy;
copy = copy->next;
}
if (jobno > counter || jobno <= 0)
{
fprintf(stderr, "ERROR: Invalid job number\n");
return;
}
copy = ll->next;
previous = ll;
int checker = 1;
while (copy != NULL)
{
if (checker == jobno)
{
break;
}
checker++;
previous = copy;
copy = copy->next;
}
int stat;
signal(SIGTTOU, SIG_IGN);
signal(SIGTTIN, SIG_IGN);
tcsetpgrp(STDIN_FILENO, copy->data);
kill(copy->data, SIGCONT);
*currfg = copy->data;
*elements = args;
for (int k = 0; k < args; k++)
{
strcpy(commfg[k], inputs[k]);
}
waitpid(copy->data, &stat, WUNTRACED);
previous->next = copy->next;
*currfg = -1;
tcsetpgrp(STDIN_FILENO, getpgrp());
signal(SIGTTOU, SIG_DFL);
signal(SIGTTIN, SIG_DFL);
if (WIFSTOPPED(stat))
{
struct Node *copy2 = ll;
while (copy2->next != NULL)
{
copy2 = copy2->next;
}
struct Node *temp2;
copy2->next = copy;
copy->next = NULL;
}
}
}