basename.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. }