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
11
13 basename — return the last component of a pathname
14
16 #include <libgen.h>
17
18 char *basename(char *path);
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 pointed to by path consists entirely of the '/' charac‐
26 ter, basename() shall return a pointer to the string "/". If the
27 string pointed to by path is exactly "//", it is implementation-defined
28 whether '/' or "//" is returned.
29
30 If path is a null pointer or points to an empty string, basename()
31 shall return a pointer to the string ".".
32
33 The basename() function may modify the string pointed to by path, and
34 may return a pointer to internal storage. The returned pointer might be
35 invalidated or the storage might be overwritten by a subsequent call to
36 basename().
37
38 The basename() function need not be thread-safe.
39
41 The basename() function shall return a pointer to the final component
42 of path.
43
45 No errors are defined.
46
47 The following sections are informative.
48
50 Using basename()
51 The following program fragment returns a pointer to the value lib,
52 which is the base name of /usr/lib.
53
54 #include <libgen.h>
55 ...
56 char *name = "/usr/lib";
57 char *base;
58
59 base = basename(name);
60 ...
61
62 Sample Input and Output Strings for basename()
63 In the following table, the input string is the value pointed to by
64 path, and the output string is the return value of the basename() func‐
65 tion.
66
67 ┌───────────────┬───────────────┐
68 │ Input String │ Output String │
69 ├───────────────┼───────────────┤
70 │"/usr/lib" │ "lib" │
71 │"/usr/" │ "usr" │
72 │"/" │ "/" │
73 │"///" │ "/" │
74 │"//usr//lib//" │ "lib" │
75 └───────────────┴───────────────┘
77 None.
78
80 None.
81
83 None.
84
86 dirname()
87
88 The Base Definitions volume of POSIX.1‐2008, <libgen.h>
89
90 The Shell and Utilities volume of POSIX.1‐2008, basename
91
93 Portions of this text are reprinted and reproduced in electronic form
94 from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
95 -- Portable Operating System Interface (POSIX), The Open Group Base
96 Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
97 cal and Electronics Engineers, Inc and The Open Group. (This is
98 POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the
99 event of any discrepancy between this version and the original IEEE and
100 The Open Group Standard, the original IEEE and The Open Group Standard
101 is the referee document. The original Standard can be obtained online
102 at http://www.unix.org/online.html .
103
104 Any typographical or formatting errors that appear in this page are
105 most likely to have been introduced during the conversion of the source
106 files to man page format. To report such errors, see https://www.ker‐
107 nel.org/doc/man-pages/reporting_bugs.html .
108
109
110
111IEEE/The Open Group 2013 BASENAME(3P)