-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
69 lines (67 loc) · 2.37 KB
/
main.c
File metadata and controls
69 lines (67 loc) · 2.37 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
// Written by:
// Logan Mahan, NetID: lmahan, CSID: mahan
// Sam Ware, NetID: sware2, CSID: sware
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "build_spec_graph.h"
/*
* The main method initializes the execution of makefile commands dependent on user input.
*/
int main(int argc, char** argv) {
int option;
char* target_makefile = NULL; // Keeps track of a makefile if '-f' is specified.
// Checks to see if '-f' followed by a target makefile was specified by user.
while ((option = getopt(argc, argv, ":f:")) != -1) {
switch(option) {
case 'f':
target_makefile = optarg;
break;
case ':':
printf("Usage: 537make <target> [-f Name of makefile]\n");
exit(-1);
case '?':
printf("Usage: 537make <target> [-f Name of makefile]\n");
exit(-1);
}
}
FILE* make; // File pointer for the makefile being used.
// If no makefile has been specified, check to see if one exists in the current working directory.
if (target_makefile == NULL) {
if((make = fopen("Makefile", "r")) == NULL){
if((make = fopen("makefile", "r")) == NULL){
fprintf(stderr, "Error: Could not find Makefile.\n");
exit(-1);
}
}
// If a makefile has been specified, check to see if it exists before reading through the makefile.
} else {
if ((make = fopen(target_makefile, "r")) == NULL) {
fprintf(stderr, "Error: Could not find Makefile \"%s\"\n", target_makefile);
exit(-1);
}
}
// If no makefile has been specified, check to see if a target has been specified to run its build commands.
if(target_makefile == NULL){
if (argc == 2) {
runMakefile(make, argv[1]);
}else if(argc == 1){
runMakefile(make, NULL);
} else{
printf("Usage: 537make <target> [-f Name of Makefile]\n");
exit(-1);
}
// If a makefile has been specified, check to see if a target has been specified to run its build commands.
} else {
if (argc == 4) {
runMakefile(make, argv[3]);
} else if (argc == 3) {
runMakefile(make, NULL);
} else {
printf("Usage: 537make <target> [-f Name of Makefile]\n");
exit(-1);
}
}
fclose(make);
return 0;
}