mkdir.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 <sys/stat.h>
  8. #include <errno.h>
  9. #include <string.h>
  10. #define REQ_PRINT_USAGE /* Require print_usage() from ../common/common.h */
  11. #define REQ_ERRPRINT /* Require errprint() from ../common/common.h */
  12. #define DESCRIPTION "Create directories."
  13. #define OPERANDS "directory ..."
  14. #include "../common/common.h"
  15. int main(int argc, char *const argv[]) {
  16. int success, argument, i = 0;
  17. char *argv0 = strdup(argv[0]);
  18. if (argc == 1) {
  19. print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
  20. return 1;
  21. }
  22. while ((argument = getopt(argc, argv, "p")) != -1) {
  23. if (argument == '?') {
  24. print_usage(argv[0], DESCRIPTION, OPERANDS, VERSION);
  25. return 0;
  26. }
  27. } argc -= optind; argv += optind;
  28. for (; i < argc; i++) {
  29. if (argv[i][0] != '-')
  30. success = !mkdir(argv[i], S_IRWXU | S_IRWXG | S_IRWXO) ? 0 : 1;
  31. if (success == 1) {
  32. return errprint(argv0, argv[i], errno);
  33. }
  34. }
  35. return 0;
  36. }