logo

utils-std

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

mkfifo.c (1498B)


  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. int c = -1;
  24. while((c = getopt(argc, argv, ":m:")) != -1)
  25. {
  26. switch(c)
  27. {
  28. case 'm':
  29. mode = new_mode(optarg, 0666, &errstr);
  30. if(errstr != NULL)
  31. {
  32. fprintf(stderr, "mkfifo: error: Failed parsing mode '%s': %s\n", optarg, errstr);
  33. return 1;
  34. }
  35. break;
  36. case ':':
  37. fprintf(stderr, "mkfifo: error: Missing operand for option: '-%c'\n", optopt);
  38. usage();
  39. return 1;
  40. case '?':
  41. fprintf(stderr, "mkfifo: error: Unrecognised option: '-%c'\n", optopt);
  42. usage();
  43. return 1;
  44. default:
  45. abort();
  46. }
  47. }
  48. argc -= optind;
  49. argv += optind;
  50. if(argc < 1)
  51. {
  52. fprintf(stderr, "mkfifo: error: Missing file argument\n");
  53. usage();
  54. return 1;
  55. }
  56. for(int i = 0; i < argc; i++)
  57. {
  58. if(mkfifo(argv[i], mode) != 0)
  59. {
  60. fprintf(
  61. stderr, "mkfifo: error: Failed creating FIFO at '%s': %s\n", argv[i], strerror(errno));
  62. return 1;
  63. }
  64. }
  65. return 0;
  66. }