17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0a44ef6dSjacobs  * Common Development and Distribution License (the "License").
6*0a44ef6dSjacobs  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*0a44ef6dSjacobs 
224bc0a2efScasper /*
23*0a44ef6dSjacobs  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
244bc0a2efScasper  * Use is subject to license terms.
254bc0a2efScasper  */
26*0a44ef6dSjacobs 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include "string.h"
337c478bd9Sstevel@tonic-gate #include "errno.h"
347c478bd9Sstevel@tonic-gate #include "sys/types.h"
357c478bd9Sstevel@tonic-gate #include "stdlib.h"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include "lp.h"
387c478bd9Sstevel@tonic-gate #include "printers.h"
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /**
417c478bd9Sstevel@tonic-gate  ** getpwheel() - GET PRINT WHEEL INFO FROM DISK
427c478bd9Sstevel@tonic-gate  **/
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate PWHEEL *
457c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
getpwheel(char * name)467c478bd9Sstevel@tonic-gate getpwheel (
477c478bd9Sstevel@tonic-gate 	char *			name
487c478bd9Sstevel@tonic-gate )
497c478bd9Sstevel@tonic-gate #else
507c478bd9Sstevel@tonic-gate getpwheel (name)
517c478bd9Sstevel@tonic-gate 	char			*name;
527c478bd9Sstevel@tonic-gate #endif
537c478bd9Sstevel@tonic-gate {
547c478bd9Sstevel@tonic-gate 	static long		lastdir		= -1;
557c478bd9Sstevel@tonic-gate 
56*0a44ef6dSjacobs 	PWHEEL		*pwp;
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate 	register FALERT		*pa;
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	if (!name || !*name) {
627c478bd9Sstevel@tonic-gate 		errno = EINVAL;
637c478bd9Sstevel@tonic-gate 		return (0);
647c478bd9Sstevel@tonic-gate 	}
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	/*
677c478bd9Sstevel@tonic-gate 	 * Getting ``all''? If so, jump into the directory
687c478bd9Sstevel@tonic-gate 	 * wherever we left off.
697c478bd9Sstevel@tonic-gate 	 */
707c478bd9Sstevel@tonic-gate 	if (STREQU(NAME_ALL, name)) {
717c478bd9Sstevel@tonic-gate 		if (!(name = next_dir(Lp_A_PrintWheels, &lastdir)))
727c478bd9Sstevel@tonic-gate 			return (0);
737c478bd9Sstevel@tonic-gate 	} else
747c478bd9Sstevel@tonic-gate 		lastdir = -1;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	/*
777c478bd9Sstevel@tonic-gate 	 * Get the information for the alert.
787c478bd9Sstevel@tonic-gate 	 */
797c478bd9Sstevel@tonic-gate 	if (!(pa = getalert(Lp_A_PrintWheels, name))) {
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 		/*
827c478bd9Sstevel@tonic-gate 		 * Unless the world has turned weird, we shouldn't
837c478bd9Sstevel@tonic-gate 		 * get ENOTDIR if we're doing the ``all'' case--because
847c478bd9Sstevel@tonic-gate 		 * getting here in the all case meant the printwheel
857c478bd9Sstevel@tonic-gate 		 * directory exists, but ENOTDIR means it doesn't!
867c478bd9Sstevel@tonic-gate 		 */
877c478bd9Sstevel@tonic-gate 		if (errno == ENOTDIR)
887c478bd9Sstevel@tonic-gate 			errno = ENOENT; /* printwheel doesn't exist */
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 		return (0);
917c478bd9Sstevel@tonic-gate 	}
927c478bd9Sstevel@tonic-gate 
93*0a44ef6dSjacobs 	pwp = calloc(1, sizeof (*pwp));
94*0a44ef6dSjacobs 	pwp->alert = *pa;
95*0a44ef6dSjacobs 	pwp->name = Strdup(name);
967c478bd9Sstevel@tonic-gate 
97*0a44ef6dSjacobs 	return (pwp);
987c478bd9Sstevel@tonic-gate }
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /**
1017c478bd9Sstevel@tonic-gate  ** putpwheel() - PUT PRINT WHEEL INFO TO DISK
1027c478bd9Sstevel@tonic-gate  **/
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate int
1057c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
putpwheel(char * name,PWHEEL * pwheelp)1067c478bd9Sstevel@tonic-gate putpwheel (
1077c478bd9Sstevel@tonic-gate 	char *			name,
1087c478bd9Sstevel@tonic-gate 	PWHEEL *		pwheelp
1097c478bd9Sstevel@tonic-gate )
1107c478bd9Sstevel@tonic-gate #else
1117c478bd9Sstevel@tonic-gate putpwheel (name, pwheelp)
1127c478bd9Sstevel@tonic-gate 	char			*name;
1137c478bd9Sstevel@tonic-gate 	PWHEEL			*pwheelp;
1147c478bd9Sstevel@tonic-gate #endif
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	register char		*path;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	struct stat		statbuf;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if (!name || !*name) {
1227c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1237c478bd9Sstevel@tonic-gate 		return (-1);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	if (STREQU(name, NAME_ALL)) {
1277c478bd9Sstevel@tonic-gate 		errno = ENOENT;
1287c478bd9Sstevel@tonic-gate 		return (-1);
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	/*
1327c478bd9Sstevel@tonic-gate 	 * Create the parent directory for this printer
1337c478bd9Sstevel@tonic-gate 	 * if it doesn't yet exist.
1347c478bd9Sstevel@tonic-gate 	 */
1357c478bd9Sstevel@tonic-gate 	if (!(path = makepath(Lp_A_PrintWheels, name, (char *)0)))
1367c478bd9Sstevel@tonic-gate 		return (-1);
1377c478bd9Sstevel@tonic-gate 	if (Stat(path, &statbuf) == 0) {
1384bc0a2efScasper 		if (!S_ISDIR(statbuf.st_mode)) {
1397c478bd9Sstevel@tonic-gate 			Free (path);
1407c478bd9Sstevel@tonic-gate 			errno = ENOTDIR;
1417c478bd9Sstevel@tonic-gate 			return (-1);
1427c478bd9Sstevel@tonic-gate 		}
1437c478bd9Sstevel@tonic-gate 	} else if (errno != ENOENT || mkdir_lpdir(path, MODE_DIR) == -1) {
1447c478bd9Sstevel@tonic-gate 		Free (path);
1457c478bd9Sstevel@tonic-gate 		return (-1);
1467c478bd9Sstevel@tonic-gate 	}
1477c478bd9Sstevel@tonic-gate 	Free (path);
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	/*
1507c478bd9Sstevel@tonic-gate 	 * Now write out the alert condition.
1517c478bd9Sstevel@tonic-gate 	 */
1527c478bd9Sstevel@tonic-gate 	if (putalert(Lp_A_PrintWheels, name, &(pwheelp->alert)) == -1)
1537c478bd9Sstevel@tonic-gate 		return (-1);
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	return (0);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate /**
1597c478bd9Sstevel@tonic-gate  ** delpwheel() - DELETE PRINT WHEEL INFO FROM DISK
1607c478bd9Sstevel@tonic-gate  **/
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
1637c478bd9Sstevel@tonic-gate static int		_delpwheel ( char * );
1647c478bd9Sstevel@tonic-gate #else
1657c478bd9Sstevel@tonic-gate static int		_delpwheel();
1667c478bd9Sstevel@tonic-gate #endif
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate int
1697c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
delpwheel(char * name)1707c478bd9Sstevel@tonic-gate delpwheel (
1717c478bd9Sstevel@tonic-gate 	char *			name
1727c478bd9Sstevel@tonic-gate )
1737c478bd9Sstevel@tonic-gate #else
1747c478bd9Sstevel@tonic-gate delpwheel (name)
1757c478bd9Sstevel@tonic-gate 	char			*name;
1767c478bd9Sstevel@tonic-gate #endif
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	long			lastdir;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	if (!name || !*name) {
1827c478bd9Sstevel@tonic-gate 		errno = EINVAL;
1837c478bd9Sstevel@tonic-gate 		return (-1);
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if (STREQU(NAME_ALL, name)) {
1877c478bd9Sstevel@tonic-gate 		lastdir = -1;
1887c478bd9Sstevel@tonic-gate 		while ((name = next_dir(Lp_A_PrintWheels, &lastdir)))
1897c478bd9Sstevel@tonic-gate 			if (_delpwheel(name) == -1)
1907c478bd9Sstevel@tonic-gate 				return (-1);
1917c478bd9Sstevel@tonic-gate 		return (0);
1927c478bd9Sstevel@tonic-gate 	} else
1937c478bd9Sstevel@tonic-gate 		return (_delpwheel(name));
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /**
1977c478bd9Sstevel@tonic-gate  ** _delpwheel()
1987c478bd9Sstevel@tonic-gate  **/
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate static int
2017c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
_delpwheel(char * name)2027c478bd9Sstevel@tonic-gate _delpwheel (
2037c478bd9Sstevel@tonic-gate 	char *			name
2047c478bd9Sstevel@tonic-gate )
2057c478bd9Sstevel@tonic-gate #else
2067c478bd9Sstevel@tonic-gate _delpwheel (name)
2077c478bd9Sstevel@tonic-gate 	char			*name;
2087c478bd9Sstevel@tonic-gate #endif
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate 	register char		*path;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	if (delalert(Lp_A_PrintWheels, name) == -1)
2137c478bd9Sstevel@tonic-gate 		return (-1);
2147c478bd9Sstevel@tonic-gate 	if (!(path = makepath(Lp_A_PrintWheels, name, (char *)0)))
2157c478bd9Sstevel@tonic-gate 		return (-1);
2167c478bd9Sstevel@tonic-gate 	if (Rmdir(path)) {
2177c478bd9Sstevel@tonic-gate 		Free (path);
2187c478bd9Sstevel@tonic-gate 		return (-1);
2197c478bd9Sstevel@tonic-gate 	}
2207c478bd9Sstevel@tonic-gate 	Free (path);
2217c478bd9Sstevel@tonic-gate 	return (0);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate /**
2257c478bd9Sstevel@tonic-gate  **  freepwheel() - FREE MEMORY ALLOCATED FOR PRINT WHEEL STRUCTURE
2267c478bd9Sstevel@tonic-gate  **/
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate void
2297c478bd9Sstevel@tonic-gate #if	defined(__STDC__)
freepwheel(PWHEEL * ppw)2307c478bd9Sstevel@tonic-gate freepwheel (
2317c478bd9Sstevel@tonic-gate 	PWHEEL *		ppw
2327c478bd9Sstevel@tonic-gate )
2337c478bd9Sstevel@tonic-gate #else
2347c478bd9Sstevel@tonic-gate freepwheel (ppw)
2357c478bd9Sstevel@tonic-gate 	PWHEEL			*ppw;
2367c478bd9Sstevel@tonic-gate #endif
2377c478bd9Sstevel@tonic-gate {
2387c478bd9Sstevel@tonic-gate 	if (!ppw)
2397c478bd9Sstevel@tonic-gate 		return;
2407c478bd9Sstevel@tonic-gate 	if (ppw->name)
2417c478bd9Sstevel@tonic-gate 		Free (ppw->name);
2427c478bd9Sstevel@tonic-gate 	if (ppw->alert.shcmd)
2437c478bd9Sstevel@tonic-gate 		Free (ppw->alert.shcmd);
244*0a44ef6dSjacobs 	Free (ppw);
245*0a44ef6dSjacobs 
2467c478bd9Sstevel@tonic-gate 	return;
2477c478bd9Sstevel@tonic-gate }
248