logo

utils-std

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

mkfifo.c (1540B)


  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 <assert.h>
  7. #include <errno.h>
  8. #include <stdio.h> // fprintf
  9. #include <stdlib.h> // abort
  10. #include <string.h> // strerror
  11. #include <sys/stat.h> // mkfifo
  12. #include <unistd.h> // getopt
  13. mode_t filemask;
  14. static void
  15. usage(void)
  16. {
  17. fprintf(stderr, "Usage: mkfifo [-m mode] file\n");
  18. }
  19. int
  20. main(int argc, char *argv[])
  21. {
  22. mode_t mode = 0666;
  23. const char *errstr = NULL;
  24. int c = -1;
  25. while((c = getopt(argc, argv, ":m:")) != -1)
  26. {
  27. switch(c)
  28. {
  29. case 'm':
  30. mode = new_mode(optarg, 0666, &errstr);
  31. if(errstr != NULL)
  32. {
  33. fprintf(stderr, "mkfifo: error: Failed parsing mode '%s': %s\n", optarg, errstr);
  34. return 1;
  35. }
  36. break;
  37. case ':':
  38. fprintf(stderr, "mkfifo: error: Missing operand for option: '-%c'\n", optopt);
  39. usage();
  40. return 1;
  41. case '?':
  42. fprintf(stderr, "mkfifo: error: Unrecognised option: '-%c'\n", optopt);
  43. usage();
  44. return 1;
  45. default:
  46. abort();
  47. }
  48. }
  49. argc -= optind;
  50. argv += optind;
  51. assert(errno == 0);
  52. if(argc < 1)
  53. {
  54. fprintf(stderr, "mkfifo: error: Missing file argument\n");
  55. usage();
  56. return 1;
  57. }
  58. for(int i = 0; i < argc; i++)
  59. {
  60. if(mkfifo(argv[i], mode) != 0)
  61. {
  62. fprintf(
  63. stderr, "mkfifo: error: Failed creating FIFO at '%s': %s\n", argv[i], strerror(errno));
  64. return 1;
  65. }
  66. }
  67. return 0;
  68. }