xref: /onnv-gate/usr/src/cmd/lp/lib/printers/printwheels.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  */
22*871Scasper /*
23*871Scasper  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*871Scasper  * Use is subject to license terms.
25*871Scasper  */
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate 
30*871Scasper #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include "string.h"
340Sstevel@tonic-gate #include "errno.h"
350Sstevel@tonic-gate #include "sys/types.h"
360Sstevel@tonic-gate #include "stdlib.h"
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #include "lp.h"
390Sstevel@tonic-gate #include "printers.h"
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /**
420Sstevel@tonic-gate  ** getpwheel() - GET PRINT WHEEL INFO FROM DISK
430Sstevel@tonic-gate  **/
440Sstevel@tonic-gate 
450Sstevel@tonic-gate PWHEEL *
460Sstevel@tonic-gate #if	defined(__STDC__)
470Sstevel@tonic-gate getpwheel (
480Sstevel@tonic-gate 	char *			name
490Sstevel@tonic-gate )
500Sstevel@tonic-gate #else
510Sstevel@tonic-gate getpwheel (name)
520Sstevel@tonic-gate 	char			*name;
530Sstevel@tonic-gate #endif
540Sstevel@tonic-gate {
550Sstevel@tonic-gate 	static long		lastdir		= -1;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	static PWHEEL		pwheel;
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	register FALERT		*pa;
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	if (!name || !*name) {
630Sstevel@tonic-gate 		errno = EINVAL;
640Sstevel@tonic-gate 		return (0);
650Sstevel@tonic-gate 	}
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	/*
680Sstevel@tonic-gate 	 * Getting ``all''? If so, jump into the directory
690Sstevel@tonic-gate 	 * wherever we left off.
700Sstevel@tonic-gate 	 */
710Sstevel@tonic-gate 	if (STREQU(NAME_ALL, name)) {
720Sstevel@tonic-gate 		if (!(name = next_dir(Lp_A_PrintWheels, &lastdir)))
730Sstevel@tonic-gate 			return (0);
740Sstevel@tonic-gate 	} else
750Sstevel@tonic-gate 		lastdir = -1;
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	/*
780Sstevel@tonic-gate 	 * Get the information for the alert.
790Sstevel@tonic-gate 	 */
800Sstevel@tonic-gate 	if (!(pa = getalert(Lp_A_PrintWheels, name))) {
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 		/*
830Sstevel@tonic-gate 		 * Unless the world has turned weird, we shouldn't
840Sstevel@tonic-gate 		 * get ENOTDIR if we're doing the ``all'' case--because
850Sstevel@tonic-gate 		 * getting here in the all case meant the printwheel
860Sstevel@tonic-gate 		 * directory exists, but ENOTDIR means it doesn't!
870Sstevel@tonic-gate 		 */
880Sstevel@tonic-gate 		if (errno == ENOTDIR)
890Sstevel@tonic-gate 			errno = ENOENT; /* printwheel doesn't exist */
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 		return (0);
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	pwheel.alert = *pa;
950Sstevel@tonic-gate 	pwheel.name = Strdup(name);
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	return (&pwheel);
980Sstevel@tonic-gate }
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate /**
1010Sstevel@tonic-gate  ** putpwheel() - PUT PRINT WHEEL INFO TO DISK
1020Sstevel@tonic-gate  **/
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate int
1050Sstevel@tonic-gate #if	defined(__STDC__)
1060Sstevel@tonic-gate putpwheel (
1070Sstevel@tonic-gate 	char *			name,
1080Sstevel@tonic-gate 	PWHEEL *		pwheelp
1090Sstevel@tonic-gate )
1100Sstevel@tonic-gate #else
1110Sstevel@tonic-gate putpwheel (name, pwheelp)
1120Sstevel@tonic-gate 	char			*name;
1130Sstevel@tonic-gate 	PWHEEL			*pwheelp;
1140Sstevel@tonic-gate #endif
1150Sstevel@tonic-gate {
1160Sstevel@tonic-gate 	register char		*path;
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate 	struct stat		statbuf;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate 	if (!name || !*name) {
1220Sstevel@tonic-gate 		errno = EINVAL;
1230Sstevel@tonic-gate 		return (-1);
1240Sstevel@tonic-gate 	}
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	if (STREQU(name, NAME_ALL)) {
1270Sstevel@tonic-gate 		errno = ENOENT;
1280Sstevel@tonic-gate 		return (-1);
1290Sstevel@tonic-gate 	}
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	/*
1320Sstevel@tonic-gate 	 * Create the parent directory for this printer
1330Sstevel@tonic-gate 	 * if it doesn't yet exist.
1340Sstevel@tonic-gate 	 */
1350Sstevel@tonic-gate 	if (!(path = makepath(Lp_A_PrintWheels, name, (char *)0)))
1360Sstevel@tonic-gate 		return (-1);
1370Sstevel@tonic-gate 	if (Stat(path, &statbuf) == 0) {
138*871Scasper 		if (!S_ISDIR(statbuf.st_mode)) {
1390Sstevel@tonic-gate 			Free (path);
1400Sstevel@tonic-gate 			errno = ENOTDIR;
1410Sstevel@tonic-gate 			return (-1);
1420Sstevel@tonic-gate 		}
1430Sstevel@tonic-gate 	} else if (errno != ENOENT || mkdir_lpdir(path, MODE_DIR) == -1) {
1440Sstevel@tonic-gate 		Free (path);
1450Sstevel@tonic-gate 		return (-1);
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 	Free (path);
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	/*
1500Sstevel@tonic-gate 	 * Now write out the alert condition.
1510Sstevel@tonic-gate 	 */
1520Sstevel@tonic-gate 	if (putalert(Lp_A_PrintWheels, name, &(pwheelp->alert)) == -1)
1530Sstevel@tonic-gate 		return (-1);
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	return (0);
1560Sstevel@tonic-gate }
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate /**
1590Sstevel@tonic-gate  ** delpwheel() - DELETE PRINT WHEEL INFO FROM DISK
1600Sstevel@tonic-gate  **/
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate #if	defined(__STDC__)
1630Sstevel@tonic-gate static int		_delpwheel ( char * );
1640Sstevel@tonic-gate #else
1650Sstevel@tonic-gate static int		_delpwheel();
1660Sstevel@tonic-gate #endif
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate int
1690Sstevel@tonic-gate #if	defined(__STDC__)
1700Sstevel@tonic-gate delpwheel (
1710Sstevel@tonic-gate 	char *			name
1720Sstevel@tonic-gate )
1730Sstevel@tonic-gate #else
1740Sstevel@tonic-gate delpwheel (name)
1750Sstevel@tonic-gate 	char			*name;
1760Sstevel@tonic-gate #endif
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate 	long			lastdir;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	if (!name || !*name) {
1820Sstevel@tonic-gate 		errno = EINVAL;
1830Sstevel@tonic-gate 		return (-1);
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	if (STREQU(NAME_ALL, name)) {
1870Sstevel@tonic-gate 		lastdir = -1;
1880Sstevel@tonic-gate 		while ((name = next_dir(Lp_A_PrintWheels, &lastdir)))
1890Sstevel@tonic-gate 			if (_delpwheel(name) == -1)
1900Sstevel@tonic-gate 				return (-1);
1910Sstevel@tonic-gate 		return (0);
1920Sstevel@tonic-gate 	} else
1930Sstevel@tonic-gate 		return (_delpwheel(name));
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate /**
1970Sstevel@tonic-gate  ** _delpwheel()
1980Sstevel@tonic-gate  **/
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate static int
2010Sstevel@tonic-gate #if	defined(__STDC__)
2020Sstevel@tonic-gate _delpwheel (
2030Sstevel@tonic-gate 	char *			name
2040Sstevel@tonic-gate )
2050Sstevel@tonic-gate #else
2060Sstevel@tonic-gate _delpwheel (name)
2070Sstevel@tonic-gate 	char			*name;
2080Sstevel@tonic-gate #endif
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate 	register char		*path;
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	if (delalert(Lp_A_PrintWheels, name) == -1)
2130Sstevel@tonic-gate 		return (-1);
2140Sstevel@tonic-gate 	if (!(path = makepath(Lp_A_PrintWheels, name, (char *)0)))
2150Sstevel@tonic-gate 		return (-1);
2160Sstevel@tonic-gate 	if (Rmdir(path)) {
2170Sstevel@tonic-gate 		Free (path);
2180Sstevel@tonic-gate 		return (-1);
2190Sstevel@tonic-gate 	}
2200Sstevel@tonic-gate 	Free (path);
2210Sstevel@tonic-gate 	return (0);
2220Sstevel@tonic-gate }
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate /**
2250Sstevel@tonic-gate  **  freepwheel() - FREE MEMORY ALLOCATED FOR PRINT WHEEL STRUCTURE
2260Sstevel@tonic-gate  **/
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate void
2290Sstevel@tonic-gate #if	defined(__STDC__)
2300Sstevel@tonic-gate freepwheel (
2310Sstevel@tonic-gate 	PWHEEL *		ppw
2320Sstevel@tonic-gate )
2330Sstevel@tonic-gate #else
2340Sstevel@tonic-gate freepwheel (ppw)
2350Sstevel@tonic-gate 	PWHEEL			*ppw;
2360Sstevel@tonic-gate #endif
2370Sstevel@tonic-gate {
2380Sstevel@tonic-gate 	if (!ppw)
2390Sstevel@tonic-gate 		return;
2400Sstevel@tonic-gate 	if (ppw->name)
2410Sstevel@tonic-gate 		Free (ppw->name);
2420Sstevel@tonic-gate 	if (ppw->alert.shcmd)
2430Sstevel@tonic-gate 		Free (ppw->alert.shcmd);
2440Sstevel@tonic-gate 	return;
2450Sstevel@tonic-gate }
246