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