Advertisement
Egor_Vakar

Aktios 6.2(C)

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