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 For an explanation of the terms used in this section, see
59 attributes(7).
60
61 ┌──────────────────────┬───────────────┬─────────┐
62 │Interface │ Attribute │ Value │
63 ├──────────────────────┼───────────────┼─────────┤
64 │basename(), dirname() │ Thread safety │ MT-Safe │
65 └──────────────────────┴───────────────┴─────────┘
67 POSIX.1-2001, POSIX.1-2008.
68
70 There are two different versions of basename() - the POSIX version
71 described above, and the GNU version, which one gets after
72
73 #define _GNU_SOURCE /* See feature_test_macros(7) */
74 #include <string.h>
75
76 The GNU version never modifies its argument, and returns the empty
77 string when path has a trailing slash, and in particular also when it
78 is "/". There is no GNU version of dirname().
79
80 With glibc, one gets the POSIX version of basename() when <libgen.h> is
81 included, and the GNU version otherwise.
82
84 In the glibc implementation, the POSIX versions of these functions mod‐
85 ify the path argument, and segfault when called with a static string
86 such as "/usr/".
87
88 Before glibc 2.2.1, the glibc version of dirname() did not correctly
89 handle pathnames with trailing '/' characters, and generated a segfault
90 if given a NULL argument.
91
93 The following code snippet demonstrates the use of basename() and
94 dirname():
95 char *dirc, *basec, *bname, *dname;
96 char *path = "/etc/passwd";
97
98 dirc = strdup(path);
99 basec = strdup(path);
100 dname = dirname(dirc);
101 bname = basename(basec);
102 printf("dirname=%s, basename=%s\n", dname, bname);
103
105 basename(1), dirname(1)
106
108 This page is part of release 4.16 of the Linux man-pages project. A
109 description of the project, information about reporting bugs, and the
110 latest version of this page, can be found at
111 https://www.kernel.org/doc/man-pages/.
112
113
114
115GNU 2017-09-15 BASENAME(3)