logo

utils-std

Collection of commonly available Unix tools

path.c (705B)


  1. // utils-std: Collection of commonly available Unix tools
  2. // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-License-Identifier: MPL-2.0
  4. #include "./path.h"
  5. #include <string.h> // strrchr
  6. char *
  7. static_basename(char *path)
  8. {
  9. char *sep = strrchr(path, '/');
  10. return (sep == NULL) ? path : sep + 1;
  11. }
  12. char *
  13. path_split_static(char *path, bool trim)
  14. {
  15. char *child = NULL;
  16. size_t path_len = strlen(path);
  17. // delete trailing slashes
  18. if(trim)
  19. for(int i = path_len - 1; i > 0 && path[i] == '/'; i--)
  20. path[i] = 0;
  21. for(int i = path_len - 1; i > 0; i--)
  22. if(path[i] == '/')
  23. {
  24. path[i] = 0;
  25. child = &path[i + 1];
  26. break;
  27. }
  28. return child;
  29. }