So my code is supposed to take two inputs one being an int that will be representing a date in unix time, and the other is supposed to be a directory to search through. it then checks the files access time and if it is within a day of the inputted date it will print the file name and the date is was accessed. I apologize in advance for bad naming, my code:
This is the output:Code:/*Description: Takes a date in posix seconds and a directory as input and searches all files *and subdirectories for anything that has been accessed,modified, or created within *24 hours of the specified date. */ #include <sys/stat.h> #include <dirent.h> #include <stdio.h> #include <time.h> #include <string.h> #include <stdlib.h> /* *Description: takes the posix seconds value cal_time of type time_t and *returns it as a string in the format of: month day hours:minutes. */ char * format_time(time_t cal_time){ struct tm *time_struct; static char string[30]; time_struct=localtime(&cal_time); strftime(string, sizeof string, "%h %e %H:%M", time_struct); return(string); }// end format_time function int main(int argc, char *argv[]) { int i; struct stat buf; char *fileType; int date = atoi(argv[1]); //checks input if the path given is in fact a directory for (i=1; i<argc; i++) { printf("Times of %s:\n",argv[i]); if (lstat(argv[i], &buf) < 0) { printf("lstat error\n"); continue; } //checks to see if path given is a directory, if it isnt the program throws an error and exits if (!S_ISDIR(buf.st_mode)) { printf("%s is not a directory\n",argv[2]); exit(0); } printf("Access time is %s\n", format_time(buf.st_atime)); printf("Modify time is %s\n", format_time(buf.st_mtime)); printf("I-node change time is %s\n", format_time(buf.st_ctime)); printf("\n"); }//end for loop tsSearch(date,argv[2]); }// end main function /* *Description: loops through the directory and checks all the files access/modified/i-node times *and prints out the files and those times if the time is within a day of the inputted "date", if the file being checked is a *directory the function is recursively called */ void tsSearch(int date, char* path){ DIR *dp; struct dirent *dirp; struct stat buf; char* slash = "/\0"; char* temp = strcat(path,slash); //appends a slash to the directory char* tmp; char* filepath; //int atime; //time_t a; int k = strlen(temp); //printf("%d\n", k); //checks to see if the path of the directory that is given is a valid directory, if not program throws an error and exits if ((dp = opendir(path)) == NULL){ printf("can't open %s\n", path); exit(0); } while ((dirp = readdir(dp)) != NULL){ int j = strlen(dirp->d_name); str_copy(temp,tmp); fixtemp(tmp,k,j); str_copy(temp,tmp); printf("%s\n",dirp->d_name); filepath = strcat(tmp,dirp->d_name); printf("%s\n",filepath); //atime = getATime(filepath); printf("%s, %s, Access\n", dirp->d_name, format_time(buf.st_atime)); /*if(S_ISDIR(filepath)) tsSearch(date, filepath);*/ //if(dirp->d_name == "." || dirp->d_name == ".."){} /*if(atime > date-86400 && atime < date+86400) printf("%s, %s, Accessed\n", dirp->d_name, format_time(atime));*/ fixtemp(tmp,k,j); fixtemp(filepath,k,j); }//end while loop closedir(dp); }// end tsSearch function void fixtemp(char* path, int i, int j){ int k = i; for(;k<i+j;k++) path[k] = '\0'; } void str_copy(char *s, char *t){ for(;*s!='\0';s++){ *t = *s; t++; } }//end strcopy function void str_cat(char *s, char *t){ str_copy(s,t+strlen(t)); }//end function strcat //gets the access time of the file at path int getATime(char* path){ printf("%s\n", path); struct stat buf; //time_t tmp = buf.st_atime; printf("Accessed: %s\n", format_time(buf.st_atime)); int t = 1; return t; }// end getATime function
ive come to the conclusion through testing that my problem lies with the fixtemp function and the line "filepath = strcat(tmp,dirp->d_name);" because it causes an issue with struct stat for some reason. I was under the impression that if i gave a valid filepath struct stat would work.Code:jimmy@ubuntu:~/Desktop$ ./tsSearch2 1302757200 /home/jimmy/Desktop/herpderp Times of 1302757200: lstat error Times of /home/jimmy/Desktop/herpderp: Access time is Apr 18 21:09 Modify time is Apr 18 21:09 I-node change time is Apr 18 21:09 18-SystemIO.ppt /home/jimmy/Desktop/herpderp/18-SystemIO.ppt Segmentation fault
i have a section of code commented out, im kinda lookin for an opinion on it, would that recursive call to tsSearch work or would it just give me the same problem with the struct stat?
Also i know it isnt wrapped around anything at the moment while it is commented out, but i was also having issues when checking for "." and "..", i tried srcmp as well but it didnt work.
I'd appreciate any help.