Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | None | 0 0
  1. //test
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <dirent.h>
  7. #include <fcntl.h>
  8. #include <sys/stat.h>
  9. #include <string.h>
  10.  
  11. int parse(char *dirName) {
  12.     DIR *dir;
  13.  
  14.     if ((dir = opendir(dirName)) == NULL) {
  15.         printf("Error at opendir().");
  16.         exit(1);
  17.     }
  18.  
  19.     struct dirent *d;
  20.     struct stat st;
  21.     char path[50];
  22.     int counter = 0;
  23.  
  24.     while ((d = readdir(dir))) {
  25.         if ((strcmp(d->d_name, ".") == 0) || (strcmp(d->d_name, "..") == 0))
  26.             continue;
  27.         snprintf(path, 50, "%s/%s", dirName, d->d_name);
  28.     printf("%s\n", path);
  29.         if (lstat(path, &st) == -1) {
  30.             printf("Error at lstat().");
  31.             exit(2);
  32.         }
  33.         if (S_ISDIR(st.st_mode)) {
  34.             counter += parse(path);
  35.         }
  36.         if (S_ISREG(st.st_mode)) {
  37.         printf("yes\n");
  38.             counter ++;
  39.         }
  40.     }
  41.     closedir(dir);
  42.     return counter;
  43. }
  44.  
  45. int main (int argc, char *argv[]) {
  46.     if (argc != 2) {
  47.         printf("Usage: Program <%s> file directory", argv[0]);
  48.         exit(0);
  49.     }
  50.     printf("Files: %d", parse(argv[1]));
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement