dirname.c 766 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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. #define __need_NULL
  8. #include <stddef.h>
  9. #include <libgen.h>
  10. char *dirname(char *path)
  11. {
  12. static const char null_or_empty_or_noslash[] = ".";
  13. register char *s;
  14. register char *last;
  15. char *first;
  16. last = s = path;
  17. if (s != NULL) {
  18. LOOP:
  19. while (*s && (*s != '/')) ++s;
  20. first = s;
  21. while (*s == '/') ++s;
  22. if (*s) {
  23. last = first;
  24. goto LOOP;
  25. }
  26. if (last == path) {
  27. if (*last != '/') {
  28. goto DOT;
  29. }
  30. if ((*++last == '/') && (last[1] == 0)) {
  31. ++last;
  32. }
  33. }
  34. *last = 0;
  35. return path;
  36. }
  37. DOT:
  38. return (char *) null_or_empty_or_noslash;
  39. }