-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile.c
152 lines (139 loc) · 3.45 KB
/
compile.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
144
145
146
147
148
149
150
151
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
struct lists
{
char array[50][100];
};
int exe_cnt =0; // Total number of excerises
struct lists get_directory(char *);
void compile();
int Complete();
int main(int argc,char **argv)
{
if(argc == 1)
{
printf("Enter a valid argument verify/watch \n");
exit(-1);
}
char *directory = malloc(128 * sizeof(char));
struct lists dir_list;
dir_list = get_directory((char *)directory);
if(strcmp("watch",argv[1]) == 0)
{
int j=0;
while(j<exe_cnt)
{
sprintf(directory,"%s", dir_list.array[j]);
printf("inside while %s", directory);
compile(directory);
if(Complete(directory) != 1)
{
printf("Remove //I AM NOT DONE comment to continue compilation \n");
char *u;
scanf("%c",u);
}
else j++;
}
}
else if(strcmp("verify",argv[1]) == 0)
{
printf("\n Inside verify %s \n",directory);
for(int j=1; j<exe_cnt; j++)
{
sprintf(directory,"%s",dir_list.array[j]);
compile(directory);
}
}
else
{
printf("Commnad not found \n");
}
}
struct lists get_directory(char *path)
{
char cwd[PATH_MAX] = {0}, command[128] = {0}, *line = NULL;
size_t len = 0;
struct lists result;
printf("Value passed in get_directory: :%s \n",path);
if (getcwd(cwd, sizeof(cwd)) != NULL)
{
printf("problem- \n");
}
else
{
perror("getcwd() error");
}
sprintf(command,"%s%s%s","ls ",cwd,"/Problems/ > file.txt");
system(command);
printf("\n Total command after strcats: %s \n", command);
FILE *fp;
if((fp = fopen("file.txt","r"))== NULL)
{
perror("Unable to Open file");
exit(1);
}
printf("\n path %s, cwd: %s \n",path,cwd);
sprintf(path,"%s%s",cwd,"/Problems/");
printf("late path: %s \n", path);
int j = 0;
while(getline(&line,&len,fp)!= -1){
sprintf(result.array[j], "%s%s", path, line);
printf("%s",result.array[j]);
j++;
}
exe_cnt = j;
return result;
//sprintf(path,"%s%s%d%s",cwd,"/Problems/",i,".c");
//printf("%s\n", path);
}
void compile(char *paths)
{
char h[64] = {0};
char hint[64] = {0};
char *gcc = malloc(128 * sizeof(char));
sprintf(gcc,"%s%s","gcc ",paths);
int j = system(gcc);
//printf("%d", j);
if (j == 0)
{
printf("\nCONGRATULATIONS! problem compiled\n");
}
else
{
printf("\nproblem not compiled, \n solve error to compile\n for hint type <problem.hint>\n");
strcpy(h, "problem.hint");
scanf("%s", hint);
if (strcmp(h, hint) == 0)
{
printf("try checking print statement\n");
}
}
}
int Complete(char *files)
{
char temp[512] = {0};
char *file_name = malloc( 128 * sizeof(char));
FILE *fp;
char *str = "//I AM NOT DONE";
sprintf(file_name,"%s",strtok(files,"\n"));
printf("%s \n",files);
if((fp = fopen(files,"r")) == NULL)
{
perror("File Opening Error");
return(-1);
}
while(fgets(temp,512,fp) != NULL){
if(strstr(temp,str) != NULL)
{
return(0);
}
}
if(fp)
{
fclose(fp);
}
return(1);
}