.\" (c) 2000 by Michael Kerrisk (michael.kerrisk@gmx.net) .\" .\" 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. .\" .\" 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 .\" .TH DIRNAME 3 2000-12-14 "GNU" "Linux Programmer's Manual" .SH NAME dirname, basename \- 分析路徑成員 .SH "總覽 (SYNOPSIS)" .nf .B #include .sp .BI "char *dirname(char " "*path" ");" .BI "char *basename(char " "*path" ");" .fi .SH "描述 (DESCRIPTION)" .B dirname 和 .B basename 把 以 null 結尾 的 路徑名 分解為 目錄 和 檔名. 一般情況下, .B dirname 返回 路徑名 的 前面部分, 直到 (但不包括) 最後一個 '/', 而 .B basename 則 返回 最後一個 '/' 後面 的 內容. 如果 路徑名 以 '/' 結尾, 該 '/' 被認為 不是 路徑名 的 一部分. .PP 如果 路徑名 .I path 不包含 斜槓 '/', .B dirname 返回 字符串 ".", 而 .B basename 返回 .IR path 的 副本. 如果 路徑名 .I path 是 "/", 則 .B dirname 和 .B basename 均 返回 "/". 如果 路徑名 .I path 是 NULL 指針 或 指向 空串, 則 .B dirname 和 .B basename 均 返回 ".". .PP 把 .BR dirname 返回的 字符串, "/", 和 .B basename 返回的 字符串 連接 起來, 能夠 產生 一個 完整 的 路徑名. .PP 無論 .B dirname 還是 .B basename 都 有可能 更改 .IR path 的 內容, 因此 如果 需要 保護 原有 路徑名, 應該 傳送 副本 作為 參數. 此外, .B dirname 和 .B basename 返回的 指針 可能 指向 一塊 靜態分配 的 記憶體, 會被 下次 調用 覆蓋. .PP 下面 的 例子 (摘自 SUSv2) 展示了 對於 不同的 路徑名, .B dirname 和 .B basename 返回 的 字符串: .sp .nf .B path dirname basename "/usr/lib" "/usr" "lib" "/usr/" "/" "usr" "usr" "." "usr" "/" "/" "/" "." "." "." ".." "." ".." .fi .SH "示例 (EXAMPLE)" .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); free(dirc); free(basec); .fi .SH "返回值 (RETURN VALUE)" .B dirname 和 .B basename 均 返回 以 null 結尾的 字符串 的 指針. .SH "BUGS" 在 glibc 的 各個 版本 中, 直到 (並包括) 2.2.1, .B dirname 無法 正確 處理 以 '/' 字符 結尾 的 路徑名. 如果 參數 是 NULL 指針, 他 還會 產生 段沖突 (segmentation violation). .SH "遵循 (CONFORMING TO)" SUSv2 .SH "另見 (SEE ALSO)" .BR dirname (1), .BR basename (1) .SH "[中文版維護人]" .B 徐明 .SH "[中文版最新更新]" .BR 2003/05/13 .SH "《中國Linux論壇man手冊頁翻譯計劃》" .BI http://cmpp.linuxforum.net