Advertisement
olcayertas

hw4

May 16th, 2012
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.70 KB | None | 0 0
  1. // Bismillah
  2.  
  3. #include <stdio.h>
  4. #include <unistd.h>
  5. #include <dirent.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <errno.h>
  11. #include <fcntl.h>
  12. #include <limits.h>
  13. #include <pwd.h>
  14. #include <grp.h>
  15. #include <time.h>
  16. #include <locale.h>
  17. #include <langinfo.h>
  18. #include <stdint.h>
  19. #include <error.h>
  20. #include <pthread.h>
  21.  
  22. #define MAX_THREAD 10  /*maximum Thread sayisi*/
  23. #ifndef PATH_MAX
  24. #define PATH_MAX 255
  25. #endif
  26. #define MAX_FILE 1000
  27.  
  28. void* recursion(void *path);
  29.  
  30. int isdirectory(char *path) {
  31.     struct stat statbuf;
  32.  
  33.     if (stat(path, &statbuf) == -1)
  34.         return 0;
  35.     else
  36.         return S_ISDIR(statbuf.st_mode);
  37. }
  38.  
  39. int dcounter = 0, fcounter = 0, isdir = 0;
  40. char directorypath[MAX_THREAD][PATH_MAX];
  41. char filespath[MAX_FILE][PATH_MAX];
  42.  
  43. /* index of thread id array */
  44. int i = 0;
  45. pthread_t tid[MAX_THREAD];
  46.  
  47. int main(int argc, char **argv) {
  48.     /* variables*/
  49.     /*current directory path*/
  50.     char current[PATH_MAX];
  51.     /*temprory file or directory name*/
  52.     char *name;
  53.     /*directory structor*/
  54.     struct dirent *direntp;
  55.     /*Directories pointer*/
  56.     DIR *dirptr;
  57.     /*buffersize*/
  58.     int bufsize;
  59.     /*a file variable*/
  60.     int fd[2];
  61.     /*file pointer*/
  62.     FILE* fptr;
  63.     void *deneme;
  64.     int error;
  65.  
  66.     /*usage*/
  67.     if (argc < 0) {
  68.         fprintf(stderr, "Usage: %s \n", argv[0]);
  69.         return 2;
  70.     }
  71.  
  72.     /*current directory path*/
  73.     if (getcwd(current, PATH_MAX) == NULL) {
  74.         perror("Failed the get current working directory");
  75.         exit(0);
  76.     }
  77.  
  78.     /*create pipe*/
  79.     if (pipe(fd) == -1) {
  80.         perror("Failed to create the pipe");
  81.         return 1;
  82.     }
  83.  
  84.     //printf("tid = %d \n" , (int)tid);
  85.     //deneme = (void *) current;
  86.     //printf("Current working directory = %s \n", (char*) deneme);
  87.  
  88.     if (pthread_create(&tid[i], NULL, recursion, (void*) current)) {
  89.         fprintf(stderr, "Cannot create thread\n");
  90.         exit(1);
  91.     }
  92.  
  93.     // threadin isini sonlandirmasi beklendi
  94.     if (error = pthread_join(tid[i], NULL))
  95.         fprintf(stderr, "Failed to join thread:\n%s\n", strerror(error));
  96.  
  97.     return 0;
  98. }
  99.  
  100. void* recursion(void *mypath) {
  101.     char temp[PATH_MAX];
  102.     DIR *dirptr;
  103.     /*directory structor*/
  104.     struct dirent *direntp;
  105.     /*temprory file or directory name*/
  106.     char *name;
  107.     char *path = (char *) mypath;
  108.     int error;
  109.    
  110.     //printf("buraya geliyor mu\n");
  111.     printf("tid = %ld \n", (long int) pthread_self());
  112.  
  113.     /*gelen directory acilir*/
  114.     if ((dirptr = opendir(path)) == NULL)
  115.         fprintf(stderr, "[%ld]Failed the open directory %s \n", (long) getpid(), path);
  116.  
  117.     strcpy(temp, path);
  118.  
  119.     while ((direntp = readdir(dirptr)) != NULL) {
  120.         name = direntp->d_name;
  121.  
  122.         if ((strcmp(name, "..") == 0) || (strcmp(name, ".") == 0))
  123.             continue;
  124.  
  125.         strcat(temp, "/");
  126.         strcat(temp, name);
  127.         printf("%s \n", temp);
  128.  
  129.         /*Directory path arrayi olusturulur*/
  130.         if (isdirectory(temp)) {
  131.             strcpy(directorypath[dcounter], temp);
  132.             dcounter++;
  133.         }
  134.         /*File pathleri olusturulur*/
  135.         else {
  136.             strcpy(filespath[fcounter], temp);
  137.             fcounter++;
  138.         }
  139.  
  140.         strcpy(temp, path);
  141.  
  142.     } // end while
  143.  
  144.     while (dcounter > 0){
  145.         dcounter--;
  146.         i++;
  147.  
  148.         if (pthread_create(&tid[i], NULL, recursion, (void*) directorypath[dcounter])) {
  149.             fprintf(stderr, "Cannot create thread\n");
  150.             exit(1);
  151.         }
  152.  
  153.         if (error = pthread_join(tid[i], NULL))
  154.             fprintf(stderr, "Failed to join thread  %s\n", strerror(error));
  155.  
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement