Fortuned's Reading and Writing /* and while it has stuff to write */ while (!dead_pipe){ int pfd = fileno(pipe_in); int wrc = 0; /* we read from it */ memset (readbuf, 0, sizeof (readbuf)); rc = read(pfd, readbuf, sizeof(readbuf)); if (rc == -1){ if (errno == EINTR) continue; snprintf(errstring, sizeof(errstring), "read returned error '%s'", strerror(errno)); goto cleanup; }else if (rc == 0){ /* EOF is reached, hooray. */ break; } /* and write it to our caller */ while (wrc < rc){ int bytes_written = 0; if ((bytes_written = write (fd, readbuf, strlen (readbuf))) == -1){ if ((errno == EINTR) || (errno == EAGAIN)) continue; warn("failed to write to the fd, error '%s'", strerror(errno)); goto pipe_done; } wrc += bytes_written; } } Fortuned's Main Loop /* this is the main loop */ while(1){ FILE* pipe_in; char readbuf[1024]; fd_set wset; /* first of all, open the device file for writing */ fd = open(device_file, OPEN_FLAGS, OPEN_COMMON); log ("succesfully opened!"); if (fd == -1){ snprintf(errstring, sizeof(errstring), "failed to open the fifo. error '%s'", strerror(errno)); goto cleanup; } /* first of all, make sure we handle SIGPIPE gracefully */ signal(SIGPIPE, sigpipehandler); /* we open a pipe with the fortune binary */ if ((pipe_in = popen(FORTUNE_BINARY, "r")) == NULL){ snprintf(errstring, sizeof(errstring), "could not popen(%s)", FORTUNE_BINARY); goto cleanup; }