logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/

mkfifo.c (1496B)


  1. // utils-std: Collection of commonly available Unix tools
  2. // SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. // SPDX-License-Identifier: MPL-2.0
  4. #define _POSIX_C_SOURCE 200809L
  5. #include "../lib/mode.h"
  6. #include <errno.h>
  7. #include <stdio.h> // fprintf
  8. #include <stdlib.h> // abort
  9. #include <string.h> // strerror
  10. #include <sys/stat.h> // mkfifo
  11. #include <unistd.h> // getopt
  12. mode_t filemask;
  13. static void
  14. usage(void)
  15. {
  16. fprintf(stderr, "Usage: mkfifo [-m mode] file\n");
  17. }
  18. int
  19. main(int argc, char *argv[])
  20. {
  21. mode_t mode = 0666;
  22. const char *errstr = NULL;
  23. for(int c = -1; (c = getopt(argc, argv, ":m:")) != -1;)
  24. {
  25. switch(c)
  26. {
  27. case 'm':
  28. mode = new_mode(optarg, 0666, &errstr);
  29. if(errstr != NULL)
  30. {
  31. fprintf(stderr, "mkfifo: error: Failed parsing mode '%s': %s\n", optarg, errstr);
  32. return 1;
  33. }
  34. break;
  35. case ':':
  36. fprintf(stderr, "mkfifo: error: Missing operand for option: '-%c'\n", optopt);
  37. usage();
  38. return 1;
  39. case '?':
  40. fprintf(stderr, "mkfifo: error: Unrecognised option: '-%c'\n", optopt);
  41. usage();
  42. return 1;
  43. default:
  44. abort();
  45. }
  46. }
  47. argc -= optind;
  48. argv += optind;
  49. if(argc < 1)
  50. {
  51. fprintf(stderr, "mkfifo: error: Missing file argument\n");
  52. usage();
  53. return 1;
  54. }
  55. for(int i = 0; i < argc; i++)
  56. {
  57. if(mkfifo(argv[i], mode) != 0)
  58. {
  59. fprintf(
  60. stderr, "mkfifo: error: Failed creating FIFO at '%s': %s\n", argv[i], strerror(errno));
  61. return 1;
  62. }
  63. }
  64. return 0;
  65. }