Penguin
Annotated edit history of basename(3) version 2, including all changes. View license author blame.
Rev Author # Line
1 PerryLorier 1 DIRNAME
2 !!!DIRNAME
3 !!NAME
4
5
6 dirname, basename - Parse pathname components
7 !!SYNOPSIS
8
9
10 __#include__ <string.h>
11 __char *dirname(char__''*path'' __);__
12 __char *basename(char__ ''*path''__);__
13
14 !!DESCRIPTION
15
16 __dirname__ and __basename__ break a null-terminated
17 pathname string into directory and filename components. In
18 the usual case, __dirname__ returns the string up to, but
19 not including, the final '/', and __basename__ returns
20 the component following the final '/'. Trailing '/'
21 characters are not counted as part of the
22 pathname.
23
24 If ''path'' does not contain a slash, __dirname__
25 returns the string __basename__
26 returns a copy of ''path''. If ''path'' is the string
27 ''dirname__ and __basename__ return the string __path'' is a NULL
28 pointer or points to an empty string, then both
29 __dirname__ and __basename__ return the string
30
31
32
33 Concatenating the string returned by __dirname__, a
34 __basename__
35 yields a complete pathname.
36
37
38 Both __dirname__ and __basename__ may modify the
39 contents of ''path'', so if you need to preserve the
40 pathname string, copies should be passed to these functions.
41 Furthermore, __dirname__ and __basename__ may return
42 pointers to statically allocated memory which may
43 overwritten by subsequent calls.
44
45
46 The following list of examples (taken from SUSv2) shows the
47 strings returned by __dirname__ and __basename__ for
48 different paths:
49
50 |^path|^dirname|^basename
51 |"/usr/lib"|"/usr"|"lib"
52 |"/usr/"|"/"|"usr"
53 |"usr"|"."|"usr"
54 |"/"|"/"|"/"
55 |"."|"."|"."
56 |".."|"."|".."
57
58 !!EXAMPLE
59
60 char *dirc, *basec, *bname, *dname;
61 char *path = "/etc/passwd";
62
63 dirc = strdup(path);
64 basec = strdup(path);
65 dname = dirname(dirc);
66 bname = basename(basec);
67 printf("dirname=%s, basename=%s\n", dname, bname);
68 free(dirc);
69 free(basec);
70
71
72 !!RETURN VALUE
73
74
75 Both __dirname__ and __basename__ return pointers to null-terminated strings.
76 !!BUGS
77
78
79 In versions of glibc up to and including 2.2.1,
80 __dirname__ does not correctly handle pathnames with
81 trailing '/' characters, and generates a segmentation
82 violation if given a NULL argument.
83 !!CONFORMING TO
84
85 [SUSv2]
86 !!SEE ALSO
87
88
89 dirname(1), basename(1),
90 ----
2 !PerryLorier 91 !PerryLorier:
1 PerryLorier 92 I after reading this man page you're probably as paranoid about this function as I am. Here is an efficient and reliable implementation:
93 char *mybasename(char *x)
94 {
95 if (!x)
96 return x;
97 return strrchr(x) ? strrchr(x)+1 : x;
98 }
99 if this is listed as being static then the compiler should do a nice job of optimising this.
This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.