get_last_path_component.c 877 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 tarball for details.
  8. */
  9. #include "libbb.h"
  10. /*
  11. * "/" -> "/"
  12. * "abc" -> "abc"
  13. * "abc/def" -> "def"
  14. * "abc/def/" -> ""
  15. */
  16. char* FAST_FUNC bb_get_last_path_component_nostrip(const char *path)
  17. {
  18. char *slash = strrchr(path, '/');
  19. if (!slash || (slash == path && !slash[1]))
  20. return (char*)path;
  21. return slash + 1;
  22. }
  23. /*
  24. * "/" -> "/"
  25. * "abc" -> "abc"
  26. * "abc/def" -> "def"
  27. * "abc/def/" -> "def" !!
  28. */
  29. char* FAST_FUNC bb_get_last_path_component_strip(char *path)
  30. {
  31. char *slash = last_char_is(path, '/');
  32. if (slash)
  33. while (*slash == '/' && slash != path)
  34. *slash-- = '\0';
  35. return bb_get_last_path_component_nostrip(path);
  36. }