Advertisement
razvanth21

Untitled

Nov 13th, 2018
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <dirent.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <unistd.h>
  7. #include <fcntl.h>
  8. #include <string.h>
  9.  
  10. int count(char *filepath, char option)
  11. {
  12.     int fd;
  13.    
  14.     if ((fd = open(filepath, O_RDONLY)) == -1)
  15.     {
  16.         fprintf(stderr, "Error while opening '%s' file.\n", filepath);
  17.         exit(EXIT_FAILURE + 1);
  18.     }
  19.    
  20.     char block[30];
  21.     int i, r, count = 0;
  22.    
  23.     while ((r = read(fd, block, sizeof(block))))
  24.     {
  25.         switch (option)
  26.         {
  27.             case 'c':
  28.             {
  29.                 for (i = 0; i < r; i++)
  30.                     if ((block[i] >= 'a' && block[i] <= 'z') || (block[i] >= 'A' && block[i] <= 'Z'))
  31.                         count ++;
  32.  
  33.                 break;
  34.             }
  35.             case 'l':
  36.             {
  37.                 for (i = 0; i < r; i++)
  38.                     if (block[i] == '\n')
  39.                         count ++;
  40.  
  41.                 break;
  42.             }
  43.             default:
  44.             {
  45.                 fprintf(stderr, "Invalid option.\n");
  46.                 exit(EXIT_FAILURE + 2);
  47.             }
  48.         }
  49.     }
  50.    
  51.     if (close(fd) == -1)
  52.     {
  53.         fprintf(stderr, "Error while closing '%s' file.\n", filepath);
  54.         exit(EXIT_FAILURE + 3);
  55.     }
  56.    
  57.     return count;
  58. }
  59.  
  60. void traverse(char *dirpath, char option, int fd)
  61. {
  62.     DIR *dir;
  63.    
  64.     if ((dir = opendir(dirpath)) == NULL)
  65.     {
  66.         fprintf(stderr, "Error while opening the '%s' directory.\n", dirpath);
  67.         exit(EXIT_FAILURE + 4);
  68.     }
  69.    
  70.     struct dirent *d;
  71.     struct stat st;
  72.    
  73.     char path[256];
  74.    
  75.     while ((d = readdir(dir)) != NULL)
  76.     {
  77.         if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
  78.             continue;
  79.        
  80.         sprintf(path, "%s/%s", dirpath, d->d_name);
  81.        
  82.         if (lstat(path, &st) < 0)
  83.         {
  84.             fprintf(stderr, "Error while getting file '%s' stats.\n", path);
  85.             exit(EXIT_FAILURE + 7);
  86.         }
  87.        
  88.         if (S_ISREG(st.st_mode))
  89.         {
  90.             char buff[280];
  91.            
  92.             sprintf(buff, "F <%s> <%d>\n", path, count(path, option));
  93.             fprintf(stdout, "%s", buff);
  94.            
  95.             write(fd, buff, strlen(buff));
  96.         }
  97.        
  98.         if (S_ISLNK(st.st_mode))
  99.         {
  100.             char buff[512], link_path[256], new_path[256];
  101.            
  102.             if (readlink(path, link_path, sizeof(link_path)) == -1)
  103.             {
  104.                 fprintf(stderr, "Error while getting links' path.\n");
  105.                 exit(EXIT_FAILURE + 8);
  106.             }
  107.            
  108.             sprintf(new_path, "%s/%s", dirpath, link_path);
  109.             sprintf(buff, "L <%s> <%d>\n", path, count(new_path, option));
  110.            
  111.             fprintf(stdout, "%s", buff);
  112.            
  113.             write(fd, buff, strlen(buff));
  114.         }
  115.        
  116.         if (S_ISDIR(st.st_mode))
  117.             traverse(path, option, fd);
  118.     }
  119.    
  120.     if (closedir(dir) == -1)
  121.     {
  122.         fprintf(stderr, "Error while closing the '%s' directory.\n", dirpath);
  123.         exit(EXIT_FAILURE + 9);
  124.     }
  125. }
  126.  
  127.  
  128. int main(int argc, char **argv)
  129. {
  130.     if (argc != 3)
  131.     {
  132.         fprintf(stderr, "Sintaxa: %s <cale_dir> <optiune>\n", argv[0]);
  133.         exit(EXIT_FAILURE);
  134.     }
  135.    
  136.     if (open("log.txt", O_RDONLY) != -1)
  137.     {
  138.         fprintf(stderr, "File 'log.txt' does already exist.\n");
  139.         exit(EXIT_FAILURE + 5);
  140.     }
  141.    
  142.     int fd;
  143.    
  144.     if ((fd = open("log.txt", O_WRONLY | O_CREAT, 0666)) == -1)
  145.     {
  146.         fprintf(stderr, "Error while creating 'log.txt' file.\n");
  147.         exit(EXIT_FAILURE + 6);
  148.     }
  149.    
  150.     traverse(argv[1], argv[2][1], fd);
  151.    
  152.     if (close(fd) == -1)
  153.     {
  154.         fprintf(stderr, "Error while closing the 'log.txt' file.\n");
  155.         exit(EXIT_FAILURE + 3);
  156.     }
  157.    
  158.     return 0;
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement