OSDN Git Service

LDP: Update original to LDP v3.79
[linuxjm/LDP_man-pages.git] / original / man3 / basename.3
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  2014-06-13 "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 .sp
34 .BI "char *dirname(char *" path );
35
36 .BI "char *basename(char *" path );
37 .fi
38 .SH DESCRIPTION
39 Warning: there are two different functions
40 .BR basename ()
41 - see below.
42 .LP
43 The functions
44 .BR dirname ()
45 and
46 .BR basename ()
47 break a null-terminated pathname string into directory
48 and filename components.
49 In the usual case,
50 .BR dirname ()
51 returns the string up to, but not including, the final \(aq/\(aq, and
52 .BR basename ()
53 returns the component following the final \(aq/\(aq.
54 Trailing \(aq/\(aq characters are not counted as part of the pathname.
55 .PP
56 If
57 .I path
58 does not contain a slash,
59 .BR dirname ()
60 returns the string "." while
61 .BR basename ()
62 returns a copy of
63 .IR path .
64 If
65 .I path
66 is the string "/", then both
67 .BR dirname ()
68 and
69 .BR basename ()
70 return the string "/".
71 If
72 .I path
73 is a null pointer or points to an empty string, then both
74 .BR dirname ()
75 and
76 .BR basename ()
77 return the string ".".
78 .PP
79 Concatenating the string returned by
80 .BR dirname (),
81 a "/", and the string returned by
82 .BR basename ()
83 yields a complete pathname.
84 .PP
85 Both
86 .BR dirname ()
87 and
88 .BR basename ()
89 may modify the contents of
90 .IR path ,
91 so it may be desirable to pass a copy when calling one of
92 these functions.
93 .PP
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
97 .IR path ,
98 so that the string referred to by
99 .I path
100 should not be modified or freed until the pointer returned by
101 the function is no longer required.
102 .PP
103 The following list of examples (taken from SUSv2)
104 shows the strings returned by
105 .BR dirname ()
106 and
107 .BR basename ()
108 for different paths:
109 .sp
110 .TS
111 lb lb lb
112 l l l l.
113 path            dirname basename
114 /usr/lib        /usr    lib
115 /usr/           /       usr
116 usr             .       usr
117 /               /       /
118 \&.             .       .
119 \&..            .       ..
120 .TE
121 .SH RETURN VALUE
122 Both
123 .BR dirname ()
124 and
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 .SS Multithreading (see pthreads(7))
131 The
132 .BR basename ()
133 and
134 .BR dirname ()
135 functions are thread-safe.
136 .SH CONFORMING TO
137 POSIX.1-2001.
138 .SH NOTES
139 There are two different versions of
140 .BR basename ()
141 - the POSIX version described above, and the GNU version, which one gets
142 after
143 .br
144 .nf
145
146 .BR "    #define _GNU_SOURCE" "         /* See feature_test_macros(7) */"
147 .br
148 .B "    #include <string.h>"
149
150 .fi
151 The GNU version never modifies its argument, and returns the
152 empty string when
153 .I path
154 has a trailing slash, and in particular also when it is "/".
155 There is no GNU version of
156 .BR dirname ().
157 .LP
158 With glibc, one gets the POSIX version of
159 .BR basename ()
160 when
161 .I <libgen.h>
162 is included, and the GNU version otherwise.
163 .SH BUGS
164 In the glibc implementation of the POSIX versions of these functions
165 they modify their argument, and segfault when called with a static string
166 like "/usr/".
167 Before glibc 2.2.1, the glibc version of
168 .BR dirname ()
169 did not correctly handle pathnames with trailing \(aq/\(aq characters,
170 and generated a segfault if given a NULL argument.
171 .SH EXAMPLE
172 .in +4n
173 .nf
174 char *dirc, *basec, *bname, *dname;
175 char *path = "/etc/passwd";
176
177 dirc = strdup(path);
178 basec = strdup(path);
179 dname = dirname(dirc);
180 bname = basename(basec);
181 printf("dirname=%s, basename=%s\\n", dname, bname);
182 .fi
183 .in
184 .SH SEE ALSO
185 .BR basename (1),
186 .BR dirname (1)
187 .SH COLOPHON
188 This page is part of release 3.79 of the Linux
189 .I man-pages
190 project.
191 A description of the project,
192 information about reporting bugs,
193 and the latest version of this page,
194 can be found at
195 \%http://www.kernel.org/doc/man\-pages/.