OSDN Git Service

(split) LDP: Restore and add Copyrights for draft pages
[linuxjm/LDP_man-pages.git] / draft / 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 .\"*******************************************************************
28 .\"
29 .\" This file was generated with po4a. Translate the source file.
30 .\"
31 .\"*******************************************************************
32 .\"
33 .\" Japanese Version Copyright (c) 2001 NAKANO Takeo all rights reserved.
34 .\" Translated 2001-03-31, NAKANO Takeo <nakano@apm.seikei.ac.jp>
35 .\" Updated 2005-02-26, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
36 .\" Updated 2007-05-01, Akihiro MOTOKI, LDP v2.46
37 .\"
38 .TH BASENAME 3 2009\-03\-30 GNU "Linux Programmer's Manual"
39 .SH 名前
40 basename, dirname \- パス名を解析して各部分を取り出す
41 .SH 書式
42 .nf
43 \fB#include <libgen.h>\fP
44 .sp
45 \fBchar *dirname(char *\fP\fIpath\fP\fB);\fP
46
47 \fBchar *basename(char *\fP\fIpath\fP\fB);\fP
48 .fi
49 .SH 説明
50 警告: \fBbasename\fP()  には異なるバージョンが 2つ存在する。下記の「注意」の節を参照のこと。
51 .LP
52 \fBdirname\fP()  と \fBbasename\fP()  は、NULL で終端されたパス名の文字列を、 ディレクトリ部分・ファイル名部分に分割する。
53 通常は、 \fBdirname\fP()  は最後の \(aq/\(aq までの部分 (最後の \(aq/\(aq は含まない) を返し、
54 \fBbasename\fP()  は最後の \(aq/\(aq 以降の部分を返す。 文字列の末尾についた \(aq/\(aq
55 文字は、パス名の一部とはみなされない。
56 .PP
57 \fIpath\fP に '/' 文字がない場合は、 \fBdirname\fP()  は文字列 "." を返し、 \fBbasename\fP()  は \fIpath\fP
58 と同じ内容を返す。 \fIpath\fP が文字列 "/" に等しい場合は、 \fBdirname\fP()  も \fBbasename\fP()  も文字列 "/"
59 を返す。 \fIpath\fP が NULL ポインタだったり、空の文字列を指していた場合は、 \fBdirname\fP()  も \fBbasename\fP()
60 も文字列 "." を返す。
61 .PP
62 \fBdirname\fP()  の返した文字列、 "/"、 \fBbasename\fP()  の返した文字列、 を順に結合すると、完全なパス名が得られる。
63 .PP
64 \fBdirname\fP()  と \fBbasename\fP()  は、いずれも \fIpath\fP の内容を変更することがある。
65 したがって、これらの関数を呼び出す際には コピーを渡すのが望ましい。
66 .PP
67 これらの関数は、静的に割り当てられたメモリへのポインタを返すことがあり、 これらの領域は後の関数呼び出しで上書きされるかもしれない。
68 また、これらの関数は \fIpath\fP の一部分を指すポインタを返すこともある。そのため、 \fIpath\fP
69 で参照される文字列は、関数が返すポインタが不要になるまでは 変更したり free したりすべきではない。
70 .PP
71 以下の一連の例 (SUSv2 から引用) は、 いろいろな path に対して \fBdirname\fP()  と \fBbasename\fP()
72 が返す文字列を表したものである。
73 .sp
74 .TS
75 lb lb lb
76 l l l l.
77 path            dirname basename
78 /usr/lib        /usr    lib
79 /usr/           /       usr
80 usr             .       usr
81 /               /       /
82 \.              .       .
83 \..             .       ..
84 .TE
85 .SH 返り値
86 \fBdirname\fP()  と \fBbasename\fP()  は、いずれも NULL で終端された文字列へのポインタを返す。 (これらのポインタを
87 \fBfree\fP(3)  に渡さないこと。)
88 .SH 準拠
89 POSIX.1\-2001.
90 .SH 注意
91 \fBbasename\fP()  には 2種類の異なるバージョンがある。 一つはすでに説明した POSIX バージョンであり、 もう一つは GNU
92 バージョンである。 GNU バージョンを使用するには以下のようにする。
93 .br
94 .nf
95
96 \fB    #define _GNU_SOURCE\fP         /* feature_test_macros(7) 参照 */
97 .br
98 \fB#include <string.h>\fP
99
100 .fi
101 GNU バージョンは引き数を変更することはなく、 \fIpath\fP の末尾が '/'の場合は空の文字列を返す。 特に \fIpath\fP が "/"
102 の場合も空文字列を返す。 \fBdirname\fP()  には GNU バージョンはない。
103 .LP
104 glibc では、 \fI<libgen.h>\fP をインクルードすると POSIX バージョンの \fBbasename\fP()
105 が使用され、それ以外の場合は GNU バージョンとなる。
106 .SH バグ
107 glibc の POSIX バージョンの実装では、引き数の内容が変更され、 引き数に ("/usr/" などの) 定数文字列を指定されると
108 セグメンテーションフォールトを起こす。 バージョン 2.2.1 以前の glibc では、 glibc の \fBdirname\fP()  は末尾が
109 \(aq/\(aq 文字になっているパス名を正しく扱えず、 引き数が NULL だとセグメンテーションフォールトを起こした。
110 .SH 例
111 .in +4n
112 .nf
113 char *dirc, *basec, *bname, *dname;
114 char *path = "/etc/passwd";
115
116 dirc = strdup(path);
117 basec = strdup(path);
118 dname = dirname(dirc);
119 bname = basename(basec);
120 printf("dirname=%s, basename=%s\en", dname, bname);
121 .fi
122 .in
123 .SH 関連項目
124 \fBbasename\fP(1), \fBdirname\fP(1)
125 .SH この文書について
126 この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.53 の一部
127 である。プロジェクトの説明とバグ報告に関する情報は
128 http://www.kernel.org/doc/man\-pages/ に書かれている。