#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
void search ( char *filename , char *dirname ){
int f = 0;
struct dirent *dirPointer;
DIR *d;
d = opendir( dirname );
if ( d == NULL ) {
fprintf( stderr, "%s %d: opendir() failed (%s)\n",
dirname, errno, strerror( errno ));
exit( EXIT_FAILURE );
}
while ( (dirPointer=readdir(d)) != NULL ) {
if (strcmp( dirPointer->d_name, filename) == 0 ) {
// print absolute path of file
printf("File located\n");
f = 1;
}
}
if ( f == 0 ) {
printf("File not found\n");
}
closedir ( d );
printf("\n");
}
int main( int argc, char *argv[] ) {
int x = argc;
int count;
printf("Number of args: %d\n",x);
if ( x == 2 ){
//search for file or dir in current dir
printf("Searching\n");
search( argv[1] , "." );
}
if ( x > 2 ) {
//first n-1 are files to search for
//n is the folder to search
for ( count = 2 ; count < argc ; count++ ) {
search( argv[count],argv[argc]);
}
}
if ( x == 1 ) {
printf("Not enough input\n");
}
return 0;
}