-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuiltInCmds.c
More file actions
executable file
·142 lines (136 loc) · 3.23 KB
/
builtInCmds.c
File metadata and controls
executable file
·142 lines (136 loc) · 3.23 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <stdio.h>
#include <stdlib.h> // exit
#include <unistd.h> // chdir
#include <sys/types.h>
#include <signal.h>
#include "builtInCmds.h"
#include "debug.h"
/** Function names, arguments and return values must follow the convension. **/
/* Changes current working directory. Accepts only exactly 1 arg. */
Jobs* cdMain(int cmdlArgc, char **cmdlArgv, Jobs* jobs) {
DEBUG(printf("#DEBUG# Calling cd.\n"););
if (cmdlArgc != 2) {
printf("%s: wrong number of arguments\n", cmdlArgv[0]);
//return 1;
}
if (chdir(cmdlArgv[1]) == -1) {
printf("%s: cannot change directory\n", cmdlArgv[1]);
//return 2;
}
return jobs;
}
/* Exit from the programme. Accepts only 0 args. */
Jobs* exitMain(int cmdlArgc, char **cmdlArgv, Jobs* jobs) {
DEBUG(printf("#DEBUG# Calling exit.\n"););
if (cmdlArgc != 1) {
printf("%s: wrong number of arguments\n", cmdlArgv[0]);
return jobs;
}
if (jobs != NULL) {
printf("There is at least one suspended job\n");
return jobs;
} else {
exit(0);
}
}
Jobs* fgMain(int cmdlArgc, char **cmdlArgv, Jobs* jobs) {
int index;
DEBUG(printf("#DEBUG# Calling fg.\n"););
if (cmdlArgc != 2) {
printf("%s: wrong number of arguments\n", cmdlArgv[0]);
return jobs;
}
index = atoi(cmdlArgv[1]);
if (index > 0) {
jobs = wakeJob(jobs, index);
} else {
printf("fg: no such job\n");
}
return jobs;
}
Jobs* jobsMain(int cmdlArgc, char **cmdlArgv, Jobs* jobs) {
Jobs* temp = jobs;
int count = 0;
DEBUG(printf("#DEBUG# Calling jobs.\n"););
if (cmdlArgc != 1) {
printf("%s: wrong number of arguments\n", cmdlArgv[0]);
return jobs;
}
while (temp) {
printf("[%d] %s\n", ++count, temp->cmd);
temp = temp->next;
}
if (count == 0) {
printf("No suspended jobs\n");
}
return jobs;
}
Jobs* wakeJob(Jobs* jobs, int index) {
int i = 0;
Jobs* temp;
temp = jobs;
for (;i < index-1;i++) {
temp = temp->next;
}
if (temp == NULL) {
printf("fg: no such job\n");
return jobs;
} else {
printf("Job wake up: %s\n", temp->cmd);
wakeChildren(temp->pidList);
jobs = waitChildren(temp->pidList, jobs);
}
return jobs;
}
Jobs* waitChildren(pid_t* childPid, Jobs* jobs) {
int childStatus;
int childCount = 0;
int i;
tcsetpgrp(STDIN_FILENO, childPid[0]);
tcsetpgrp(STDOUT_FILENO, childPid[0]);
while (childPid[childCount] != 0) {
DEBUG(printf("childpid: %d\n", childPid[childCount]););
childCount++;
}
for(i = 0; childPid[i] != 0; i++) {
DEBUG(printf("waiting for %d\n", childPid[i]););
waitpid(childPid[i], &childStatus, WUNTRACED);
DEBUG(
if (WIFSIGNALED(childStatus))
printf("#DEBUG# Child was terminated by signal %d.\n",
WTERMSIG(childStatus));
);
}
if (!WIFSTOPPED(childStatus)) {
//delete the node
Jobs* temp;
if (jobs->pidList[0] == childPid[0]) {
//first node
temp = jobs->next;
free(jobs->pidList);
free(jobs);
jobs = temp;
} else {
//not first node
Jobs* delPtr;
temp = jobs;
while(temp->next->pidList[0] != childPid[0]) {
temp = temp->next;
}
delPtr = temp->next;
temp->next = temp->next->next;
free(delPtr->pidList);
free(delPtr);
}
}
tcsetpgrp(STDIN_FILENO, getpid());
tcsetpgrp(STDOUT_FILENO, getpid());
return jobs;
}
void wakeChildren(pid_t* childPid) {
int i = 0;
while (childPid[i] != 0) {
kill(childPid[i],SIGCONT);
i++;
}
}