| 1 |
/* Extract the last component (base name) of a file name. |
| 2 |
|
| 3 |
Copyright (C) 1998, 2001, 2003-2006, 2009-2024 Free Software Foundation, |
| 4 |
Inc. |
| 5 |
|
| 6 |
This file is free software: you can redistribute it and/or modify |
| 7 |
it under the terms of the GNU Lesser General Public License as |
| 8 |
published by the Free Software Foundation; either version 2.1 of the |
| 9 |
License, or (at your option) any later version. |
| 10 |
|
| 11 |
This file is distributed in the hope that it will be useful, |
| 12 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
GNU Lesser General Public License for more details. |
| 15 |
|
| 16 |
You should have received a copy of the GNU Lesser General Public License |
| 17 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 18 |
|
| 19 |
#ifndef _BASENAME_LGPL_H |
| 20 |
#define _BASENAME_LGPL_H |
| 21 |
|
| 22 |
/* This file uses _GL_ATTRIBUTE_PURE. */ |
| 23 |
#if !_GL_CONFIG_H_INCLUDED |
| 24 |
#error "Please include config.h first." |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <stddef.h> |
| 28 |
|
| 29 |
#ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT |
| 30 |
# define DOUBLE_SLASH_IS_DISTINCT_ROOT 0 |
| 31 |
#endif |
| 32 |
|
| 33 |
#ifdef __cplusplus |
| 34 |
extern "C" { |
| 35 |
#endif |
| 36 |
|
| 37 |
|
| 38 |
/* Return the address of the last file name component of FILENAME. |
| 39 |
If FILENAME has some trailing slash(es), they are considered to be |
| 40 |
part of the last component. |
| 41 |
If FILENAME has no relative file name components because it is a file |
| 42 |
system root, return the empty string. |
| 43 |
Examples: |
| 44 |
FILENAME RESULT |
| 45 |
"foo.c" "foo.c" |
| 46 |
"foo/bar.c" "bar.c" |
| 47 |
"/foo/bar.c" "bar.c" |
| 48 |
"foo/bar/" "bar/" |
| 49 |
"foo/bar//" "bar//" |
| 50 |
"/" "" |
| 51 |
"//" "" |
| 52 |
"" "" |
| 53 |
The return value is a tail of the given FILENAME; do NOT free() it! */ |
| 54 |
|
| 55 |
/* This function was traditionally called 'basename', but we avoid this |
| 56 |
function name because |
| 57 |
* Various platforms have different functions in their libc. |
| 58 |
In particular, the glibc basename(), defined in <string.h>, does |
| 59 |
not consider trailing slashes to be part of the component: |
| 60 |
FILENAME RESULT |
| 61 |
"foo/bar/" "" |
| 62 |
"foo/bar//" "" |
| 63 |
* The 'basename' command eliminates trailing slashes and for a root |
| 64 |
produces a non-empty result: |
| 65 |
FILENAME RESULT |
| 66 |
"foo/bar/" "bar" |
| 67 |
"foo/bar//" "bar" |
| 68 |
"/" "/" |
| 69 |
"//" "/" |
| 70 |
*/ |
| 71 |
extern char *last_component (char const *filename) _GL_ATTRIBUTE_PURE; |
| 72 |
|
| 73 |
/* Return the length of the basename FILENAME. |
| 74 |
Typically FILENAME is the value returned by base_name or last_component. |
| 75 |
Act like strlen (FILENAME), except omit all trailing slashes. */ |
| 76 |
extern size_t base_len (char const *filename) _GL_ATTRIBUTE_PURE; |
| 77 |
|
| 78 |
|
| 79 |
#ifdef __cplusplus |
| 80 |
} /* extern "C" */ |
| 81 |
#endif |
| 82 |
|
| 83 |
#endif /* _BASENAME_LGPL_H */ |