dirname.c 747 B

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