Advertisement
keker123

Untitled

May 12th, 2024
788
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.78 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <sys/types.h>
  6. #include <sys/wait.h>
  7. #include <dirent.h>
  8. #include <err.h>
  9.  
  10.  
  11. int semaphoreRead = -1;
  12. int semaphoreWrite = -1;
  13.  
  14. void acquire() {
  15.     if (write(semaphoreWrite, "a", 1) == -1) {
  16.         err(1, "write");
  17.     }
  18. }
  19.  
  20. void release() {
  21.     char buf;
  22.     if (read(semaphoreRead, &buf, 1) == -1) {
  23.         err(1, "read");
  24.     }
  25. }
  26.  
  27. void make(char *path);
  28.  
  29. void watcher(char *executable) {
  30.     char path[300];
  31.     getcwd(path, 300);
  32.     printf("path: %s\n", path);
  33.     printf("watcher: %s\n", executable);
  34.     pid_t pid = fork();
  35.     if (pid == -1) {
  36.         err(1, "fork");
  37.     } else if (pid == 0) { // Child process
  38.         char fullpath[400];
  39.         snprintf(fullpath, sizeof(fullpath), "%s/%s", path, executable + 2);
  40.         printf("fullpath: %s\n", fullpath);
  41.         char* params[] = {executable, NULL};
  42.         execvp("fullpath", &executable);
  43.         err(1, "execp");
  44.     } else { // Parent process
  45.         int status;
  46.         waitpid(pid, &status, 0);
  47.         release();
  48.     }
  49. }
  50.  
  51. void traverse_directory(char *path) {
  52.     DIR *dir = opendir(path);
  53.     printf("dir: %s\n", path);
  54.     if (dir == NULL) {
  55.         err(1, "opendir");
  56.     }
  57.  
  58.     struct dirent *entry;
  59.     while ((entry = readdir(dir)) != NULL) {
  60.         if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) {
  61.             continue;
  62.         }
  63.  
  64.         char full_path[PATH_MAX];
  65.         snprintf(full_path, sizeof(full_path), "%s/%s", path, entry->d_name);
  66.         printf("full_path: %s\n", full_path);
  67.         if (entry->d_type == DT_DIR) { // If entry is a directory, recursively traverse it
  68.             printf("full_path_dir: %s\n", full_path);
  69.             traverse_directory(full_path);
  70.         } else if (access(full_path, X_OK) == 0) { // If entry is executable, run watcher
  71.             acquire();
  72.             printf("full_path exec: %s\n", full_path);
  73.             watcher(full_path);
  74.         }
  75.     }
  76.  
  77.     closedir(dir);
  78. }
  79.  
  80. void make(char *path) {
  81.     traverse_directory(path);
  82.     while (semaphoreWrite > 0) {
  83.         acquire();
  84.     }
  85. }
  86.  
  87. int main(int argc, char *argv[]) {
  88.     if (argc != 2) {
  89.         fprintf(stderr, "Usage: %s <max_parallelism>\n", argv[0]);
  90.         exit(1);
  91.     }
  92.  
  93.     int max_parallelism = atoi(argv[1]);
  94.     if (max_parallelism <= 0) {
  95.         fprintf(stderr, "Invalid argument: %s\n", argv[1]);
  96.         exit(1);
  97.     }
  98.  
  99.     // Create semaphore using a pipe
  100.     int pipefd[2];
  101.     if (pipe(pipefd) == -1) {
  102.         err(1, "pipe");
  103.     }
  104.     semaphoreRead = pipefd[0]; // Use the read end of the pipe as the semaphore
  105.     semaphoreWrite = pipefd[1];
  106.  
  107.     make(".");
  108.     close(pipefd[0]);
  109.     close(pipefd[1]);
  110.  
  111.     return 0;
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement