1basename(3) Library Functions Manual basename(3)
2
3
4
6 basename, dirname - parse pathname components
7
9 Standard C library (libc, -lc)
10
12 #include <libgen.h>
13
14 char *dirname(char *path);
15 char *basename(char *path);
16
18 Warning: there are two different functions basename(); see below.
19
20 The functions dirname() and basename() break a null-terminated pathname
21 string into directory and filename components. In the usual case,
22 dirname() returns the string up to, but not including, the final '/',
23 and basename() returns the component following the final '/'. Trailing
24 '/' characters are not counted as part of the pathname.
25
26 If path does not contain a slash, dirname() returns the string "."
27 while basename() returns a copy of path. If path is the string "/",
28 then both dirname() and basename() return the string "/". If path is a
29 null pointer or points to an empty string, then both dirname() and
30 basename() return the string ".".
31
32 Concatenating the string returned by dirname(), a "/", and the string
33 returned by basename() yields a complete pathname.
34
35 Both dirname() and basename() may modify the contents of path, so it
36 may be desirable to pass a copy when calling one of these functions.
37
38 These functions may return pointers to statically allocated memory
39 which may be overwritten by subsequent calls. Alternatively, they may
40 return a pointer to some part of path, so that the string referred to
41 by path should not be modified or freed until the pointer returned by
42 the function is no longer required.
43
44 The following list of examples (taken from SUSv2) shows the strings re‐
45 turned by dirname() and basename() for different paths:
46
47 path dirname basename
48 /usr/lib /usr lib
49 /usr/ / usr
50 usr . usr
51 / / /
52 . . .
53 .. . ..
54
56 Both dirname() and basename() return pointers to null-terminated
57 strings. (Do not pass these pointers to free(3).)
58
60 For an explanation of the terms used in this section, see at‐
61 tributes(7).
62
63 ┌────────────────────────────────────────────┬───────────────┬─────────┐
64 │Interface │ Attribute │ Value │
65 ├────────────────────────────────────────────┼───────────────┼─────────┤
66 │basename(), dirname() │ Thread safety │ MT-Safe │
67 └────────────────────────────────────────────┴───────────────┴─────────┘
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 POSIX.1-2008.
85
87 POSIX.1-2001.
88
90 In the glibc implementation, the POSIX versions of these functions mod‐
91 ify the path argument, and segfault when called with a static string
92 such as "/usr/".
93
94 Before glibc 2.2.1, the glibc version of dirname() did not correctly
95 handle pathnames with trailing '/' characters, and generated a segfault
96 if given a NULL argument.
97
99 The following code snippet demonstrates the use of basename() and
100 dirname():
101 char *dirc, *basec, *bname, *dname;
102 char *path = "/etc/passwd";
103
104 dirc = strdup(path);
105 basec = strdup(path);
106 dname = dirname(dirc);
107 bname = basename(basec);
108 printf("dirname=%s, basename=%s\n", dname, bname);
109
111 basename(1), dirname(1)
112
113
114
115Linux man-pages 6.04 2023-03-30 basename(3)