#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <dirent.h>
#include <fnmatch.h>

int find_item_in_directory(const char *dirname, const char *pattern) {
DIR *theDir;
struct dirent *theItem;
int res;

if ((theDir = opendir(dirname)) == NULL) {
	(void) perror(dirname);
	return 1;
}

while ((theItem = readdir(theDir)) != NULL) {
	res = fnmatch(pattern, theItem->d_name, 0);
	if (res == 0) {
		(void)printf("%s\n", theItem->d_name);
	}
}
(void)closedir(theDir);
return 0;
}

int main(int argc, char *argv[]) {
int i;

if (argc < 2) {
	(void)fprintf(stderr, "Usage is: %s pattern dir [dir...]\n", argv[0]);
	return 0;
}
for (i = 2; i < argc; i++)
	(void)find_item_in_directory(argv[i], argv[1]);
return 0;
}
