xref: /minix3/minix/commands/lp/lp.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1 /*	lp 1.4 - Send file to the lineprinter		Author: Kees J. Bot
2  *								3 Dec 1989
3  */
4 #define nil 0
5 #include <sys/types.h>
6 #include <stdio.h>
7 #include <limits.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <string.h>
12 #include <errno.h>
13 #include <sys/wait.h>
14 
15 char LPD1[] = "/usr/sbin/lpd";		/* Proper place of lpd */
16 char LPD2[] = "/usr/bin/lpd";		/* Minix has no sbin directories. */
17 
report(char * mess)18 void report(char *mess)
19 {
20 	fprintf(stderr, "lp: %s: %s\n", mess, strerror(errno));
21 }
22 
fatal(char * mess)23 void fatal(char *mess)
24 {
25 	report(mess);
26 	exit(1);
27 }
28 
lp(char * file)29 void lp(char *file)
30 /* Start the lpd daemon giving it the file to spool and print. */
31 {
32 	int pid, status;
33 
34 	if (file[0] != '/' || (pid= fork()) == 0) {
35 		execl(LPD1, LPD1, file, (char *) nil);
36 		if (errno != ENOENT) fatal(LPD1);
37 		execl(LPD2, LPD2, file, (char *) nil);
38 		fatal(LPD2);
39 	}
40 
41 	if (pid < 0) fatal("can't fork");
42 
43 	if (waitpid(pid, &status, 0) < 0) fatal("wait");
44 
45 	if (status != 0) exit(1);
46 }
47 
48 char path[PATH_MAX+1];
49 int cwdsize;
50 
main(int argc,char ** argp)51 int main(int argc, char **argp)
52 {
53 	int e=0;
54 	char *file;
55 
56 	if (argc <= 1) lp("stdin");
57 
58 	/* Lpd requires full path names, so find out where we are. */
59 	if (getcwd(path, sizeof(path)) == nil)
60 		fatal("Can't determine current directory");
61 
62 	cwdsize= strlen(path);
63 
64 	/* Hand each file to lpd. */
65 	while ((file= *++argp) != nil) {
66 
67 		close(0);
68 
69 		if (open(file, O_RDONLY) != 0) {
70 			report(file);
71 			e=1;
72 			continue;
73 		}
74 		if (file[0] == '/') {
75 			lp(file);
76 			continue;
77 		}
78 		if (cwdsize + 1 + strlen(file) + 1 > sizeof(path)) {
79 			fprintf(stderr,
80 				"lp: full pathname of %s is too long\n",
81 				file);
82 			e=1;
83 			continue;
84 		}
85 		path[cwdsize] = '/';
86 		strcpy(path + cwdsize + 1, file);
87 
88 		lp(path);
89 	}
90 	exit(e);
91 }
92