1BASENAME(3) Linux Programmer's Manual BASENAME(3)
2
3
4
6 basename, dirname - parse pathname components
7
9 #include <libgen.h>
10
11 char *dirname(char *path);
12
13 char *basename(char *path);
14
16 Warning: there are two different functions basename() - see below.
17
18 The functions dirname() and basename() break a null-terminated pathname
19 string into directory and filename components. In the usual case,
20 dirname() returns the string up to, but not including, the final '/',
21 and basename() returns the component following the final '/'. Trailing
22 '/' characters are not counted as part of the pathname.
23
24 If path does not contain a slash, dirname() returns the string "."
25 while basename() returns a copy of path. If path is the string "/",
26 then both dirname() and basename() return the string "/". If path is a
27 NULL pointer or points to an empty string, then both dirname() and
28 basename() return the string ".".
29
30 Concatenating the string returned by dirname(), a "/", and the string
31 returned by basename() yields a complete pathname.
32
33 Both dirname() and basename() may modify the contents of path, so it
34 may be desirable to pass a copy when calling one of these functions.
35
36 These functions may return pointers to statically allocated memory
37 which may be overwritten by subsequent calls. Alternatively, they may
38 return a pointer to some part of path, so that the string referred to
39 by path should not be modified or freed until the pointer returned by
40 the function is no longer required.
41
42 The following list of examples (taken from SUSv2) shows the strings
43 returned by dirname() and basename() for different paths:
44
45 path dirname basename
46 "/usr/lib" "/usr" "lib"
47 "/usr/" "/" "usr"
48 "usr" "." "usr"
49 "/" "/" "/"
50 "." "." "."
51 ".." "." ".."
52
54 Both dirname() and basename() return pointers to null-terminated
55 strings. (Do not pass these pointers to free(3).)
56
58 POSIX.1-2001.
59
61 There are two different versions of basename() - the POSIX version
62 described above, and the GNU version, which one gets after
63
64 #define _GNU_SOURCE
65 #include <string.h>
66
67 The GNU version never modifies its argument, and returns the empty
68 string when path has a trailing slash, and in particular also when it
69 is "/". There is no GNU version of dirname().
70
71 With glibc, one gets the POSIX version of basename() when <libgen.h> is
72 included, and the GNU version otherwise.
73
75 In the glibc implementation of the POSIX versions of these functions
76 they modify their argument, and segfault when called with a static
77 string like "/usr/". Before glibc 2.2.1, the glibc version of
78 dirname() did not correctly handle pathnames with trailing '/' characā
79 ters, and generated a segfault if given a NULL argument.
80
82 char *dirc, *basec, *bname, *dname;
83 char *path = "/etc/passwd";
84
85 dirc = strdup(path);
86 basec = strdup(path);
87 dname = dirname(dirc);
88 bname = basename(basec);
89 printf("dirname=%s, basename=%s\n", dname, bname);
90
92 basename(1), dirname(1), feature_test_macros(7)
93
95 This page is part of release 3.22 of the Linux man-pages project. A
96 description of the project, and information about reporting bugs, can
97 be found at http://www.kernel.org/doc/man-pages/.
98
99
100
101GNU 2009-03-30 BASENAME(3)