rm.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 <errno.h>
  8. #include <string.h>
  9. #define REQ_PRINT_USAGE /* Require print_usage() from ../common/common.h */
  10. #define REQ_ERRPRINT /* Require errprint() from ../common/common.h */
  11. #define DESCRIPTION "Remove files."
  12. #define OPERANDS "[-Rr] file ..."
  13. #include "../common/common.h"
  14. int main(int argc, char *const argv[]) {
  15. int argument, i = 0;
  16. char param[256], *argv0 = strdup(argv[0]);
  17. while ((argument = getopt(argc, argv, "Rr")) != -1) {
  18. if (argument == '?') {
  19. print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
  20. return 1;
  21. }
  22. param[argument] = argument;
  23. } argc -= optind; argv += optind;
  24. if (argc < 1) {
  25. print_usage(argv0, DESCRIPTION, OPERANDS, VERSION);
  26. return 1;
  27. }
  28. for (; i < argc; i++) {
  29. if (!param['r'] || !param['R']) unlink(argv[i]);
  30. else remove(argv[i]); /* TODO: Actually
  31. * recursively remove
  32. * the directory */
  33. if (errno) return errprint(argv0, argv[i], errno);
  34. }
  35. return 0;
  36. }