1 .\" Copyright (c) 2000 by Michael Kerrisk (mtk.manpages@gmail.com)
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" Created, 14 Dec 2000 by Michael Kerrisk
25 .TH BASENAME 3 2009-03-30 "GNU" "Linux Programmer's Manual"
27 basename, dirname \- parse pathname components
30 .B #include <libgen.h>
32 .BI "char *dirname(char *" path );
34 .BI "char *basename(char *" path );
37 Warning: there are two different functions
45 break a null-terminated pathname string into directory
46 and filename components.
49 returns the string up to, but not including, the final \(aq/\(aq, and
51 returns the component following the final \(aq/\(aq.
52 Trailing \(aq/\(aq characters are not counted as part of the pathname.
56 does not contain a slash,
58 returns the string "." while
64 is the string "/", then both
68 return the string "/".
71 is a NULL pointer or points to an empty string, then both
75 return the string ".".
77 Concatenating the string returned by
79 a "/", and the string returned by
81 yields a complete pathname.
87 may modify the contents of
89 so it may be desirable to pass a copy when calling one of
92 These functions may return pointers to statically allocated memory
93 which may be overwritten by subsequent calls.
94 Alternatively, they may return a pointer to some part of
96 so that the string referred to by
98 should not be modified or freed until the pointer returned by
99 the function is no longer required.
101 The following list of examples (taken from SUSv2)
102 shows the strings returned by
109 .B "path dirname basename"
110 "/usr/lib" "/usr" "lib"
122 return pointers to null-terminated strings.
123 (Do not pass these pointers to
128 There are two different versions of
130 - the POSIX version described above, and the GNU version, which one gets
135 .B " #define _GNU_SOURCE"
137 .B " #include <string.h>"
140 The GNU version never modifies its argument, and returns the
143 has a trailing slash, and in particular also when it is "/".
144 There is no GNU version of
147 With glibc, one gets the POSIX version of
151 is included, and the GNU version otherwise.
153 In the glibc implementation of the POSIX versions of these functions
154 they modify their argument, and segfault when called with a static string
156 Before glibc 2.2.1, the glibc version of
158 did not correctly handle pathnames with trailing \(aq/\(aq characters,
159 and generated a segfault if given a NULL argument.
163 char *dirc, *basec, *bname, *dname;
164 char *path = "/etc/passwd";
167 basec = strdup(path);
168 dname = dirname(dirc);
169 bname = basename(basec);
170 printf("dirname=%s, basename=%s\\n", dname, bname);
176 .BR feature_test_macros (7)