1 /* $Id: spoolpipe.c,v 1.1 2002/09/23 19:25:06 bdenney Exp $
6 * Read a pipe that stays open, send the data to a temp file. Print
7 * the temp file if no new input data is seen within a period of time. This
8 * is useful, e.g., to create separate spool files without exiting a program
9 * that doesn't close it's printer output file.
11 * ---------------------------------------------------------------------------
13 * Copyright (c) 2002 Cegis Enterprises, Inc. Syracuse, NY 13215
15 * ---------------------------------------------------------------------------
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version 2
20 * of the License, or (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 * --------------------------------------------------------------------------
34 * 2002/05/20 - Initial programming
36 * --------------------------------------------------------------------------
39 #define BUF_SIZE 4*1024
47 #include <sys/types.h>
53 unsigned int delay
= 60; // default delay, in seconds
54 unsigned int count_down
= 0;
56 char buffer
[BUF_SIZE
];
57 ssize_t readcnt
, writecnt
;
59 int didwait
= 0; // have we already waited?
60 int havedata
= 0; // have we read anything in?
67 int main (int argc
, char *argv
[]) {
70 printf("Command line arguments (%d):\n", argc
);
71 for (q
= 0; q
< argc
; q
++) {
72 printf(" %d = %s\n", q
, argv
[q
]);
76 if ((argc
< 3) || (argc
> 4)) {
77 printf("usage: %s <inpipe> <tempfilename> [<maxdelay>]\n", argv
[0]);
81 if (argc
== 4) { // get delay
82 delay
= strtol(argv
[3], (char **) NULL
, 10);
84 printf("Unable to convert maximum delay value: %s\n", argv
[3]);
90 printf("Delay is set to %d seconds.\n", delay
);
93 infd
= open(argv
[1], O_RDONLY
| O_NONBLOCK
);
95 printf("Error opening input pipe %s: %d - %s\n", argv
[1], errno
, strerror(errno
));
99 outfd
= open(argv
[2], O_RDWR
| O_CREAT
| O_TRUNC
, S_IRWXU
);
101 printf("Error opening output file %s: %d - %s\n", argv
[2], errno
, strerror(errno
));
106 while (1) { // must kill with a signal....
107 readcnt
= read(infd
, buffer
, (size_t) BUF_SIZE
);
109 printf("read() returned with readcnt = %d, errno = %d\n", readcnt
, errno
);
111 if ((readcnt
== -1) && (errno
!= EAGAIN
)) { // EAGAIN - no data waiting, can ignore
112 printf("Error reading input pipe: %d - %s\n", errno
, strerror(errno
));
114 } else { // no errors reading input pipe
116 writecnt
= write(outfd
, buffer
, readcnt
);
117 if (writecnt
== -1) {
118 printf("Error writing output file: %d - %s\n", errno
, strerror(errno
));
121 didwait
= 0; // reset wait flag (wait again)
123 havedata
= 1; // set flag that we got some data
124 } else { //readcnt must = 0
125 if (!didwait
) { //have not waited yet...
126 if (count_down
> 0) { // sleep a bit
130 didwait
= 1; // set wait flag (don't wait again)
132 } else { // already waited
133 if (havedata
) { // have data to print, close & reopen output file
134 if (close(outfd
) != 0) {
135 printf("Error closing output file: %d - %s\n", errno
, strerror(errno
));
140 printf("Spooling temp file...\n");
145 printf("Error forking new process: %d - %s\n", errno
, strerror(errno
));
148 if (pid
== 0) { // we're now running in the child process...
149 execlp("lpr", "lpr", argv
[2], NULL
);
150 exit(99); // should never get here...
151 } // we're now running in the child process...
152 if (pid
> 0) { // we're running in the parent process...
153 wait_pid
= waitpid(pid
, (int *)&wait_status
, 0);
154 if (wait_pid
!= pid
) { // some sort of error
155 printf("Wait for 'lpr' command returned abnormally!\n");
156 if (WIFEXITED(wait_status
)) {
157 printf(" 'lpr' exited normally.\n");
159 printf(" 'lpr' exited abnormally, return code = %d.\n", WEXITSTATUS(wait_status
));
161 if (WIFSIGNALED(wait_status
)) {
162 printf(" 'lpr' received uncaught signal %d\n", WTERMSIG(wait_status
));
164 } // some sort of error
165 } // we're running in the parent process...
166 outfd
= open(argv
[2], O_RDWR
| O_TRUNC
);
168 printf("Error re-opening output file: %d - %s\n", errno
, strerror(errno
));
171 } // have data to print, close & repoen output file.
172 havedata
= 0; // no more data waiting
174 didwait
= 0; // reset wait flag (wait again)
176 } // readcnt must = 0
177 } // no errors reading input pipe
178 } // must kill with a signal...
181 } // eof(spoolpipe.c)