-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
57 lines (49 loc) · 1.22 KB
/
Copy pathmain.c
File metadata and controls
57 lines (49 loc) · 1.22 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
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <string.h>
int file_checker(char *file) {
struct stat st;
return stat(file, &st) == 0 && S_ISREG(st.st_mode);
}
int file_extension_checker(const char *file) {
const char *extension = strrchr(file, '.');
return extension != NULL &&
(strcmp(extension, ".c") == 0 ||
strcmp(extension, ".h") == 0);
}
void compile(const char *file) {
char command[256];
snprintf(command, sizeof(command), "gcc %s -o output", file);
system(command);
}
void run_output(void) {
system("./output");
}
void clean_output(void) {
system("rm -f output");
}
void help(void) {
printf("Usage: command <file>\n");
printf("Compile, execute and clean the output of a C source file.\n");
}
int main(int argc, char **argv) {
int i = 0;
if (argc == 2) {
if (argv[1] && strcmp(argv[1], "-h") == 0) {
help();
} else {
if (file_checker(argv[1]) == 1 && file_extension_checker(argv[1]) == 1) {
compile(argv[1]);
run_output();
clean_output();
} else {
printf("File is not a valid C source or header file or does not exist.\n");
}
}
} else {
printf("please enter one argument to use the command\n");
}
return 0;
}