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