Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- #include <dirent.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <stdlib.h>
- #include <time.h>
- #include <pwd.h>
- char * selTable[4];
- char selection[20];
- char * arg;
- int dot_entry(char *str){
- return (strcmp(".", str) == 0) ||
- (strcmp("..", str) == 0);
- }
- void procArg(char *uSource, char *uSelect, char *uArg){
- int j;
- selTable[0] = "-name";
- selTable[1] = "-ntime";
- selTable[2] = "-user";
- selTable[3] = NULL;
- fprintf(stdout,"source = %s, selection = %s, arg = %s.\n",uSource,uSelect,uArg);
- for(j=0; j<3; j=j+1){
- if(strcasecmp(uSelect,selTable[j])==0){
- strcpy(selection, uSelect);
- arg= (char *) malloc(strlen(uArg)*sizeof(char));
- strcpy(arg, uArg);
- }
- }
- }
- void visit_directory(char *path){
- DIR *directory;
- struct dirent *entry;
- struct stat file_attributes;
- struct passwd *pw;
- char *full_path;
- int path_length;
- if ((directory = opendir(path)) == NULL) {
- fprintf(stderr, "invalid directory: %s\n", path);
- exit(EXIT_FAILURE);
- }
- while ((entry = readdir(directory)) != NULL) {
- if (!dot_entry(entry->d_name)) {
- path_length = strlen(path) + strlen(entry->d_name) + 2;
- full_path = (char *) malloc(path_length * sizeof(char));
- strcat(full_path,path);
- strcat(full_path,"/");
- strcat(full_path, entry->d_name);
- if(lstat(full_path, &file_attributes)< 0){
- perror("There was an issue with this path de-allocating memory.\n");
- free(full_path);
- continue;
- }
- else{
- fprintf(stdout,"DIR %s\n",path);
- int mod_time = (int)file_attributes.st_mtime;
- int userID = file_attributes.st_uid;
- if(strcasecmp(selection,"-name")==0){
- if(strcasecmp(arg, entry->d_name)==0){
- if(S_ISDIR(file_attributes.st_mode)){
- fprintf(stdout, "DIR %s\n", entry->d_name);
- visit_directory(full_path);
- }
- else if(S_ISREG(file_attributes.st_mode)){
- fprintf(stdout, "REG %s\n", entry->d_name);
- }
- else{
- fprintf(stdout, "OTH %s\n", entry->d_name);
- }
- }
- }
- else if(strcasecmp(selection,"-mtime")==0){
- time_t mytime = time(NULL);
- int ctime = atoi(arg)*60;
- int utime = mytime - (ctime);
- if(utime <= mod_time){
- if(S_ISDIR(file_attributes.st_mode)){
- fprintf(stdout, "DIR %s\n", entry->d_name);
- visit_directory(full_path);
- }
- else if(S_ISREG(file_attributes.st_mode)){
- fprintf(stdout, "REG %s\n", entry->d_name);
- }
- else{
- fprintf(stdout, "OTH %s\n", entry->d_name);
- }
- }
- }
- else if(strcasecmp(selection,"-user")==0){
- fprintf(stdout,"DIR %s\n",path);
- fprintf(stdout,"%s\n",arg);
- pw = getpwnam(arg);
- fprintf(stdout,"----- %s\n",pw->pw_uid);
- //printf(" %-8.8s", pw->pw_name);
- /*if(strcasecmp(arg, userID)==0){
- if(S_ISDIR(file_attributes.st_mode)){
- fprintf(stdout, "DIR %s\n", entry->d_name);
- visit_directory(full_path);
- }
- else if(S_ISREG(file_attributes.st_mode)){
- fprintf(stdout, "REG %s\n", entry->d_name);
- }
- else{
- fprintf(stdout, "OTH %s\n", entry->d_name);
- }
- }*/
- }
- }
- }
- }
- free(full_path);
- closedir(directory);
- }
- int main(int argc, char *argv[]){
- if (argc <= 1) {
- fprintf(stderr, "no directory specified\n");
- exit(EXIT_FAILURE);
- }
- fprintf(stdout,"%s %s %s\n",argv[1],argv[2],argv[3]);
- procArg(argv[1],argv[2],argv[3]);
- //fprintf(stdout,"DIR %s\n",argv[1]);
- visit_directory(argv[1]);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement