[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Question about basename() / base_name()

From: Eric Blake
Subject: Re: Question about basename() / base_name()
Date: Tue, 23 Dec 2014 10:07:13 -0700
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0

On 12/23/2014 09:53 AM, Tim Rühsen wrote:
> Hi,
> I want to avoid the basename() POSIX/GNU incompatibility and also would like 
> to have basename() working on Windows (it is a user requesting this for Wget).

You don't want basename() and dirname() (the POSIX rules on those
functions mean that they cannot work for Windows drive letters).  You
WANT to use base_name() and dir_name().

> I see the gnulib module 'dirname' contains the base_name() function.

base_name() returns a malloc'd string that contains the basename of the
input string.  You can also use last_component() to get a pointer to the
basename within the input string rather than a malloc'd copy (the
semantics are slightly different since base_name() malloc's "." for the
empty string while last_component() cannot).  And just as
last_component() can be used to avoid xalloc() so that a library doesn't
die on allocation failures, so can mdir_name() avoid xalloc in place of

> But I can't find any documentation for it.

Where would you suggest such documentation beyond what is already in

/* In general, we can't use the builtin 'dirname' function if available,
   since it has different meanings in different environments.
   In some environments the builtin 'dirname' modifies its argument.

   Return the leading directories part of FILE, allocated with malloc.
   Works properly even if there are trailing slashes (by effectively
   ignoring them).  Return NULL on failure.

   If lstat (FILE) would succeed, then { chdir (dir_name (FILE));
   lstat (base_name (FILE)); } will access the same file.  Likewise,
   if the sequence { chdir (dir_name (FILE));
   rename (base_name (FILE), "foo"); } succeeds, you have renamed FILE
   to "foo" in the same directory FILE was in.  */
char *
mdir_name (char const *file)

/* Just like mdir_name (dirname-lgpl.c), except, rather than
   returning NULL upon malloc failure, here, we report the
   "memory exhausted" condition and exit.  */
char *
dir_name (char const *file)

/* Return the address of the last file name component of NAME.  If
   NAME has no relative file name components because it is a file
   system root, return the empty string.  */
char *
last_component (char const *name)

> Thus my question: Is this function 'stable' or are you going to change the 
> interface in the future ? (if going to change, what do you suggest as 
> alternative).

They are stable.  Use them.

Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

Attachment: signature.asc
Description: OpenPGP digital signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]