-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.c
106 lines (90 loc) · 2.14 KB
/
commands.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
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
#include <stdio.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <signal.h>
#include "linkedlist.h"
#include "helper.h"
#include "commands.h"
struct node *head;
// bg
// Create background process
struct node* bg(struct node *head, char *args[]) // TODO: pass other arguments the user may have entered
{
char *command = args[1]; // token after 'bg'
// TODO: fix temp magic number 80
if(StrMatch(command, "", 80)) // if no command
{
printf("please provide an executable or command\n");
return head;
}
char *list[] = {args[2], args[3], args[4], NULL}; // rest of the arguments
pid_t pid_fork = fork(); // create child process
// fork failed
if(pid_fork < 0)
{
printf("error: fork failed\n");
return head;
}
else if(pid_fork == 0) // child process created, child gets pid of 0 from fork()
{
// execute command
if(execvp(command, list) < 0)
{
printf("execution of %s failed\n", command);
exit(1);
}
}
else // pid > 0, parent process, parent gets child's pid as pid from fork()
{
// add child process to list
head = AddFront(head, pid_fork);
DetailPrintList(head); // for testing
sleep(1);
}
return head;
}
// bglist
void bglist(struct node *head)
{
// check length
int listLen = ListLength(head);
if(listLen == 0){
printf("There are no background jobs\n");
return;
}
DetailPrintList(head); // main print
printf("Total background jobs: %d\n", listLen);
}
// bgkill
void bgkill(struct node *head, pid_t pid)
{
//pid_t pid = atoi(args[1]);
RemoveNode(head, pid);
kill(pid, SIGTERM); // TODO: make this kill the pid that is passed
}
// bgstop
void bgstop()
{
}
// bgstart
void bgstart()
{
}
// pstat
void pstat()
{
}
void killall(struct node *head)
{
// kill all processes in list
pid_t pid;
struct node *current = head;
while(current != NULL)
{
pid = current->pid;
kill(pid, SIGTERM);
current = current->next;
}
}