#include #include #include #include #include #include #include #include #include /*GLOBAL VARIABLES*/ struct stat stat_buffer; struct dirent directory; char * file_adate = NULL; char * file_mdate = NULL; char * file_size = NULL; /*METHOD DECLARATION*/ void Usage(){ // printf("------USAGE------"); // printf("filesearch [-ams] [filename] [-l] [pattern] "); // printf("-a Time of last access. "); // printf("-m Time of last data modification."); // printf("-s For regular files, the file size in bytes."); // printf(" For symbolic links, the length in bytes of the"); // printf(" pathname contained in the symbolic link."); // prinft("-----------------"); } void GetArguments(int argc, char **argv, char** switches, int* swc, char*** arguments, int* arc) { int k = 0; int l = 0; int i = 1; int j = 0; if(argc == 1) { Usage(); return; } for (i = 1; i < argc; i++) if (argv[i][0] == '-') { (*swc) += strlen(argv[i]) - 1; (*arc)++; } /////char * ptr_switches; /////char * ptr_arguments; (*arc) = argc - 1 - (*arc); printf("GetArguments: NUMBER OF SWITCHES = %d NUMBER OF ARGUMENTS = %d\n", *swc, *arc); /*Upon successful completion with size not equal to 0, malloc() shall return a pointer to the allocated space. If size is 0, either a null pointer or a unique pointer that can be successfully passed to free() shall be returned. Otherwise, it shall return a null pointer and set errno to indicate the error.*/ (*switches) = (char *) malloc( ( sizeof(char) * (*swc) ) + 1); if ((*switches) == NULL){ printf("ERROE ENCOUNTERED in (*switches) = (char*) malloc((sizeof(char) * (*swc)) + 1)"); exit (1); } (*arguments) = (char**) malloc( ( sizeof(char *) * (*arc) ) + 1); if ((*arguments) == NULL){ printf("ERROE ENCOUNTERED in (*arguments) = malloc(sizeof(char*) * (*arc))"); exit (1); } (*switches)[ sizeof(char) * (*swc) ] = 0; int J; for (i = 1; i < argc; i++){ if (argv[i][0] == '-') { J = strlen(argv[i]) - 1; for (j = 0; j < J; j++) (*switches)[l++] = argv[i][j+1]; } else (*arguments)[k++] = argv[i]; } printf ("(*switches) = %s, (*arguments) = %s\n", *switches,**arguments); } void PrintFileInfo(char * fn) { if(fn){ printf("PrintFileInfo for %s\n", fn); if(stat(fn, &stat_buffer)== -1) perror("filesearch"); if (file_mdate) printf("Time of last modification : %s\n", asctime(localtime(&stat_buffer.st_mtime))); if (file_adate) printf("Time of last access : %s\n", asctime(localtime(&stat_buffer.st_atime))); if (file_size) printf("Size of file : %d\n", (int)stat_buffer.st_size); } } /*END METHOD DECLARATION*/ int main(int argc, char **argv) { int k = 0; char * switches; int swc = 0; char ** arguments; int arc = 0; setbuf(stdout, NULL); //if(stat("/home/samba/salardif/filesearch", &stat_buffer) == -1) // perror("filesearch"); GetArguments(argc, argv, &switches, &swc, &arguments, &arc); file_mdate = strrchr(switches, 'm'); file_size = strrchr(switches, 's'); file_adate = strrchr(switches, 'a'); for (k = 0; k < arc; k++) { PrintFileInfo(arguments[k]); } return 0; }