I want to know how to use the select part or the scandir function..
My program wants to ignore hidden files when scanning a directory, so i wrote a function called 'matchDetails' which checks if the file passed to it is hidden or not.
-- it is hidden it returns 0.
-- it is not hidden it returns 1.
What select should do is,, select results from scanning the directory and ignore any results that return a 0.
As you can see from above i have put the matchDetails function in the select argument space.. so all hidden files should be ignored.Code:count = scandir(cwdir,&direntp,matchDetails,alphasort);
this is a quote of what the scandir and select function should do:
The scandir() function scans the directory dir, calling
select() on each directory entry. Entries for which
select() returns non-zero are stored in strings allocated
via malloc(), sorted using qsort() with the comparison
function compar(), and collected in array namelist which
is allocated via malloc(). If select is NULL, all entries
are selected.Code:/*Used to find process directories*/ int matchDetails(char* filename){ int status,regstat; char *pattern = "^[^.].*$"; /*Match all strings without a begining '.'*/ char *result; regex_t re; if((regstat = regcomp(&re,pattern,REG_EXTENDED | REG_NOSUB)) != 0){ result = get_regerror(regstat,&re); fprintf(stderr,"%s\n",result); return(0); } if((status = regexec(&re,filename,(size_t) 0,NULL,0)) != 0) return(0); regfree(&re); return(1); } /*Used to store regex errors*/ char *get_regerror(int errcode,regex_t *compiled){ size_t length = regerror(errcode,compiled,NULL,0); char *buffer = malloc(length); (void) regerror(errcode,compiled,buffer,length); return buffer; }
Please tell me how this function should work,, and what i have done wrong or not wrong!!!!!
Thankyou!!



LinkBack URL
About LinkBacks


