OSDN Git Service

cc2fd36e242292824465d3c6f73f853695af7f52
[linuxjm/LDP_man-pages.git] / draft / 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 .\" Japanese Version Copyright (c) 2001 NAKANO Takeo all rights reserved.
26 .\" Translated 2001-03-31, NAKANO Takeo <nakano@apm.seikei.ac.jp>
27 .\" Updated 2005-02-26, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
28 .\" Updated 2007-05-01, Akihiro MOTOKI, LDP v2.46
29 .\"
30 .TH BASENAME 3  2009-03-30 "GNU" "Linux Programmer's Manual"
31 .\"O .SH NAME
32 .\"O basename, dirname \- parse pathname components
33 .SH 名前
34 basename, dirname \- パス名を解析して各部分を取り出す
35 .\"O .SH SYNOPSIS
36 .SH 書式
37 .nf
38 .B #include <libgen.h>
39 .sp
40 .BI "char *dirname(char *" path );
41
42 .BI "char *basename(char *" path );
43 .fi
44 .\"O .SH DESCRIPTION
45 .SH 説明
46 .\"O Warning: there are two different functions
47 .\"O .BR basename ()
48 .\"O - see below.
49 警告:
50 .BR basename ()
51 には異なるバージョンが 2つ存在する。下記の「注意」の節を参照のこと。
52 .LP
53 .\"O The functions
54 .\"O .BR dirname ()
55 .\"O and
56 .\"O .BR basename ()
57 .\"O break a null-terminated pathname string into directory
58 .\"O and filename components.
59 .\"O In the usual case,
60 .\"O .BR dirname ()
61 .\"O returns the string up to, but not including, the final \(aq/\(aq, and
62 .\"O .BR basename ()
63 .\"O returns the component following the final \(aq/\(aq.
64 .\"O Trailing \(aq/\(aq characters are not counted as part of the pathname.
65 .BR dirname ()
66
67 .BR basename ()
68 は、NULL で終端されたパス名の文字列を、
69 ディレクトリ部分・ファイル名部分に分割する。
70 通常は、
71 .BR dirname ()
72 は最後の \(aq/\(aq までの部分 (最後の \(aq/\(aq は含まない) を返し、
73 .BR basename ()
74 は最後の \(aq/\(aq 以降の部分を返す。
75 文字列の末尾についた \(aq/\(aq 文字は、パス名の一部とはみなされない。
76 .PP
77 .\"O If
78 .\"O .I path
79 .\"O does not contain a slash,
80 .\"O .BR dirname ()
81 .\"O returns the string "." while
82 .\"O .BR basename ()
83 .\"O returns a copy of
84 .\"O .IR path .
85 .\"O If
86 .\"O .I path
87 .\"O is the string "/", then both
88 .\"O .BR dirname ()
89 .\"O and
90 .\"O .BR basename ()
91 .\"O return the string "/".
92 .\"O If
93 .\"O .I path
94 .\"O is a NULL pointer or points to an empty string, then both
95 .\"O .BR dirname ()
96 .\"O and
97 .\"O .BR basename ()
98 .\"O return the string ".".
99 .I path
100 に '/' 文字がない場合は、
101 .BR dirname ()
102 は文字列 "." を返し、
103 .BR basename ()
104
105 .I path
106 と同じ内容を返す。
107 .I path
108 が文字列 "/" に等しい場合は、
109 .BR dirname ()
110
111 .BR basename ()
112 も文字列 "/" を返す。
113 .I path
114 が NULL ポインタだったり、空の文字列を指していた場合は、
115 .BR dirname ()
116
117 .BR basename ()
118 も文字列 "." を返す。
119 .PP
120 .\"O Concatenating the string returned by
121 .\"O .BR dirname (),
122 .\"O a "/", and the string returned by
123 .\"O .BR basename ()
124 .\"O yields a complete pathname.
125 .BR dirname ()
126 の返した文字列、 "/"、
127 .BR basename ()
128 の返した文字列、
129 を順に結合すると、完全なパス名が得られる。
130 .PP
131 .\"O Both
132 .\"O .BR dirname ()
133 .\"O and
134 .\"O .BR basename ()
135 .\"O may modify the contents of
136 .\"O .IR path ,
137 .\"O so it may be desirable to pass a copy when calling one of
138 .\"O these functions.
139 .BR dirname ()
140
141 .BR basename ()
142 は、いずれも
143 .I path
144 の内容を変更することがある。
145 したがって、これらの関数を呼び出す際には
146 コピーを渡すのが望ましい。
147 .PP
148 .\"O These functions may return pointers to statically allocated memory
149 .\"O which may be overwritten by subsequent calls.
150 .\"O Alternatively, they may return a pointer to some part of
151 .\"O .IR path ,
152 .\"O so that the string referred to by
153 .\"O .I path
154 .\"O should not be modified or freed until the pointer returned by
155 .\"O the function is no longer required.
156 これらの関数は、静的に割り当てられたメモリへのポインタを返すことがあり、
157 これらの領域は後の関数呼び出しで上書きされるかもしれない。
158 また、これらの関数は
159 .I path
160 の一部分を指すポインタを返すこともある。そのため、
161 .I path
162 で参照される文字列は、関数が返すポインタが不要になるまでは
163 変更したり free したりすべきではない。
164 .PP
165 .\"O The following list of examples (taken from SUSv2)
166 .\"O shows the strings returned by
167 .\"O .BR dirname ()
168 .\"O and
169 .\"O .BR basename ()
170 .\"O for different paths:
171 以下の一連の例 (SUSv2 から引用) は、
172 いろいろな path に対して
173 .BR dirname ()
174
175 .BR basename ()
176 が返す文字列を表したものである。
177 .sp
178 .nf
179 .B "path         dirname    basename"
180 "/usr/lib"    "/usr"    "lib"
181 "/usr/"       "/"       "usr"
182 "usr"         "."       "usr"
183 "/"           "/"       "/"
184 "."           "."       "."
185 ".."          "."       ".."
186 .fi
187 .\"O .SH "RETURN VALUE"
188 .SH 返り値
189 .\"O Both
190 .\"O .BR dirname ()
191 .\"O and
192 .\"O .BR basename ()
193 .\"O return pointers to null-terminated strings.
194 .\"O (Do not pass these pointers to
195 .\"O .BR free (3).)
196 .BR dirname ()
197
198 .BR basename ()
199 は、いずれも NULL で終端された文字列へのポインタを返す。
200 (これらのポインタを
201 .BR free (3)
202 に渡さないこと。)
203 .\"O .SH "CONFORMING TO"
204 .SH 準拠
205 POSIX.1-2001.
206 .\"O .SH NOTES
207 .SH 注意
208 .\"O There are two different versions of
209 .\"O .BR basename ()
210 .\"O - the POSIX version described above, and the GNU version, which one gets
211 .\"O after
212 .BR basename ()
213 には 2種類の異なるバージョンがある。
214 一つはすでに説明した POSIX バージョンであり、
215 もう一つは GNU バージョンである。
216 GNU バージョンを使用するには以下のようにする。
217 .br
218 .nf
219
220 .\"O .BR "    #define _GNU_SOURCE" "         /* See feature_test_macros(7) */"
221 .BR "    #define _GNU_SOURCE" "         /* feature_test_macros(7) 参照 */"
222 .br
223 .B "    #include <string.h>"
224
225 .fi
226 .\"O The GNU version never modifies its argument, and returns the
227 .\"O empty string when
228 .\"O .I path
229 .\"O has a trailing slash, and in particular also when it is "/".
230 .\"O There is no GNU version of
231 .\"O .BR dirname ().
232 GNU バージョンは引き数を変更することはなく、
233 .I path
234 の末尾が '/'の場合は空の文字列を返す。
235 特に
236 .I path
237 が "/" の場合も空文字列を返す。
238 .BR dirname ()
239 には GNU バージョンはない。
240 .LP
241 .\"O With glibc, one gets the POSIX version of
242 .\"O .BR basename ()
243 .\"O when
244 .\"O .I <libgen.h>
245 .\"O is included, and the GNU version otherwise.
246 glibc では、
247 .I <libgen.h>
248 をインクルードすると POSIX バージョンの
249 .BR basename ()
250 が使用され、それ以外の場合は GNU バージョンとなる。
251 .\"O .SH BUGS
252 .SH バグ
253 .\"O In the glibc implementation of the POSIX versions of these functions
254 .\"O they modify their argument, and segfault when called with a static string
255 .\"O like "/usr/".
256 glibc の POSIX バージョンの実装では、引き数の内容が変更され、
257 引き数に ("/usr/" などの) 定数文字列を指定されると
258 セグメンテーションフォールトを起こす。
259 .\"O Before glibc 2.2.1, the glibc version of
260 .\"O .BR dirname ()
261 .\"O did not correctly handle pathnames with trailing \(aq/\(aq characters,
262 .\"O and generated a segfault if given a NULL argument.
263 バージョン 2.2.1 以前の glibc では、 glibc の
264 .BR dirname ()
265 は末尾が \(aq/\(aq 文字になっているパス名を正しく扱えず、
266 引き数が NULL だとセグメンテーションフォールトを起こした。
267 .\"O .SH EXAMPLE
268 .SH 例
269 .in +4n
270 .nf
271 char *dirc, *basec, *bname, *dname;
272 char *path = "/etc/passwd";
273
274 dirc = strdup(path);
275 basec = strdup(path);
276 dname = dirname(dirc);
277 bname = basename(basec);
278 printf("dirname=%s, basename=%s\\n", dname, bname);
279 .fi
280 .in
281 .\"O .SH "SEE ALSO"
282 .SH 関連項目
283 .BR basename (1),
284 .BR dirname (1)