basename.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. /*
  3. * Copyright (C) 2022, 2023 Ferass El Hafidi <vitali64pmemail@protonmail.com>
  4. */
  5. #include <unistd.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <libgen.h>
  10. #define REQ_PRINT_USAGE /* print_usage() from ../common/common.h */
  11. #define DESCRIPTION "Return non-directory portion of <string>."
  12. #define OPERANDS "string [suffix]"
  13. #include "../common/common.h"
  14. int main(int argc, char *argv[]) {
  15. int status;
  16. long unsigned int i;
  17. char *basenamestr;
  18. if (argc < 2) {
  19. print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
  20. return 1;
  21. }
  22. basenamestr = basename(argv[1]);
  23. if (argc == 3) {
  24. for (i = 1; i <= strlen(argv[2]); ++i) {
  25. if (argv[1][strlen(argv[1]) - i] ==
  26. argv[2][strlen(argv[2]) - i])
  27. /* Check */
  28. status = 0;
  29. else status = 1;
  30. }
  31. if (status == 0) {
  32. for (i = 1; i <= strlen(argv[2]); ++i) {
  33. if (argv[1][strlen(argv[1]) - i] ==
  34. argv[2][strlen(argv[2]) - i]) {
  35. /* Replace with spaces for now ... */
  36. argv[1][strlen(argv[1]) - i] = ' ';
  37. } // = '\0';
  38. }
  39. }
  40. }
  41. printf("%s\n", basenamestr);
  42. return 0;
  43. }