xref: /titanic_51/usr/src/cmd/lp/lib/printers/printwheels.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 #ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.13	*/
27 /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
28 
29 #include "string.h"
30 #include "errno.h"
31 #include "sys/types.h"
32 #include "stdlib.h"
33 
34 #include "lp.h"
35 #include "printers.h"
36 
37 /**
38  ** getpwheel() - GET PRINT WHEEL INFO FROM DISK
39  **/
40 
41 PWHEEL *
42 #if	defined(__STDC__)
43 getpwheel (
44 	char *			name
45 )
46 #else
47 getpwheel (name)
48 	char			*name;
49 #endif
50 {
51 	static long		lastdir		= -1;
52 
53 	static PWHEEL		pwheel;
54 
55 	register FALERT		*pa;
56 
57 
58 	if (!name || !*name) {
59 		errno = EINVAL;
60 		return (0);
61 	}
62 
63 	/*
64 	 * Getting ``all''? If so, jump into the directory
65 	 * wherever we left off.
66 	 */
67 	if (STREQU(NAME_ALL, name)) {
68 		if (!(name = next_dir(Lp_A_PrintWheels, &lastdir)))
69 			return (0);
70 	} else
71 		lastdir = -1;
72 
73 	/*
74 	 * Get the information for the alert.
75 	 */
76 	if (!(pa = getalert(Lp_A_PrintWheels, name))) {
77 
78 		/*
79 		 * Unless the world has turned weird, we shouldn't
80 		 * get ENOTDIR if we're doing the ``all'' case--because
81 		 * getting here in the all case meant the printwheel
82 		 * directory exists, but ENOTDIR means it doesn't!
83 		 */
84 		if (errno == ENOTDIR)
85 			errno = ENOENT; /* printwheel doesn't exist */
86 
87 		return (0);
88 	}
89 
90 	pwheel.alert = *pa;
91 	pwheel.name = Strdup(name);
92 
93 	return (&pwheel);
94 }
95 
96 /**
97  ** putpwheel() - PUT PRINT WHEEL INFO TO DISK
98  **/
99 
100 int
101 #if	defined(__STDC__)
102 putpwheel (
103 	char *			name,
104 	PWHEEL *		pwheelp
105 )
106 #else
107 putpwheel (name, pwheelp)
108 	char			*name;
109 	PWHEEL			*pwheelp;
110 #endif
111 {
112 	register char		*path;
113 
114 	struct stat		statbuf;
115 
116 
117 	if (!name || !*name) {
118 		errno = EINVAL;
119 		return (-1);
120 	}
121 
122 	if (STREQU(name, NAME_ALL)) {
123 		errno = ENOENT;
124 		return (-1);
125 	}
126 
127 	/*
128 	 * Create the parent directory for this printer
129 	 * if it doesn't yet exist.
130 	 */
131 	if (!(path = makepath(Lp_A_PrintWheels, name, (char *)0)))
132 		return (-1);
133 	if (Stat(path, &statbuf) == 0) {
134 		if (!(statbuf.st_mode & S_IFDIR)) {
135 			Free (path);
136 			errno = ENOTDIR;
137 			return (-1);
138 		}
139 	} else if (errno != ENOENT || mkdir_lpdir(path, MODE_DIR) == -1) {
140 		Free (path);
141 		return (-1);
142 	}
143 	Free (path);
144 
145 	/*
146 	 * Now write out the alert condition.
147 	 */
148 	if (putalert(Lp_A_PrintWheels, name, &(pwheelp->alert)) == -1)
149 		return (-1);
150 
151 	return (0);
152 }
153 
154 /**
155  ** delpwheel() - DELETE PRINT WHEEL INFO FROM DISK
156  **/
157 
158 #if	defined(__STDC__)
159 static int		_delpwheel ( char * );
160 #else
161 static int		_delpwheel();
162 #endif
163 
164 int
165 #if	defined(__STDC__)
166 delpwheel (
167 	char *			name
168 )
169 #else
170 delpwheel (name)
171 	char			*name;
172 #endif
173 {
174 	long			lastdir;
175 
176 
177 	if (!name || !*name) {
178 		errno = EINVAL;
179 		return (-1);
180 	}
181 
182 	if (STREQU(NAME_ALL, name)) {
183 		lastdir = -1;
184 		while ((name = next_dir(Lp_A_PrintWheels, &lastdir)))
185 			if (_delpwheel(name) == -1)
186 				return (-1);
187 		return (0);
188 	} else
189 		return (_delpwheel(name));
190 }
191 
192 /**
193  ** _delpwheel()
194  **/
195 
196 static int
197 #if	defined(__STDC__)
198 _delpwheel (
199 	char *			name
200 )
201 #else
202 _delpwheel (name)
203 	char			*name;
204 #endif
205 {
206 	register char		*path;
207 
208 	if (delalert(Lp_A_PrintWheels, name) == -1)
209 		return (-1);
210 	if (!(path = makepath(Lp_A_PrintWheels, name, (char *)0)))
211 		return (-1);
212 	if (Rmdir(path)) {
213 		Free (path);
214 		return (-1);
215 	}
216 	Free (path);
217 	return (0);
218 }
219 
220 /**
221  **  freepwheel() - FREE MEMORY ALLOCATED FOR PRINT WHEEL STRUCTURE
222  **/
223 
224 void
225 #if	defined(__STDC__)
226 freepwheel (
227 	PWHEEL *		ppw
228 )
229 #else
230 freepwheel (ppw)
231 	PWHEEL			*ppw;
232 #endif
233 {
234 	if (!ppw)
235 		return;
236 	if (ppw->name)
237 		Free (ppw->name);
238 	if (ppw->alert.shcmd)
239 		Free (ppw->alert.shcmd);
240 	return;
241 }
242