Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <dirent.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <sys/wait.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <ctype.h>
- #define BUFF_SIZE (1 << 12)
- void procRegFile(const char path[]) {
- char aux[BUFF_SIZE + 5];
- sprintf(aux, "%s.exe", path);
- char *argv[] = {
- "gcc",
- "-Wall",
- "-o",
- aux,
- (char*)path,
- NULL
- };
- exit(execvp("gcc", argv));
- }
- void procEntry(const char path[], int *kids) {
- struct stat statbuf;
- if (stat(path, &statbuf)) {
- perror("");
- exit(-1);
- }
- if (!S_ISREG(statbuf.st_mode)) {
- return;
- }
- const char *const suf = strrchr(path, '.');
- if (strcmp(suf, ".c")) {
- return;
- }
- pid_t kidPid = fork();
- if (kidPid < 0) {
- perror("");
- exit(-1);
- }
- if (kidPid == 0) {//kid
- procRegFile(path);
- }
- (*kids)++;
- }
- void iterDir(const char path[]) {
- DIR *dir = opendir(path);
- if (dir == NULL) {
- perror("");
- exit(-1);
- }
- int kids = 0;
- for (struct dirent *dep; (dep = readdir(dir));) {
- const char *const dname = dep->d_name;
- if (!strcmp(dname, ".") || !strcmp(dname, "..")) {
- continue;
- }
- char absPath[BUFF_SIZE];
- sprintf(absPath, "%s/%s", path, dname);
- procEntry(absPath, &kids);
- }
- if (closedir(dir)) {
- perror("");
- }
- int wstat = 0;
- pid_t wr;
- while ((wr = wait(&wstat))) {
- if (wr == -1) {
- perror("");
- exit(-1);
- }
- kids--;
- if (kids == 0) {
- break;
- }
- }
- }
- int main(int argc, char *argv[]) {
- if (argc != 2) {
- fprintf(stderr, "Wrong usage!\n");
- exit(1);
- }
- iterDir(argv[1]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement