xref: /illumos-gate/usr/src/cmd/lp/lib/printers/delprinter.c (revision e3ae4b35c024af1196582063ecee3ab79367227d)
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 /*
27  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
32 
33 #include "errno.h"
34 #include "sys/types.h"
35 #include "stdlib.h"
36 
37 #include "lp.h"
38 #include "printers.h"
39 
40 #if	defined(__STDC__)
41 static int		_delprinter ( char * );
42 #else
43 static int		_delprinter();
44 #endif
45 
46 /**
47  ** delprinter()
48  **/
49 
50 int
51 #if	defined(__STDC__)
52 delprinter (
53 	char *			name
54 )
55 #else
56 delprinter (name)
57 	char			*name;
58 #endif
59 {
60 	long			lastdir;
61 
62 
63 	if (!name || !*name) {
64 		errno = EINVAL;
65 		return (-1);
66 	}
67 
68 	if (!Lp_A_Printers || !Lp_A_Interfaces) {
69 		getadminpaths (LPUSER);
70 		if (!Lp_A_Printers || !Lp_A_Interfaces)
71 			return (0);
72 	}
73 
74 	if (STREQU(NAME_ALL, name)) {
75 		lastdir = -1;
76 		while ((name = next_dir(Lp_A_Printers, &lastdir)))
77 			if (_delprinter(name) == -1)
78 				return (-1);
79 		return (0);
80 	} else
81 		return (_delprinter(name));
82 }
83 
84 /**
85  ** _delprinter()
86  **/
87 
88 static int
89 #if	defined(__STDC__)
90 _delprinter (
91 	char *			name
92 )
93 #else
94 _delprinter (name)
95 	char			*name;
96 #endif
97 {
98 	register char		*path;
99 #ifdef LP_USE_PAPI_ATTR
100 	char			ppdfile[BUFSIZ];
101 #endif
102 
103 #define RMFILE(X)	if (!(path = getprinterfile(name, X))) \
104 				return (-1); \
105 			if (rmfile(path) == -1) { \
106 				Free (path); \
107 				return (-1); \
108 			} \
109 			Free (path)
110 	RMFILE (COMMENTFILE);
111 	RMFILE (CONFIGFILE);
112 	RMFILE (FALLOWFILE);
113 	RMFILE (FDENYFILE);
114 	RMFILE (UALLOWFILE);
115 	RMFILE (UDENYFILE);
116 	RMFILE (STATUSFILE);
117 	RMFILE (FAULTMESSAGEFILE);
118 
119 	delalert (Lp_A_Printers, name);
120 
121 	if (!(path = makepath(Lp_A_Interfaces, name, (char *)0)))
122 		return (-1);
123 	if (rmfile(path) == -1) {
124 		Free (path);
125 		return (-1);
126 	}
127 	Free (path);
128 
129 #ifdef LP_USE_PAPI_ATTR
130 	/* Check if the printer has a ppd file, if it does delete it */
131 	(void) snprintf(ppdfile, sizeof (ppdfile), "%s.ppd", name);
132 
133 	if (!(path = makepath(ETCDIR, "ppd", ppdfile, (char *)0)))
134 	{
135 		return (-1);
136 	}
137 	if (rmfile(path) == -1)
138 	{
139 		Free(path);
140 		return (-1);
141 	}
142 	Free(path);
143 #endif
144 
145 	if (!(path = getprinterfile(name, (char *)0)))
146 		return (-1);
147 	if (Rmdir(path) == -1) {
148 		Free (path);
149 		return (-1);
150 	}
151 	Free (path);
152 
153 	return (0);
154 }
155