-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunCmd.c
More file actions
executable file
·111 lines (86 loc) · 2.29 KB
/
runCmd.c
File metadata and controls
executable file
·111 lines (86 loc) · 2.29 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
/*/////////////////////////////
CS 537 Prog 3
Authors: Peyton Gardipee
Tushar Verma
File: runCMD.c
*///////////////////////////
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include "fileParser.h"
#include "graphNode.h"
#include "constants.h"
#include "runCmd.h"
#include "outOfDate.h"
targetNode* runCmd(targetNode *headTarget)
{
targetList *currDependentTarget;
targetNode* currTarget;
//Base Case: If there are no children, return 0;
if(headTarget->dependentTargets->nextTarget == NULL)
{
currTarget = headTarget;
execute(currTarget);
return headTarget;
}
else
{
currDependentTarget = headTarget->dependentTargets->nextTarget;
while(currDependentTarget != NULL)
{
currTarget = runCmd(currDependentTarget->target);
execute(currTarget);
currDependentTarget = currDependentTarget->nextTarget;
}
currTarget = headTarget;
execute(currTarget);
}
return headTarget;
}
//Function used to execute commands of a given target
void execute(targetNode *currTarget)
{
//Variables used by fork, execvp, and wait
char** args;
pid_t child;
int cstatus, index;
//index = 0;
if(outOfDate(currTarget))
{
if(!currTarget->hasVisited)
{
index = 0;
while(currTarget->commands[index] != NULL)
{
args = currTarget->commands[index];
if ((child = fork()) == 0)
{
//We are in the child
execvp(args[0], args);
exit(1);
}
else
{ // Parent process
if (child == (pid_t)(-1))
{
fprintf(stderr, "Fork failed.\n");
exit(1);
}
else
{
//Wait for child to complete
wait(&cstatus);
}
}
index++;
}
}
}
currTarget->hasVisited = 1;
}