Advertisement
Guest User

Untitled

a guest
Mar 26th, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <dirent.h>
  4. #include <sys/stat.h>
  5. #include <time.h>
  6. #include <stdlib.h>
  7. #include "limits.h"
  8. #include <string.h>
  9.  
  10.  
  11. void listFiles(const char *path);
  12.  
  13. time_t lowerDate, upperDate;
  14. long long lowerSize, upperSize;
  15.  
  16. int main(int argc, char** argv)
  17. {
  18.         char *path = argv[1];
  19.  
  20.         struct tm tm;
  21.         memset( &tm,  0, sizeof( struct tm ) );
  22.         strptime(argv[2], "%D", &tm);
  23.  
  24.         lowerDate = mktime(&tm);
  25.  
  26.         memset( &tm,  0, sizeof( struct tm ) );
  27.         strptime(argv[3], "%D", &tm);
  28.  
  29.         upperDate = mktime(&tm);
  30.  
  31.         char* strLowerSize = argv[4];
  32.         char* strUpperSize = argv[5];
  33.  
  34.         char* endptr;
  35.         lowerSize = strtoll(strLowerSize, &endptr, 10);
  36.         upperSize = strtoll(strUpperSize, &endptr, 10);
  37.  
  38.         listFiles(path);
  39.  
  40.  
  41.  
  42.         return 0;
  43. }
  44.  
  45. /**
  46.  * Lists all files and sub-directories at given path.
  47.  */
  48. void listFiles(const char *path)
  49. {
  50.         struct dirent *dp;
  51.         DIR *dir = opendir(path);
  52.  
  53.         // Unable to open directory stream
  54.         if (!dir)
  55.                 return;
  56.  
  57.         while ((dp = readdir(dir)) != NULL)
  58.         {
  59.                 char* dir = malloc(strlen(path)+ NAME_MAX + 1);
  60.                 strcpy(dir, path);
  61.                 strcat(dir,"/");
  62.                 strcat(dir, dp->d_name);
  63.                 if(dp->d_type == DT_DIR&& strcmp(".", dp->d_name) && strcmp("..", dp->d_name) ) {
  64.  
  65.                         //  printf("%s\n", dir);
  66.                         listFiles(dir);
  67.                 } else if(dp->d_type == DT_REG) {
  68.                         struct stat st;
  69.                         stat(dir, &st);
  70.                         time_t lastModified = st.st_mtim.tv_sec;
  71.                         off_t size = st.st_size;
  72.                         if(lastModified >= lowerDate && lastModified <= upperDate && size >= lowerSize && size <= upperSize) {
  73.                                 printf("File: %s -  %s \n", dp->d_name, ctime(&lastModified));
  74.                         }
  75.            
  76.                         // check whether file matches conditions or not and output if  necessary
  77.  
  78.                 }
  79.         }
  80.  
  81.         // Close directory stream
  82.         closedir(dir);
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement