fifo.c 2.9 KB

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