basename-lgpl.h 2.8 KB

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