1 .\" Copyright (c) 2000 by Michael Kerrisk (mtk.manpages@gmail.com)
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.
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.
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
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" Created, 14 Dec 2000 by Michael Kerrisk
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
30 .TH BASENAME 3 2009-03-30 "GNU" "Linux Programmer's Manual"
32 .\"O basename, dirname \- parse pathname components
34 basename, dirname \- パス名を解析して各部分を取り出す
38 .B #include <libgen.h>
40 .BI "char *dirname(char *" path );
42 .BI "char *basename(char *" path );
46 .\"O Warning: there are two different functions
51 には異なるバージョンが 2つ存在する。下記の「注意」の節を参照のこと。
57 .\"O break a null-terminated pathname string into directory
58 .\"O and filename components.
59 .\"O In the usual case,
61 .\"O returns the string up to, but not including, the final \(aq/\(aq, and
63 .\"O returns the component following the final \(aq/\(aq.
64 .\"O Trailing \(aq/\(aq characters are not counted as part of the pathname.
68 は、NULL で終端されたパス名の文字列を、
69 ディレクトリ部分・ファイル名部分に分割する。
72 は最後の \(aq/\(aq までの部分 (最後の \(aq/\(aq は含まない) を返し、
74 は最後の \(aq/\(aq 以降の部分を返す。
75 文字列の末尾についた \(aq/\(aq 文字は、パス名の一部とはみなされない。
79 .\"O does not contain a slash,
81 .\"O returns the string "." while
83 .\"O returns a copy of
87 .\"O is the string "/", then both
91 .\"O return the string "/".
94 .\"O is a NULL pointer or points to an empty string, then both
98 .\"O return the string ".".
114 が NULL ポインタだったり、空の文字列を指していた場合は、
120 .\"O Concatenating the string returned by
122 .\"O a "/", and the string returned by
124 .\"O yields a complete pathname.
129 を順に結合すると、完全なパス名が得られる。
135 .\"O may modify the contents of
137 .\"O so it may be desirable to pass a copy when calling one of
138 .\"O these functions.
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
152 .\"O so that the string referred to by
154 .\"O should not be modified or freed until the pointer returned by
155 .\"O the function is no longer required.
156 これらの関数は、静的に割り当てられたメモリへのポインタを返すことがあり、
157 これらの領域は後の関数呼び出しで上書きされるかもしれない。
160 の一部分を指すポインタを返すこともある。そのため、
162 で参照される文字列は、関数が返すポインタが不要になるまでは
163 変更したり free したりすべきではない。
165 .\"O The following list of examples (taken from SUSv2)
166 .\"O shows the strings returned by
170 .\"O for different paths:
171 以下の一連の例 (SUSv2 から引用) は、
179 .B "path dirname basename"
180 "/usr/lib" "/usr" "lib"
187 .\"O .SH "RETURN VALUE"
193 .\"O return pointers to null-terminated strings.
194 .\"O (Do not pass these pointers to
199 は、いずれも NULL で終端された文字列へのポインタを返す。
203 .\"O .SH "CONFORMING TO"
208 .\"O There are two different versions of
210 .\"O - the POSIX version described above, and the GNU version, which one gets
214 一つはすでに説明した POSIX バージョンであり、
216 GNU バージョンを使用するには以下のようにする。
220 .\"O .BR " #define _GNU_SOURCE" " /* See feature_test_macros(7) */"
221 .BR " #define _GNU_SOURCE" " /* feature_test_macros(7) 参照 */"
223 .B " #include <string.h>"
226 .\"O The GNU version never modifies its argument, and returns the
227 .\"O empty string when
229 .\"O has a trailing slash, and in particular also when it is "/".
230 .\"O There is no GNU version of
232 GNU バージョンは引き数を変更することはなく、
234 の末尾が '/'の場合は空の文字列を返す。
241 .\"O With glibc, one gets the POSIX version of
245 .\"O is included, and the GNU version otherwise.
248 をインクルードすると POSIX バージョンの
250 が使用され、それ以外の場合は GNU バージョンとなる。
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
256 glibc の POSIX バージョンの実装では、引き数の内容が変更され、
257 引き数に ("/usr/" などの) 定数文字列を指定されると
259 .\"O Before glibc 2.2.1, the glibc version of
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 の
265 は末尾が \(aq/\(aq 文字になっているパス名を正しく扱えず、
266 引き数が NULL だとセグメンテーションフォールトを起こした。
271 char *dirc, *basec, *bname, *dname;
272 char *path = "/etc/passwd";
275 basec = strdup(path);
276 dname = dirname(dirc);
277 bname = basename(basec);
278 printf("dirname=%s, basename=%s\\n", dname, bname);