Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <sys/types.h>
- #include <dirent.h>
- #include <sys/stat.h>
- #include <time.h>
- #include <stdlib.h>
- #include "limits.h"
- #include <string.h>
- void listFiles(const char *path);
- time_t lowerDate, upperDate;
- long long lowerSize, upperSize;
- int main(int argc, char** argv)
- {
- char *path = argv[1];
- struct tm tm;
- memset( &tm, 0, sizeof( struct tm ) );
- strptime(argv[2], "%D", &tm);
- lowerDate = mktime(&tm);
- memset( &tm, 0, sizeof( struct tm ) );
- strptime(argv[3], "%D", &tm);
- upperDate = mktime(&tm);
- char* strLowerSize = argv[4];
- char* strUpperSize = argv[5];
- char* endptr;
- lowerSize = strtoll(strLowerSize, &endptr, 10);
- upperSize = strtoll(strUpperSize, &endptr, 10);
- listFiles(path);
- return 0;
- }
- /**
- * Lists all files and sub-directories at given path.
- */
- void listFiles(const char *path)
- {
- struct dirent *dp;
- DIR *dir = opendir(path);
- // Unable to open directory stream
- if (!dir)
- return;
- while ((dp = readdir(dir)) != NULL)
- {
- char* dir = malloc(strlen(path)+ NAME_MAX + 1);
- strcpy(dir, path);
- strcat(dir,"/");
- strcat(dir, dp->d_name);
- if(dp->d_type == DT_DIR&& strcmp(".", dp->d_name) && strcmp("..", dp->d_name) ) {
- // printf("%s\n", dir);
- listFiles(dir);
- } else if(dp->d_type == DT_REG) {
- struct stat st;
- stat(dir, &st);
- time_t lastModified = st.st_mtim.tv_sec;
- off_t size = st.st_size;
- if(lastModified >= lowerDate && lastModified <= upperDate && size >= lowerSize && size <= upperSize) {
- printf("File: %s - %s \n", dp->d_name, ctime(&lastModified));
- }
- // check whether file matches conditions or not and output if necessary
- }
- }
- // Close directory stream
- closedir(dir);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement