123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- #include <stdio.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <string.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <unistd.h>
- #define BUF_SIZE 512
- char buffer[BUF_SIZE];
- char *bufp=buffer;
- int n = 0;
- int fifo_fd;
- int get_one_char() {
-
- if (n <= 0) {
-
- n = read(fifo_fd,buffer,BUF_SIZE);
-
- if (n > 0) {
-
- bufp = buffer;
- } else {
- return(EOF);
- }
- }
- n--;
- return(*bufp++);
- }
- char *read_line() {
- char *line, *temp_line;
- int current_max_line;
- int i;
- int char_read;
- #ifdef DEBUG
- printf("read_line - start\n");
- #endif
-
- char_read = get_one_char();
-
- if (char_read == EOF) {
- return(NULL);
- }
- if (char_read == 0) {
- return(NULL);
- }
-
-
- current_max_line = BUF_SIZE;
- line = (char *)malloc(current_max_line);
- i = 0;
-
- while((char_read != '\n')
- && (char_read != EOF)
- && (char_read != 0)) {
-
- if (i >= current_max_line) {
-
- temp_line = line;
- line = (char *)malloc(current_max_line+BUF_SIZE);
- memcpy(line,temp_line,current_max_line);
- current_max_line+=BUF_SIZE;
- free(temp_line);
- }
-
- line[i] = char_read;
-
- i++;
- char_read = get_one_char();
- }
- line[i] = '\0';
- #ifdef DEBUG
- printf("read_line: line=%s\n",line);
- #endif
- return(line);
- }
- void close_fifo(char *fifo_file_name) {
-
- if (fifo_fd >= 0) {
- close(fifo_fd);
- }
- #ifdef DEBUG
- printf("FIFO closed:%s\n",fifo_file_name);
- #endif
-
- unlink(fifo_file_name);
- #ifdef DEBUG
- printf("FIFO deleted\n");
- #endif
- }
- int open_fifo(char *fifo_file_name) {
-
- if (mkfifo(fifo_file_name,O_RDWR|O_CREAT|O_NOCTTY) != 0) {
- fprintf(stderr,"Error creating FIFO: %s\n",fifo_file_name);
- return(0);
- }
- #ifdef DEBUG
- printf("FIFO created\n");
- #endif
-
- if (chmod(fifo_file_name,S_IWUSR|S_IRUSR|S_IWGRP|S_IWOTH) < 0) {
- fprintf(stderr,"Unable to change permissions on FIFO\n");
- close_fifo(fifo_file_name);
- return(0);
- }
-
- if ((fifo_fd=open(fifo_file_name,O_RDONLY|O_NONBLOCK)) < 0) {
- fprintf(stderr,"Error opening FIFO for reading\n");
- close_fifo(fifo_file_name);
- return(0);
- }
- #ifdef DEBUG
- printf("FIFO opened\n");
- #endif
- return(1);
- }
|