__xpg_basename.c 632 B

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