Thread: scandir select function

  1. #1
    Registered User
    Join Date
    Apr 2002

    scandir select function

    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.

    count = scandir(cwdir,&direntp,matchDetails,alphasort);
    As you can see from above i have put the matchDetails function in the select argument space.. so all hidden files should be ignored.

    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.
    /*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);
      if((status = regexec(&re,filename,(size_t) 0,NULL,0)) != 0)
    /*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!!!!!


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    > int matchDetails(char* filename){
    Are you sure you're getting a filename, and not a pathname?

    Have you tested matchDetails

    int main ( int argc, char *argv[] ) {
      printf( "%s = %d\n", argv[1], matchDetails(argv[1]) );
      return 0;

  3. #3
    Registered User
    Join Date
    Apr 2002
    Im definitely passing a filename to matchDetail.

    IT tests on the filename.

    What matchDetails does is takes one file and checks using the regular expression, if it is a hidden file (hidden files have dots .. ).
    If it is a hidden file then it returns 0
    if it is not hidden then it returns 1

    i dont want the hidden files listed so thats what the purpose of matchDetails is..

    i want it so scandir does not list hidden files,, therefore,, i have created a LS -L and not a LS -AL..

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Then I suggest you read the manual page more carefully next time
    int scandir(const char *dirname, struct dirent **namelist[],
    int (*select)(struct dirent *),
    int (*compar)(struct dirent **, struct dirent **));
    The select function receives a dirent*, not a char*

    Then perhaps
    int matchDetails( struct dirent *dptr){
    if((status = regexec(&re,dptr->d_name,(size_t) 0,NULL,0)) != 0)
    Same for your qsort function, just in case you were passing char* to that as well

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 02:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  5. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
Website Security Test