-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
146 lines (131 loc) · 3.53 KB
/
main.c
File metadata and controls
146 lines (131 loc) · 3.53 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
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
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <time.h>
#include <dirent.h>
#include <sys/types.h>
#include <unistd.h>
#include <signal.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
void exec_one(char ** args);
//PARSE ARGS METHOD
char ** parse_args(char * line, char delimiter) {
int size = 6;
char ** s1 = (char **)malloc(size * sizeof(char *));
int i = 0;
char * pos;
// Parse the string using the given delimiter
while(line){
s1[i] = strsep(&line, &delimiter);
// If there is newline, make it null
if ((pos=strchr(s1[i], '\n')) != NULL) {
*pos = '\0';
}
i++;
}
// Put a terminating null pointer at the end
s1[i] = 0;
return s1;
}
/*
void redir_in(char ** args)
Description:
Implements redirecting in by writing to a file
Arguments:
char ** args -> A pointer to the original string
Return Value:
int
*/
int redir_in(char ** args){
//printf("this happened!\n");
int file, i, retval;
i = 0;
//printf("%s\n",args[3]);
while (args[i]) {
if (strcmp(args[i],">") == 0){
break;
}
i++;
}
//printf("this is i: %d\n", i);
//printf("this is the thing at i: %s\n", args[i]);
file = i + 1;
int fd, b, c;
fd = open(args[file], O_CREAT | O_WRONLY | O_TRUNC | O_RDONLY, 0644);
b = dup(STDOUT_FILENO);
c = dup2(fd, STDOUT_FILENO);
args[i]=0;
retval = execvp(args[0], args);
dup2(b,c);
return retval;
}
void compile_and_run(char * argv){
//printf("\nCOMPILING AND RUNNING YOUR CODE...\n");
//getting the name of the file from the command line
char line[100];
strcpy(line, argv);
//creating: "gcc -o code filename.c"
char cmdline1[100];
strcpy(cmdline1, "gcc -o code ");
strcat(cmdline1, line);
//creating: "./a.out > filename"
char cmdline2[100];
strcpy(cmdline2, "./code > ");
//remove .c from the string
int length = strlen(line);
int i;
char filename[100];
for (i = 0; i < length - 2; i++){
filename[i] = line[i];
}
strcat(cmdline2, filename);
//printf("%s\n",cmdline1);
//printf("%s\n",cmdline2);
//forks of a process that compiles the code file
int exec_success;
int f = fork();
if (f == 0) { // child process
char ** cmd = parse_args(cmdline1, ' ');
//printf("arg 0: %s\narg 1: %s\narg 2: %s\narg 3: %s\narg 4: %s\n", cmd[0], cmd[1], cmd[2], cmd[3], cmd[4]);
//printf("here\n");
exec_success = execvp(cmd[0], cmd);
}
else if (f < 0) { //fork failed
printf("fork failed :(\n");
}
else { //
int returnStatus;
waitpid(f, &returnStatus, 0); // Parent process waits here for child to terminate.
}
//forks off a process to redirect the ./code to a new file
int redir_success;
f = fork();
if (f == 0) { // child process
char ** cmd2 = parse_args(cmdline2, ' ');
//printf("arg 0: %s\narg 1: %s\narg 2: %s\n", cmd2[0], cmd2[1], cmd2[2]);
redir_success = redir_in(cmd2);
//printf("redir_success: %d\n", redir_success);
if (redir_success < 0){
char * errormsg = "Your code generated ERRORS! Go to terminal to see what they are.";
int fd;
fd = open(filename, O_WRONLY);
write(fd, errormsg, strlen(errormsg));
}
//printf("this finished\n");
}
else if (f < 0) { //fork failed
//printf("fork failed :(\n");
}
else { //
int returnStatus;
waitpid(f, &returnStatus, 0); // Parent process waits here for child to terminate.
}
}
// int main(int argc, char *argv[]){
// char * arg;
// arg = argv[1];
// compile_and_run(arg);
// return 0;
// }