basename.c 698 B

123456789101112131415161718192021222324252627282930
  1. /* vim: tabstop=4 shiftwidth=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2018 K. Lange
  5. *
  6. * basename - print file name
  7. */
  8. #include <stdio.h>
  9. #include <string.h>
  10. #include <libgen.h>
  11. int main(int argc, char * argv[]) {
  12. if (argc < 2) {
  13. fprintf(stderr, "%s: expected argument\n", argv[0]);
  14. return 1;
  15. }
  16. char * c = basename(argv[1]);
  17. if (argc > 2) {
  18. char * suffix = argv[2];
  19. char * found = strstr(c + strlen(c) - strlen(suffix), suffix);
  20. if (found && (found - c == (int)(strlen(c)-strlen(suffix)))) {
  21. *found = '\0';
  22. }
  23. }
  24. fprintf(stdout, "%s\n", c);
  25. return 0;
  26. }