-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler_posix.c
147 lines (141 loc) · 4.08 KB
/
compiler_posix.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
/* compiler_posix.c */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h> /* requires _GNU_SOURCE to be defined */
#include <errno.h>
void fatal_stop(const char* message)
{
fprintf(stderr,"%s: fatal error: %s\n",PROGRAM_NAME,message);
_exit(1);
}
int lookup_ext(const char** ext,const char* source)
{
/* look through files in the current working directory */
int i;
int top;
int srclen;
DIR* pdir;
const char* useSource;
top = 0;
*ext = NULL;
/* locate file name part of source string */
srclen = strlen(source);
i = srclen - 1;
while (i>=0 && source[i]!='/')
--i;
++i;
useSource = source+i;
/* open needed directory */
if (i > 0) {
/* file location is specified in part of source string */
stringbuf part;
init_stringbuf(&part);
assign_stringbuf_ex(&part,source,i);
pdir = opendir(part.buffer);
destroy_stringbuf(&part);
}
else
/* file location is understood to be current directory */
pdir = opendir(".");
/* get length of source string that is used for matching */
srclen = strlen(useSource);
if (pdir != NULL) {
struct dirent* ent;
while (1) {
ent = readdir(pdir);
if (ent == NULL)
break;
if (ent->d_type == DT_REG) {
int len;
char* pext;
/* obtain file extension of entry */
len = strlen(ent->d_name);
pext = ent->d_name+len;
while (len>0 && *pext!='.') {
--len;
--pext;
}
/* check to see if prefix.ext matches prefix */
if (top<MAX_EXTENSIONS && *pext=='.' && len==srclen && strncmp(useSource,ent->d_name,srclen)==0) {
/* the extension must belong to one of the handled extensions */
ext[top] = check_extension(pext);
if (ext[top] != NULL)
++top;
}
}
}
closedir(pdir);
}
else if (errno == EACCES)
fatal_stop("cannot open current directory: permission denied");
else
fatal_stop("cannot open current directory");
return top;
}
int check_file(const char* fileName)
{
struct stat st;
if (stat(fileName,&st) == 0) {
/* check file type */
if ((st.st_mode & S_IFMT) != S_IFREG)
return FILE_CHECK_NOT_REGULAR_FILE;
/* check permissions */
if (access(fileName,R_OK) == -1) {
if (errno == EACCES)
return FILE_CHECK_ACCESS_DENIED;
return -1;
}
return FILE_CHECK_SUCCESS;
}
if (errno == EACCES)
return FILE_CHECK_ACCESS_DENIED;
if (errno == ENOENT)
return FILE_CHECK_DOES_NOT_EXIST;
return -1;
}
int invoke_compiler(const char* compilerName,const char* arguments,const char* redirect)
{
int i;
int fd;
int status;
pid_t pid;
char* argv[MAX_ARGUMENT+1]; /* include the terminating NULL ptr */
pid = fork();
if (pid == -1)
return -1;
if (pid != 0) {
waitpid(pid,&status,0);
if (WIFEXITED(status))
return WEXITSTATUS(status);
return -1;
}
/* child process */
/* TODO: hook into source parser if available */
i = 0;
status = 0;
while (arguments[i] && status<MAX_ARGUMENT) {
argv[status++] = (char*) (arguments+i);
while ( arguments[i] )
++i;
++i;
}
argv[status] = NULL;
/* If a redirect output file was specified, redirect the process's stdout
* to the specified file.
*/
if (redirect != NULL) {
fd = open(redirect,O_CREAT | O_WRONLY,0666);
if (fd == -1) {
fatal_stop("cannot open redirect file");
}
if (dup2(fd,STDOUT_FILENO) == -1) {
fatal_stop("failed to redirect output to file");
}
close(fd);
}
if (execvp(compilerName,argv) == -1)
_exit(1);
return 0;
}