madvice

wc.c

Sep 28th, 2025
103
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.21 KB | Software | 0 0
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <pthread.h>
  4. #include <string.h>
  5.  
  6.  
  7. #define MAX_FILES_COUNT 1000
  8. #define MAX_LINE_LENGTH 10000
  9.  
  10. #define OUT 0
  11. #define IN 1
  12.  
  13. struct file_info {
  14.     char* file_name;
  15.     FILE* file_ptr;
  16. };
  17.  
  18.  
  19. int total_lines, total_words, total_chars;
  20. pthread_mutex_t mutex;
  21.  
  22.  
  23. void* compute_word_count(void* args) {
  24.  
  25.     struct file_info* file = (struct file_info*) args;
  26.     int lines, words, chars;
  27.  
  28.     char* file_name = file -> file_name;
  29.     FILE* file_ptr = file -> file_ptr;
  30.  
  31.     lines = words = chars = 0;
  32.  
  33.     char buffer[MAX_LINE_LENGTH];
  34.  
  35.     while(fgets(buffer, sizeof(buffer), file_ptr) != NULL) {
  36.         lines++;
  37.         int i = 0;
  38.         int state = OUT;
  39.         while(buffer[i] != '\0') {
  40.             chars++;
  41.             if(buffer[i] == ' ' || buffer[i] == '\t' || buffer[i] == '\n')
  42.                 state = OUT;
  43.             else if(state == OUT) {
  44.                 words++;
  45.                 state = IN;
  46.             }
  47.             i++;
  48.         }
  49.     }
  50.     printf("%2d %4d %8d %16s\n", lines, words, chars, file_name);
  51.     pthread_mutex_lock(&mutex);
  52.     total_lines += lines;
  53.     total_words += words;
  54.     total_chars += chars;
  55.     pthread_mutex_unlock(&mutex);
  56. }
  57.  
  58.  
  59. void get_word_count(struct file_info files[], int count) {
  60.     pthread_t tid[MAX_FILES_COUNT];
  61.     pthread_mutex_init(&mutex, NULL);
  62.     for(int i = 0; i < count; i++) {
  63.         pthread_create(&tid[i], NULL, &compute_word_count, &files[i]);
  64.     }
  65.     for(int i = 0; i < count; i++) {
  66.         pthread_join(tid[i], NULL);
  67.     }
  68.     pthread_mutex_destroy(&mutex);
  69.  
  70.     printf("%2d %4d %8d %16s\n", total_lines, total_words, total_chars, "total");
  71. }
  72.  
  73.  
  74. int main(int argc, char* argv[])
  75. {
  76.     FILE* file_ptr[MAX_FILES_COUNT];
  77.     struct file_info files[MAX_FILES_COUNT];
  78.     int valid_files = 0;
  79.     if(argc - 1 > MAX_FILES_COUNT) {
  80.         fprintf(stderr, "File count limit exceeded. Remove %d files.", argc - 1 - MAX_FILES_COUNT);
  81.     }
  82.     for(int i = 0; i < argc - 1; i++) {
  83.         file_ptr[valid_files] = fopen(argv[i + 1], "r");
  84.         if(file_ptr[valid_files] == NULL)
  85.             fprintf(stderr, "File %s not found\n", argv[i + 1]);
  86.         else {
  87.             files[valid_files].file_ptr = file_ptr[valid_files];
  88.             files[valid_files].file_name = argv[i + 1];
  89.             valid_files++;
  90.         }
  91.     }
  92.  
  93.     get_word_count(files, valid_files);
  94.  
  95.     for(int i = 0; i < valid_files; i++) {
  96.         fclose(file_ptr[i]);
  97.     }
  98.  
  99.     return 0;
  100. }
Advertisement
Comments
Add Comment
Please, Sign In to add comment