__xpg_basename.c 612 B

1234567891011121314151617181920212223242526272829303132333435
  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 *__xpg_basename(register char *path)
  9. {
  10. static const char null_or_empty[] = ".";
  11. char *first;
  12. register char *last;
  13. first = (char *) null_or_empty;
  14. if (path && *path) {
  15. first = path;
  16. last = path - 1;
  17. do {
  18. if ((*path != '/') && (path > ++last)) {
  19. last = first = path;
  20. }
  21. } while (*++path);
  22. if (*first == '/') {
  23. last = first;
  24. }
  25. last[1] = 0;
  26. }
  27. return first;
  28. }