.\" Copyright (c) 2000 by Michael Kerrisk (mtk.manpages@gmail.com) .\" .\" %%%LICENSE_START(VERBATIM) .\" Permission is granted to make and distribute verbatim copies of this .\" manual provided the copyright notice and this permission notice are .\" preserved on all copies. .\" .\" Permission is granted to copy and distribute modified versions of this .\" manual under the conditions for verbatim copying, provided that the .\" entire resulting derived work is distributed under the terms of a .\" permission notice identical to this one. .\" .\" Since the Linux kernel and libraries are constantly changing, this .\" manual page may be incorrect or out-of-date. The author(s) assume no .\" responsibility for errors or omissions, or for damages resulting from .\" the use of the information contained herein. The author(s) may not .\" have taken the same level of care in the production of this manual, .\" which is licensed free of charge, as they might when working .\" professionally. .\" .\" Formatted or processed versions of this manual, if unaccompanied by .\" the source, must acknowledge the copyright and authors of this work. .\" %%%LICENSE_END .\" .\" Created, 14 Dec 2000 by Michael Kerrisk .\" .\"******************************************************************* .\" .\" This file was generated with po4a. Translate the source file. .\" .\"******************************************************************* .\" .\" Japanese Version Copyright (c) 2001 NAKANO Takeo all rights reserved. .\" Translated 2001-03-31, NAKANO Takeo .\" Updated 2005-02-26, Akihiro MOTOKI .\" Updated 2007-05-01, Akihiro MOTOKI, LDP v2.46 .\" .TH BASENAME 3 2009\-03\-30 GNU "Linux Programmer's Manual" .SH 名前 basename, dirname \- パス名を解析して各部分を取り出す .SH 書式 .nf \fB#include \fP .sp \fBchar *dirname(char *\fP\fIpath\fP\fB);\fP \fBchar *basename(char *\fP\fIpath\fP\fB);\fP .fi .SH 説明 警告: \fBbasename\fP() には異なるバージョンが 2つ存在する。下記の「注意」の節を参照のこと。 .LP \fBdirname\fP() と \fBbasename\fP() は、ヌルで終端されたパス名の文字列を、 ディレクトリ部分・ファイル名部分に分割する。 通常は、 \fBdirname\fP() は最後の \(aq/\(aq までの部分 (最後の \(aq/\(aq は含まない) を返し、 \fBbasename\fP() は最後の \(aq/\(aq 以降の部分を返す。 文字列の末尾についた \(aq/\(aq 文字は、パス名の一部とはみなされない。 .PP \fIpath\fP に '/' 文字がない場合は、 \fBdirname\fP() は文字列 "." を返し、 \fBbasename\fP() は \fIpath\fP と同じ内容を返す。 \fIpath\fP が文字列 "/" に等しい場合は、 \fBdirname\fP() も \fBbasename\fP() も文字列 "/" を返す。 \fIpath\fP が ヌルポインタだったり、空の文字列を指していた場合は、 \fBdirname\fP() も \fBbasename\fP() も文字列 "." を返す。 .PP \fBdirname\fP() の返した文字列、 "/"、 \fBbasename\fP() の返した文字列、 を順に結合すると、完全なパス名が得られる。 .PP \fBdirname\fP() と \fBbasename\fP() は、いずれも \fIpath\fP の内容を変更することがある。 したがって、これらの関数を呼び出す際には コピーを渡すのが望ましい。 .PP これらの関数は、静的に割り当てられたメモリへのポインタを返すことがあり、 これらの領域は後の関数呼び出しで上書きされるかもしれない。 また、これらの関数は \fIpath\fP の一部分を指すポインタを返すこともある。そのため、 \fIpath\fP で参照される文字列は、関数が返すポインタが不要になるまでは 変更したり free したりすべきではない。 .PP 以下の一連の例 (SUSv2 から引用) は、 いろいろな path に対して \fBdirname\fP() と \fBbasename\fP() が返す文字列を表したものである。 .sp .TS lb lb lb l l l l. path dirname basename /usr/lib /usr lib /usr/ / usr usr . usr / / / \&. . . \&.. . .. .TE .SH 返り値 \fBdirname\fP() と \fBbasename\fP() は、いずれもヌルで終端された文字列へのポインタを返す。 (これらのポインタを \fBfree\fP(3) に渡さないこと。) .SH 準拠 POSIX.1\-2001. .SH 注意 \fBbasename\fP() には 2種類の異なるバージョンがある。 一つはすでに説明した POSIX バージョンであり、 もう一つは GNU バージョンである。 GNU バージョンを使用するには以下のようにする。 .br .nf \fB #define _GNU_SOURCE\fP /* feature_test_macros(7) 参照 */ .br \fB#include \fP .fi GNU バージョンは引き数を変更することはなく、 \fIpath\fP の末尾が '/'の場合は空の文字列を返す。 特に \fIpath\fP が "/" の場合も空文字列を返す。 \fBdirname\fP() には GNU バージョンはない。 .LP glibc では、 \fI\fP をインクルードすると POSIX バージョンの \fBbasename\fP() が使用され、それ以外の場合は GNU バージョンとなる。 .SH バグ glibc の POSIX バージョンの実装では、引き数の内容が変更され、 引き数に ("/usr/" などの) 定数文字列を指定されると セグメンテーションフォールトを起こす。 バージョン 2.2.1 以前の glibc では、 glibc の \fBdirname\fP() は末尾が \(aq/\(aq 文字になっているパス名を正しく扱えず、 引き数が NULL だとセグメンテーションフォールトを起こした。 .SH 例 .in +4n .nf char *dirc, *basec, *bname, *dname; char *path = "/etc/passwd"; dirc = strdup(path); basec = strdup(path); dname = dirname(dirc); bname = basename(basec); printf("dirname=%s, basename=%s\en", dname, bname); .fi .in .SH 関連項目 \fBbasename\fP(1), \fBdirname\fP(1) .SH この文書について この man ページは Linux \fIman\-pages\fP プロジェクトのリリース 3.65 の一部 である。プロジェクトの説明とバグ報告に関する情報は http://www.kernel.org/doc/man\-pages/ に書かれている。