1BASENAME(3)                Linux Programmer's Manual               BASENAME(3)
2
3
4

NAME

6       basename, dirname - parse pathname components
7

SYNOPSIS

9       #include <libgen.h>
10
11       char *dirname(char *path);
12
13       char *basename(char *path);
14

DESCRIPTION

16       Warning: there are two different functions basename() - see below.
17
18       The functions dirname() and basename() break a null-terminated pathname
19       string into directory and filename  components.   In  the  usual  case,
20       dirname()  returns  the string up to, but not including, the final '/',
21       and basename() returns the component following the final '/'.  Trailing
22       '/' characters are not counted as part of the pathname.
23
24       If  path  does  not  contain  a slash, dirname() returns the string "."
25       while basename() returns a copy of path.  If path is  the  string  "/",
26       then both dirname() and basename() return the string "/".  If path is a
27       NULL pointer or points to an empty  string,  then  both  dirname()  and
28       basename() return the string ".".
29
30       Concatenating  the  string returned by dirname(), a "/", and the string
31       returned by basename() yields a complete pathname.
32
33       Both dirname() and basename() may modify the contents of  path,  so  it
34       may be desirable to pass a copy when calling one of these functions.
35
36       These  functions  may  return  pointers  to statically allocated memory
37       which may be overwritten by subsequent calls.  Alternatively, they  may
38       return  a  pointer to some part of path, so that the string referred to
39       by path should not be modified or freed until the pointer  returned  by
40       the function is no longer required.
41
42       The  following  list  of  examples (taken from SUSv2) shows the strings
43       returned by dirname() and basename() for different paths:
44
45
46       path       dirname   basename
47       /usr/lib   /usr      lib
48       /usr/      /         usr
49       usr        .         usr
50       /          /         /
51       .          .         .
52       ..         .         ..
53

RETURN VALUE

55       Both  dirname()  and  basename()  return  pointers  to  null-terminated
56       strings.  (Do not pass these pointers to free(3).)
57

CONFORMING TO

59       POSIX.1-2001.
60

NOTES

62       There  are  two  different  versions  of basename() - the POSIX version
63       described above, and the GNU version, which one gets after
64
65           #define _GNU_SOURCE         /* See feature_test_macros(7) */
66           #include <string.h>
67
68       The GNU version never modifies its  argument,  and  returns  the  empty
69       string  when  path has a trailing slash, and in particular also when it
70       is "/".  There is no GNU version of dirname().
71
72       With glibc, one gets the POSIX version of basename() when <libgen.h> is
73       included, and the GNU version otherwise.
74

BUGS

76       In  the  glibc  implementation of the POSIX versions of these functions
77       they modify their argument, and segfault  when  called  with  a  static
78       string  like  "/usr/".   Before  glibc  2.2.1,  the  glibc  version  of
79       dirname() did not correctly handle pathnames with trailing '/'  characā€
80       ters, and generated a segfault if given a NULL argument.
81

EXAMPLE

83           char *dirc, *basec, *bname, *dname;
84           char *path = "/etc/passwd";
85
86           dirc = strdup(path);
87           basec = strdup(path);
88           dname = dirname(dirc);
89           bname = basename(basec);
90           printf("dirname=%s, basename=%s\n", dname, bname);
91

SEE ALSO

93       basename(1), dirname(1)
94

COLOPHON

96       This  page  is  part of release 3.53 of the Linux man-pages project.  A
97       description of the project, and information about reporting  bugs,  can
98       be found at http://www.kernel.org/doc/man-pages/.
99
100
101
102GNU                               2009-03-30                       BASENAME(3)
Impressum