Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.40 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <stdlib.h>
  4. #include <dirent.h>
  5. #include <string.h>
  6. #include <sys/stat.h>
  7. #include <time.h>
  8.  
  9. char* getType(int fileType) {
  10.     char* type;
  11.  
  12.     switch (fileType) {
  13.         case DT_REG:
  14.             type = "file";
  15.             break;
  16.         case DT_DIR:
  17.             type = "dir";
  18.             break;
  19.         case DT_FIFO:
  20.             type = "fifo";
  21.             break;
  22.         case DT_SOCK:
  23.             type = "sock";
  24.             break;
  25.         case DT_CHR:
  26.             type = "char dev";
  27.             break;
  28.         case DT_BLK:
  29.             type = "block dev";
  30.             break;
  31.         case DT_LNK:
  32.             type = "slink";
  33.             break;
  34.         default:
  35.             type ="error";
  36.             break;
  37.     }
  38.  
  39.     return type;
  40. }
  41.  
  42. void searchDir(char* dirName) {
  43.     DIR* currentDirectory = opendir(dirName);
  44.  
  45.     if(currentDirectory == NULL) {
  46.         printf("Nie ma takiego katalogu\n");
  47.     } else {
  48.         struct dirent* thisDir;
  49.  
  50.         while((thisDir = readdir(currentDirectory)) != NULL) {
  51.             if(strcmp(thisDir->d_name, ".") && strcmp(thisDir->d_name, "..")) {
  52.                 char* newPath = calloc(1024, sizeof(char));
  53.                 struct stat stats;
  54.  
  55.                 strcat(newPath, dirName);
  56.                 strcat(newPath, "/");
  57.                 strcat(newPath, thisDir->d_name);
  58.                 lstat(newPath, &stats);
  59.  
  60.                 char* type = getType(thisDir->d_type);
  61.                 char* path = realpath(newPath, NULL);
  62.  
  63.                 printf("%s:\n Pełna ścieżka: %s\n Rodzaj pliku: %s\n Rozmiar pliku: %ld B\n Data ostatniego dostępu: %ld\n Data ostatnie modyfikacji: %ld\n", thisDir->d_name, path, type, stats.st_size, stats.st_atime, stats.st_mtime);
  64.  
  65.                 if(thisDir->d_type == DT_DIR && thisDir->d_type != DT_LNK) {
  66.                     searchDir(newPath);
  67.                 }
  68.  
  69.                 free(newPath);
  70.             }
  71.         }
  72.     }
  73. }    
  74.    
  75.  
  76. int main(int argc, char* argv[]) {
  77.     char* startDir = argv[1];
  78.     char* equality = argv[2];
  79.     char* date = argv[3];
  80.  
  81.     if(argc < 4) {
  82.         printf("Podano złą liczbę argumentów\n");
  83.     } else if(strcmp(equality, "=") && strcmp(equality, ">") && strcmp(equality, "<")) {
  84.         printf("Wprowadzono nieprawidłowy znak porówniania\n");
  85.     } else {
  86.         searchDir(startDir);
  87.     }
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement