xref: /illumos-gate/usr/src/cmd/lp/lib/printers/printwheels.c (revision 9164a50bf932130cbb5097a16f6986873ce0e6e5)
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 /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
31 
32 #include "string.h"
33 #include "errno.h"
34 #include "sys/types.h"
35 #include "stdlib.h"
36 
37 #include "lp.h"
38 #include "printers.h"
39 
40 /**
41  ** getpwheel() - GET PRINT WHEEL INFO FROM DISK
42  **/
43 
44 PWHEEL *
45 #if	defined(__STDC__)
46 getpwheel (
47 	char *			name
48 )
49 #else
50 getpwheel (name)
51 	char			*name;
52 #endif
53 {
54 	static long		lastdir		= -1;
55 
56 	PWHEEL		*pwp;
57 
58 	register FALERT		*pa;
59 
60 
61 	if (!name || !*name) {
62 		errno = EINVAL;
63 		return (0);
64 	}
65 
66 	/*
67 	 * Getting ``all''? If so, jump into the directory
68 	 * wherever we left off.
69 	 */
70 	if (STREQU(NAME_ALL, name)) {
71 		if (!(name = next_dir(Lp_A_PrintWheels, &lastdir)))
72 			return (0);
73 	} else
74 		lastdir = -1;
75 
76 	/*
77 	 * Get the information for the alert.
78 	 */
79 	if (!(pa = getalert(Lp_A_PrintWheels, name))) {
80 
81 		/*
82 		 * Unless the world has turned weird, we shouldn't
83 		 * get ENOTDIR if we're doing the ``all'' case--because
84 		 * getting here in the all case meant the printwheel
85 		 * directory exists, but ENOTDIR means it doesn't!
86 		 */
87 		if (errno == ENOTDIR)
88 			errno = ENOENT; /* printwheel doesn't exist */
89 
90 		return (0);
91 	}
92 
93 	pwp = calloc(1, sizeof (*pwp));
94 	pwp->alert = *pa;
95 	pwp->name = Strdup(name);
96 
97 	return (pwp);
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 	Free (ppw);
245 
246 	return;
247 }
248