mkfifo.c 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #include <fcntl.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <sys/statvfs.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <errno.h>
  8. #include <stdio.h>
  9. int main(int argc, char** argv) {
  10. char temp[] = "/tmp/stattest-XXXXXX";
  11. const char file[] = "/mkfifo_fifo";
  12. int len = sizeof(temp) + sizeof(file);
  13. char* path = malloc(len * sizeof(char));
  14. if (path == NULL) {
  15. fprintf(stderr, "Could not allocate: %s\n", strerror(errno));
  16. exit(1);
  17. }
  18. path = strncat(path, mktemp(temp), sizeof(temp));
  19. path = strncat(path, file, sizeof(file));
  20. if (mkdir(temp, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) {
  21. fprintf(stderr, "mkdir %s: %s\n", temp, strerror(errno));
  22. exit(1);
  23. }
  24. if (mkfifo(path, S_IRUSR) == -1) {
  25. fprintf(stderr, "mkfifo %s: %s\n", path, strerror(errno));
  26. exit(1);
  27. }
  28. struct stat sb;
  29. if (stat(path, &sb) != 0) {
  30. fprintf(stderr, "stat: %s\n", strerror(errno));
  31. exit(1);
  32. }
  33. if (!(sb.st_mode & S_IFIFO)) {
  34. fprintf(stderr, "Not a FIFO: %d\n", sb.st_mode);
  35. exit(1);
  36. }
  37. }