Guest User

Untitled

a guest
Apr 6th, 2015
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.72 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <pthread.h>
  5. #include <dirent.h>
  6. #include <string.h>
  7.  
  8. static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
  9.  
  10. struct thread_info {
  11.     pthread_t thread_id;
  12.     int thread_num;
  13.     char *file;
  14.     char *current_dir;
  15. };
  16.  
  17. static void *count_lines(void *args)
  18. {
  19.     pthread_mutex_lock(&lock);    
  20.  
  21.     int count = 0;
  22.     struct thread_info *tinfo = args;
  23.     printf("%s\n", tinfo->file);
  24.     char *line;
  25.     line = (char*) malloc (sizeof(char));
  26.    
  27.     FILE *file = fopen(tinfo->file, "r");
  28.     fgets(line, 256, file);
  29.     fclose(file);
  30.  
  31.     if(count < 1)
  32.     {
  33.         printf("%s%s contient %d lignes\n", tinfo->current_dir, tinfo->file, count);
  34.     } else
  35.     {
  36.         printf("%s%s contient %d ligne\n", tinfo->current_dir, tinfo->file, count);
  37.     }    
  38.  
  39.     pthread_mutex_unlock(&lock);
  40.     pthread_exit(NULL);
  41. }
  42.  
  43. int main(int argc, char* argv[]) {
  44.     char directory[256];
  45.     struct dirent *dir;
  46.     char *current_dir = getcwd(directory, 256);
  47.     DIR* d = opendir(current_dir);
  48.     struct thread_info *tinfo;
  49.  
  50.     while((dir = readdir(d)) != NULL)
  51.     {
  52.         if(strcmp(dir->d_name, "..") != 0 && strcmp(dir->d_name, ".") != 0)
  53.         {
  54.             if(dir->d_type == 8)
  55.             {    
  56.             }
  57.         }
  58.     }
  59.                
  60.     pthread_attr_t attr;
  61.     pthread_attr_init(&attr);
  62.                
  63.     tinfo = malloc(sizeof(struct thread_info));
  64.  
  65.     tinfo->file = "contributors.txt";
  66.     tinfo->current_dir = current_dir;
  67.    
  68.     pthread_create(&tinfo->thread_id, &attr, count_lines, &tinfo);
  69.     pthread_join(tinfo->thread_id,  NULL);
  70.    
  71.     pthread_mutex_destroy(&lock);
  72.  
  73.     return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment