First of all, I didn't wanted to get into this, since it is OS specific..
I started out by telling you which functions to look up and read on, since they are implemented differently on every OS.
Anyway I've found a solution which is quite ANSI C complient..
Code:
#include <stdio.h>
#include <dirent.h>
#include <string.h>
int found_state(char* name, char* state, int offset);
int main(int argc, char* argv[])
{
DIR* dir_pointer;
struct dirent* directory;
int len, found = 0;
if(argc < 2)
{
printf("Usage: %s <state> [dir]\n\n", argv[0]);
printf("\tIn order to search for every instace of .doc files\n");
printf("\tin the current directory use:\n");
printf("\t\t%s .doc\n", argv[0]);
printf("\tIf it should be searched for in your /usr/lib dir use:\n");
printf("\t\t%s .doc /usr/lib\n", argv[0]);
return -1;
}
len = strlen(argv[1]);
if(argc == 2)
dir_pointer = opendir(".");
else
dir_pointer = opendir(argv[2]);
if(!dir_pointer)
{
printf("Error reading from directory\n");
return -1;
}
printf("Found files:\n");
for (directory = readdir(dir_pointer);
directory != NULL;
directory = readdir(dir_pointer))
{
if(found_state(directory->d_name,
argv[1],
strlen(directory->d_name) - len))
{
found = 1;
printf("%s\n", directory->d_name);
}
}
if(!found)
printf("No files found to match %s\n", argv[1]);
closedir(dir_pointer);
return 0;
}
int found_state(char* name, char* state, int offset)
{
int count;
if(offset < 1)
return 0;
for(count = 0;
name[offset + count] != '\0'
&& state[count] != '\0'
&& name[offset + count] == state[count]; count++)
/* just compare */;
if(name[offset + count] == '\0' && state[count] == '\0')
return 1;
return 0;
}
Now the found_state() function can be altered, infact the entire program can be altered..
But this is the basic directory/algorithem for achieving such things.