OSDN Git Service

(split) Import translated manuals from JM CVS Repository.
[linuxjm/LDP_man-pages.git] / original / man3 / basename.3
1 .\" Copyright (c) 2000 by Michael Kerrisk (mtk.manpages@gmail.com)
2 .\"
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.
6 .\"
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.
11 .\"
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
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\" License.
23 .\" Created, 14 Dec 2000 by Michael Kerrisk
24 .\"
25 .TH BASENAME 3  2009-03-30 "GNU" "Linux Programmer's Manual"
26 .SH NAME
27 basename, dirname \- parse pathname components
28 .SH SYNOPSIS
29 .nf
30 .B #include <libgen.h>
31 .sp
32 .BI "char *dirname(char *" path );
33
34 .BI "char *basename(char *" path );
35 .fi
36 .SH DESCRIPTION
37 Warning: there are two different functions
38 .BR basename ()
39 - see below.
40 .LP
41 The functions
42 .BR dirname ()
43 and
44 .BR basename ()
45 break a null-terminated pathname string into directory
46 and filename components.
47 In the usual case,
48 .BR dirname ()
49 returns the string up to, but not including, the final \(aq/\(aq, and
50 .BR basename ()
51 returns the component following the final \(aq/\(aq.
52 Trailing \(aq/\(aq characters are not counted as part of the pathname.
53 .PP
54 If
55 .I path
56 does not contain a slash,
57 .BR dirname ()
58 returns the string "." while
59 .BR basename ()
60 returns a copy of
61 .IR path .
62 If
63 .I path
64 is the string "/", then both
65 .BR dirname ()
66 and
67 .BR basename ()
68 return the string "/".
69 If
70 .I path
71 is a NULL pointer or points to an empty string, then both
72 .BR dirname ()
73 and
74 .BR basename ()
75 return the string ".".
76 .PP
77 Concatenating the string returned by
78 .BR dirname (),
79 a "/", and the string returned by
80 .BR basename ()
81 yields a complete pathname.
82 .PP
83 Both
84 .BR dirname ()
85 and
86 .BR basename ()
87 may modify the contents of
88 .IR path ,
89 so it may be desirable to pass a copy when calling one of
90 these functions.
91 .PP
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
95 .IR path ,
96 so that the string referred to by
97 .I path
98 should not be modified or freed until the pointer returned by
99 the function is no longer required.
100 .PP
101 The following list of examples (taken from SUSv2)
102 shows the strings returned by
103 .BR dirname ()
104 and
105 .BR basename ()
106 for different paths:
107 .sp
108 .nf
109 .B "path         dirname    basename"
110 "/usr/lib"    "/usr"    "lib"
111 "/usr/"       "/"       "usr"
112 "usr"         "."       "usr"
113 "/"           "/"       "/"
114 "."           "."       "."
115 ".."          "."       ".."
116 .fi
117 .SH "RETURN VALUE"
118 Both
119 .BR dirname ()
120 and
121 .BR basename ()
122 return pointers to null-terminated strings.
123 (Do not pass these pointers to
124 .BR free (3).)
125 .SH "CONFORMING TO"
126 POSIX.1-2001.
127 .SH NOTES
128 There are two different versions of
129 .BR basename ()
130 - the POSIX version described above, and the GNU version, which one gets
131 after
132 .br
133 .nf
134
135 .B "    #define _GNU_SOURCE"
136 .br
137 .B "    #include <string.h>"
138
139 .fi
140 The GNU version never modifies its argument, and returns the
141 empty string when
142 .I path
143 has a trailing slash, and in particular also when it is "/".
144 There is no GNU version of
145 .BR dirname ().
146 .LP
147 With glibc, one gets the POSIX version of
148 .BR basename ()
149 when
150 .I <libgen.h>
151 is included, and the GNU version otherwise.
152 .SH BUGS
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
155 like "/usr/".
156 Before glibc 2.2.1, the glibc version of
157 .BR dirname ()
158 did not correctly handle pathnames with trailing \(aq/\(aq characters,
159 and generated a segfault if given a NULL argument.
160 .SH EXAMPLE
161 .in +4n
162 .nf
163 char *dirc, *basec, *bname, *dname;
164 char *path = "/etc/passwd";
165
166 dirc = strdup(path);
167 basec = strdup(path);
168 dname = dirname(dirc);
169 bname = basename(basec);
170 printf("dirname=%s, basename=%s\\n", dname, bname);
171 .fi
172 .in
173 .SH "SEE ALSO"
174 .BR basename (1),
175 .BR dirname (1),
176 .BR feature_test_macros (7)