1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
"minifind.c" 64 lines, 1307 characters
/*minifind.c
*
* Problem Statement: a program that finds a file under directories.
* Input: command type
* Output: paths where files are
* Usage: minifind dir filename
*/
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include <dirent.h>
#include <stdlib.h>
void where_filename(char *, char *);
char * mk_dirname(char *);
main(int ac, char *av[])
{
if( ac != 3){
fprintf( stderr, "usage: %s source destination\n", *av);
exit(1);
}
where_filename(av[1],av[2]);
}
void where_filename(char * dir_name, char * file_name){
DIR * dir_ptr;
struct dirent * direntp;
struct stat info;
char * tmp_dirname;
if( (dir_ptr = opendir(dir_name)) == NULL)
fprintf(stderr,"minifind: cannot open %s\n", dir_name);
else
{
while((direntp = readdir( dir_ptr) ) != NULL ){
if((!strcmp(direntp->d_name, ".") == 0) && (!strcmp(direntp->d_name, "..") == 0)){
stat(direntp->d_name, &info);
// can't check if file is directory
if(S_ISDIR(info.st_mode)){ printf("%s\n", direntp->d_name);
return where_filename(direntp->d_name,file_name);
}
}
if( strcmp(direntp->d_name,file_name) == 0){
printf("%s/%s\n", dir_name, file_name);
}
}
closedir(dir_ptr);
}
}
}
|