Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dirname() in C: is the manual wrong?

Tags:

c

dirname

Citing the manual here:

The functions dirname() and basename() break a null-terminated pathname string into directory and filename components. In the usual case, dirname() returns the string up to, but not including, the final '/', and basename() returns the component following the final '/'. Trailing '/' characters are not counted as part of the pathname.

And later on, you have this little table:

path         dirname    basename
"/usr/lib"    "/usr"    "lib"
"/usr/"       "/"       "usr"        // wat?
"usr"         "."       "usr"
"/"           "/"       "/"
"."           "."       "."
".."          "."       ".."

Why is dirname( "/usr/") returning "/" and not "/usr" ?
The sentence in the manual tells me I should get /usr as a result.
I tested the actual result in a dummy program and it behaves just like the manual says.

#include <libgen.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, char const *argv[])
{
    const char *mydir="/usr/";
    char *dummy  = strdup( mydir );
    char *dummy2 = strdup( mydir );

    char *dname = dirname( dummy );
    char *bname = basename( dummy2 );


    printf("mydir: '%s', dname: '%s', bname: '%s'\n", mydir, dname, bname);

    free( dummy );
    free( dummy2 );

    return 0;
}


$ ./test  
mydir: '/usr/', dname: '/', bname: 'usr'

Now, what I would expect would be:

path         dirname    basename
"/usr/"       "/usr"       ""        // more consistent?

So.. anybody understands what's going on here?

like image 388
Gui13 Avatar asked Dec 10 '12 16:12

Gui13


People also ask

What does the dirname () function do?

Definition and Usage. The dirname() function returns the path of the parent directory.

What does dirname do in C?

The dirname() function takes a pointer to a character string that contains a path name, and returns a pointer to a string that is a path name of the parent directory of that file.


1 Answers

Trailing '/' characters are not counted as part of the pathname.

Hence "/usr/" is the same as "/usr", which might denote a file or a directory with name (directory entry named) usr in the directory /. The function dirname returns the parent directory of the path. The parent directory of /usr is /. Seems entirely consistent.

like image 153
chill Avatar answered Sep 27 '22 21:09

chill