/* * fnmatch. * Match files in the current directory to a pattern passed as a parameter. * usage: fnmatch * * E.g. * fnmatch "*.c" * * Make sure to enclose the pattern paramter in quotes so the shell does not interpret characters such as '*'. */ #include #include #include #include #include #include int main(int argc, char **argv) { struct dirent **entries; int i; int nument; int ret; if(argc!=2) { fprintf(stderr,"usage: %s \n",argv[0]); fflush(stderr); return 1; } if((nument=scandir(".",&entries,NULL,alphasort))==-1) { fprintf(stderr,"scandir failed, %s\n",strerror(errno)); fflush(stderr); return 1; } for(i=0; id_name,FNM_PATHNAME))==FNM_NOMATCH) /*compare each file to the pattern*/ { continue; } else if(ret==0) { fprintf(stdout,"%s\n",entries[i]->d_name); fflush(stdout); } else { fprintf(stderr,"fnmatch failed, %s\n",strerror(errno)); fflush(stderr); } } for(i=0; i