1 .\" Copyright (c) 2000 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date. The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein. The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" Created, 14 Dec 2000 by Michael Kerrisk
27 .TH BASENAME 3 2017-09-15 "GNU" "Linux Programmer's Manual"
29 basename, dirname \- parse pathname components
32 .B #include <libgen.h>
34 .BI "char *dirname(char *" path );
36 .BI "char *basename(char *" path );
39 Warning: there are two different functions
47 break a null-terminated pathname string into directory
48 and filename components.
51 returns the string up to, but not including, the final \(aq/\(aq, and
53 returns the component following the final \(aq/\(aq.
54 Trailing \(aq/\(aq characters are not counted as part of the pathname.
58 does not contain a slash,
60 returns the string "." while
66 is the string "/", then both
70 return the string "/".
73 is a null pointer or points to an empty string, then both
77 return the string ".".
79 Concatenating the string returned by
81 a "/", and the string returned by
83 yields a complete pathname.
89 may modify the contents of
91 so it may be desirable to pass a copy when calling one of
94 These functions may return pointers to statically allocated memory
95 which may be overwritten by subsequent calls.
96 Alternatively, they may return a pointer to some part of
98 so that the string referred to by
100 should not be modified or freed until the pointer returned by
101 the function is no longer required.
103 The following list of examples (taken from SUSv2)
104 shows the strings returned by
113 path dirname basename
127 return pointers to null-terminated strings.
128 (Do not pass these pointers to
131 For an explanation of the terms used in this section, see
137 Interface Attribute Value
141 T} Thread safety MT-Safe
144 POSIX.1-2001, POSIX.1-2008.
146 There are two different versions of
148 - the POSIX version described above, and the GNU version, which one gets
153 .BR " #define _GNU_SOURCE" " /* See feature_test_macros(7) */"
154 .B " #include <string.h>"
158 The GNU version never modifies its argument, and returns the
161 has a trailing slash, and in particular also when it is "/".
162 There is no GNU version of
165 With glibc, one gets the POSIX version of
169 is included, and the GNU version otherwise.
171 In the glibc implementation,
172 the POSIX versions of these functions modify the
174 argument, and segfault when called with a static string
177 Before glibc 2.2.1, the glibc version of
179 did not correctly handle pathnames with trailing \(aq/\(aq characters,
180 and generated a segfault if given a NULL argument.
182 The following code snippet demonstrates the use of
188 char *dirc, *basec, *bname, *dname;
189 char *path = "/etc/passwd";
192 basec = strdup(path);
193 dname = dirname(dirc);
194 bname = basename(basec);
195 printf("dirname=%s, basename=%s\\n", dname, bname);