1 .\" Copyright (c) 2000 by Michael Kerrisk (mtk.manpages@gmail.com)
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" Created, 14 Dec 2000 by Michael Kerrisk
25 .\" Japanese Version Copyright (c) 2001 NAKANO Takeo all rights reserved.
26 .\" Translated 2001-03-31, NAKANO Takeo <nakano@apm.seikei.ac.jp>
27 .\" Updated 2005-02-26, Akihiro MOTOKI <amotoki@dd.iij4u.or.jp>
28 .\" Updated 2007-05-01, Akihiro MOTOKI, LDP v2.46
30 .TH BASENAME 3 2009-03-30 "GNU" "Linux Programmer's Manual"
32 .\"O basename, dirname \- parse pathname components
34 basename, dirname \- ¥Ñ¥¹Ì¾¤ò²òÀϤ·¤Æ³ÆÉôʬ¤ò¼è¤ê½Ð¤¹
38 .B #include <libgen.h>
40 .BI "char *dirname(char *" path );
42 .BI "char *basename(char *" path );
46 .\"O Warning: there are two different functions
51 ¤Ë¤Ï°Û¤Ê¤ë¥Ð¡¼¥¸¥ç¥ó¤¬ 2¤Ä¸ºß¤¹¤ë¡£²¼µ¤Î¡ÖÃí°Õ¡×¤ÎÀá¤ò»²¾È¤Î¤³¤È¡£
57 .\"O break a null-terminated pathname string into directory
58 .\"O and filename components.
59 .\"O In the usual case,
61 .\"O returns the string up to, but not including, the final \(aq/\(aq, and
63 .\"O returns the component following the final \(aq/\(aq.
64 .\"O Trailing \(aq/\(aq characters are not counted as part of the pathname.
68 ¤Ï¡¢NULL ¤Ç½ªÃ¼¤µ¤ì¤¿¥Ñ¥¹Ì¾¤Îʸ»úÎó¤ò¡¢
69 ¥Ç¥£¥ì¥¯¥È¥êÉôʬ¡¦¥Õ¥¡¥¤¥ë̾Éôʬ¤Ëʬ³ä¤¹¤ë¡£
72 ¤ÏºÇ¸å¤Î \(aq/\(aq ¤Þ¤Ç¤ÎÉôʬ (ºÇ¸å¤Î \(aq/\(aq ¤Ï´Þ¤Þ¤Ê¤¤) ¤òÊÖ¤·¡¢
74 ¤ÏºÇ¸å¤Î \(aq/\(aq °Ê¹ß¤ÎÉôʬ¤òÊÖ¤¹¡£
75 ʸ»úÎó¤ÎËöÈø¤Ë¤Ä¤¤¤¿ \(aq/\(aq ʸ»ú¤Ï¡¢¥Ñ¥¹Ì¾¤Î°ìÉô¤È¤Ï¤ß¤Ê¤µ¤ì¤Ê¤¤¡£
79 .\"O does not contain a slash,
81 .\"O returns the string "." while
83 .\"O returns a copy of
87 .\"O is the string "/", then both
91 .\"O return the string "/".
94 .\"O is a NULL pointer or points to an empty string, then both
98 .\"O return the string ".".
100 ¤Ë '/' ʸ»ú¤¬¤Ê¤¤¾ì¹ç¤Ï¡¢
102 ¤Ïʸ»úÎó "." ¤òÊÖ¤·¡¢
108 ¤¬Ê¸»úÎó "/" ¤ËÅù¤·¤¤¾ì¹ç¤Ï¡¢
112 ¤âʸ»úÎó "/" ¤òÊÖ¤¹¡£
114 ¤¬ NULL ¥Ý¥¤¥ó¥¿¤À¤Ã¤¿¤ê¡¢¶õ¤Îʸ»úÎó¤ò»Ø¤·¤Æ¤¤¤¿¾ì¹ç¤Ï¡¢
118 ¤âʸ»úÎó "." ¤òÊÖ¤¹¡£
120 .\"O Concatenating the string returned by
122 .\"O a "/", and the string returned by
124 .\"O yields a complete pathname.
126 ¤ÎÊÖ¤·¤¿Ê¸»úÎó¡¢ "/"¡¢
129 ¤ò½ç¤Ë·ë¹ç¤¹¤ë¤È¡¢´°Á´¤Ê¥Ñ¥¹Ì¾¤¬ÆÀ¤é¤ì¤ë¡£
135 .\"O may modify the contents of
137 .\"O so it may be desirable to pass a copy when calling one of
138 .\"O these functions.
144 ¤ÎÆâÍƤòÊѹ¹¤¹¤ë¤³¤È¤¬¤¢¤ë¡£
145 ¤·¤¿¤¬¤Ã¤Æ¡¢¤³¤ì¤é¤Î´Ø¿ô¤ò¸Æ¤Ó½Ð¤¹ºÝ¤Ë¤Ï
146 ¥³¥Ô¡¼¤òÅϤ¹¤Î¤¬Ë¾¤Þ¤·¤¤¡£
148 .\"O These functions may return pointers to statically allocated memory
149 .\"O which may be overwritten by subsequent calls.
150 .\"O Alternatively, they may return a pointer to some part of
152 .\"O so that the string referred to by
154 .\"O should not be modified or freed until the pointer returned by
155 .\"O the function is no longer required.
156 ¤³¤ì¤é¤Î´Ø¿ô¤Ï¡¢ÀÅŪ¤Ë³ä¤êÅö¤Æ¤é¤ì¤¿¥á¥â¥ê¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¤³¤È¤¬¤¢¤ê¡¢
157 ¤³¤ì¤é¤ÎÎΰè¤Ï¸å¤Î´Ø¿ô¸Æ¤Ó½Ð¤·¤Ç¾å½ñ¤¤µ¤ì¤ë¤«¤â¤·¤ì¤Ê¤¤¡£
160 ¤Î°ìÉôʬ¤ò»Ø¤¹¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¤³¤È¤â¤¢¤ë¡£¤½¤Î¤¿¤á¡¢
162 ¤Ç»²¾È¤µ¤ì¤ëʸ»úÎó¤Ï¡¢´Ø¿ô¤¬ÊÖ¤¹¥Ý¥¤¥ó¥¿¤¬ÉÔÍפˤʤë¤Þ¤Ç¤Ï
163 Êѹ¹¤·¤¿¤ê free ¤·¤¿¤ê¤¹¤Ù¤¤Ç¤Ï¤Ê¤¤¡£
165 .\"O The following list of examples (taken from SUSv2)
166 .\"O shows the strings returned by
170 .\"O for different paths:
171 °Ê²¼¤Î°ìÏ¢¤ÎÎã (SUSv2 ¤«¤é°úÍÑ) ¤Ï¡¢
172 ¤¤¤í¤¤¤í¤Ê path ¤ËÂФ·¤Æ
176 ¤¬ÊÖ¤¹Ê¸»úÎó¤òɽ¤·¤¿¤â¤Î¤Ç¤¢¤ë¡£
179 .B "path dirname basename"
180 "/usr/lib" "/usr" "lib"
187 .\"O .SH "RETURN VALUE"
193 .\"O return pointers to null-terminated strings.
194 .\"O (Do not pass these pointers to
199 ¤Ï¡¢¤¤¤º¤ì¤â NULL ¤Ç½ªÃ¼¤µ¤ì¤¿Ê¸»úÎó¤Ø¤Î¥Ý¥¤¥ó¥¿¤òÊÖ¤¹¡£
203 .\"O .SH "CONFORMING TO"
208 .\"O There are two different versions of
210 .\"O - the POSIX version described above, and the GNU version, which one gets
213 ¤Ë¤Ï 2¼ïÎà¤Î°Û¤Ê¤ë¥Ð¡¼¥¸¥ç¥ó¤¬¤¢¤ë¡£
214 °ì¤Ä¤Ï¤¹¤Ç¤ËÀâÌÀ¤·¤¿ POSIX ¥Ð¡¼¥¸¥ç¥ó¤Ç¤¢¤ê¡¢
215 ¤â¤¦°ì¤Ä¤Ï GNU ¥Ð¡¼¥¸¥ç¥ó¤Ç¤¢¤ë¡£
216 GNU ¥Ð¡¼¥¸¥ç¥ó¤ò»ÈÍѤ¹¤ë¤Ë¤Ï°Ê²¼¤Î¤è¤¦¤Ë¤¹¤ë¡£
220 .B " #define _GNU_SOURCE"
222 .B " #include <string.h>"
225 .\"O The GNU version never modifies its argument, and returns the
226 .\"O empty string when
228 .\"O has a trailing slash, and in particular also when it is "/".
229 .\"O There is no GNU version of
231 GNU ¥Ð¡¼¥¸¥ç¥ó¤Ï°ú¤¿ô¤òÊѹ¹¤¹¤ë¤³¤È¤Ï¤Ê¤¯¡¢
233 ¤ÎËöÈø¤¬ '/'¤Î¾ì¹ç¤Ï¶õ¤Îʸ»úÎó¤òÊÖ¤¹¡£
236 ¤¬ "/" ¤Î¾ì¹ç¤â¶õʸ»úÎó¤òÊÖ¤¹¡£
238 ¤Ë¤Ï GNU ¥Ð¡¼¥¸¥ç¥ó¤Ï¤Ê¤¤¡£
240 .\"O With glibc, one gets the POSIX version of
244 .\"O is included, and the GNU version otherwise.
247 ¤ò¥¤¥ó¥¯¥ë¡¼¥É¤¹¤ë¤È POSIX ¥Ð¡¼¥¸¥ç¥ó¤Î
249 ¤¬»ÈÍѤµ¤ì¡¢¤½¤ì°Ê³°¤Î¾ì¹ç¤Ï GNU ¥Ð¡¼¥¸¥ç¥ó¤È¤Ê¤ë¡£
252 .\"O In the glibc implementation of the POSIX versions of these functions
253 .\"O they modify their argument, and segfault when called with a static string
255 glibc ¤Î POSIX ¥Ð¡¼¥¸¥ç¥ó¤Î¼ÂÁõ¤Ç¤Ï¡¢°ú¤¿ô¤ÎÆâÍƤ¬Êѹ¹¤µ¤ì¡¢
256 °ú¤¿ô¤Ë ("/usr/" ¤Ê¤É¤Î) Äê¿ôʸ»úÎó¤ò»ØÄꤵ¤ì¤ë¤È
257 ¥»¥°¥á¥ó¥Æ¡¼¥·¥ç¥ó¥Õ¥©¡¼¥ë¥È¤òµ¯¤³¤¹¡£
258 .\"O Before glibc 2.2.1, the glibc version of
260 .\"O did not correctly handle pathnames with trailing \(aq/\(aq characters,
261 .\"O and generated a segfault if given a NULL argument.
262 ¥Ð¡¼¥¸¥ç¥ó 2.2.1 °ÊÁ°¤Î glibc ¤Ç¤Ï¡¢ glibc ¤Î
264 ¤ÏËöÈø¤¬ \(aq/\(aq ʸ»ú¤Ë¤Ê¤Ã¤Æ¤¤¤ë¥Ñ¥¹Ì¾¤òÀµ¤·¤¯°·¤¨¤º¡¢
265 °ú¤¿ô¤¬ NULL ¤À¤È¥»¥°¥á¥ó¥Æ¡¼¥·¥ç¥ó¥Õ¥©¡¼¥ë¥È¤òµ¯¤³¤·¤¿¡£
270 char *dirc, *basec, *bname, *dname;
271 char *path = "/etc/passwd";
274 basec = strdup(path);
275 dname = dirname(dirc);
276 bname = basename(basec);
277 printf("dirname=%s, basename=%s\\n", dname, bname);
284 .BR feature_test_macros (7)