-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct.c
78 lines (73 loc) · 1.89 KB
/
struct.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
#include "struct.h"
/* This function creates a new list of nodes */
void initNodeList(struct node **node){
for(int i=0; i<N_EVENTS; i++)
node[i] = NULL;
}
int existNode(struct node **node, int id){
int j, r=-1;
for(j=0; node[j] != NULL; j++){
if (node[j]->id == id) {
r = j;
break;
}
}
return r;
}
/* Create a new node with id, cmd, args, n and pid and append it to our array of nodes*/
int newNodeList(struct node **node, int id, char **arg, int n){
if (existNode(node, id)==-1){
int j,i;
for(j=0; node[j] != NULL; j++){}
NodeList aux = (NodeList) malloc (sizeof(struct node));
aux->id = id;
for(i=0; i<20; i++){
aux->args[i] = NULL;
}
aux->args[0] = strdup("./");
strcat(aux->args[0], strdup(arg[0]));
for(i=1; i<n; i++)
aux->args[i] = strdup(arg[i]);
aux->nargs = n;
for(i=0; i<20; i++)
aux->conW[i] = 0;
aux->nconW = 0;
node[j] = aux;
return 0;
}
else {
return 1;
}
}
void removeNode(struct node **node, int id){
int i, j, idN = existNode(node, id);
for(i = idN; node[i] != NULL && node[i+1] != NULL; i++) {
node[i]->id = node[i+1]->id;
node[i]->nargs = node[i+1]->nargs;
node[i]->nconW = node[i+1]->nconW;
for(j=0; node[i+1]->args[j]!=NULL; j++){
node[i]->args[j] = strdup(node[i+1]->args[j]);
}
for(;j<20;j++){
node[i]->args[j] = NULL;
}
for(j=0; node[i+1]->conW[j]!=0 ; j++){
node[i]->conW[j] = node[i+1]->conW[j];
}
for(;j<20;j++){
node[i]->conW[j] = 0;
}
}
node[i] = NULL;
}
void changeComponent(struct node **node, int id, int narg, char **arg){
char *aux = strdup("./");
strcat(aux, arg[1]);
int i, idN = existNode(node, id);
for(i=0; i<20; i++)
node[idN]->args[i] = NULL;
node[idN]->args[0] = strdup(aux);
for(i=1; arg[i+1] != NULL; i++)
node[idN]->args[i] = strdup(arg[i+1]);
node[idN]->nargs=narg-1;
}