1 .\" (c) 2000 by Michael Kerrisk (michael.kerrisk@gmx.net)
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.
17 .\" Formatted or processed versions of this manual, if unaccompanied by
18 .\" the source, must acknowledge the copyright and authors of this work.
20 .\" Created, 14 Dec 2000 by Michael Kerrisk
22 .TH DIRNAME 3 2000-12-14 "GNU" "Linux Programmer's Manual"
24 dirname, basename \- Parse pathname components
27 .B #include <libgen.h>
29 .BI "char *dirname(char *" path );
31 .BI "char *basename(char *" path );
38 break a null-terminated pathname string into directory
39 and filename components.
42 returns the string up to, but not including, the final '/', and
44 returns the component following the final '/'.
45 Trailing '/' characters are not counted as part of the pathname.
49 does not contain a slash,
51 returns the string "." while
57 is the string "/", then both
61 return the string "/".
64 is a NULL pointer or points to an empty string, then both
68 return the string ".".
70 Concatenating the string returned by
72 a "/", and the string returned by
74 yields a complete pathname.
80 may modify the contents of
82 so if you need to preserve the pathname string,
83 copies should be passed to these functions. Furthermore,
87 may return pointers to statically allocated memory
88 which may be overwritten by subsequent calls.
90 The following list of examples (taken from SUSv2)
91 shows the strings returned by
100 "/usr/lib" "/usr" "lib"
109 char *dirc, *basec, *bname, *dname;
110 char *path = "/etc/passwd";
113 basec = strdup(path);
114 dname = dirname(dirc);
115 bname = basename(basec);
116 printf("dirname=%s, basename=%s\\n", dname, bname);
125 return pointers to null-terminated strings.
127 In versions of glibc up to and including 2.2.1,
129 does not correctly handle pathnames with trailing '/' characters,
130 and generates a segmentation violation if given a NULL argument.