3
0

basename.c 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini basename implementation for busybox
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. *
  9. */
  10. /* BB_AUDIT SUSv3 compliant */
  11. /* http://www.opengroup.org/onlinepubs/007904975/utilities/basename.html */
  12. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  13. *
  14. * Changes:
  15. * 1) Now checks for too many args. Need at least one and at most two.
  16. * 2) Don't check for options, as per SUSv3.
  17. * 3) Save some space by using strcmp(). Calling strncmp() here was silly.
  18. */
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include "busybox.h"
  23. int basename_main(int argc, char **argv)
  24. {
  25. size_t m, n;
  26. char *s;
  27. if (((unsigned int)(argc-2)) >= 2) {
  28. bb_show_usage();
  29. }
  30. s = bb_get_last_path_component(*++argv);
  31. if (*++argv) {
  32. n = strlen(*argv);
  33. m = strlen(s);
  34. if ((m > n) && ((strcmp)(s+m-n, *argv) == 0)) {
  35. s[m-n] = '\0';
  36. }
  37. }
  38. puts(s);
  39. fflush_stdout_and_exit(EXIT_SUCCESS);
  40. }