__xpg_basename.c 689 B

123456789101112131415161718192021222324252627282930313233343536373839
  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 <libgen.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. }
  29. #ifndef __USE_GNU
  30. # undef basename
  31. weak_alias(__xpg_basename,basename)
  32. #endif