basename-lgpl.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* Extract the last component (base name) of a file name.
  2. Copyright (C) 1998, 2001, 2003-2006, 2009-2022 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. #include <stddef.h>
  17. #ifndef DOUBLE_SLASH_IS_DISTINCT_ROOT
  18. # define DOUBLE_SLASH_IS_DISTINCT_ROOT 0
  19. #endif
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /* Return the address of the last file name component of FILENAME.
  24. If FILENAME has some trailing slash(es), they are considered to be
  25. part of the last component.
  26. If FILENAME has no relative file name components because it is a file
  27. system root, return the empty string.
  28. Examples:
  29. FILENAME RESULT
  30. "foo.c" "foo.c"
  31. "foo/bar.c" "bar.c"
  32. "/foo/bar.c" "bar.c"
  33. "foo/bar/" "bar/"
  34. "foo/bar//" "bar//"
  35. "/" ""
  36. "//" ""
  37. "" ""
  38. The return value is a tail of the given FILENAME; do NOT free() it! */
  39. /* This function was traditionally called 'basename', but we avoid this
  40. function name because
  41. * Various platforms have different functions in their libc.
  42. In particular, the glibc basename(), defined in <string.h>, does
  43. not consider trailing slashes to be part of the component:
  44. FILENAME RESULT
  45. "foo/bar/" ""
  46. "foo/bar//" ""
  47. * The 'basename' command eliminates trailing slashes and for a root
  48. produces a non-empty result:
  49. FILENAME RESULT
  50. "foo/bar/" "bar"
  51. "foo/bar//" "bar"
  52. "/" "/"
  53. "//" "/"
  54. */
  55. extern char *last_component (char const *filename) _GL_ATTRIBUTE_PURE;
  56. /* Return the length of the basename FILENAME.
  57. Typically FILENAME is the value returned by base_name or last_component.
  58. Act like strlen (FILENAME), except omit all trailing slashes. */
  59. extern size_t base_len (char const *filename) _GL_ATTRIBUTE_PURE;
  60. #ifdef __cplusplus
  61. } /* extern "C" */
  62. #endif
  63. #endif /* _BASENAME_LGPL_H */