home *** CD-ROM | disk | FTP | other *** search
- /* psif.c */
-
- #ifndef lint
- static char rcsid[] = "$Header: /usr/jjc/lprps/RCS/psif.c,v 1.2 90/12/18 09:59:45 jjc Rel $";
- #endif
-
- #include <stdio.h>
- #include <string.h>
- #include <memory.h>
- #include <sys/types.h>
- #include <sys/file.h>
- #include <sys/stat.h>
-
- long lseek();
-
- #ifndef TEXT_FILTER
- #define TEXT_FILTER "/usr/local/lib/psif-text"
- #endif /* not TEXT_FILTER */
-
- #ifndef PS_FILTER
- #define PS_FILTER "/usr/local/lib/psif-ps"
- #endif /* not PS_FILTER */
-
- #ifndef ERROR_EXIT_CODE
- #define ERROR_EXIT_CODE 1
- #endif /* not ERROR_EXIT_CODE */
-
- char *prog;
-
- void sys_error(s)
- char *s;
- {
- if (prog)
- (void)fprintf(stderr, "%s: ", prog);
- perror(s);
- exit(ERROR_EXIT_CODE);
- }
-
- /* ARGSUSED */
- main(argc, argv)
- int argc;
- char **argv;
- {
- char buf[BUFSIZ];
- char *command;
- int nread;
- struct stat statb;
- prog = argv[0];
- if ((nread = read(0, buf, 2)) < 0)
- sys_error("read");
- if (nread != 2 || buf[0] != '%' || buf[1] != '!')
- command = TEXT_FILTER;
- else
- command = PS_FILTER;
- if (fstat(0, &statb) < 0)
- sys_error("fstat");
- if ((statb.st_mode & S_IFMT) != S_IFREG) {
- /* this happens with lpr -p */
- int pid;
- int fd[2];
- if (pipe(fd) < 0)
- sys_error("pipe");
- if ((pid = fork()) < 0)
- sys_error("fork");
- else if (pid == 0) {
- /* in the child */
- int d;
- if (close(fd[0]) < 0)
- sys_error("close");
- d = fd[1];
- if (nread != 0) {
- if (write(d, buf, nread) < 0)
- sys_error("write");
- while ((nread = read(0, buf, sizeof(buf))) > 0)
- if (write(d, buf, nread) < 0)
- sys_error("write");
- if (nread < 0)
- sys_error("read");
- }
- if (close(d) < 0)
- sys_error("close");
- exit(0);
- }
- if (close(fd[1]) < 0)
- sys_error("close");
- if (close(0) < 0)
- sys_error("close");
- if (dup(fd[0]) < 0)
- sys_error("dup");
- if (close(fd[0]) < 0)
- sys_error("close");
- }
- else {
- if (lseek(0, (off_t)0, L_SET) < 0)
- sys_error("lseek");
- }
- execv(command, argv);
- sys_error("execv");
- }
-