lp.c

Go to the documentation of this file.
00001 /*      lp 1.4 - Send file to the lineprinter           Author: Kees J. Bot
00002  *                                                              3 Dec 1989
00003  */
00004 #define nil 0
00005 #include <sys/types.h>
00006 #include <stdio.h>
00007 #include <limits.h>
00008 #include <stdlib.h>
00009 #include <unistd.h>
00010 #include <fcntl.h>
00011 #include <string.h>
00012 #include <errno.h>
00013 #include <sys/wait.h>
00014 
00015 char LPD1[] = "/usr/sbin/lpd";          /* Proper place of lpd */
00016 char LPD2[] = "/usr/bin/lpd";           /* Minix has no sbin directories. */
00017 
00018 void report(char *mess)
00019 {
00020         fprintf(stderr, "lp: %s: %s\n", mess, strerror(errno));
00021 }
00022 
00023 void fatal(char *mess)
00024 {
00025         report(mess);
00026         exit(1);
00027 }
00028 
00029 void lp(char *file)
00030 /* Start the lpd daemon giving it the file to spool and print. */
00031 {
00032         int pid, status;
00033 
00034         if (file[0] != '/' || (pid= fork()) == 0) {
00035                 execl(LPD1, LPD1, file, (char *) nil);
00036                 if (errno != ENOENT) fatal(LPD1);
00037                 execl(LPD2, LPD2, file, (char *) nil);
00038                 fatal(LPD2);
00039         }
00040 
00041         if (pid < 0) fatal("can't fork");
00042 
00043         if (waitpid(pid, &status, 0) < 0) fatal("wait");
00044 
00045         if (status != 0) exit(1);
00046 }
00047 
00048 char path[PATH_MAX+1];
00049 int cwdsize;
00050 
00051 int main(int argc, char **argp)
00052 {
00053         int e=0;
00054         char *file;
00055 
00056         if (argc <= 1) lp("stdin");
00057 
00058         /* Lpd requires full path names, so find out where we are. */
00059         if (getcwd(path, sizeof(path)) == nil)
00060                 fatal("Can't determine current directory");
00061 
00062         cwdsize= strlen(path);
00063 
00064         /* Hand each file to lpd. */
00065         while ((file= *++argp) != nil) {
00066 
00067                 close(0);
00068 
00069                 if (open(file, O_RDONLY) != 0) {
00070                         report(file);
00071                         e=1;
00072                         continue;
00073                 }
00074                 if (file[0] == '/') {
00075                         lp(file);
00076                         continue;
00077                 }
00078                 if (cwdsize + 1 + strlen(file) + 1 > sizeof(path)) {
00079                         fprintf(stderr,
00080                                 "lp: full pathname of %s is too long\n",
00081                                 file);
00082                         e=1;
00083                         continue;
00084                 }
00085                 path[cwdsize] = '/';
00086                 strcpy(path + cwdsize + 1, file);
00087 
00088                 lp(path);
00089         }
00090         exit(e);
00091 }

Generated on Fri Apr 14 22:57:08 2006 for minix by  doxygen 1.4.6