Thread: fnmatch matching directory

  1. #1
    Registered User
    Join Date
    Sep 2014
    Posts
    121

    fnmatch matching directory

    Hello, I`ve just read the ref of fnmatch() and I am intending to read a text file line by line and find if the line contains a directory path. So from what I read this code:
    Code:
    if ( fnmatch("?/*.?/", buffer, 0) ) 
        addFront(pDirs, bufer);
    should match with not perfect accuracy a simple right written directory. So I`ve tried it and it does match every line in the file. Let`s say the file is this:
    Code:
    /home/user/Downloads/
    /home/user/SomeFolder/
    
    /home/
    dasdsa
    /home/user/OtherDir/
    So after the reading of the file I got 6 elements in the list, te emtpy line, the nondir string ant the others, which are OK. But that`s not what was intended. Any help?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,455
    The first thing I suggest is that you check the return value from the function, to make clear your intention.

    Code:
    if ( fnmatch("?/*.?/", buffer, 0) == 0)
    Remember this function returns zero if it finds a match, some non-zero value otherwise.

    Next if that doesn't help, have you checked your wildcard string works at the command line?

    Jim

  3. #3
    Registered User
    Join Date
    Sep 2014
    Posts
    121
    Hello, I`ve found a way to match the directory entries without the empty lines and non dir strings:
    Code:
    while ( fgets(buff, 512, fp) ) {
    #ifdef __gnu_linux__
            if ( (fnmres = fnmatch("/*", buff,  0)) == 0)
                printf("[DBG][fnmatch()]: %s %d\n",  buff, fnmres);
    #endif
    However it also matches files like /home/user/log.txt, but for now it fits for my desires.
    Quote Originally Posted by jimblumberg View Post
    The first thing I suggest is that you check the return value from the function, to make clear your intention.

    Code:
    if ( fnmatch("?/*.?/", buffer, 0) == 0)
    Remember this function returns zero if it finds a match, some non-zero value otherwise.

    Next if that doesn't help, have you checked your wildcard string works at the command line?

    Jim
    Last edited by heatblazer; 09-14-2014 at 02:14 AM. Reason: found a solution

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    121
    Actually I came with a better idea, which fits my desire:
    Code:
    tested = strip(buff, ' ');
            tested = strip(tested, '\n');
            tested = strip(tested, '\t');
            if ( tested[0] == '/' && tested[strlen(tested)-1] == '/') {
                printf("#####%s\n", tested);
    }
    where char* strip(const char*, char) is a function that creates a new string by stripping a deimiter character from original string. Stripping, all newlines, tabs and whitespaces, leads to the simple logic that if the first char is / and the last is / the file has a huge chance to be a valid directory, thus opendir can be called for the check.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 04-30-2010, 06:13 PM
  2. Object matching
    By taurus in forum General AI Programming
    Replies: 21
    Last Post: 10-15-2009, 05:05 AM
  3. illegal else without matching if HELP!!!!
    By vodka9er in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2007, 04:09 PM
  4. file matching
    By ercumania in forum C Programming
    Replies: 7
    Last Post: 07-24-2006, 12:34 PM
  5. Pattern matching
    By manofsteel972 in forum C Programming
    Replies: 8
    Last Post: 10-08-2004, 08:23 PM

Tags for this Thread