fifo.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <string.h>
  6. #include <ctype.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #define BUF_SIZE 512
  10. char buffer[BUF_SIZE];
  11. char *bufp=buffer;
  12. int n = 0;
  13. int fifo_fd;
  14. int get_one_char() {
  15. /* If the buffer is empty */
  16. if (n <= 0) {
  17. /* Get some stuff */
  18. n = read(fifo_fd,buffer,BUF_SIZE);
  19. /* If we got some stuff */
  20. if (n > 0) {
  21. /* Point back to the start of the buffer */
  22. bufp = buffer;
  23. } else { /* we didn't get any stuff */
  24. return(EOF);
  25. }
  26. }
  27. n--; /* Decrement the number of chars in the buffer */
  28. return(*bufp++);
  29. }
  30. char *read_line() {
  31. char *line, *temp_line;
  32. int current_max_line;
  33. int i;
  34. int char_read;
  35. #ifdef DEBUG
  36. printf("read_line - start\n");
  37. #endif
  38. /* Any data in the fifo? */
  39. char_read = get_one_char();
  40. /* No. */
  41. if (char_read == EOF) {
  42. return(NULL); /* No data */
  43. }
  44. if (char_read == 0) {
  45. return(NULL); /* No data */
  46. }
  47. /* We have data */
  48. /* Allocate some memory to put the line */
  49. current_max_line = BUF_SIZE;
  50. line = (char *)malloc(current_max_line);
  51. i = 0;
  52. /* While we didn't hit the end of line */
  53. while((char_read != '\n')
  54. && (char_read != EOF)
  55. && (char_read != 0)) {
  56. /* If our buffer is too small */
  57. if (i >= current_max_line) {
  58. /* Reallocate it */
  59. temp_line = line;
  60. line = (char *)malloc(current_max_line+BUF_SIZE);
  61. memcpy(line,temp_line,current_max_line);
  62. current_max_line+=BUF_SIZE;
  63. free(temp_line);
  64. }
  65. /* Put the new character into the line */
  66. line[i] = char_read;
  67. /* Get the next character */
  68. i++;
  69. char_read = get_one_char();
  70. }
  71. line[i] = '\0'; /* terminate the string */
  72. #ifdef DEBUG
  73. printf("read_line: line=%s\n",line);
  74. #endif
  75. return(line);
  76. }
  77. void close_fifo(char *fifo_file_name) {
  78. /* If we opened it, close it */
  79. if (fifo_fd >= 0) {
  80. close(fifo_fd);
  81. }
  82. #ifdef DEBUG
  83. printf("FIFO closed:%s\n",fifo_file_name);
  84. #endif
  85. /* We are done - clean up */
  86. unlink(fifo_file_name);
  87. #ifdef DEBUG
  88. printf("FIFO deleted\n");
  89. #endif
  90. }
  91. int open_fifo(char *fifo_file_name) {
  92. /* Make the fifo */
  93. if (mkfifo(fifo_file_name,O_RDWR|O_CREAT|O_NOCTTY) != 0) {
  94. fprintf(stderr,"Error creating FIFO: %s\n",fifo_file_name);
  95. return(0);
  96. }
  97. #ifdef DEBUG
  98. printf("FIFO created\n");
  99. #endif
  100. /* Set the permissions on the fifo
  101. so that everyone can write to it
  102. but only owner can read from it. */
  103. if (chmod(fifo_file_name,S_IWUSR|S_IRUSR|S_IWGRP|S_IWOTH) < 0) {
  104. fprintf(stderr,"Unable to change permissions on FIFO\n");
  105. close_fifo(fifo_file_name);
  106. return(0);
  107. }
  108. /* Open the fifo for reading */
  109. if ((fifo_fd=open(fifo_file_name,O_RDONLY|O_NONBLOCK)) < 0) {
  110. fprintf(stderr,"Error opening FIFO for reading\n");
  111. close_fifo(fifo_file_name);
  112. return(0);
  113. }
  114. #ifdef DEBUG
  115. printf("FIFO opened\n");
  116. #endif
  117. return(1);
  118. }