lbasename.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* Libiberty basename. Like basename, but is not overridden by the
  2. system C library.
  3. Copyright (C) 2001-2022 Free Software Foundation, Inc.
  4. This file is part of the libiberty library.
  5. Libiberty is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Library General Public
  7. License as published by the Free Software Foundation; either
  8. version 2 of the License, or (at your option) any later version.
  9. Libiberty is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Library General Public License for more details.
  13. You should have received a copy of the GNU Library General Public
  14. License along with libiberty; see the file COPYING.LIB. If
  15. not, write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
  16. Boston, MA 02110-1301, USA. */
  17. /*
  18. @deftypefn Replacement {const char*} lbasename (const char *@var{name})
  19. Given a pointer to a string containing a typical pathname
  20. (@samp{/usr/src/cmd/ls/ls.c} for example), returns a pointer to the
  21. last component of the pathname (@samp{ls.c} in this case). The
  22. returned pointer is guaranteed to lie within the original
  23. string. This latter fact is not true of many vendor C
  24. libraries, which return special strings or modify the passed
  25. strings for particular input.
  26. In particular, the empty string returns the same empty string,
  27. and a path ending in @code{/} returns the empty string after it.
  28. @end deftypefn
  29. */
  30. #ifdef HAVE_CONFIG_H
  31. #include "config.h"
  32. #endif
  33. #include "ansidecl.h"
  34. #include "libiberty.h"
  35. #include "safe-ctype.h"
  36. #include "filenames.h"
  37. const char *
  38. unix_lbasename (const char *name)
  39. {
  40. const char *base;
  41. for (base = name; *name; name++)
  42. if (IS_UNIX_DIR_SEPARATOR (*name))
  43. base = name + 1;
  44. return base;
  45. }
  46. const char *
  47. dos_lbasename (const char *name)
  48. {
  49. const char *base;
  50. /* Skip over a possible disk name. */
  51. if (ISALPHA (name[0]) && name[1] == ':')
  52. name += 2;
  53. for (base = name; *name; name++)
  54. if (IS_DOS_DIR_SEPARATOR (*name))
  55. base = name + 1;
  56. return base;
  57. }
  58. const char *
  59. lbasename (const char *name)
  60. {
  61. #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
  62. return dos_lbasename (name);
  63. #else
  64. return unix_lbasename (name);
  65. #endif
  66. }