basename.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /* Return the basename of a pathname.
  2. This file is in the public domain. */
  3. /*
  4. @deftypefn Supplemental char* basename (const char *@var{name})
  5. Returns a pointer to the last component of pathname @var{name}.
  6. Behavior is undefined if the pathname ends in a directory separator.
  7. @end deftypefn
  8. */
  9. #ifdef HAVE_CONFIG_H
  10. #include "config.h"
  11. #endif
  12. #include "ansidecl.h"
  13. #include "libiberty.h"
  14. #include "safe-ctype.h"
  15. #ifndef DIR_SEPARATOR
  16. #define DIR_SEPARATOR '/'
  17. #endif
  18. #if defined (_WIN32) || defined (__MSDOS__) || defined (__DJGPP__) || \
  19. defined (__OS2__)
  20. #define HAVE_DOS_BASED_FILE_SYSTEM
  21. #ifndef DIR_SEPARATOR_2
  22. #define DIR_SEPARATOR_2 '\\'
  23. #endif
  24. #endif
  25. /* Define IS_DIR_SEPARATOR. */
  26. #ifndef DIR_SEPARATOR_2
  27. # define IS_DIR_SEPARATOR(ch) ((ch) == DIR_SEPARATOR)
  28. #else /* DIR_SEPARATOR_2 */
  29. # define IS_DIR_SEPARATOR(ch) \
  30. (((ch) == DIR_SEPARATOR) || ((ch) == DIR_SEPARATOR_2))
  31. #endif /* DIR_SEPARATOR_2 */
  32. char *
  33. basename (const char *name)
  34. {
  35. const char *base;
  36. #if defined (HAVE_DOS_BASED_FILE_SYSTEM)
  37. /* Skip over the disk name in MSDOS pathnames. */
  38. if (ISALPHA (name[0]) && name[1] == ':')
  39. name += 2;
  40. #endif
  41. for (base = name; *name; name++)
  42. {
  43. if (IS_DIR_SEPARATOR (*name))
  44. {
  45. base = name + 1;
  46. }
  47. }
  48. return (char *) base;
  49. }