Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.84 KB | None | 0 0
  1. #define _XOPEN_SOURCE 500
  2. #include <stdio.h>
  3. #include <malloc.h>
  4. #include <stdlib.h>
  5. #include <dirent.h>
  6. #include <string.h>
  7. #include <sys/stat.h>
  8. #include <time.h>
  9. #include <ftw.h>
  10. #include <stdint.h>
  11.  
  12. char* getType(int fileType) {
  13.     char* type;
  14.  
  15.     switch (fileType) {
  16.         case FTW_F:
  17.             type = "file";
  18.             break;
  19.         case FTW_D:
  20.             type = "dir";
  21.             break;
  22.         case FTW_SL:
  23.             type = "slink";
  24.             break;
  25.         default:
  26.             type ="error";
  27.             break;
  28.     }
  29.  
  30.     return type;
  31. }
  32.  
  33. char* getDateFromTimestamp(time_t timestamp, struct tm* ts) {
  34.     char* date = calloc(80, sizeof(char));
  35.  
  36.     *ts = *localtime(&timestamp);
  37.     strftime(date, 80*sizeof(char), "%a %Y-%m-%d %H:%M:%S %Z", ts);
  38.     return date;
  39. }
  40.  
  41. static int printData(const char *fpath, const struct stat *sb, int tflag, struct FTW *ftwbuf) {
  42.     struct tm ts;
  43.     char* type = getType(tflag);
  44.     char* path = realpath(fpath, NULL);
  45.     char* aDate = getDateFromTimestamp(sb->st_atime, &ts);
  46.     char* mDate = getDateFromTimestamp(sb->st_mtime, &ts);
  47.     printf("%s:\n Pełna ścieżka: %s\n Rodzaj pliku: %s\n Rozmiar pliku: %ld B\n Data ostatniego dostępu: %s\n Data ostatnie modyfikacji: %s\n", fpath, path, type, sb->st_size, aDate, mDate);
  48.     return 0;
  49. }
  50.  
  51. int main(int argc, char* argv[]) {
  52.     char* startDir = argv[1];
  53.     char* mode = argv[2];
  54.     char* day = argv[3];
  55.     char* month = argv[4];
  56.     char* year = argv[5];
  57.     int flags = 0;
  58.     flags |= FTW_PHYS;
  59.  
  60.     if(argc < 6) {
  61.         printf("Podano złą liczbę argumentów\n");
  62.     } else if(strcmp(mode, "=") && strcmp(mode, ">") && strcmp(mode, "<")) {
  63.         printf("Wprowadzono nieprawidłowy znak porówniania\n");
  64.     } else {
  65.         nftw(startDir, checkData, 20, flags);
  66.     }
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement