forked from leandropozer/shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
executable file
·144 lines (125 loc) · 2.72 KB
/
list.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
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
143
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
void ListCreate(LIST *list)
{
list->first = NULL;
list->last = NULL;
}
void initCommand(COMMAND *cmd)
{
cmd->isBackground = 0;
cmd->input_r = 0;
cmd->output_r = 0;
cmd->output_r_append = 0;
cmd->size = 0;
cmd->id = -1;
cmd->pid = -1;
cmd->pipe[0] = -1;
cmd->pipe[1] = -1;
}
int ListIsEmpty(LIST *list)
{
if(list->first == NULL) return 1;
return 0;
}
int ListInsert(LIST *list, PROCESS *proc, COMMAND *cmd)
{
NODE *newNode = (NODE *)malloc(sizeof(NODE));
if (newNode != NULL)
{
newNode->proc = proc;
newNode->cmd = cmd;
newNode->next = NULL;
newNode->prev = list->last;
if (list->last != NULL)
list->last->next = newNode;
else
list->first = newNode;
list->last = newNode;
return 1;
}
return 0;
}
int ListRemoveByPid(LIST *list, pid_t pid)
{
if (!ListIsEmpty(list))
{
NODE *aux = list->first;
while(aux->proc->pid != pid)
{
if (aux != list->last)
aux = aux->next;
else return 0;
}
if (aux == list->first)
list->first = aux->next;
else
aux->prev->next = aux->next;
if (aux == list->last)
list->last = aux->prev;
else
aux->next->prev = aux->prev;
free(aux->proc);
free(aux);
return 1;
}
return 0;
}
PROCESS *ListGetCurrentProcess(LIST *list)
{
NODE *aux = list->first;
while (aux != NULL)
{
if((strcmp(aux->proc->status, "Running") == 0) && !aux->proc->isBackground)
return aux->proc;
aux = aux->next;
}
return NULL;
}
PROCESS * ListGetLastStopped(LIST *list)
{
if(!ListIsEmpty(list))
{
NODE *aux = list->last;
while (aux != NULL)
{
if(strcmp(aux->proc->status, "Stopped") == 0)
return aux->proc;
aux = aux->prev;
}
}
return NULL;
}
PROCESS * ListGetProcess(LIST *list, pid_t pid)
{
if(!ListIsEmpty(list))
{
NODE *aux = list->last;
while (aux != NULL)
{
if(aux->proc->pid == pid)
return aux->proc;
aux = aux->prev;
}
}
return NULL;
}
void ListPurgeCmds(LIST *list)
{
if(!ListIsEmpty(list))
{
NODE *aux = list->first;
while(aux != NULL)
{
int i;
for(i = 0; i < aux->cmd->size; i++)
free(aux->cmd->args[i]);
free(aux->cmd->args);
free(aux->cmd);
aux = aux->next;
}
ListCreate(list);
}
}