xref: /illumos-gate/usr/src/cmd/lp/lib/printers/putprinter.c (revision 7c478bd95313f5f23a4c958a745db2134aa0324)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate 
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
28*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
29*7c478bd9Sstevel@tonic-gate  */
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include "sys/types.h"
36*7c478bd9Sstevel@tonic-gate #include "sys/stat.h"
37*7c478bd9Sstevel@tonic-gate #include "stdio.h"
38*7c478bd9Sstevel@tonic-gate #include "string.h"
39*7c478bd9Sstevel@tonic-gate #include "errno.h"
40*7c478bd9Sstevel@tonic-gate #include "stdlib.h"
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate #include "lp.h"
43*7c478bd9Sstevel@tonic-gate #include "printers.h"
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate #include <unistd.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/wait.h>
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #define	SHELL "/bin/sh"
49*7c478bd9Sstevel@tonic-gate #define	PPDZIP ".gz"
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate extern struct {
52*7c478bd9Sstevel@tonic-gate 	char			*v;
53*7c478bd9Sstevel@tonic-gate 	short			len,
54*7c478bd9Sstevel@tonic-gate 				okremote;
55*7c478bd9Sstevel@tonic-gate }			prtrheadings[];
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate static void		print_sdn (int, char *, SCALED);
60*7c478bd9Sstevel@tonic-gate static void		print_l (int, char *, char **);
61*7c478bd9Sstevel@tonic-gate static void		print_str (int, char *, char *);
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
64*7c478bd9Sstevel@tonic-gate static int addPrintersPPD(char *name, PRINTER *prbufp);
65*7c478bd9Sstevel@tonic-gate static int copyPPDFile(char *ppd, char *printersPPD);
66*7c478bd9Sstevel@tonic-gate static int unzipPPDFile(char *ppd, char *printersPPD);
67*7c478bd9Sstevel@tonic-gate #endif
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate #else
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate static void		print_sdn(),
72*7c478bd9Sstevel@tonic-gate 			print_l(),
73*7c478bd9Sstevel@tonic-gate 			print_str();
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
76*7c478bd9Sstevel@tonic-gate static int addPrintersPPD();
77*7c478bd9Sstevel@tonic-gate static int copyPPDFile();
78*7c478bd9Sstevel@tonic-gate static int unzipPPDFile();
79*7c478bd9Sstevel@tonic-gate #endif
80*7c478bd9Sstevel@tonic-gate 
81*7c478bd9Sstevel@tonic-gate #endif
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate unsigned long		ignprinter	= 0;
84*7c478bd9Sstevel@tonic-gate int			ppdopt		= 0;
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate /**
87*7c478bd9Sstevel@tonic-gate  ** putprinter() - WRITE PRINTER STRUCTURE TO DISK FILES
88*7c478bd9Sstevel@tonic-gate  **/
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate int
91*7c478bd9Sstevel@tonic-gate putprinter(char *name, PRINTER *prbufp)
92*7c478bd9Sstevel@tonic-gate {
93*7c478bd9Sstevel@tonic-gate 	register char *		path;
94*7c478bd9Sstevel@tonic-gate 	register char *		stty;
95*7c478bd9Sstevel@tonic-gate 	register char *		speed;
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	int fdin, fdout;
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	int			fld;
100*7c478bd9Sstevel@tonic-gate 
101*7c478bd9Sstevel@tonic-gate 	char			buf[BUFSIZ];
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	struct stat		statbuf1,
104*7c478bd9Sstevel@tonic-gate 				statbuf2;
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 
107*7c478bd9Sstevel@tonic-gate 	badprinter = 0;
108*7c478bd9Sstevel@tonic-gate 
109*7c478bd9Sstevel@tonic-gate 	if (!name || !*name) {
110*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
111*7c478bd9Sstevel@tonic-gate 		return (-1);
112*7c478bd9Sstevel@tonic-gate 	}
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	if (STREQU(NAME_ALL, name)) {
115*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
116*7c478bd9Sstevel@tonic-gate 		return (-1);
117*7c478bd9Sstevel@tonic-gate 	}
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	/*
120*7c478bd9Sstevel@tonic-gate 	 * First go through the structure and see if we have
121*7c478bd9Sstevel@tonic-gate 	 * anything strange.
122*7c478bd9Sstevel@tonic-gate 	 */
123*7c478bd9Sstevel@tonic-gate 	if (!okprinter(name, prbufp, 1)) {
124*7c478bd9Sstevel@tonic-gate 		errno = EINVAL;
125*7c478bd9Sstevel@tonic-gate 		return (-1);
126*7c478bd9Sstevel@tonic-gate 	}
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate 	if (!Lp_A_Printers || !Lp_A_Interfaces) {
129*7c478bd9Sstevel@tonic-gate 		getadminpaths (LPUSER);
130*7c478bd9Sstevel@tonic-gate 		if (!Lp_A_Printers || !Lp_A_Interfaces)
131*7c478bd9Sstevel@tonic-gate 			return (0);
132*7c478bd9Sstevel@tonic-gate 	}
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	/*
135*7c478bd9Sstevel@tonic-gate 	 * Create the parent directory for this printer
136*7c478bd9Sstevel@tonic-gate 	 * if it doesn't yet exist.
137*7c478bd9Sstevel@tonic-gate 	 */
138*7c478bd9Sstevel@tonic-gate 	if (!(path = getprinterfile(name, (char *)0)))
139*7c478bd9Sstevel@tonic-gate 		return (-1);
140*7c478bd9Sstevel@tonic-gate 	if (Stat(path, &statbuf1) == 0) {
141*7c478bd9Sstevel@tonic-gate 		if (!(statbuf1.st_mode & S_IFDIR)) {
142*7c478bd9Sstevel@tonic-gate 			Free (path);
143*7c478bd9Sstevel@tonic-gate 			errno = ENOTDIR;
144*7c478bd9Sstevel@tonic-gate 			return (-1);
145*7c478bd9Sstevel@tonic-gate 		}
146*7c478bd9Sstevel@tonic-gate 	} else if (errno != ENOENT || mkdir_lpdir(path, MODE_DIR) == -1) {
147*7c478bd9Sstevel@tonic-gate 		Free (path);
148*7c478bd9Sstevel@tonic-gate 		return (-1);
149*7c478bd9Sstevel@tonic-gate 	}
150*7c478bd9Sstevel@tonic-gate 	Free (path);
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate 	/*
153*7c478bd9Sstevel@tonic-gate 	 * Create the copy of the interface program, unless
154*7c478bd9Sstevel@tonic-gate 	 * that would be silly or not desired.
155*7c478bd9Sstevel@tonic-gate 	 * Conversely, make sure the interface program doesn't
156*7c478bd9Sstevel@tonic-gate 	 * exist for a remote printer.
157*7c478bd9Sstevel@tonic-gate 	 */
158*7c478bd9Sstevel@tonic-gate 	if (prbufp->remote) {
159*7c478bd9Sstevel@tonic-gate 		if (!(path = makepath(Lp_A_Interfaces, name, (char *)0)))
160*7c478bd9Sstevel@tonic-gate 			return (-1);
161*7c478bd9Sstevel@tonic-gate 		(void)rmfile (path);
162*7c478bd9Sstevel@tonic-gate 		Free (path);
163*7c478bd9Sstevel@tonic-gate 	}
164*7c478bd9Sstevel@tonic-gate 	if (prbufp->interface && (ignprinter & BAD_INTERFACE) == 0) {
165*7c478bd9Sstevel@tonic-gate 		if (Stat(prbufp->interface, &statbuf1) == -1)
166*7c478bd9Sstevel@tonic-gate 			return (-1);
167*7c478bd9Sstevel@tonic-gate 		if (!(path = makepath(Lp_A_Interfaces, name, (char *)0)))
168*7c478bd9Sstevel@tonic-gate 			return (-1);
169*7c478bd9Sstevel@tonic-gate 		if (
170*7c478bd9Sstevel@tonic-gate 			Stat(path, &statbuf2) == -1
171*7c478bd9Sstevel@tonic-gate 		     || statbuf1.st_dev != statbuf2.st_dev
172*7c478bd9Sstevel@tonic-gate 		     || statbuf1.st_ino != statbuf2.st_ino
173*7c478bd9Sstevel@tonic-gate 		) {
174*7c478bd9Sstevel@tonic-gate 			register int		n;
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 			if ((fdin = open_locked(prbufp->interface, "r", 0)) < 0) {
177*7c478bd9Sstevel@tonic-gate 				Free (path);
178*7c478bd9Sstevel@tonic-gate 				return (-1);
179*7c478bd9Sstevel@tonic-gate 			}
180*7c478bd9Sstevel@tonic-gate 			if ((fdout = open_locked(path, "w", MODE_EXEC)) < 0) {
181*7c478bd9Sstevel@tonic-gate 				Free (path);
182*7c478bd9Sstevel@tonic-gate 				close(fdin);
183*7c478bd9Sstevel@tonic-gate 				return (-1);
184*7c478bd9Sstevel@tonic-gate 			}
185*7c478bd9Sstevel@tonic-gate 			while ((n = read(fdin, buf, BUFSIZ)) > 0)
186*7c478bd9Sstevel@tonic-gate 				write (fdout, buf,  n);
187*7c478bd9Sstevel@tonic-gate 			close(fdout);
188*7c478bd9Sstevel@tonic-gate 			close(fdin);
189*7c478bd9Sstevel@tonic-gate 		}
190*7c478bd9Sstevel@tonic-gate 		Free (path);
191*7c478bd9Sstevel@tonic-gate 	}
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
194*7c478bd9Sstevel@tonic-gate 	/*
195*7c478bd9Sstevel@tonic-gate 	 * Handle PPD (Postscript Printer Definition) file for printer
196*7c478bd9Sstevel@tonic-gate 	 * if this printer has been configured with one
197*7c478bd9Sstevel@tonic-gate 	 */
198*7c478bd9Sstevel@tonic-gate 	if ((prbufp->ppd != NULL) && (ppdopt))
199*7c478bd9Sstevel@tonic-gate 	{
200*7c478bd9Sstevel@tonic-gate 		if (addPrintersPPD(name, prbufp) != 0)
201*7c478bd9Sstevel@tonic-gate 		{
202*7c478bd9Sstevel@tonic-gate 			/* failed to added the printers PPD file */
203*7c478bd9Sstevel@tonic-gate 			return (-1);
204*7c478bd9Sstevel@tonic-gate 		}
205*7c478bd9Sstevel@tonic-gate 	}
206*7c478bd9Sstevel@tonic-gate #endif
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	/*
209*7c478bd9Sstevel@tonic-gate 	 * If this printer is dialed up, remove any baud rates
210*7c478bd9Sstevel@tonic-gate 	 * from the stty option list and move the last one to
211*7c478bd9Sstevel@tonic-gate 	 * the ".speed" member if the ".speed" member isn't already
212*7c478bd9Sstevel@tonic-gate 	 * set. Conversely, if this printer is directly connected,
213*7c478bd9Sstevel@tonic-gate 	 * move any value from the ".speed" member to the stty list.
214*7c478bd9Sstevel@tonic-gate 	 */
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 	stty = (prbufp->stty? Strdup(prbufp->stty) : 0);
217*7c478bd9Sstevel@tonic-gate 	if (prbufp->speed)
218*7c478bd9Sstevel@tonic-gate 		speed = Strdup(prbufp->speed);
219*7c478bd9Sstevel@tonic-gate 	else
220*7c478bd9Sstevel@tonic-gate 		speed = 0;
221*7c478bd9Sstevel@tonic-gate 
222*7c478bd9Sstevel@tonic-gate 	if (prbufp->dial_info && stty) {
223*7c478bd9Sstevel@tonic-gate 		register char		*newstty,
224*7c478bd9Sstevel@tonic-gate 					*p,
225*7c478bd9Sstevel@tonic-gate 					*q;
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 		register int		len;
228*7c478bd9Sstevel@tonic-gate 
229*7c478bd9Sstevel@tonic-gate 		if (!(q = newstty = Malloc(strlen(stty) + 1))) {
230*7c478bd9Sstevel@tonic-gate 			Free (stty);
231*7c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
232*7c478bd9Sstevel@tonic-gate 			return (-1);
233*7c478bd9Sstevel@tonic-gate 		}
234*7c478bd9Sstevel@tonic-gate 		newstty[0] = 0;	/* start with empty copy */
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 		for (
237*7c478bd9Sstevel@tonic-gate 			p = strtok(stty, " ");
238*7c478bd9Sstevel@tonic-gate 			p;
239*7c478bd9Sstevel@tonic-gate 			p = strtok((char *)0, " ")
240*7c478bd9Sstevel@tonic-gate 		) {
241*7c478bd9Sstevel@tonic-gate 			len = strlen(p);
242*7c478bd9Sstevel@tonic-gate 			if (strspn(p, "0123456789") == len) {
243*7c478bd9Sstevel@tonic-gate 				/*
244*7c478bd9Sstevel@tonic-gate 				 * If "prbufp->speed" isn't set, then
245*7c478bd9Sstevel@tonic-gate 				 * use the speed we just found. Don't
246*7c478bd9Sstevel@tonic-gate 				 * check "speed", because if more than
247*7c478bd9Sstevel@tonic-gate 				 * one speed was given in the list, we
248*7c478bd9Sstevel@tonic-gate 				 * want the last one.
249*7c478bd9Sstevel@tonic-gate 				 */
250*7c478bd9Sstevel@tonic-gate 				if (!prbufp->speed) {
251*7c478bd9Sstevel@tonic-gate 					if (speed)
252*7c478bd9Sstevel@tonic-gate 						Free (speed);
253*7c478bd9Sstevel@tonic-gate 					speed = Strdup(p);
254*7c478bd9Sstevel@tonic-gate 				}
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate 			} else {
257*7c478bd9Sstevel@tonic-gate 				/*
258*7c478bd9Sstevel@tonic-gate 				 * Not a speed, so copy it to the
259*7c478bd9Sstevel@tonic-gate 				 * new stty string.
260*7c478bd9Sstevel@tonic-gate 				 */
261*7c478bd9Sstevel@tonic-gate 				if (q != newstty)
262*7c478bd9Sstevel@tonic-gate 					*q++ = ' ';
263*7c478bd9Sstevel@tonic-gate 				strcpy (q, p);
264*7c478bd9Sstevel@tonic-gate 				q += len;
265*7c478bd9Sstevel@tonic-gate 			}
266*7c478bd9Sstevel@tonic-gate 		}
267*7c478bd9Sstevel@tonic-gate 
268*7c478bd9Sstevel@tonic-gate 		Free (stty);
269*7c478bd9Sstevel@tonic-gate 		stty = newstty;
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 	} else if (!prbufp->dial_info && speed) {
272*7c478bd9Sstevel@tonic-gate 		register char		*newstty;
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate 		newstty = Malloc(strlen(stty) + 1 + strlen(speed) + 1);
275*7c478bd9Sstevel@tonic-gate 		if (!newstty) {
276*7c478bd9Sstevel@tonic-gate 			if (stty)
277*7c478bd9Sstevel@tonic-gate 				Free (stty);
278*7c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
279*7c478bd9Sstevel@tonic-gate 			return (-1);
280*7c478bd9Sstevel@tonic-gate 		}
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 		if (stty) {
283*7c478bd9Sstevel@tonic-gate 			strcpy (newstty, stty);
284*7c478bd9Sstevel@tonic-gate 			strcat (newstty, " ");
285*7c478bd9Sstevel@tonic-gate 			strcat (newstty, speed);
286*7c478bd9Sstevel@tonic-gate 			Free (stty);
287*7c478bd9Sstevel@tonic-gate 		} else
288*7c478bd9Sstevel@tonic-gate 			strcpy (newstty, speed);
289*7c478bd9Sstevel@tonic-gate 		Free (speed);
290*7c478bd9Sstevel@tonic-gate 		speed = 0;
291*7c478bd9Sstevel@tonic-gate 
292*7c478bd9Sstevel@tonic-gate 		stty = newstty;
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 	}
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate 	/*
297*7c478bd9Sstevel@tonic-gate 	 * Open the configuration file and write out the printer
298*7c478bd9Sstevel@tonic-gate 	 * configuration.
299*7c478bd9Sstevel@tonic-gate 	 */
300*7c478bd9Sstevel@tonic-gate 
301*7c478bd9Sstevel@tonic-gate 	if (!(path = getprinterfile(name, CONFIGFILE))) {
302*7c478bd9Sstevel@tonic-gate 		if (stty)
303*7c478bd9Sstevel@tonic-gate 			Free (stty);
304*7c478bd9Sstevel@tonic-gate 		if (speed)
305*7c478bd9Sstevel@tonic-gate 			Free (speed);
306*7c478bd9Sstevel@tonic-gate 		return (-1);
307*7c478bd9Sstevel@tonic-gate 	}
308*7c478bd9Sstevel@tonic-gate 	if ((fdout = open_locked(path, "w", MODE_READ)) < 0) {
309*7c478bd9Sstevel@tonic-gate 		Free (path);
310*7c478bd9Sstevel@tonic-gate 		if (stty)
311*7c478bd9Sstevel@tonic-gate 			Free (stty);
312*7c478bd9Sstevel@tonic-gate 		if (speed)
313*7c478bd9Sstevel@tonic-gate 			Free (speed);
314*7c478bd9Sstevel@tonic-gate 		return (-1);
315*7c478bd9Sstevel@tonic-gate 	}
316*7c478bd9Sstevel@tonic-gate 	Free (path);
317*7c478bd9Sstevel@tonic-gate 
318*7c478bd9Sstevel@tonic-gate 	errno = 0;
319*7c478bd9Sstevel@tonic-gate 	for (fld = 0; fld < PR_MAX; fld++) {
320*7c478bd9Sstevel@tonic-gate 		if (prbufp->remote && !prtrheadings[fld].okremote)
321*7c478bd9Sstevel@tonic-gate 			continue;
322*7c478bd9Sstevel@tonic-gate 
323*7c478bd9Sstevel@tonic-gate 		switch (fld) {
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate #define HEAD	prtrheadings[fld].v
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate 		case PR_BAN:
328*7c478bd9Sstevel@tonic-gate 			{
329*7c478bd9Sstevel@tonic-gate 				char *ptr = NAME_ON;
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 				switch (prbufp->banner) {
332*7c478bd9Sstevel@tonic-gate 				case BAN_ALWAYS:
333*7c478bd9Sstevel@tonic-gate 					ptr = NAME_ON;
334*7c478bd9Sstevel@tonic-gate 					break;
335*7c478bd9Sstevel@tonic-gate 				case BAN_NEVER:
336*7c478bd9Sstevel@tonic-gate 					ptr = NAME_OFF;
337*7c478bd9Sstevel@tonic-gate 					break;
338*7c478bd9Sstevel@tonic-gate 				case BAN_OPTIONAL:
339*7c478bd9Sstevel@tonic-gate 					ptr = NAME_OPTIONAL;
340*7c478bd9Sstevel@tonic-gate 					break;
341*7c478bd9Sstevel@tonic-gate 				}
342*7c478bd9Sstevel@tonic-gate 				(void)fdprintf(fdout, "%s %s\n", HEAD, ptr);
343*7c478bd9Sstevel@tonic-gate 			}
344*7c478bd9Sstevel@tonic-gate 			break;
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate 		case PR_CPI:
347*7c478bd9Sstevel@tonic-gate 			print_sdn(fdout, HEAD, prbufp->cpi);
348*7c478bd9Sstevel@tonic-gate 			break;
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate 		case PR_CS:
351*7c478bd9Sstevel@tonic-gate 			if (!emptylist(prbufp->char_sets))
352*7c478bd9Sstevel@tonic-gate 				print_l(fdout, HEAD, prbufp->char_sets);
353*7c478bd9Sstevel@tonic-gate 			break;
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate 		case PR_ITYPES:
356*7c478bd9Sstevel@tonic-gate 			/*
357*7c478bd9Sstevel@tonic-gate 			 * Put out the header even if the list is empty,
358*7c478bd9Sstevel@tonic-gate 			 * to distinguish no input types from the default.
359*7c478bd9Sstevel@tonic-gate 			 */
360*7c478bd9Sstevel@tonic-gate 			print_l(fdout, HEAD, prbufp->input_types);
361*7c478bd9Sstevel@tonic-gate 			break;
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate 		case PR_DEV:
364*7c478bd9Sstevel@tonic-gate 			print_str(fdout, HEAD, prbufp->device);
365*7c478bd9Sstevel@tonic-gate 			break;
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate 		case PR_DIAL:
368*7c478bd9Sstevel@tonic-gate 			print_str(fdout, HEAD, prbufp->dial_info);
369*7c478bd9Sstevel@tonic-gate 			break;
370*7c478bd9Sstevel@tonic-gate 
371*7c478bd9Sstevel@tonic-gate 		case PR_RECOV:
372*7c478bd9Sstevel@tonic-gate 			print_str(fdout, HEAD, prbufp->fault_rec);
373*7c478bd9Sstevel@tonic-gate 			break;
374*7c478bd9Sstevel@tonic-gate 
375*7c478bd9Sstevel@tonic-gate 		case PR_INTFC:
376*7c478bd9Sstevel@tonic-gate 			print_str(fdout, HEAD, prbufp->interface);
377*7c478bd9Sstevel@tonic-gate 			break;
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate 		case PR_LPI:
380*7c478bd9Sstevel@tonic-gate 			print_sdn(fdout, HEAD, prbufp->lpi);
381*7c478bd9Sstevel@tonic-gate 			break;
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate 		case PR_LEN:
384*7c478bd9Sstevel@tonic-gate 			print_sdn(fdout, HEAD, prbufp->plen);
385*7c478bd9Sstevel@tonic-gate 			break;
386*7c478bd9Sstevel@tonic-gate 
387*7c478bd9Sstevel@tonic-gate 		case PR_LOGIN:
388*7c478bd9Sstevel@tonic-gate 			if (prbufp->login & LOG_IN)
389*7c478bd9Sstevel@tonic-gate 				(void)fdprintf(fdout, "%s\n", HEAD);
390*7c478bd9Sstevel@tonic-gate 			break;
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate 		case PR_PTYPE:
393*7c478bd9Sstevel@tonic-gate 		{
394*7c478bd9Sstevel@tonic-gate 			char			**printer_types;
395*7c478bd9Sstevel@tonic-gate 
396*7c478bd9Sstevel@tonic-gate 			/*
397*7c478bd9Sstevel@tonic-gate 			 * For backward compatibility for those who
398*7c478bd9Sstevel@tonic-gate 			 * use only "->printer_type", we have to play
399*7c478bd9Sstevel@tonic-gate 			 * some games here.
400*7c478bd9Sstevel@tonic-gate 			 */
401*7c478bd9Sstevel@tonic-gate 			if (prbufp->printer_type && !prbufp->printer_types)
402*7c478bd9Sstevel@tonic-gate 				printer_types = getlist(
403*7c478bd9Sstevel@tonic-gate 					prbufp->printer_type,
404*7c478bd9Sstevel@tonic-gate 					LP_WS,
405*7c478bd9Sstevel@tonic-gate 					LP_SEP
406*7c478bd9Sstevel@tonic-gate 				);
407*7c478bd9Sstevel@tonic-gate 			else
408*7c478bd9Sstevel@tonic-gate 				printer_types = prbufp->printer_types;
409*7c478bd9Sstevel@tonic-gate 
410*7c478bd9Sstevel@tonic-gate 			if (!printer_types || !*printer_types)
411*7c478bd9Sstevel@tonic-gate 				print_str(fdout, HEAD, NAME_UNKNOWN);
412*7c478bd9Sstevel@tonic-gate 			else
413*7c478bd9Sstevel@tonic-gate 				print_l(fdout, HEAD, printer_types);
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 			if (printer_types != prbufp->printer_types)
416*7c478bd9Sstevel@tonic-gate 				freelist (printer_types);
417*7c478bd9Sstevel@tonic-gate 			break;
418*7c478bd9Sstevel@tonic-gate 		}
419*7c478bd9Sstevel@tonic-gate 
420*7c478bd9Sstevel@tonic-gate 		case PR_REMOTE:
421*7c478bd9Sstevel@tonic-gate 			print_str(fdout, HEAD, prbufp->remote);
422*7c478bd9Sstevel@tonic-gate 			break;
423*7c478bd9Sstevel@tonic-gate 
424*7c478bd9Sstevel@tonic-gate 		case PR_SPEED:
425*7c478bd9Sstevel@tonic-gate 			print_str(fdout, HEAD, speed);
426*7c478bd9Sstevel@tonic-gate 			break;
427*7c478bd9Sstevel@tonic-gate 
428*7c478bd9Sstevel@tonic-gate 		case PR_STTY:
429*7c478bd9Sstevel@tonic-gate 			print_str(fdout, HEAD, stty);
430*7c478bd9Sstevel@tonic-gate 			break;
431*7c478bd9Sstevel@tonic-gate 
432*7c478bd9Sstevel@tonic-gate 		case PR_WIDTH:
433*7c478bd9Sstevel@tonic-gate 			print_sdn(fdout, HEAD, prbufp->pwid);
434*7c478bd9Sstevel@tonic-gate 			break;
435*7c478bd9Sstevel@tonic-gate 
436*7c478bd9Sstevel@tonic-gate #if	defined(CAN_DO_MODULES)
437*7c478bd9Sstevel@tonic-gate 		case PR_MODULES:
438*7c478bd9Sstevel@tonic-gate 			/*
439*7c478bd9Sstevel@tonic-gate 			 * Put out the header even if the list is empty,
440*7c478bd9Sstevel@tonic-gate 			 * to distinguish no modules from the default.
441*7c478bd9Sstevel@tonic-gate 			 */
442*7c478bd9Sstevel@tonic-gate 			print_l(fdout, HEAD, prbufp->modules);
443*7c478bd9Sstevel@tonic-gate 			break;
444*7c478bd9Sstevel@tonic-gate #endif
445*7c478bd9Sstevel@tonic-gate 
446*7c478bd9Sstevel@tonic-gate 		case PR_OPTIONS:
447*7c478bd9Sstevel@tonic-gate 			print_l(fdout, HEAD, prbufp->options);
448*7c478bd9Sstevel@tonic-gate 			break;
449*7c478bd9Sstevel@tonic-gate 
450*7c478bd9Sstevel@tonic-gate 		case PR_PPD:
451*7c478bd9Sstevel@tonic-gate 		{
452*7c478bd9Sstevel@tonic-gate 			print_str(fdout, HEAD, prbufp->ppd);
453*7c478bd9Sstevel@tonic-gate 			break;
454*7c478bd9Sstevel@tonic-gate 		}
455*7c478bd9Sstevel@tonic-gate 		}
456*7c478bd9Sstevel@tonic-gate 
457*7c478bd9Sstevel@tonic-gate 	}
458*7c478bd9Sstevel@tonic-gate 	if (stty)
459*7c478bd9Sstevel@tonic-gate 		Free (stty);
460*7c478bd9Sstevel@tonic-gate 	if (speed)
461*7c478bd9Sstevel@tonic-gate 		Free (speed);
462*7c478bd9Sstevel@tonic-gate 	if (errno != 0) {
463*7c478bd9Sstevel@tonic-gate 		close(fdout);
464*7c478bd9Sstevel@tonic-gate 		return (-1);
465*7c478bd9Sstevel@tonic-gate 	}
466*7c478bd9Sstevel@tonic-gate 	close(fdout);
467*7c478bd9Sstevel@tonic-gate 
468*7c478bd9Sstevel@tonic-gate 	/*
469*7c478bd9Sstevel@tonic-gate 	 * If we have a description of the printer,
470*7c478bd9Sstevel@tonic-gate 	 * write it out to a separate file.
471*7c478bd9Sstevel@tonic-gate 	 */
472*7c478bd9Sstevel@tonic-gate 	if (prbufp->description) {
473*7c478bd9Sstevel@tonic-gate 
474*7c478bd9Sstevel@tonic-gate 		if (!(path = getprinterfile(name, COMMENTFILE)))
475*7c478bd9Sstevel@tonic-gate 			return (-1);
476*7c478bd9Sstevel@tonic-gate 
477*7c478bd9Sstevel@tonic-gate 		if (dumpstring(path, prbufp->description) == -1) {
478*7c478bd9Sstevel@tonic-gate 			Free (path);
479*7c478bd9Sstevel@tonic-gate 			return (-1);
480*7c478bd9Sstevel@tonic-gate 		}
481*7c478bd9Sstevel@tonic-gate 		Free (path);
482*7c478bd9Sstevel@tonic-gate 
483*7c478bd9Sstevel@tonic-gate 	}
484*7c478bd9Sstevel@tonic-gate 
485*7c478bd9Sstevel@tonic-gate 	/*
486*7c478bd9Sstevel@tonic-gate 	 * Now write out the alert condition.
487*7c478bd9Sstevel@tonic-gate 	 */
488*7c478bd9Sstevel@tonic-gate 	if (
489*7c478bd9Sstevel@tonic-gate 		prbufp->fault_alert.shcmd
490*7c478bd9Sstevel@tonic-gate 	     && putalert(Lp_A_Printers, name, &(prbufp->fault_alert)) == -1
491*7c478bd9Sstevel@tonic-gate 	)
492*7c478bd9Sstevel@tonic-gate 		return (-1);
493*7c478bd9Sstevel@tonic-gate 
494*7c478bd9Sstevel@tonic-gate 	return (0);
495*7c478bd9Sstevel@tonic-gate }
496*7c478bd9Sstevel@tonic-gate 
497*7c478bd9Sstevel@tonic-gate /**
498*7c478bd9Sstevel@tonic-gate  ** print_sdn() - PRINT SCALED DECIMAL NUMBER WITH HEADER
499*7c478bd9Sstevel@tonic-gate  ** print_l() - PRINT (char **) LIST WITH HEADER
500*7c478bd9Sstevel@tonic-gate  ** print_str() - PRINT STRING WITH HEADER
501*7c478bd9Sstevel@tonic-gate  **/
502*7c478bd9Sstevel@tonic-gate 
503*7c478bd9Sstevel@tonic-gate static void
504*7c478bd9Sstevel@tonic-gate print_sdn(int fd, char *head, SCALED sdn)
505*7c478bd9Sstevel@tonic-gate {
506*7c478bd9Sstevel@tonic-gate 	if (sdn.val <= 0)
507*7c478bd9Sstevel@tonic-gate 		return;
508*7c478bd9Sstevel@tonic-gate 
509*7c478bd9Sstevel@tonic-gate 	(void)fdprintf (fd, "%s ", head);
510*7c478bd9Sstevel@tonic-gate 	fdprintsdn (fd, sdn);
511*7c478bd9Sstevel@tonic-gate 
512*7c478bd9Sstevel@tonic-gate 	return;
513*7c478bd9Sstevel@tonic-gate }
514*7c478bd9Sstevel@tonic-gate 
515*7c478bd9Sstevel@tonic-gate static void
516*7c478bd9Sstevel@tonic-gate print_l(int fd, char *head, char **list)
517*7c478bd9Sstevel@tonic-gate {
518*7c478bd9Sstevel@tonic-gate 	(void)fdprintf (fd, "%s ", head);
519*7c478bd9Sstevel@tonic-gate 	printlist_setup (0, 0, LP_SEP, 0);
520*7c478bd9Sstevel@tonic-gate 	fdprintlist (fd, list);
521*7c478bd9Sstevel@tonic-gate 	printlist_unsetup ();
522*7c478bd9Sstevel@tonic-gate 
523*7c478bd9Sstevel@tonic-gate 	return;
524*7c478bd9Sstevel@tonic-gate }
525*7c478bd9Sstevel@tonic-gate 
526*7c478bd9Sstevel@tonic-gate static void
527*7c478bd9Sstevel@tonic-gate print_str(int fd, char *head, char *str)
528*7c478bd9Sstevel@tonic-gate {
529*7c478bd9Sstevel@tonic-gate 	if (!str || !*str)
530*7c478bd9Sstevel@tonic-gate 		return;
531*7c478bd9Sstevel@tonic-gate 
532*7c478bd9Sstevel@tonic-gate 	(void)fdprintf (fd, "%s %s\n", head, str);
533*7c478bd9Sstevel@tonic-gate 
534*7c478bd9Sstevel@tonic-gate 	return;
535*7c478bd9Sstevel@tonic-gate }
536*7c478bd9Sstevel@tonic-gate 
537*7c478bd9Sstevel@tonic-gate 
538*7c478bd9Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
539*7c478bd9Sstevel@tonic-gate /*
540*7c478bd9Sstevel@tonic-gate  * Function:     addPrintersPPD()
541*7c478bd9Sstevel@tonic-gate  *
542*7c478bd9Sstevel@tonic-gate  * Description:  Handle PPD (Postscript Printer Definition) file for this
543*7c478bd9Sstevel@tonic-gate  *               printer if it has been configured with one
544*7c478bd9Sstevel@tonic-gate  *
545*7c478bd9Sstevel@tonic-gate  */
546*7c478bd9Sstevel@tonic-gate 
547*7c478bd9Sstevel@tonic-gate static int
548*7c478bd9Sstevel@tonic-gate addPrintersPPD(char *name, PRINTER *prbufp)
549*7c478bd9Sstevel@tonic-gate 
550*7c478bd9Sstevel@tonic-gate {
551*7c478bd9Sstevel@tonic-gate 	int result = 0;
552*7c478bd9Sstevel@tonic-gate 	char *path = NULL;
553*7c478bd9Sstevel@tonic-gate 	char *ppd = NULL;
554*7c478bd9Sstevel@tonic-gate 	char  buf[BUFSIZ];
555*7c478bd9Sstevel@tonic-gate 	struct stat statbuf;
556*7c478bd9Sstevel@tonic-gate 
557*7c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%s.ppd", name);
558*7c478bd9Sstevel@tonic-gate 	if (prbufp->remote)
559*7c478bd9Sstevel@tonic-gate 	{
560*7c478bd9Sstevel@tonic-gate 		/* make sure the PPD file doesn't exist for a remote printer */
561*7c478bd9Sstevel@tonic-gate 		if (!(path = makepath(ETCDIR, "ppd", buf, (char *)0)))
562*7c478bd9Sstevel@tonic-gate 		{
563*7c478bd9Sstevel@tonic-gate 			result = -1;
564*7c478bd9Sstevel@tonic-gate 		}
565*7c478bd9Sstevel@tonic-gate 		else
566*7c478bd9Sstevel@tonic-gate 		{
567*7c478bd9Sstevel@tonic-gate 			(void) rmfile(path);
568*7c478bd9Sstevel@tonic-gate 		}
569*7c478bd9Sstevel@tonic-gate 	}
570*7c478bd9Sstevel@tonic-gate 
571*7c478bd9Sstevel@tonic-gate 	if ((result == 0) && (prbufp->ppd != NULL))
572*7c478bd9Sstevel@tonic-gate 	{
573*7c478bd9Sstevel@tonic-gate 		ppd = strdup(prbufp->ppd);
574*7c478bd9Sstevel@tonic-gate 
575*7c478bd9Sstevel@tonic-gate 		if (ppd == NULL)
576*7c478bd9Sstevel@tonic-gate 		{
577*7c478bd9Sstevel@tonic-gate 			result = -1;
578*7c478bd9Sstevel@tonic-gate 		}
579*7c478bd9Sstevel@tonic-gate 		else
580*7c478bd9Sstevel@tonic-gate 		{
581*7c478bd9Sstevel@tonic-gate 			/* Check the PPD file given exists */
582*7c478bd9Sstevel@tonic-gate 
583*7c478bd9Sstevel@tonic-gate 			if (Stat(ppd, &statbuf) == -1)
584*7c478bd9Sstevel@tonic-gate 			{
585*7c478bd9Sstevel@tonic-gate 				/*
586*7c478bd9Sstevel@tonic-gate 				 * The given ppd files does not exist, but
587*7c478bd9Sstevel@tonic-gate 				 * check if there is a zipped version of the
588*7c478bd9Sstevel@tonic-gate 				 * file that we can use instead
589*7c478bd9Sstevel@tonic-gate 				 */
590*7c478bd9Sstevel@tonic-gate 				if (strstr(ppd, PPDZIP) != NULL)
591*7c478bd9Sstevel@tonic-gate 				{
592*7c478bd9Sstevel@tonic-gate 					/* this is a zipped file so exit */
593*7c478bd9Sstevel@tonic-gate 					result = -1;
594*7c478bd9Sstevel@tonic-gate 				}
595*7c478bd9Sstevel@tonic-gate 				else
596*7c478bd9Sstevel@tonic-gate 				{
597*7c478bd9Sstevel@tonic-gate 					ppd = Realloc(ppd,
598*7c478bd9Sstevel@tonic-gate 						strlen(ppd)+strlen(PPDZIP)+2);
599*7c478bd9Sstevel@tonic-gate 					if (ppd != NULL)
600*7c478bd9Sstevel@tonic-gate 					{
601*7c478bd9Sstevel@tonic-gate 						ppd = strcat(ppd, PPDZIP);
602*7c478bd9Sstevel@tonic-gate 						if (Stat(ppd, &statbuf) == -1)
603*7c478bd9Sstevel@tonic-gate 						{
604*7c478bd9Sstevel@tonic-gate 							/*
605*7c478bd9Sstevel@tonic-gate 							 * this zipped version
606*7c478bd9Sstevel@tonic-gate 							 * of the file does not
607*7c478bd9Sstevel@tonic-gate 							 * exist either
608*7c478bd9Sstevel@tonic-gate 							 */
609*7c478bd9Sstevel@tonic-gate 							result = -1;
610*7c478bd9Sstevel@tonic-gate 						}
611*7c478bd9Sstevel@tonic-gate 					}
612*7c478bd9Sstevel@tonic-gate 					else
613*7c478bd9Sstevel@tonic-gate 					{
614*7c478bd9Sstevel@tonic-gate 						result = -1;
615*7c478bd9Sstevel@tonic-gate 					}
616*7c478bd9Sstevel@tonic-gate 				}
617*7c478bd9Sstevel@tonic-gate 			}
618*7c478bd9Sstevel@tonic-gate 		}
619*7c478bd9Sstevel@tonic-gate 
620*7c478bd9Sstevel@tonic-gate 		/*
621*7c478bd9Sstevel@tonic-gate 		 * Create the copy of the PPD file for this printer
622*7c478bd9Sstevel@tonic-gate 		 * unless that would be silly or not desired
623*7c478bd9Sstevel@tonic-gate 		 */
624*7c478bd9Sstevel@tonic-gate 
625*7c478bd9Sstevel@tonic-gate 		if (result == 0)
626*7c478bd9Sstevel@tonic-gate 		{
627*7c478bd9Sstevel@tonic-gate 			if (!(path = makepath(ETCDIR, "ppd", buf, (char *)0)))
628*7c478bd9Sstevel@tonic-gate 			{
629*7c478bd9Sstevel@tonic-gate 				result = -1;
630*7c478bd9Sstevel@tonic-gate 			}
631*7c478bd9Sstevel@tonic-gate 		}
632*7c478bd9Sstevel@tonic-gate 
633*7c478bd9Sstevel@tonic-gate 		/*
634*7c478bd9Sstevel@tonic-gate 		 * At this point we may have a zipped or unzipped ppd file, if
635*7c478bd9Sstevel@tonic-gate 		 * it's unzipped just copy it otherwise unzip it to the
636*7c478bd9Sstevel@tonic-gate 		 * printer's ppd file (/etc/lp/ppd/<printer>.ppd)
637*7c478bd9Sstevel@tonic-gate 		 */
638*7c478bd9Sstevel@tonic-gate 
639*7c478bd9Sstevel@tonic-gate 		if (result == 0)
640*7c478bd9Sstevel@tonic-gate 		{
641*7c478bd9Sstevel@tonic-gate 			if (strstr(ppd, PPDZIP) == NULL)
642*7c478bd9Sstevel@tonic-gate 			{
643*7c478bd9Sstevel@tonic-gate 				result = copyPPDFile(ppd, path);
644*7c478bd9Sstevel@tonic-gate 			}
645*7c478bd9Sstevel@tonic-gate 			else
646*7c478bd9Sstevel@tonic-gate 			{
647*7c478bd9Sstevel@tonic-gate 				result = unzipPPDFile(ppd, path);
648*7c478bd9Sstevel@tonic-gate 			}
649*7c478bd9Sstevel@tonic-gate 
650*7c478bd9Sstevel@tonic-gate 			(void) chown_lppath(path);
651*7c478bd9Sstevel@tonic-gate 			(void) chmod(path, 0644);
652*7c478bd9Sstevel@tonic-gate 		}
653*7c478bd9Sstevel@tonic-gate 
654*7c478bd9Sstevel@tonic-gate 		if (ppd != NULL)
655*7c478bd9Sstevel@tonic-gate 		{
656*7c478bd9Sstevel@tonic-gate 			Free(ppd);
657*7c478bd9Sstevel@tonic-gate 		}
658*7c478bd9Sstevel@tonic-gate 		if (path != NULL)
659*7c478bd9Sstevel@tonic-gate 		{
660*7c478bd9Sstevel@tonic-gate 			Free(path);
661*7c478bd9Sstevel@tonic-gate 		}
662*7c478bd9Sstevel@tonic-gate 	}
663*7c478bd9Sstevel@tonic-gate 
664*7c478bd9Sstevel@tonic-gate 	return (result);
665*7c478bd9Sstevel@tonic-gate } /* addPrintersPPD() */
666*7c478bd9Sstevel@tonic-gate 
667*7c478bd9Sstevel@tonic-gate 
668*7c478bd9Sstevel@tonic-gate /*
669*7c478bd9Sstevel@tonic-gate  * Function:     copyPPDFile()
670*7c478bd9Sstevel@tonic-gate  *
671*7c478bd9Sstevel@tonic-gate  * Description:  Copy the given ppd file to the printer's file in /etc/lp/ppd
672*7c478bd9Sstevel@tonic-gate  *
673*7c478bd9Sstevel@tonic-gate  */
674*7c478bd9Sstevel@tonic-gate 
675*7c478bd9Sstevel@tonic-gate static int
676*7c478bd9Sstevel@tonic-gate copyPPDFile(char *ppd, char *printersPPD)
677*7c478bd9Sstevel@tonic-gate 
678*7c478bd9Sstevel@tonic-gate {
679*7c478bd9Sstevel@tonic-gate 	int  result = 0;
680*7c478bd9Sstevel@tonic-gate 	register int n = 0;
681*7c478bd9Sstevel@tonic-gate 	int  fdin  = 0;
682*7c478bd9Sstevel@tonic-gate 	int  fdout = 0;
683*7c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
684*7c478bd9Sstevel@tonic-gate 
685*7c478bd9Sstevel@tonic-gate 	if ((ppd != NULL) && (printersPPD != NULL))
686*7c478bd9Sstevel@tonic-gate 	{
687*7c478bd9Sstevel@tonic-gate 		if ((fdin = open_locked(ppd, "r", 0)) < 0)
688*7c478bd9Sstevel@tonic-gate 		{
689*7c478bd9Sstevel@tonic-gate 			result = -1;
690*7c478bd9Sstevel@tonic-gate 		}
691*7c478bd9Sstevel@tonic-gate 		else
692*7c478bd9Sstevel@tonic-gate 		{
693*7c478bd9Sstevel@tonic-gate 			fdout = open_locked(printersPPD, "w", MODE_EXEC);
694*7c478bd9Sstevel@tonic-gate 			if (fdout < 0)
695*7c478bd9Sstevel@tonic-gate 			{
696*7c478bd9Sstevel@tonic-gate 				close(fdin);
697*7c478bd9Sstevel@tonic-gate 				result = -1;
698*7c478bd9Sstevel@tonic-gate 			}
699*7c478bd9Sstevel@tonic-gate 		}
700*7c478bd9Sstevel@tonic-gate 
701*7c478bd9Sstevel@tonic-gate 		if (result == 0)
702*7c478bd9Sstevel@tonic-gate 		{
703*7c478bd9Sstevel@tonic-gate 			while ((n = read(fdin, buf, BUFSIZ)) > 0)
704*7c478bd9Sstevel@tonic-gate 			{
705*7c478bd9Sstevel@tonic-gate 				write(fdout, buf,  n);
706*7c478bd9Sstevel@tonic-gate 			}
707*7c478bd9Sstevel@tonic-gate 			close(fdout);
708*7c478bd9Sstevel@tonic-gate 			close(fdin);
709*7c478bd9Sstevel@tonic-gate 		}
710*7c478bd9Sstevel@tonic-gate 	}
711*7c478bd9Sstevel@tonic-gate 	else
712*7c478bd9Sstevel@tonic-gate 	{
713*7c478bd9Sstevel@tonic-gate 		result = -1;
714*7c478bd9Sstevel@tonic-gate 	}
715*7c478bd9Sstevel@tonic-gate 
716*7c478bd9Sstevel@tonic-gate 	return (result);
717*7c478bd9Sstevel@tonic-gate } /* copyPPDFile() */
718*7c478bd9Sstevel@tonic-gate 
719*7c478bd9Sstevel@tonic-gate 
720*7c478bd9Sstevel@tonic-gate 
721*7c478bd9Sstevel@tonic-gate /*
722*7c478bd9Sstevel@tonic-gate  * Function:     unzipPPDFile()
723*7c478bd9Sstevel@tonic-gate  *
724*7c478bd9Sstevel@tonic-gate  * Description:  Unzip the given ppd file to the printer's file in /etc/lp/ppd.
725*7c478bd9Sstevel@tonic-gate  *               This is done by forking and running the unzip utility on the
726*7c478bd9Sstevel@tonic-gate  *               zipped ppd file.
727*7c478bd9Sstevel@tonic-gate  *
728*7c478bd9Sstevel@tonic-gate  */
729*7c478bd9Sstevel@tonic-gate 
730*7c478bd9Sstevel@tonic-gate static int
731*7c478bd9Sstevel@tonic-gate unzipPPDFile(char *ppd, char *printersPPD)
732*7c478bd9Sstevel@tonic-gate 
733*7c478bd9Sstevel@tonic-gate {
734*7c478bd9Sstevel@tonic-gate 	int  result = -1;
735*7c478bd9Sstevel@tonic-gate 	char *cmdLine = NULL;
736*7c478bd9Sstevel@tonic-gate 	pid_t childPID = 0;
737*7c478bd9Sstevel@tonic-gate 	int   stat = 0;
738*7c478bd9Sstevel@tonic-gate 	int   clSize = 0;
739*7c478bd9Sstevel@tonic-gate 
740*7c478bd9Sstevel@tonic-gate 
741*7c478bd9Sstevel@tonic-gate 	if ((ppd != NULL) && (printersPPD != NULL))
742*7c478bd9Sstevel@tonic-gate 	{
743*7c478bd9Sstevel@tonic-gate 		childPID = fork();
744*7c478bd9Sstevel@tonic-gate 
745*7c478bd9Sstevel@tonic-gate 		switch (childPID)
746*7c478bd9Sstevel@tonic-gate 		{
747*7c478bd9Sstevel@tonic-gate 			case -1:
748*7c478bd9Sstevel@tonic-gate 			{
749*7c478bd9Sstevel@tonic-gate 				/* return error */
750*7c478bd9Sstevel@tonic-gate 				break;
751*7c478bd9Sstevel@tonic-gate 			}
752*7c478bd9Sstevel@tonic-gate 
753*7c478bd9Sstevel@tonic-gate 			case 0:
754*7c478bd9Sstevel@tonic-gate 			{
755*7c478bd9Sstevel@tonic-gate 				/* child process  - so execute something */
756*7c478bd9Sstevel@tonic-gate 
757*7c478bd9Sstevel@tonic-gate 				clSize = strlen("/usr/bin/rm -f ") +
758*7c478bd9Sstevel@tonic-gate 						strlen(printersPPD) +
759*7c478bd9Sstevel@tonic-gate 						strlen("/usr/bin/gzip -dc ") +
760*7c478bd9Sstevel@tonic-gate 						strlen(ppd) +
761*7c478bd9Sstevel@tonic-gate 						strlen(printersPPD) + 20;
762*7c478bd9Sstevel@tonic-gate 				cmdLine = malloc(clSize);
763*7c478bd9Sstevel@tonic-gate 				if (cmdLine != NULL)
764*7c478bd9Sstevel@tonic-gate 				{
765*7c478bd9Sstevel@tonic-gate 
766*7c478bd9Sstevel@tonic-gate 					(void) snprintf(cmdLine, clSize,
767*7c478bd9Sstevel@tonic-gate 				"/usr/bin/rm -f %s; /usr/bin/gzip -dc %s > %s",
768*7c478bd9Sstevel@tonic-gate 							printersPPD, ppd,
769*7c478bd9Sstevel@tonic-gate 							printersPPD);
770*7c478bd9Sstevel@tonic-gate 					result = execl(SHELL, SHELL, "-c",
771*7c478bd9Sstevel@tonic-gate 							cmdLine, NULL);
772*7c478bd9Sstevel@tonic-gate 					exit(result);
773*7c478bd9Sstevel@tonic-gate 				}
774*7c478bd9Sstevel@tonic-gate 				break;
775*7c478bd9Sstevel@tonic-gate 			}
776*7c478bd9Sstevel@tonic-gate 
777*7c478bd9Sstevel@tonic-gate 			default:
778*7c478bd9Sstevel@tonic-gate 			{
779*7c478bd9Sstevel@tonic-gate 				/* parent process, child pid is in childPID */
780*7c478bd9Sstevel@tonic-gate 
781*7c478bd9Sstevel@tonic-gate 				while (wait(&stat) != childPID);
782*7c478bd9Sstevel@tonic-gate 
783*7c478bd9Sstevel@tonic-gate 				if ((stat & 0xff00) == 0)
784*7c478bd9Sstevel@tonic-gate 				{
785*7c478bd9Sstevel@tonic-gate 					result = 0;
786*7c478bd9Sstevel@tonic-gate 				}
787*7c478bd9Sstevel@tonic-gate 				break;
788*7c478bd9Sstevel@tonic-gate 			}
789*7c478bd9Sstevel@tonic-gate 		}
790*7c478bd9Sstevel@tonic-gate 	}
791*7c478bd9Sstevel@tonic-gate 
792*7c478bd9Sstevel@tonic-gate 	return (result);
793*7c478bd9Sstevel@tonic-gate } /* unzipPPDFile() */
794*7c478bd9Sstevel@tonic-gate #endif
795