xref: /onnv-gate/usr/src/cmd/lp/lib/printers/putprinter.c (revision 871:f917b2ed7d21)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
230Sstevel@tonic-gate /*	  All Rights Reserved  	*/
240Sstevel@tonic-gate 
250Sstevel@tonic-gate 
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
280Sstevel@tonic-gate  * Use is subject to license terms.
290Sstevel@tonic-gate  */
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #include "sys/types.h"
360Sstevel@tonic-gate #include "sys/stat.h"
370Sstevel@tonic-gate #include "stdio.h"
380Sstevel@tonic-gate #include "string.h"
390Sstevel@tonic-gate #include "errno.h"
400Sstevel@tonic-gate #include "stdlib.h"
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #include "lp.h"
430Sstevel@tonic-gate #include "printers.h"
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include <unistd.h>
460Sstevel@tonic-gate #include <sys/wait.h>
470Sstevel@tonic-gate 
480Sstevel@tonic-gate #define	SHELL "/bin/sh"
490Sstevel@tonic-gate #define	PPDZIP ".gz"
500Sstevel@tonic-gate 
510Sstevel@tonic-gate extern struct {
520Sstevel@tonic-gate 	char			*v;
530Sstevel@tonic-gate 	short			len,
540Sstevel@tonic-gate 				okremote;
550Sstevel@tonic-gate }			prtrheadings[];
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #if	defined(__STDC__)
580Sstevel@tonic-gate 
590Sstevel@tonic-gate static void		print_sdn (int, char *, SCALED);
600Sstevel@tonic-gate static void		print_l (int, char *, char **);
610Sstevel@tonic-gate static void		print_str (int, char *, char *);
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
640Sstevel@tonic-gate static int addPrintersPPD(char *name, PRINTER *prbufp);
650Sstevel@tonic-gate static int copyPPDFile(char *ppd, char *printersPPD);
660Sstevel@tonic-gate static int unzipPPDFile(char *ppd, char *printersPPD);
670Sstevel@tonic-gate #endif
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #else
700Sstevel@tonic-gate 
710Sstevel@tonic-gate static void		print_sdn(),
720Sstevel@tonic-gate 			print_l(),
730Sstevel@tonic-gate 			print_str();
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
760Sstevel@tonic-gate static int addPrintersPPD();
770Sstevel@tonic-gate static int copyPPDFile();
780Sstevel@tonic-gate static int unzipPPDFile();
790Sstevel@tonic-gate #endif
800Sstevel@tonic-gate 
810Sstevel@tonic-gate #endif
820Sstevel@tonic-gate 
830Sstevel@tonic-gate unsigned long		ignprinter	= 0;
840Sstevel@tonic-gate int			ppdopt		= 0;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate /**
870Sstevel@tonic-gate  ** putprinter() - WRITE PRINTER STRUCTURE TO DISK FILES
880Sstevel@tonic-gate  **/
890Sstevel@tonic-gate 
900Sstevel@tonic-gate int
putprinter(char * name,PRINTER * prbufp)910Sstevel@tonic-gate putprinter(char *name, PRINTER *prbufp)
920Sstevel@tonic-gate {
930Sstevel@tonic-gate 	register char *		path;
940Sstevel@tonic-gate 	register char *		stty;
950Sstevel@tonic-gate 	register char *		speed;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	int fdin, fdout;
980Sstevel@tonic-gate 
990Sstevel@tonic-gate 	int			fld;
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	char			buf[BUFSIZ];
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate 	struct stat		statbuf1,
1040Sstevel@tonic-gate 				statbuf2;
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	badprinter = 0;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 	if (!name || !*name) {
1100Sstevel@tonic-gate 		errno = EINVAL;
1110Sstevel@tonic-gate 		return (-1);
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	if (STREQU(NAME_ALL, name)) {
1150Sstevel@tonic-gate 		errno = EINVAL;
1160Sstevel@tonic-gate 		return (-1);
1170Sstevel@tonic-gate 	}
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	/*
1200Sstevel@tonic-gate 	 * First go through the structure and see if we have
1210Sstevel@tonic-gate 	 * anything strange.
1220Sstevel@tonic-gate 	 */
1230Sstevel@tonic-gate 	if (!okprinter(name, prbufp, 1)) {
1240Sstevel@tonic-gate 		errno = EINVAL;
1250Sstevel@tonic-gate 		return (-1);
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate 	if (!Lp_A_Printers || !Lp_A_Interfaces) {
1290Sstevel@tonic-gate 		getadminpaths (LPUSER);
1300Sstevel@tonic-gate 		if (!Lp_A_Printers || !Lp_A_Interfaces)
1310Sstevel@tonic-gate 			return (0);
1320Sstevel@tonic-gate 	}
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	/*
1350Sstevel@tonic-gate 	 * Create the parent directory for this printer
1360Sstevel@tonic-gate 	 * if it doesn't yet exist.
1370Sstevel@tonic-gate 	 */
1380Sstevel@tonic-gate 	if (!(path = getprinterfile(name, (char *)0)))
1390Sstevel@tonic-gate 		return (-1);
1400Sstevel@tonic-gate 	if (Stat(path, &statbuf1) == 0) {
141*871Scasper 		if (!S_ISDIR(statbuf1.st_mode)) {
1420Sstevel@tonic-gate 			Free (path);
1430Sstevel@tonic-gate 			errno = ENOTDIR;
1440Sstevel@tonic-gate 			return (-1);
1450Sstevel@tonic-gate 		}
1460Sstevel@tonic-gate 	} else if (errno != ENOENT || mkdir_lpdir(path, MODE_DIR) == -1) {
1470Sstevel@tonic-gate 		Free (path);
1480Sstevel@tonic-gate 		return (-1);
1490Sstevel@tonic-gate 	}
1500Sstevel@tonic-gate 	Free (path);
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	/*
1530Sstevel@tonic-gate 	 * Create the copy of the interface program, unless
1540Sstevel@tonic-gate 	 * that would be silly or not desired.
1550Sstevel@tonic-gate 	 * Conversely, make sure the interface program doesn't
1560Sstevel@tonic-gate 	 * exist for a remote printer.
1570Sstevel@tonic-gate 	 */
1580Sstevel@tonic-gate 	if (prbufp->remote) {
1590Sstevel@tonic-gate 		if (!(path = makepath(Lp_A_Interfaces, name, (char *)0)))
1600Sstevel@tonic-gate 			return (-1);
1610Sstevel@tonic-gate 		(void)rmfile (path);
1620Sstevel@tonic-gate 		Free (path);
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 	if (prbufp->interface && (ignprinter & BAD_INTERFACE) == 0) {
1650Sstevel@tonic-gate 		if (Stat(prbufp->interface, &statbuf1) == -1)
1660Sstevel@tonic-gate 			return (-1);
1670Sstevel@tonic-gate 		if (!(path = makepath(Lp_A_Interfaces, name, (char *)0)))
1680Sstevel@tonic-gate 			return (-1);
1690Sstevel@tonic-gate 		if (
1700Sstevel@tonic-gate 			Stat(path, &statbuf2) == -1
1710Sstevel@tonic-gate 		     || statbuf1.st_dev != statbuf2.st_dev
1720Sstevel@tonic-gate 		     || statbuf1.st_ino != statbuf2.st_ino
1730Sstevel@tonic-gate 		) {
1740Sstevel@tonic-gate 			register int		n;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 			if ((fdin = open_locked(prbufp->interface, "r", 0)) < 0) {
1770Sstevel@tonic-gate 				Free (path);
1780Sstevel@tonic-gate 				return (-1);
1790Sstevel@tonic-gate 			}
1800Sstevel@tonic-gate 			if ((fdout = open_locked(path, "w", MODE_EXEC)) < 0) {
1810Sstevel@tonic-gate 				Free (path);
1820Sstevel@tonic-gate 				close(fdin);
1830Sstevel@tonic-gate 				return (-1);
1840Sstevel@tonic-gate 			}
1850Sstevel@tonic-gate 			while ((n = read(fdin, buf, BUFSIZ)) > 0)
1860Sstevel@tonic-gate 				write (fdout, buf,  n);
1870Sstevel@tonic-gate 			close(fdout);
1880Sstevel@tonic-gate 			close(fdin);
1890Sstevel@tonic-gate 		}
1900Sstevel@tonic-gate 		Free (path);
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
1940Sstevel@tonic-gate 	/*
1950Sstevel@tonic-gate 	 * Handle PPD (Postscript Printer Definition) file for printer
1960Sstevel@tonic-gate 	 * if this printer has been configured with one
1970Sstevel@tonic-gate 	 */
1980Sstevel@tonic-gate 	if ((prbufp->ppd != NULL) && (ppdopt))
1990Sstevel@tonic-gate 	{
2000Sstevel@tonic-gate 		if (addPrintersPPD(name, prbufp) != 0)
2010Sstevel@tonic-gate 		{
2020Sstevel@tonic-gate 			/* failed to added the printers PPD file */
2030Sstevel@tonic-gate 			return (-1);
2040Sstevel@tonic-gate 		}
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate #endif
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	/*
2090Sstevel@tonic-gate 	 * If this printer is dialed up, remove any baud rates
2100Sstevel@tonic-gate 	 * from the stty option list and move the last one to
2110Sstevel@tonic-gate 	 * the ".speed" member if the ".speed" member isn't already
2120Sstevel@tonic-gate 	 * set. Conversely, if this printer is directly connected,
2130Sstevel@tonic-gate 	 * move any value from the ".speed" member to the stty list.
2140Sstevel@tonic-gate 	 */
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	stty = (prbufp->stty? Strdup(prbufp->stty) : 0);
2170Sstevel@tonic-gate 	if (prbufp->speed)
2180Sstevel@tonic-gate 		speed = Strdup(prbufp->speed);
2190Sstevel@tonic-gate 	else
2200Sstevel@tonic-gate 		speed = 0;
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	if (prbufp->dial_info && stty) {
2230Sstevel@tonic-gate 		register char		*newstty,
2240Sstevel@tonic-gate 					*p,
2250Sstevel@tonic-gate 					*q;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 		register int		len;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 		if (!(q = newstty = Malloc(strlen(stty) + 1))) {
2300Sstevel@tonic-gate 			Free (stty);
2310Sstevel@tonic-gate 			errno = ENOMEM;
2320Sstevel@tonic-gate 			return (-1);
2330Sstevel@tonic-gate 		}
2340Sstevel@tonic-gate 		newstty[0] = 0;	/* start with empty copy */
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate 		for (
2370Sstevel@tonic-gate 			p = strtok(stty, " ");
2380Sstevel@tonic-gate 			p;
2390Sstevel@tonic-gate 			p = strtok((char *)0, " ")
2400Sstevel@tonic-gate 		) {
2410Sstevel@tonic-gate 			len = strlen(p);
2420Sstevel@tonic-gate 			if (strspn(p, "0123456789") == len) {
2430Sstevel@tonic-gate 				/*
2440Sstevel@tonic-gate 				 * If "prbufp->speed" isn't set, then
2450Sstevel@tonic-gate 				 * use the speed we just found. Don't
2460Sstevel@tonic-gate 				 * check "speed", because if more than
2470Sstevel@tonic-gate 				 * one speed was given in the list, we
2480Sstevel@tonic-gate 				 * want the last one.
2490Sstevel@tonic-gate 				 */
2500Sstevel@tonic-gate 				if (!prbufp->speed) {
2510Sstevel@tonic-gate 					if (speed)
2520Sstevel@tonic-gate 						Free (speed);
2530Sstevel@tonic-gate 					speed = Strdup(p);
2540Sstevel@tonic-gate 				}
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 			} else {
2570Sstevel@tonic-gate 				/*
2580Sstevel@tonic-gate 				 * Not a speed, so copy it to the
2590Sstevel@tonic-gate 				 * new stty string.
2600Sstevel@tonic-gate 				 */
2610Sstevel@tonic-gate 				if (q != newstty)
2620Sstevel@tonic-gate 					*q++ = ' ';
2630Sstevel@tonic-gate 				strcpy (q, p);
2640Sstevel@tonic-gate 				q += len;
2650Sstevel@tonic-gate 			}
2660Sstevel@tonic-gate 		}
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 		Free (stty);
2690Sstevel@tonic-gate 		stty = newstty;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	} else if (!prbufp->dial_info && speed) {
2720Sstevel@tonic-gate 		register char		*newstty;
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 		newstty = Malloc(strlen(stty) + 1 + strlen(speed) + 1);
2750Sstevel@tonic-gate 		if (!newstty) {
2760Sstevel@tonic-gate 			if (stty)
2770Sstevel@tonic-gate 				Free (stty);
2780Sstevel@tonic-gate 			errno = ENOMEM;
2790Sstevel@tonic-gate 			return (-1);
2800Sstevel@tonic-gate 		}
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 		if (stty) {
2830Sstevel@tonic-gate 			strcpy (newstty, stty);
2840Sstevel@tonic-gate 			strcat (newstty, " ");
2850Sstevel@tonic-gate 			strcat (newstty, speed);
2860Sstevel@tonic-gate 			Free (stty);
2870Sstevel@tonic-gate 		} else
2880Sstevel@tonic-gate 			strcpy (newstty, speed);
2890Sstevel@tonic-gate 		Free (speed);
2900Sstevel@tonic-gate 		speed = 0;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 		stty = newstty;
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	}
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	/*
2970Sstevel@tonic-gate 	 * Open the configuration file and write out the printer
2980Sstevel@tonic-gate 	 * configuration.
2990Sstevel@tonic-gate 	 */
3000Sstevel@tonic-gate 
3010Sstevel@tonic-gate 	if (!(path = getprinterfile(name, CONFIGFILE))) {
3020Sstevel@tonic-gate 		if (stty)
3030Sstevel@tonic-gate 			Free (stty);
3040Sstevel@tonic-gate 		if (speed)
3050Sstevel@tonic-gate 			Free (speed);
3060Sstevel@tonic-gate 		return (-1);
3070Sstevel@tonic-gate 	}
3080Sstevel@tonic-gate 	if ((fdout = open_locked(path, "w", MODE_READ)) < 0) {
3090Sstevel@tonic-gate 		Free (path);
3100Sstevel@tonic-gate 		if (stty)
3110Sstevel@tonic-gate 			Free (stty);
3120Sstevel@tonic-gate 		if (speed)
3130Sstevel@tonic-gate 			Free (speed);
3140Sstevel@tonic-gate 		return (-1);
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 	Free (path);
3170Sstevel@tonic-gate 
3180Sstevel@tonic-gate 	errno = 0;
3190Sstevel@tonic-gate 	for (fld = 0; fld < PR_MAX; fld++) {
3200Sstevel@tonic-gate 		if (prbufp->remote && !prtrheadings[fld].okremote)
3210Sstevel@tonic-gate 			continue;
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 		switch (fld) {
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate #define HEAD	prtrheadings[fld].v
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 		case PR_BAN:
3280Sstevel@tonic-gate 			{
3290Sstevel@tonic-gate 				char *ptr = NAME_ON;
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 				switch (prbufp->banner) {
3320Sstevel@tonic-gate 				case BAN_ALWAYS:
3330Sstevel@tonic-gate 					ptr = NAME_ON;
3340Sstevel@tonic-gate 					break;
3350Sstevel@tonic-gate 				case BAN_NEVER:
3360Sstevel@tonic-gate 					ptr = NAME_OFF;
3370Sstevel@tonic-gate 					break;
3380Sstevel@tonic-gate 				case BAN_OPTIONAL:
3390Sstevel@tonic-gate 					ptr = NAME_OPTIONAL;
3400Sstevel@tonic-gate 					break;
3410Sstevel@tonic-gate 				}
3420Sstevel@tonic-gate 				(void)fdprintf(fdout, "%s %s\n", HEAD, ptr);
3430Sstevel@tonic-gate 			}
3440Sstevel@tonic-gate 			break;
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 		case PR_CPI:
3470Sstevel@tonic-gate 			print_sdn(fdout, HEAD, prbufp->cpi);
3480Sstevel@tonic-gate 			break;
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 		case PR_CS:
3510Sstevel@tonic-gate 			if (!emptylist(prbufp->char_sets))
3520Sstevel@tonic-gate 				print_l(fdout, HEAD, prbufp->char_sets);
3530Sstevel@tonic-gate 			break;
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 		case PR_ITYPES:
3560Sstevel@tonic-gate 			/*
3570Sstevel@tonic-gate 			 * Put out the header even if the list is empty,
3580Sstevel@tonic-gate 			 * to distinguish no input types from the default.
3590Sstevel@tonic-gate 			 */
3600Sstevel@tonic-gate 			print_l(fdout, HEAD, prbufp->input_types);
3610Sstevel@tonic-gate 			break;
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 		case PR_DEV:
3640Sstevel@tonic-gate 			print_str(fdout, HEAD, prbufp->device);
3650Sstevel@tonic-gate 			break;
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate 		case PR_DIAL:
3680Sstevel@tonic-gate 			print_str(fdout, HEAD, prbufp->dial_info);
3690Sstevel@tonic-gate 			break;
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate 		case PR_RECOV:
3720Sstevel@tonic-gate 			print_str(fdout, HEAD, prbufp->fault_rec);
3730Sstevel@tonic-gate 			break;
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 		case PR_INTFC:
3760Sstevel@tonic-gate 			print_str(fdout, HEAD, prbufp->interface);
3770Sstevel@tonic-gate 			break;
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 		case PR_LPI:
3800Sstevel@tonic-gate 			print_sdn(fdout, HEAD, prbufp->lpi);
3810Sstevel@tonic-gate 			break;
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 		case PR_LEN:
3840Sstevel@tonic-gate 			print_sdn(fdout, HEAD, prbufp->plen);
3850Sstevel@tonic-gate 			break;
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate 		case PR_LOGIN:
3880Sstevel@tonic-gate 			if (prbufp->login & LOG_IN)
3890Sstevel@tonic-gate 				(void)fdprintf(fdout, "%s\n", HEAD);
3900Sstevel@tonic-gate 			break;
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate 		case PR_PTYPE:
3930Sstevel@tonic-gate 		{
3940Sstevel@tonic-gate 			char			**printer_types;
3950Sstevel@tonic-gate 
3960Sstevel@tonic-gate 			/*
3970Sstevel@tonic-gate 			 * For backward compatibility for those who
3980Sstevel@tonic-gate 			 * use only "->printer_type", we have to play
3990Sstevel@tonic-gate 			 * some games here.
4000Sstevel@tonic-gate 			 */
4010Sstevel@tonic-gate 			if (prbufp->printer_type && !prbufp->printer_types)
4020Sstevel@tonic-gate 				printer_types = getlist(
4030Sstevel@tonic-gate 					prbufp->printer_type,
4040Sstevel@tonic-gate 					LP_WS,
4050Sstevel@tonic-gate 					LP_SEP
4060Sstevel@tonic-gate 				);
4070Sstevel@tonic-gate 			else
4080Sstevel@tonic-gate 				printer_types = prbufp->printer_types;
4090Sstevel@tonic-gate 
4100Sstevel@tonic-gate 			if (!printer_types || !*printer_types)
4110Sstevel@tonic-gate 				print_str(fdout, HEAD, NAME_UNKNOWN);
4120Sstevel@tonic-gate 			else
4130Sstevel@tonic-gate 				print_l(fdout, HEAD, printer_types);
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 			if (printer_types != prbufp->printer_types)
4160Sstevel@tonic-gate 				freelist (printer_types);
4170Sstevel@tonic-gate 			break;
4180Sstevel@tonic-gate 		}
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 		case PR_REMOTE:
4210Sstevel@tonic-gate 			print_str(fdout, HEAD, prbufp->remote);
4220Sstevel@tonic-gate 			break;
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate 		case PR_SPEED:
4250Sstevel@tonic-gate 			print_str(fdout, HEAD, speed);
4260Sstevel@tonic-gate 			break;
4270Sstevel@tonic-gate 
4280Sstevel@tonic-gate 		case PR_STTY:
4290Sstevel@tonic-gate 			print_str(fdout, HEAD, stty);
4300Sstevel@tonic-gate 			break;
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 		case PR_WIDTH:
4330Sstevel@tonic-gate 			print_sdn(fdout, HEAD, prbufp->pwid);
4340Sstevel@tonic-gate 			break;
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate #if	defined(CAN_DO_MODULES)
4370Sstevel@tonic-gate 		case PR_MODULES:
4380Sstevel@tonic-gate 			/*
4390Sstevel@tonic-gate 			 * Put out the header even if the list is empty,
4400Sstevel@tonic-gate 			 * to distinguish no modules from the default.
4410Sstevel@tonic-gate 			 */
4420Sstevel@tonic-gate 			print_l(fdout, HEAD, prbufp->modules);
4430Sstevel@tonic-gate 			break;
4440Sstevel@tonic-gate #endif
4450Sstevel@tonic-gate 
4460Sstevel@tonic-gate 		case PR_OPTIONS:
4470Sstevel@tonic-gate 			print_l(fdout, HEAD, prbufp->options);
4480Sstevel@tonic-gate 			break;
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 		case PR_PPD:
4510Sstevel@tonic-gate 		{
4520Sstevel@tonic-gate 			print_str(fdout, HEAD, prbufp->ppd);
4530Sstevel@tonic-gate 			break;
4540Sstevel@tonic-gate 		}
4550Sstevel@tonic-gate 		}
4560Sstevel@tonic-gate 
4570Sstevel@tonic-gate 	}
4580Sstevel@tonic-gate 	if (stty)
4590Sstevel@tonic-gate 		Free (stty);
4600Sstevel@tonic-gate 	if (speed)
4610Sstevel@tonic-gate 		Free (speed);
4620Sstevel@tonic-gate 	if (errno != 0) {
4630Sstevel@tonic-gate 		close(fdout);
4640Sstevel@tonic-gate 		return (-1);
4650Sstevel@tonic-gate 	}
4660Sstevel@tonic-gate 	close(fdout);
4670Sstevel@tonic-gate 
4680Sstevel@tonic-gate 	/*
4690Sstevel@tonic-gate 	 * If we have a description of the printer,
4700Sstevel@tonic-gate 	 * write it out to a separate file.
4710Sstevel@tonic-gate 	 */
4720Sstevel@tonic-gate 	if (prbufp->description) {
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 		if (!(path = getprinterfile(name, COMMENTFILE)))
4750Sstevel@tonic-gate 			return (-1);
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate 		if (dumpstring(path, prbufp->description) == -1) {
4780Sstevel@tonic-gate 			Free (path);
4790Sstevel@tonic-gate 			return (-1);
4800Sstevel@tonic-gate 		}
4810Sstevel@tonic-gate 		Free (path);
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	}
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	/*
4860Sstevel@tonic-gate 	 * Now write out the alert condition.
4870Sstevel@tonic-gate 	 */
4880Sstevel@tonic-gate 	if (
4890Sstevel@tonic-gate 		prbufp->fault_alert.shcmd
4900Sstevel@tonic-gate 	     && putalert(Lp_A_Printers, name, &(prbufp->fault_alert)) == -1
4910Sstevel@tonic-gate 	)
4920Sstevel@tonic-gate 		return (-1);
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	return (0);
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate /**
4980Sstevel@tonic-gate  ** print_sdn() - PRINT SCALED DECIMAL NUMBER WITH HEADER
4990Sstevel@tonic-gate  ** print_l() - PRINT (char **) LIST WITH HEADER
5000Sstevel@tonic-gate  ** print_str() - PRINT STRING WITH HEADER
5010Sstevel@tonic-gate  **/
5020Sstevel@tonic-gate 
5030Sstevel@tonic-gate static void
print_sdn(int fd,char * head,SCALED sdn)5040Sstevel@tonic-gate print_sdn(int fd, char *head, SCALED sdn)
5050Sstevel@tonic-gate {
5060Sstevel@tonic-gate 	if (sdn.val <= 0)
5070Sstevel@tonic-gate 		return;
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	(void)fdprintf (fd, "%s ", head);
5100Sstevel@tonic-gate 	fdprintsdn (fd, sdn);
5110Sstevel@tonic-gate 
5120Sstevel@tonic-gate 	return;
5130Sstevel@tonic-gate }
5140Sstevel@tonic-gate 
5150Sstevel@tonic-gate static void
print_l(int fd,char * head,char ** list)5160Sstevel@tonic-gate print_l(int fd, char *head, char **list)
5170Sstevel@tonic-gate {
5180Sstevel@tonic-gate 	(void)fdprintf (fd, "%s ", head);
5190Sstevel@tonic-gate 	printlist_setup (0, 0, LP_SEP, 0);
5200Sstevel@tonic-gate 	fdprintlist (fd, list);
5210Sstevel@tonic-gate 	printlist_unsetup ();
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	return;
5240Sstevel@tonic-gate }
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate static void
print_str(int fd,char * head,char * str)5270Sstevel@tonic-gate print_str(int fd, char *head, char *str)
5280Sstevel@tonic-gate {
5290Sstevel@tonic-gate 	if (!str || !*str)
5300Sstevel@tonic-gate 		return;
5310Sstevel@tonic-gate 
5320Sstevel@tonic-gate 	(void)fdprintf (fd, "%s %s\n", head, str);
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 	return;
5350Sstevel@tonic-gate }
5360Sstevel@tonic-gate 
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
5390Sstevel@tonic-gate /*
5400Sstevel@tonic-gate  * Function:     addPrintersPPD()
5410Sstevel@tonic-gate  *
5420Sstevel@tonic-gate  * Description:  Handle PPD (Postscript Printer Definition) file for this
5430Sstevel@tonic-gate  *               printer if it has been configured with one
5440Sstevel@tonic-gate  *
5450Sstevel@tonic-gate  */
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate static int
addPrintersPPD(char * name,PRINTER * prbufp)5480Sstevel@tonic-gate addPrintersPPD(char *name, PRINTER *prbufp)
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate {
5510Sstevel@tonic-gate 	int result = 0;
5520Sstevel@tonic-gate 	char *path = NULL;
5530Sstevel@tonic-gate 	char *ppd = NULL;
5540Sstevel@tonic-gate 	char  buf[BUFSIZ];
5550Sstevel@tonic-gate 	struct stat statbuf;
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), "%s.ppd", name);
5580Sstevel@tonic-gate 	if (prbufp->remote)
5590Sstevel@tonic-gate 	{
5600Sstevel@tonic-gate 		/* make sure the PPD file doesn't exist for a remote printer */
5610Sstevel@tonic-gate 		if (!(path = makepath(ETCDIR, "ppd", buf, (char *)0)))
5620Sstevel@tonic-gate 		{
5630Sstevel@tonic-gate 			result = -1;
5640Sstevel@tonic-gate 		}
5650Sstevel@tonic-gate 		else
5660Sstevel@tonic-gate 		{
5670Sstevel@tonic-gate 			(void) rmfile(path);
5680Sstevel@tonic-gate 		}
5690Sstevel@tonic-gate 	}
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	if ((result == 0) && (prbufp->ppd != NULL))
5720Sstevel@tonic-gate 	{
5730Sstevel@tonic-gate 		ppd = strdup(prbufp->ppd);
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 		if (ppd == NULL)
5760Sstevel@tonic-gate 		{
5770Sstevel@tonic-gate 			result = -1;
5780Sstevel@tonic-gate 		}
5790Sstevel@tonic-gate 		else
5800Sstevel@tonic-gate 		{
5810Sstevel@tonic-gate 			/* Check the PPD file given exists */
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate 			if (Stat(ppd, &statbuf) == -1)
5840Sstevel@tonic-gate 			{
5850Sstevel@tonic-gate 				/*
5860Sstevel@tonic-gate 				 * The given ppd files does not exist, but
5870Sstevel@tonic-gate 				 * check if there is a zipped version of the
5880Sstevel@tonic-gate 				 * file that we can use instead
5890Sstevel@tonic-gate 				 */
5900Sstevel@tonic-gate 				if (strstr(ppd, PPDZIP) != NULL)
5910Sstevel@tonic-gate 				{
5920Sstevel@tonic-gate 					/* this is a zipped file so exit */
5930Sstevel@tonic-gate 					result = -1;
5940Sstevel@tonic-gate 				}
5950Sstevel@tonic-gate 				else
5960Sstevel@tonic-gate 				{
5970Sstevel@tonic-gate 					ppd = Realloc(ppd,
5980Sstevel@tonic-gate 						strlen(ppd)+strlen(PPDZIP)+2);
5990Sstevel@tonic-gate 					if (ppd != NULL)
6000Sstevel@tonic-gate 					{
6010Sstevel@tonic-gate 						ppd = strcat(ppd, PPDZIP);
6020Sstevel@tonic-gate 						if (Stat(ppd, &statbuf) == -1)
6030Sstevel@tonic-gate 						{
6040Sstevel@tonic-gate 							/*
6050Sstevel@tonic-gate 							 * this zipped version
6060Sstevel@tonic-gate 							 * of the file does not
6070Sstevel@tonic-gate 							 * exist either
6080Sstevel@tonic-gate 							 */
6090Sstevel@tonic-gate 							result = -1;
6100Sstevel@tonic-gate 						}
6110Sstevel@tonic-gate 					}
6120Sstevel@tonic-gate 					else
6130Sstevel@tonic-gate 					{
6140Sstevel@tonic-gate 						result = -1;
6150Sstevel@tonic-gate 					}
6160Sstevel@tonic-gate 				}
6170Sstevel@tonic-gate 			}
6180Sstevel@tonic-gate 		}
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 		/*
6210Sstevel@tonic-gate 		 * Create the copy of the PPD file for this printer
6220Sstevel@tonic-gate 		 * unless that would be silly or not desired
6230Sstevel@tonic-gate 		 */
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate 		if (result == 0)
6260Sstevel@tonic-gate 		{
6270Sstevel@tonic-gate 			if (!(path = makepath(ETCDIR, "ppd", buf, (char *)0)))
6280Sstevel@tonic-gate 			{
6290Sstevel@tonic-gate 				result = -1;
6300Sstevel@tonic-gate 			}
6310Sstevel@tonic-gate 		}
6320Sstevel@tonic-gate 
6330Sstevel@tonic-gate 		/*
6340Sstevel@tonic-gate 		 * At this point we may have a zipped or unzipped ppd file, if
6350Sstevel@tonic-gate 		 * it's unzipped just copy it otherwise unzip it to the
6360Sstevel@tonic-gate 		 * printer's ppd file (/etc/lp/ppd/<printer>.ppd)
6370Sstevel@tonic-gate 		 */
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 		if (result == 0)
6400Sstevel@tonic-gate 		{
6410Sstevel@tonic-gate 			if (strstr(ppd, PPDZIP) == NULL)
6420Sstevel@tonic-gate 			{
6430Sstevel@tonic-gate 				result = copyPPDFile(ppd, path);
6440Sstevel@tonic-gate 			}
6450Sstevel@tonic-gate 			else
6460Sstevel@tonic-gate 			{
6470Sstevel@tonic-gate 				result = unzipPPDFile(ppd, path);
6480Sstevel@tonic-gate 			}
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 			(void) chown_lppath(path);
6510Sstevel@tonic-gate 			(void) chmod(path, 0644);
6520Sstevel@tonic-gate 		}
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 		if (ppd != NULL)
6550Sstevel@tonic-gate 		{
6560Sstevel@tonic-gate 			Free(ppd);
6570Sstevel@tonic-gate 		}
6580Sstevel@tonic-gate 		if (path != NULL)
6590Sstevel@tonic-gate 		{
6600Sstevel@tonic-gate 			Free(path);
6610Sstevel@tonic-gate 		}
6620Sstevel@tonic-gate 	}
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	return (result);
6650Sstevel@tonic-gate } /* addPrintersPPD() */
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate /*
6690Sstevel@tonic-gate  * Function:     copyPPDFile()
6700Sstevel@tonic-gate  *
6710Sstevel@tonic-gate  * Description:  Copy the given ppd file to the printer's file in /etc/lp/ppd
6720Sstevel@tonic-gate  *
6730Sstevel@tonic-gate  */
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate static int
copyPPDFile(char * ppd,char * printersPPD)6760Sstevel@tonic-gate copyPPDFile(char *ppd, char *printersPPD)
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate {
6790Sstevel@tonic-gate 	int  result = 0;
6800Sstevel@tonic-gate 	register int n = 0;
6810Sstevel@tonic-gate 	int  fdin  = 0;
6820Sstevel@tonic-gate 	int  fdout = 0;
6830Sstevel@tonic-gate 	char buf[BUFSIZ];
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	if ((ppd != NULL) && (printersPPD != NULL))
6860Sstevel@tonic-gate 	{
6870Sstevel@tonic-gate 		if ((fdin = open_locked(ppd, "r", 0)) < 0)
6880Sstevel@tonic-gate 		{
6890Sstevel@tonic-gate 			result = -1;
6900Sstevel@tonic-gate 		}
6910Sstevel@tonic-gate 		else
6920Sstevel@tonic-gate 		{
6930Sstevel@tonic-gate 			fdout = open_locked(printersPPD, "w", MODE_EXEC);
6940Sstevel@tonic-gate 			if (fdout < 0)
6950Sstevel@tonic-gate 			{
6960Sstevel@tonic-gate 				close(fdin);
6970Sstevel@tonic-gate 				result = -1;
6980Sstevel@tonic-gate 			}
6990Sstevel@tonic-gate 		}
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 		if (result == 0)
7020Sstevel@tonic-gate 		{
7030Sstevel@tonic-gate 			while ((n = read(fdin, buf, BUFSIZ)) > 0)
7040Sstevel@tonic-gate 			{
7050Sstevel@tonic-gate 				write(fdout, buf,  n);
7060Sstevel@tonic-gate 			}
7070Sstevel@tonic-gate 			close(fdout);
7080Sstevel@tonic-gate 			close(fdin);
7090Sstevel@tonic-gate 		}
7100Sstevel@tonic-gate 	}
7110Sstevel@tonic-gate 	else
7120Sstevel@tonic-gate 	{
7130Sstevel@tonic-gate 		result = -1;
7140Sstevel@tonic-gate 	}
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	return (result);
7170Sstevel@tonic-gate } /* copyPPDFile() */
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate /*
7220Sstevel@tonic-gate  * Function:     unzipPPDFile()
7230Sstevel@tonic-gate  *
7240Sstevel@tonic-gate  * Description:  Unzip the given ppd file to the printer's file in /etc/lp/ppd.
7250Sstevel@tonic-gate  *               This is done by forking and running the unzip utility on the
7260Sstevel@tonic-gate  *               zipped ppd file.
7270Sstevel@tonic-gate  *
7280Sstevel@tonic-gate  */
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate static int
unzipPPDFile(char * ppd,char * printersPPD)7310Sstevel@tonic-gate unzipPPDFile(char *ppd, char *printersPPD)
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate {
7340Sstevel@tonic-gate 	int  result = -1;
7350Sstevel@tonic-gate 	char *cmdLine = NULL;
7360Sstevel@tonic-gate 	pid_t childPID = 0;
7370Sstevel@tonic-gate 	int   stat = 0;
7380Sstevel@tonic-gate 	int   clSize = 0;
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	if ((ppd != NULL) && (printersPPD != NULL))
7420Sstevel@tonic-gate 	{
7430Sstevel@tonic-gate 		childPID = fork();
7440Sstevel@tonic-gate 
7450Sstevel@tonic-gate 		switch (childPID)
7460Sstevel@tonic-gate 		{
7470Sstevel@tonic-gate 			case -1:
7480Sstevel@tonic-gate 			{
7490Sstevel@tonic-gate 				/* return error */
7500Sstevel@tonic-gate 				break;
7510Sstevel@tonic-gate 			}
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 			case 0:
7540Sstevel@tonic-gate 			{
7550Sstevel@tonic-gate 				/* child process  - so execute something */
7560Sstevel@tonic-gate 
7570Sstevel@tonic-gate 				clSize = strlen("/usr/bin/rm -f ") +
7580Sstevel@tonic-gate 						strlen(printersPPD) +
7590Sstevel@tonic-gate 						strlen("/usr/bin/gzip -dc ") +
7600Sstevel@tonic-gate 						strlen(ppd) +
7610Sstevel@tonic-gate 						strlen(printersPPD) + 20;
7620Sstevel@tonic-gate 				cmdLine = malloc(clSize);
7630Sstevel@tonic-gate 				if (cmdLine != NULL)
7640Sstevel@tonic-gate 				{
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 					(void) snprintf(cmdLine, clSize,
7670Sstevel@tonic-gate 				"/usr/bin/rm -f %s; /usr/bin/gzip -dc %s > %s",
7680Sstevel@tonic-gate 							printersPPD, ppd,
7690Sstevel@tonic-gate 							printersPPD);
7700Sstevel@tonic-gate 					result = execl(SHELL, SHELL, "-c",
7710Sstevel@tonic-gate 							cmdLine, NULL);
7720Sstevel@tonic-gate 					exit(result);
7730Sstevel@tonic-gate 				}
7740Sstevel@tonic-gate 				break;
7750Sstevel@tonic-gate 			}
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 			default:
7780Sstevel@tonic-gate 			{
7790Sstevel@tonic-gate 				/* parent process, child pid is in childPID */
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 				while (wait(&stat) != childPID);
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 				if ((stat & 0xff00) == 0)
7840Sstevel@tonic-gate 				{
7850Sstevel@tonic-gate 					result = 0;
7860Sstevel@tonic-gate 				}
7870Sstevel@tonic-gate 				break;
7880Sstevel@tonic-gate 			}
7890Sstevel@tonic-gate 		}
7900Sstevel@tonic-gate 	}
7910Sstevel@tonic-gate 
7920Sstevel@tonic-gate 	return (result);
7930Sstevel@tonic-gate } /* unzipPPDFile() */
7940Sstevel@tonic-gate #endif
795