-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbg.c
69 lines (51 loc) · 1.58 KB
/
bg.c
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
#include "headers.h"
void bg(int argc, char** argv) {
if(argc != 2) {
fprintf(stderr, "bg: Syntax error, bg command takes 1 argument.\n");
return;
}
errno = 0;
char *str, *endptr;
long val;
str = argv[1];
val = strtol(str, &endptr, 10);
if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0)) {
perror("bg: Invalid arguments, either not integer or overflow.");
return;
}
if (endptr == str || *endptr != '\0') {
fprintf(stderr, "bg: Characters other than numbers found.\n");
return;
}
if(val <= 0) {
fprintf(stderr, "bg: Invalid job no., since input is <= 0.\n");
return;
}
// ----------------------------------------------------------------------------------
int job_pid = 0;
int pid, status, job_no = 0;
char* job_name = (char*) calloc(const_size, sizeof(char));
struct Node* p = head;
while(p != NULL) {
pid = waitpid(p->pid, &status, WNOHANG);
if(pid == 0) {
job_no++;
if(job_no == val) {
job_pid = p->pid;
strcpy(job_name, p->name);
break;
}
}
p = p->next;
}
if(p == NULL) {
fprintf(stderr, "bg: Invalid Job Number.\n");
return;
}
// ----------------------------------------------------------------------------------
// continue the process from stopped state
if(kill(job_pid, 18) < 0) {
perror("fg: Execution of command failed.");
return;
}
}