Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.61 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <unistd.h>
  5. #include <dirent.h>
  6. #include <fcntl.h>
  7. #include <sys/stat.h>
  8. #include <string.h>
  9. #include <ctype.h>
  10.  
  11. int counter = 0, fd, digits = 0, low = 0, up = 0, n;
  12.  
  13. int parse(char *ch, char *dirName) {
  14.     char character = *ch;
  15.     DIR *dir;
  16.  
  17.     if ((dir = opendir(dirName)) == NULL) {
  18.         printf("Error at opendir().");
  19.         exit(1);
  20.     }
  21.  
  22.     struct dirent *d;
  23.     struct stat st;
  24.     char path[50], c;
  25.  
  26.     while ((d = readdir(dir))) {
  27.         if ((strcmp(d->d_name, ".") == 0) || (strcmp(d->d_name, "..") == 0))
  28.             continue;
  29.         snprintf(path, 50, "%s/%s", dirName, d->d_name);
  30.         printf("%s\n", path);
  31.         if (lstat(path, &st) == -1) {
  32.             printf("Error at lstat().");
  33.             exit(2);
  34.         }
  35.         if (S_ISDIR(st.st_mode)) {
  36.         parse(ch, path);
  37.         }
  38.         if (S_ISREG(st.st_mode)) {
  39.             if((fd = open(path, O_RDONLY)) < 0) {
  40.             printf("Error opening input file\n");
  41.             exit(3);
  42.             }
  43.         while((n = read(fd, &c, sizeof(char))) > 0) {
  44.         if(c == character)
  45.             counter ++;
  46.         if(isdigit(c))
  47.             digits ++;
  48.         if(islower(c))
  49.             low ++;
  50.         if(isupper(c))
  51.             up ++;
  52.         }
  53.         close(fd);
  54.         }
  55.     }
  56.     printf("Digits: %d\nLower: %d\nUpper: %d\nCharacter: %d\n", digits, low, up, counter);
  57.     closedir(dir);
  58.     return 0;
  59. }
  60.  
  61. int main (int argc, char *argv[]) {
  62.     if (argc != 3) {
  63.         printf("Usage: Program <%s> char directory", argv[0]);
  64.         exit(0);
  65.     }
  66.     parse(argv[1], argv[2]);
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement