1DIRNAME(3) Linux Programmer's Manual DIRNAME(3)
2
3
4
6 dirname, basename - 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
42 returned 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 char *dirc, *basec, *bname, *dname;
54 char *path = "/etc/passwd";
55
56 dirc = strdup(path);
57 basec = strdup(path);
58 dname = dirname(dirc);
59 bname = basename(basec);
60 printf("dirname=%s, basename=%s\n", dname, bname);
61
63 Both dirname() and basename() return pointers to null-terminated
64 strings.
65
67 There are two different versions of basename() - the POSIX version
68 described above, and the GNU version, which one gets after
69
70 #define _GNU_SOURCE
71 #include <string.h>
72
73 The GNU version never modifies its argument, and returns the empty
74 string when path has a trailing slash, and in particular also when it
75 is "/". There is no GNU version of dirname().
76
77 With glibc, one gets the POSIX version of basename() when <libgen.h> is
78 included, and the GNU version otherwise.
79
81 In the glibc implementation of the POSIX versions of these functions
82 they modify their argument, and segfault when called with a static
83 string like "/usr/". Before glibc 2.2.1, the glibc version of
84 dirname() did not correctly handle pathnames with trailing '/' characā
85 ters, and generated a segfault if given a NULL argument.
86
88 POSIX.1-2001
89
91 basename(1), dirname(1), feature_test_macros(7)
92
93
94
95GNU 2000-12-14 DIRNAME(3)