1BASENAME(3P)               POSIX Programmer's Manual              BASENAME(3P)
2
3
4

PROLOG

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

NAME

12       basename — return the last component of a pathname
13

SYNOPSIS

15       #include <libgen.h>
16
17       char *basename(char *path);
18

DESCRIPTION

20       The basename() function shall take the pathname pointed to by path  and
21       return  a  pointer to the final component of the pathname, deleting any
22       trailing '/' characters.
23
24       If the string pointed to by path consists entirely of the  '/'  charac‐
25       ter,  basename()  shall  return  a  pointer  to the string "/".  If the
26       string pointed to by path is exactly "//", it is implementation-defined
27       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 internal storage. The returned pointer might be
34       invalidated or the storage might be overwritten by a subsequent call to
35       basename().   The  returned  pointer  might  also be invalidated if the
36       calling thread is terminated.
37
38       The basename() function need not be thread-safe.
39

RETURN VALUE

41       The basename() function shall return a pointer to the  final  component
42       of path.
43

ERRORS

45       No errors are defined.
46
47       The following sections are informative.
48

EXAMPLES

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
55           #include <libgen.h>
56           ...
57           char name[] = "/usr/lib";
58           char *base;
59
60           base = basename(name);
61           ...
62
63   Sample Input and Output Strings for the basename() and dirname()  Functions
64       and the basename and dirname Utilities
65       .TS
66┌──────────────┬───────────┬────────────┬──────────────┬─────────────────┬───────────┐
67basename()  │           │            │   basename   Output      Output   
68and dirname() String   String   and dirname  Written by    Written by 
69Functions pathReturned byReturned by Utilities   basename     dirname   
70Argument   basename() dirname()  string OperandUtility     Utility   
71├──────────────┼───────────┼────────────┼──────────────┼─────────────────┼───────────┤
72│"usr"         │"usr"      │"."         │usr           │usr              │.          │
73├──────────────┼───────────┼────────────┼──────────────┼─────────────────┼───────────┤
74│"usr/"        │"usr"      │"."         │usr/          │usr              │.          │
75├──────────────┼───────────┼────────────┼──────────────┼─────────────────┼───────────┤
76│""            │"."        │"."         │""            │. or empty string│.          │
77├──────────────┼───────────┼────────────┼──────────────┼─────────────────┼───────────┤
78│"/"           │"/"        │"/"         │/             │/                │/          │
79├──────────────┼───────────┼────────────┼──────────────┼─────────────────┼───────────┤
80│"//"          │"/" or "//"│"/" or "//" │//            │/ or //          │/ or //    │
81├──────────────┼───────────┼────────────┼──────────────┼─────────────────┼───────────┤
82│"///"         │"/"        │"/"         │///           │/                │/          │
83├──────────────┼───────────┼────────────┼──────────────┼─────────────────┼───────────┤
84│"/usr/"       │"usr"      │"/"         │/usr/         │usr              │/          │
85├──────────────┼───────────┼────────────┼──────────────┼─────────────────┼───────────┤
86│"/usr/lib"    │"lib"      │"/usr"      │/usr/lib      │lib              │/usr       │
87├──────────────┼───────────┼────────────┼──────────────┼─────────────────┼───────────┤
88│"//usr//lib//"│"lib"      │"//usr"     │//usr//lib//  │lib              │//usr      │
89├──────────────┼───────────┼────────────┼──────────────┼─────────────────┼───────────┤
90│"/home//dwc// │"test"     │"/home//dwc"│/home//dwc//  │test             │/home//dwc │
91│test"         │           │            │test          │                 │           │
92└───────.─S─H──"─A─P─P┴L─I─C─A─T─I─O─N──U─S─A┴G─E─"──N─o─n─e─.────┴──────────────┴─────────────────┴───────────┘
93

RATIONALE

95       None.
96

FUTURE DIRECTIONS

98       None.
99

SEE ALSO

101       dirname()
102
103       The Base Definitions volume of POSIX.1‐2017, <libgen.h>
104
105       The Shell and Utilities volume of POSIX.1‐2017, basename
106
108       Portions  of  this text are reprinted and reproduced in electronic form
109       from IEEE Std 1003.1-2017, Standard for Information Technology --  Por‐
110       table  Operating System Interface (POSIX), The Open Group Base Specifi‐
111       cations Issue 7, 2018 Edition, Copyright (C) 2018 by the  Institute  of
112       Electrical  and  Electronics Engineers, Inc and The Open Group.  In the
113       event of any discrepancy between this version and the original IEEE and
114       The  Open Group Standard, the original IEEE and The Open Group Standard
115       is the referee document. The original Standard can be  obtained  online
116       at http://www.opengroup.org/unix/online.html .
117
118       Any  typographical  or  formatting  errors that appear in this page are
119       most likely to have been introduced during the conversion of the source
120       files  to  man page format. To report such errors, see https://www.ker
121       nel.org/doc/man-pages/reporting_bugs.html .
122
123
124
125IEEE/The Open Group                  2017                         BASENAME(3P)
Impressum