mount_setattr.2: Add a reference to mount_namespaces(7) in discussion of propagation...
[man-pages.git] / man3 / basename.3
blob1492207cada86b56fc3c83a5687c834135d97f8a
1 .\" Copyright (c) 2000 by Michael Kerrisk <mtk.manpages@gmail.com>
2 .\"
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.
7 .\"
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.
12 .\"
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
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .\" Created, 14 Dec 2000 by Michael Kerrisk
26 .\"
27 .TH BASENAME 3  2021-03-22 "GNU" "Linux Programmer's Manual"
28 .SH NAME
29 basename, dirname \- parse pathname components
30 .SH SYNOPSIS
31 .nf
32 .B #include <libgen.h>
33 .PP
34 .BI "char *dirname(char *" path );
35 .BI "char *basename(char *" path );
36 .fi
37 .SH DESCRIPTION
38 Warning: there are two different functions
39 .BR basename ();
40 see below.
41 .PP
42 The functions
43 .BR dirname ()
44 and
45 .BR basename ()
46 break a null-terminated pathname string into directory
47 and filename components.
48 In the usual case,
49 .BR dirname ()
50 returns the string up to, but not including, the final \(aq/\(aq, and
51 .BR basename ()
52 returns the component following the final \(aq/\(aq.
53 Trailing \(aq/\(aq characters are not counted as part of the pathname.
54 .PP
56 .I path
57 does not contain a slash,
58 .BR dirname ()
59 returns the string "." while
60 .BR basename ()
61 returns a copy of
62 .IR path .
64 .I path
65 is the string "/", then both
66 .BR dirname ()
67 and
68 .BR basename ()
69 return the string "/".
71 .I path
72 is a null pointer or points to an empty string, then both
73 .BR dirname ()
74 and
75 .BR basename ()
76 return the string ".".
77 .PP
78 Concatenating the string returned by
79 .BR dirname (),
80 a "/", and the string returned by
81 .BR basename ()
82 yields a complete pathname.
83 .PP
84 Both
85 .BR dirname ()
86 and
87 .BR basename ()
88 may modify the contents of
89 .IR path ,
90 so it may be desirable to pass a copy when calling one of
91 these functions.
92 .PP
93 These functions may return pointers to statically allocated memory
94 which may be overwritten by subsequent calls.
95 Alternatively, they may return a pointer to some part of
96 .IR path ,
97 so that the string referred to by
98 .I path
99 should not be modified or freed until the pointer returned by
100 the function is no longer required.
102 The following list of examples (taken from SUSv2)
103 shows the strings returned by
104 .BR dirname ()
106 .BR basename ()
107 for different paths:
110 lb lb lb
111 l l l l.
112 path            dirname basename
113 /usr/lib        /usr    lib
114 /usr/           /       usr
115 usr             .       usr
116 /               /       /
117 \&.             .       .
118 \&..            .       ..
121 .SH RETURN VALUE
122 Both
123 .BR dirname ()
125 .BR basename ()
126 return pointers to null-terminated strings.
127 (Do not pass these pointers to
128 .BR free (3).)
129 .SH ATTRIBUTES
130 For an explanation of the terms used in this section, see
131 .BR attributes (7).
132 .ad l
135 allbox;
136 lbx lb lb
137 l l l.
138 Interface       Attribute       Value
140 .BR basename (),
141 .BR dirname ()
142 T}      Thread safety   MT-Safe
146 .sp 1
147 .SH CONFORMING TO
148 POSIX.1-2001, POSIX.1-2008.
149 .SH NOTES
150 There are two different versions of
151 .BR basename ()
152 - the POSIX version described above, and the GNU version, which one gets
153 after
155 .in +4n
157 .BR "    #define _GNU_SOURCE" "         /* See feature_test_macros(7) */"
158 .B "    #include <string.h>"
162 The GNU version never modifies its argument, and returns the
163 empty string when
164 .I path
165 has a trailing slash, and in particular also when it is "/".
166 There is no GNU version of
167 .BR dirname ().
169 With glibc, one gets the POSIX version of
170 .BR basename ()
171 when
172 .I <libgen.h>
173 is included, and the GNU version otherwise.
174 .SH BUGS
175 In the glibc implementation,
176 the POSIX versions of these functions modify the
177 .I path
178 argument, and segfault when called with a static string
179 such as "/usr/".
181 Before glibc 2.2.1, the glibc version of
182 .BR dirname ()
183 did not correctly handle pathnames with trailing \(aq/\(aq characters,
184 and generated a segfault if given a NULL argument.
185 .SH EXAMPLES
186 The following code snippet demonstrates the use of
187 .BR basename ()
189 .BR dirname ():
190 .in +4n
192 char *dirc, *basec, *bname, *dname;
193 char *path = "/etc/passwd";
195 dirc = strdup(path);
196 basec = strdup(path);
197 dname = dirname(dirc);
198 bname = basename(basec);
199 printf("dirname=%s, basename=%s\en", dname, bname);
202 .SH SEE ALSO
203 .BR basename (1),
204 .BR dirname (1)