dirname 中文man頁(yè)面
NAME
dirname, basename - 分析路徑成員
總覽 (SYNOPSIS)
#include <libgen.h> char *dirname(char *path); char *basename(char *path);
描述 (DESCRIPTION)
dirname 和 basename 把 以 null 結(jié)尾 的 路徑名 分解為 目錄 和 文件名. 一般情況下, dirname 返回 路徑名 的 前面部分, 直到 (但不包括) 最后一個(gè) '/', 而 basename 則 返回 最后一個(gè) '/' 后面 的 內(nèi)容. 如果 路徑名 以 '/' 結(jié)尾, 該 '/' 被認(rèn)為 不是 路徑名 的 一部分.
如果 路徑名 path 不包含 斜杠 '/', dirname 返回 字符串 ".", 而 basename 返回 path 的 副本. 如果 路徑名 path 是 "/", 則 dirname 和 basename 均 返回 "/". 如果 路徑名 path 是 NULL 指針 或 指向 空串, 則 dirname 和 basename 均 返回 ".".
把 dirname 返回的 字符串, "/", 和 basename 返回的 字符串 連接 起來(lái), 能夠 產(chǎn)生 一個(gè) 完整 的 路徑名.
無(wú)論 dirname 還是 basename 都 有可能 更改 path 的 內(nèi)容, 因此 如果 需要 保護(hù) 原有 路徑名, 應(yīng)該 傳送 副本 作為 參數(shù). 此外, dirname 和 basename 返回的 指針 可能 指向 一塊 靜態(tài)分配 的 內(nèi)存, 會(huì)被 下次 調(diào)用 覆蓋.
下面 的 例子 (摘自 SUSv2) 展示了 對(duì)于 不同的 路徑名, dirname 和 basename 返回 的 字符串:
path dirname basename "/usr/lib" "/usr" "lib" "/usr/" "/" "usr" "usr" "." "usr" "/" "/" "/" "." "." "." ".." "." ".."
示例 (EXAMPLE)
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);
返回值 (RETURN VALUE)
dirname 和 basename 均 返回 以 null 結(jié)尾的 字符串 的 指針.
BUGS
在 glibc 的 各個(gè) 版本 中, 直到 (并包括) 2.2.1, dirname 無(wú)法 正確 處理 以 '/' 字符 結(jié)尾 的 路徑名. 如果 參數(shù) 是 NULL 指針, 他 還會(huì) 產(chǎn)生 段沖突 (segmentation violation).
遵循 (CONFORMING TO)
SUSv2
另見(jiàn) (SEE ALSO)
dirname(1), basename(1)
#p#
NAME
dirname - strip non-directory suffix from file name
SYNOPSIS
dirname NAME
dirname OPTION
DESCRIPTION
Print NAME with its trailing /component removed; if NAME contains no /'s, output `.' (meaning the current directory).
- --help
- display this help and exit
- --version
- output version information and exit
AUTHOR
Written by David MacKenzie and Jim Meyering.
REPORTING BUGS
Report bugs to <bug-coreutils@gnu.org>.
COPYRIGHT
Copyright © 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
SEE ALSO
The full documentation for dirname is maintained as a Texinfo manual. If the info and dirname programs are properly installed at your site, the command
- info coreutils dirname
should give you access to the complete manual.