xref: /illumos-gate/usr/src/cmd/lp/lib/printers/printwheels.c (revision 355b4669e025ff377602b6fc7caaf30dbc218371)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
32 
33 #include "string.h"
34 #include "errno.h"
35 #include "sys/types.h"
36 #include "stdlib.h"
37 
38 #include "lp.h"
39 #include "printers.h"
40 
41 /**
42  ** getpwheel() - GET PRINT WHEEL INFO FROM DISK
43  **/
44 
45 PWHEEL *
46 #if	defined(__STDC__)
47 getpwheel (
48 	char *			name
49 )
50 #else
51 getpwheel (name)
52 	char			*name;
53 #endif
54 {
55 	static long		lastdir		= -1;
56 
57 	static PWHEEL		pwheel;
58 
59 	register FALERT		*pa;
60 
61 
62 	if (!name || !*name) {
63 		errno = EINVAL;
64 		return (0);
65 	}
66 
67 	/*
68 	 * Getting ``all''? If so, jump into the directory
69 	 * wherever we left off.
70 	 */
71 	if (STREQU(NAME_ALL, name)) {
72 		if (!(name = next_dir(Lp_A_PrintWheels, &lastdir)))
73 			return (0);
74 	} else
75 		lastdir = -1;
76 
77 	/*
78 	 * Get the information for the alert.
79 	 */
80 	if (!(pa = getalert(Lp_A_PrintWheels, name))) {
81 
82 		/*
83 		 * Unless the world has turned weird, we shouldn't
84 		 * get ENOTDIR if we're doing the ``all'' case--because
85 		 * getting here in the all case meant the printwheel
86 		 * directory exists, but ENOTDIR means it doesn't!
87 		 */
88 		if (errno == ENOTDIR)
89 			errno = ENOENT; /* printwheel doesn't exist */
90 
91 		return (0);
92 	}
93 
94 	pwheel.alert = *pa;
95 	pwheel.name = Strdup(name);
96 
97 	return (&pwheel);
98 }
99 
100 /**
101  ** putpwheel() - PUT PRINT WHEEL INFO TO DISK
102  **/
103 
104 int
105 #if	defined(__STDC__)
106 putpwheel (
107 	char *			name,
108 	PWHEEL *		pwheelp
109 )
110 #else
111 putpwheel (name, pwheelp)
112 	char			*name;
113 	PWHEEL			*pwheelp;
114 #endif
115 {
116 	register char		*path;
117 
118 	struct stat		statbuf;
119 
120 
121 	if (!name || !*name) {
122 		errno = EINVAL;
123 		return (-1);
124 	}
125 
126 	if (STREQU(name, NAME_ALL)) {
127 		errno = ENOENT;
128 		return (-1);
129 	}
130 
131 	/*
132 	 * Create the parent directory for this printer
133 	 * if it doesn't yet exist.
134 	 */
135 	if (!(path = makepath(Lp_A_PrintWheels, name, (char *)0)))
136 		return (-1);
137 	if (Stat(path, &statbuf) == 0) {
138 		if (!S_ISDIR(statbuf.st_mode)) {
139 			Free (path);
140 			errno = ENOTDIR;
141 			return (-1);
142 		}
143 	} else if (errno != ENOENT || mkdir_lpdir(path, MODE_DIR) == -1) {
144 		Free (path);
145 		return (-1);
146 	}
147 	Free (path);
148 
149 	/*
150 	 * Now write out the alert condition.
151 	 */
152 	if (putalert(Lp_A_PrintWheels, name, &(pwheelp->alert)) == -1)
153 		return (-1);
154 
155 	return (0);
156 }
157 
158 /**
159  ** delpwheel() - DELETE PRINT WHEEL INFO FROM DISK
160  **/
161 
162 #if	defined(__STDC__)
163 static int		_delpwheel ( char * );
164 #else
165 static int		_delpwheel();
166 #endif
167 
168 int
169 #if	defined(__STDC__)
170 delpwheel (
171 	char *			name
172 )
173 #else
174 delpwheel (name)
175 	char			*name;
176 #endif
177 {
178 	long			lastdir;
179 
180 
181 	if (!name || !*name) {
182 		errno = EINVAL;
183 		return (-1);
184 	}
185 
186 	if (STREQU(NAME_ALL, name)) {
187 		lastdir = -1;
188 		while ((name = next_dir(Lp_A_PrintWheels, &lastdir)))
189 			if (_delpwheel(name) == -1)
190 				return (-1);
191 		return (0);
192 	} else
193 		return (_delpwheel(name));
194 }
195 
196 /**
197  ** _delpwheel()
198  **/
199 
200 static int
201 #if	defined(__STDC__)
202 _delpwheel (
203 	char *			name
204 )
205 #else
206 _delpwheel (name)
207 	char			*name;
208 #endif
209 {
210 	register char		*path;
211 
212 	if (delalert(Lp_A_PrintWheels, name) == -1)
213 		return (-1);
214 	if (!(path = makepath(Lp_A_PrintWheels, name, (char *)0)))
215 		return (-1);
216 	if (Rmdir(path)) {
217 		Free (path);
218 		return (-1);
219 	}
220 	Free (path);
221 	return (0);
222 }
223 
224 /**
225  **  freepwheel() - FREE MEMORY ALLOCATED FOR PRINT WHEEL STRUCTURE
226  **/
227 
228 void
229 #if	defined(__STDC__)
230 freepwheel (
231 	PWHEEL *		ppw
232 )
233 #else
234 freepwheel (ppw)
235 	PWHEEL			*ppw;
236 #endif
237 {
238 	if (!ppw)
239 		return;
240 	if (ppw->name)
241 		Free (ppw->name);
242 	if (ppw->alert.shcmd)
243 		Free (ppw->alert.shcmd);
244 	return;
245 }
246