1BASENAME(3P) POSIX Programmer's Manual BASENAME(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 basename - return the last component of a pathname
13
15 #include <libgen.h>
16
17 char *basename(char *path);
18
19
21 The basename() function shall take the pathname pointed to by path and
22 return a pointer to the final component of the pathname, deleting any
23 trailing '/' characters.
24
25 If the string consists entirely of the '/' character, basename() shall
26 return a pointer to the string "/" . If the string is exactly "//", it
27 is implementation-defined whether '/' or "//" is returned.
28
29 If path is a null pointer or points to an empty string, basename()
30 shall return a pointer to the string "." .
31
32 The basename() function may modify the string pointed to by path, and
33 may return a pointer to static storage that may then be overwritten by
34 a subsequent call to basename().
35
36 The basename() function need not be reentrant. A function that is not
37 required to be reentrant is not required to be thread-safe.
38
40 The basename() function shall return a pointer to the final component
41 of path.
42
44 No errors are defined.
45
46 The following sections are informative.
47
49 Using basename()
50 The following program fragment returns a pointer to the value lib,
51 which is the base name of /usr/lib.
52
53
54 #include <libgen.h>
55 ...
56 char *name = "/usr/lib";
57 char *base;
58
59
60 base = basename(name);
61 ...
62
63 Sample Input and Output Strings for basename()
64 In the following table, the input string is the value pointed to by
65 path, and the output string is the return value of the basename() func‐
66 tion.
67
68 Input String Output String
69 "/usr/lib" "lib"
70 "/usr/" "usr"
71 "/" "/"
72 "///" "/"
73 "//usr//lib//" "lib"
74
76 None.
77
79 None.
80
82 None.
83
85 dirname(), the Base Definitions volume of IEEE Std 1003.1-2001, <lib‐
86 gen.h>, the Shell and Utilities volume of IEEE Std 1003.1-2001, base‐
87 name
88
90 Portions of this text are reprinted and reproduced in electronic form
91 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
92 -- Portable Operating System Interface (POSIX), The Open Group Base
93 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
94 Electrical and Electronics Engineers, Inc and The Open Group. In the
95 event of any discrepancy between this version and the original IEEE and
96 The Open Group Standard, the original IEEE and The Open Group Standard
97 is the referee document. The original Standard can be obtained online
98 at http://www.opengroup.org/unix/online.html .
99
100
101
102IEEE/The Open Group 2003 BASENAME(3P)