get_last_path_component.c 1004 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * bb_get_last_path_component implementation for busybox
  4. *
  5. * Copyright (C) 2001 Manuel Novoa III <mjn3@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. #include "libbb.h"
  10. const char* FAST_FUNC bb_basename(const char *name)
  11. {
  12. const char *cp = strrchr(name, '/');
  13. if (cp)
  14. return cp + 1;
  15. return name;
  16. }
  17. /*
  18. * "/" -> "/"
  19. * "abc" -> "abc"
  20. * "abc/def" -> "def"
  21. * "abc/def/" -> ""
  22. */
  23. char* FAST_FUNC bb_get_last_path_component_nostrip(const char *path)
  24. {
  25. char *slash = strrchr(path, '/');
  26. if (!slash || (slash == path && !slash[1]))
  27. return (char*)path;
  28. return slash + 1;
  29. }
  30. /*
  31. * "/" -> "/"
  32. * "abc" -> "abc"
  33. * "abc/def" -> "def"
  34. * "abc/def/" -> "def" !!
  35. */
  36. char* FAST_FUNC bb_get_last_path_component_strip(char *path)
  37. {
  38. char *slash = last_char_is(path, '/');
  39. if (slash)
  40. while (*slash == '/' && slash != path)
  41. *slash-- = '\0';
  42. return bb_get_last_path_component_nostrip(path);
  43. }