.\" Copyright (c) 2000 by Michael Kerrisk (mtk.manpages@gmail.com) .\" .\" 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. .\" Created, 14 Dec 2000 by Michael Kerrisk .\" .\" 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" .\"O .SH NAME .\"O basename, dirname \- parse pathname components .SH 名前 basename, dirname \- パス名を解析して各部分を取り出す .\"O .SH SYNOPSIS .SH 書式 .nf .B #include .sp .BI "char *dirname(char *" path ); .BI "char *basename(char *" path ); .fi .\"O .SH DESCRIPTION .SH 説明 .\"O Warning: there are two different functions .\"O .BR basename () .\"O - see below. 警告: .BR basename () には異なるバージョンが 2つ存在する。下記の「注意」の節を参照のこと。 .LP .\"O The functions .\"O .BR dirname () .\"O and .\"O .BR basename () .\"O break a null-terminated pathname string into directory .\"O and filename components. .\"O In the usual case, .\"O .BR dirname () .\"O returns the string up to, but not including, the final \(aq/\(aq, and .\"O .BR basename () .\"O returns the component following the final \(aq/\(aq. .\"O Trailing \(aq/\(aq characters are not counted as part of the pathname. .BR dirname () と .BR basename () は、NULL で終端されたパス名の文字列を、 ディレクトリ部分・ファイル名部分に分割する。 通常は、 .BR dirname () は最後の \(aq/\(aq までの部分 (最後の \(aq/\(aq は含まない) を返し、 .BR basename () は最後の \(aq/\(aq 以降の部分を返す。 文字列の末尾についた \(aq/\(aq 文字は、パス名の一部とはみなされない。 .PP .\"O If .\"O .I path .\"O does not contain a slash, .\"O .BR dirname () .\"O returns the string "." while .\"O .BR basename () .\"O returns a copy of .\"O .IR path . .\"O If .\"O .I path .\"O is the string "/", then both .\"O .BR dirname () .\"O and .\"O .BR basename () .\"O return the string "/". .\"O If .\"O .I path .\"O is a NULL pointer or points to an empty string, then both .\"O .BR dirname () .\"O and .\"O .BR basename () .\"O return the string ".". .I path に '/' 文字がない場合は、 .BR dirname () は文字列 "." を返し、 .BR basename () は .I path と同じ内容を返す。 .I path が文字列 "/" に等しい場合は、 .BR dirname () も .BR basename () も文字列 "/" を返す。 .I path が NULL ポインタだったり、空の文字列を指していた場合は、 .BR dirname () も .BR basename () も文字列 "." を返す。 .PP .\"O Concatenating the string returned by .\"O .BR dirname (), .\"O a "/", and the string returned by .\"O .BR basename () .\"O yields a complete pathname. .BR dirname () の返した文字列、 "/"、 .BR basename () の返した文字列、 を順に結合すると、完全なパス名が得られる。 .PP .\"O Both .\"O .BR dirname () .\"O and .\"O .BR basename () .\"O may modify the contents of .\"O .IR path , .\"O so it may be desirable to pass a copy when calling one of .\"O these functions. .BR dirname () と .BR basename () は、いずれも .I path の内容を変更することがある。 したがって、これらの関数を呼び出す際には コピーを渡すのが望ましい。 .PP .\"O These functions may return pointers to statically allocated memory .\"O which may be overwritten by subsequent calls. .\"O Alternatively, they may return a pointer to some part of .\"O .IR path , .\"O so that the string referred to by .\"O .I path .\"O should not be modified or freed until the pointer returned by .\"O the function is no longer required. これらの関数は、静的に割り当てられたメモリへのポインタを返すことがあり、 これらの領域は後の関数呼び出しで上書きされるかもしれない。 また、これらの関数は .I path の一部分を指すポインタを返すこともある。そのため、 .I path で参照される文字列は、関数が返すポインタが不要になるまでは 変更したり free したりすべきではない。 .PP .\"O The following list of examples (taken from SUSv2) .\"O shows the strings returned by .\"O .BR dirname () .\"O and .\"O .BR basename () .\"O for different paths: 以下の一連の例 (SUSv2 から引用) は、 いろいろな path に対して .BR dirname () と .BR basename () が返す文字列を表したものである。 .sp .nf .B "path dirname basename" "/usr/lib" "/usr" "lib" "/usr/" "/" "usr" "usr" "." "usr" "/" "/" "/" "." "." "." ".." "." ".." .fi .\"O .SH "RETURN VALUE" .SH 返り値 .\"O Both .\"O .BR dirname () .\"O and .\"O .BR basename () .\"O return pointers to null-terminated strings. .\"O (Do not pass these pointers to .\"O .BR free (3).) .BR dirname () と .BR basename () は、いずれも NULL で終端された文字列へのポインタを返す。 (これらのポインタを .BR free (3) に渡さないこと。) .\"O .SH "CONFORMING TO" .SH 準拠 POSIX.1-2001. .\"O .SH NOTES .SH 注意 .\"O There are two different versions of .\"O .BR basename () .\"O - the POSIX version described above, and the GNU version, which one gets .\"O after .BR basename () には 2種類の異なるバージョンがある。 一つはすでに説明した POSIX バージョンであり、 もう一つは GNU バージョンである。 GNU バージョンを使用するには以下のようにする。 .br .nf .\"O .BR " #define _GNU_SOURCE" " /* See feature_test_macros(7) */" .BR " #define _GNU_SOURCE" " /* feature_test_macros(7) 参照 */" .br .B " #include " .fi .\"O The GNU version never modifies its argument, and returns the .\"O empty string when .\"O .I path .\"O has a trailing slash, and in particular also when it is "/". .\"O There is no GNU version of .\"O .BR dirname (). GNU バージョンは引き数を変更することはなく、 .I path の末尾が '/'の場合は空の文字列を返す。 特に .I path が "/" の場合も空文字列を返す。 .BR dirname () には GNU バージョンはない。 .LP .\"O With glibc, one gets the POSIX version of .\"O .BR basename () .\"O when .\"O .I .\"O is included, and the GNU version otherwise. glibc では、 .I をインクルードすると POSIX バージョンの .BR basename () が使用され、それ以外の場合は GNU バージョンとなる。 .\"O .SH BUGS .SH バグ .\"O In the glibc implementation of the POSIX versions of these functions .\"O they modify their argument, and segfault when called with a static string .\"O like "/usr/". glibc の POSIX バージョンの実装では、引き数の内容が変更され、 引き数に ("/usr/" などの) 定数文字列を指定されると セグメンテーションフォールトを起こす。 .\"O Before glibc 2.2.1, the glibc version of .\"O .BR dirname () .\"O did not correctly handle pathnames with trailing \(aq/\(aq characters, .\"O and generated a segfault if given a NULL argument. バージョン 2.2.1 以前の glibc では、 glibc の .BR dirname () は末尾が \(aq/\(aq 文字になっているパス名を正しく扱えず、 引き数が NULL だとセグメンテーションフォールトを起こした。 .\"O .SH EXAMPLE .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\\n", dname, bname); .fi .in .\"O .SH "SEE ALSO" .SH 関連項目 .BR basename (1), .BR dirname (1)