Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #include <unistd.h>
- #include <string.h>
- #include <time.h>
- #define N_ARG 3
- #define N 128
- #define BUF_LIMIT 512
- #define ec_neg(s,m) \
- if( (s) < 0 ) {perror(m); exit(errno);}
- #define ec_null(s,m) \
- if( (s) == NULL ) {perror(m); exit(errno);}
- #define ec_args(s,m) \
- if( (s) != N_ARG) {printf(m); usage(); exit(ECANCELED);}
- #define ERREAD(s) \
- {printf("Couldn't read this directory\n");}
- static char* s_path;
- static int f_flag=0;
- void usage(){
- printf("Syntax : myfnd dir fname\n");
- printf("Searches into dir the file named fname\n");
- printf("and prints its absolute path and the date\n");
- printf("of its last modification.\n");
- }
- void fnd(char* path, char* name){
- DIR* dd;
- struct dirent* ff;
- struct stat fileStat;
- char buf[BUF_LIMIT];
- ec_null((getcwd(buf,N)),"Couldn't get the current working directory");
- ec_neg((chdir(path)),"Couldn't change the working directory");
- ec_null((dd=opendir(".")),"Couldn't open this directory");
- while((errno = 0, ff = readdir(dd))!=NULL){
- if(strcmp(ff->d_name,name)==0){
- printf("%s/%s/%s\t\t%s\n",s_path,path,name,(asctime(localtime(&fileStat.st_mtime))));
- f_flag=1;
- }
- ec_neg(stat(ff->d_name,&fileStat),"Couldn't read info for this file\n");
- if(S_ISDIR(fileStat.st_mode)&&(strcmp(ff->d_name,"..")!=0)&&(strcmp(ff->d_name,".")!=0)) fnd(ff->d_name,name);
- }
- if (errno!=0) ERREAD(errno)
- ec_neg((closedir(dd)),"Couldn't close this directory");
- ec_neg((chdir(buf)),"Couldn't return into the previous working directory");
- }
- int main(int argc, char *argv[]){
- ec_args(argc,"Wrong arguments number\n");
- s_path = argv[1];
- fnd(argv[1],argv[2]);
- if(!f_flag) printf("File not found\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement