Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8964277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:33:05+00:00 2026-06-15T16:33:05+00:00

Citing the manual here: The functions dirname() and basename() break a null-terminated pathname string

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:33:06+00:00Added an answer on June 15, 2026 at 4:33 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Citing source at: http://jqueryui.com/demos/dialog/#modal-form ) As an example, this works great but each time
Recently I hear Steve Jobs citing this as a main reason to not have
How to get the contents of the wav file into array so as to
I'm connecting to a remote sql server instance with the following connection string Network
I'm interested in answers citing a concrete passage in the standard, not just general
This is second consecutive time that apple has rejected my application citing the following
Silverlight is case sensitive for query string parameters so the following code would return
See the code below - I am trying to put a const object into
I am attempting to connect to a web page and return a JSON string.
I've been a .NET developer for a couple of years and am moving into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.