Advertisement
Egor_Vakar

Aktios 6.1(C)

Dec 19th, 2022
823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.15 KB | None | 0 0
  1. #include<sys/types.h>
  2. #include<sys/stat.h>
  3. #include<sys/wait.h>
  4. #include<alloca.h>
  5. #include<stdio.h>
  6. #include<stdlib.h>
  7. #include<string.h>
  8. #include<limits.h>
  9. #include<unistd.h>
  10. #include<dirent.h>
  11.  
  12.  
  13. int maxProcNum;
  14. int ProcCounter = 0;
  15. void ProcessFolder(char *curPath, char *findPath){
  16.     DIR *dp = opendir(curPath);
  17.     struct dirent *d;
  18.     struct stat buf;
  19.     char *filePath = alloca(strlen(curPath) + NAME_MAX + 2);
  20.  
  21.     int FileCounter = 0;
  22.  
  23.     d = readdir(dp);
  24.     while(d){
  25.         if(strcmp(".", d->d_name) != 0 && strcmp("..", d->d_name) != 0){
  26.             strcpy(filePath, curPath);
  27.             strcat(filePath, "/");
  28.             strcat(filePath, d->d_name);
  29.             if(d->d_type != DT_DIR){
  30.                 if(strcmp(d->d_name,findPath) == 0){
  31.                     stat(filePath, &buf);
  32.                     printf("%s\n","\n///////////////////////////////////////////RESULT///////////////////////////////////////////////////\n");
  33.                     printf("Path = %s, Size = %ld, Date = %d,Access Rights = %o, id = %d\n", filePath, buf.st_size, buf.st_ctime, buf.st_mode, buf.st_ino);
  34.                     printf("%s\n","\n//////////////////////////////////////////////////////////////////////////////////////////////////////\n");
  35.                 }
  36.                 FileCounter++;
  37.             }
  38.             if(d->d_type == DT_DIR){
  39.                 if (ProcCounter == maxProcNum){
  40.                     ProcCounter--;
  41.                     wait(NULL);
  42.                 }
  43.                 pid_t pid = fork();
  44.                 ProcCounter++;
  45.                 if (pid == 0) {
  46.                     ProcessFolder(filePath, findPath);
  47.                 }else{
  48.                     wait(NULL);
  49.                 }
  50.             }
  51.         }
  52.         d = readdir(dp);
  53.     }
  54.     stat(curPath,&buf);
  55.     printf("Path = %s, Size = %ld, FileCount = %d, ProcNumb = %d, PID = %d\n", curPath, buf.st_size, FileCounter, ProcCounter, getpid());
  56.     exit(0);
  57. }
  58.  
  59. int main(__attribute__((unused)) int argc , char *argv[] ) {
  60.  
  61.     maxProcNum = atol(argv[3]);
  62.  
  63.     ProcessFolder(realpath(argv[1],NULL), argv[2]);
  64.  
  65.     while (wait(NULL)) {}
  66.  
  67.  
  68.     return 0;
  69. }
  70.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement