dirname.c 727 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * Copyright (C) 2002 Manuel Novoa III
  3. * Copyright (C) 2000-2005 Erik Andersen <andersen@uclibc.org>
  4. *
  5. * Licensed under the LGPL v2.1, see the file COPYING.LIB in this tarball.
  6. */
  7. #include "_string.h"
  8. char *dirname(char *path)
  9. {
  10. static const char null_or_empty_or_noslash[] = ".";
  11. register char *s;
  12. register char *last;
  13. char *first;
  14. last = s = path;
  15. if (s != NULL) {
  16. LOOP:
  17. while (*s && (*s != '/')) ++s;
  18. first = s;
  19. while (*s == '/') ++s;
  20. if (*s) {
  21. last = first;
  22. goto LOOP;
  23. }
  24. if (last == path) {
  25. if (*last != '/') {
  26. goto DOT;
  27. }
  28. if ((*++last == '/') && (last[1] == 0)) {
  29. ++last;
  30. }
  31. }
  32. *last = 0;
  33. return path;
  34. }
  35. DOT:
  36. return (char *) null_or_empty_or_noslash;
  37. }