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 char *basename(char *path);
13
15 Warning: there are two different functions basename(); see below.
16
17 The functions dirname() and basename() break a null-terminated pathname
18 string into directory and filename components. In the usual case,
19 dirname() returns the string up to, but not including, the final '/',
20 and basename() returns the component following the final '/'. Trailing
21 '/' characters are not counted as part of the pathname.
22
23 If path does not contain a slash, dirname() returns the string "."
24 while basename() returns a copy of path. If path is the string "/",
25 then both dirname() and basename() return the string "/". If path is a
26 null pointer or points to an empty string, then both dirname() and
27 basename() return the string ".".
28
29 Concatenating the string returned by dirname(), a "/", and the string
30 returned by basename() yields a complete pathname.
31
32 Both dirname() and basename() may modify the contents of path, so it
33 may be desirable to pass a copy when calling one of these functions.
34
35 These functions may return pointers to statically allocated memory
36 which may be overwritten by subsequent calls. Alternatively, they may
37 return a pointer to some part of path, so that the string referred to
38 by path should not be modified or freed until the pointer returned by
39 the function is no longer required.
40
41 The following list of examples (taken from SUSv2) shows the strings re‐
42 turned by dirname() and basename() for different paths:
43
44 path dirname basename
45 /usr/lib /usr lib
46 /usr/ / usr
47 usr . usr
48 / / /
49 . . .
50 .. . ..
51
53 Both dirname() and basename() return pointers to null-terminated
54 strings. (Do not pass these pointers to free(3).)
55
57 For an explanation of the terms used in this section, see at‐
58 tributes(7).
59
60 ┌────────────────────────────────────────────┬───────────────┬─────────┐
61 │Interface │ Attribute │ Value │
62 ├────────────────────────────────────────────┼───────────────┼─────────┤
63 │basename(), dirname() │ Thread safety │ MT-Safe │
64 └────────────────────────────────────────────┴───────────────┴─────────┘
65
67 POSIX.1-2001, POSIX.1-2008.
68
70 There are two different versions of basename() - the POSIX version de‐
71 scribed 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 5.13 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 2021-03-22 BASENAME(3)