xref: /onnv-gate/usr/src/cmd/lp/lib/lp/alerts.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.17	*/
32*0Sstevel@tonic-gate /* EMACS_MODES: !fill, lnumb, !overwrite, !nodelete, !picture */
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include "stdio.h"
35*0Sstevel@tonic-gate #include "string.h"
36*0Sstevel@tonic-gate #include "errno.h"
37*0Sstevel@tonic-gate #include "limits.h"
38*0Sstevel@tonic-gate #include "unistd.h"
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #include "lp.h"
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate extern char		**environ;
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate static void		envlist(int, char **);
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate /*
47*0Sstevel@tonic-gate  * We recognize the following key phrases in the alert prototype
48*0Sstevel@tonic-gate  * file, and replace them with appropriate values.
49*0Sstevel@tonic-gate  */
50*0Sstevel@tonic-gate #define NALRT_KEYS	7
51*0Sstevel@tonic-gate # define ALRT_ENV		0
52*0Sstevel@tonic-gate # define ALRT_PWD		1
53*0Sstevel@tonic-gate # define ALRT_ULIMIT		2
54*0Sstevel@tonic-gate # define ALRT_UMASK		3
55*0Sstevel@tonic-gate # define ALRT_INTERVAL		4
56*0Sstevel@tonic-gate # define ALRT_CMD		5
57*0Sstevel@tonic-gate # define ALRT_USER		6
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate static struct {
60*0Sstevel@tonic-gate 	char			*v;
61*0Sstevel@tonic-gate 	short			len;
62*0Sstevel@tonic-gate }			shell_keys[NALRT_KEYS] = {
63*0Sstevel@tonic-gate #define	ENTRY(X)	X, sizeof(X)-1
64*0Sstevel@tonic-gate 	ENTRY("-ENVIRONMENT-"),
65*0Sstevel@tonic-gate 	ENTRY("-PWD-"),
66*0Sstevel@tonic-gate 	ENTRY("-ULIMIT-"),
67*0Sstevel@tonic-gate 	ENTRY("-UMASK-"),
68*0Sstevel@tonic-gate 	ENTRY("-INTERVAL-"),
69*0Sstevel@tonic-gate 	ENTRY("-CMD-"),
70*0Sstevel@tonic-gate 	ENTRY("-USER-"),
71*0Sstevel@tonic-gate };
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate /*
74*0Sstevel@tonic-gate  * These are used to bracket the administrator's command, so that
75*0Sstevel@tonic-gate  * we can find it easily. We're out of luck if the administrator
76*0Sstevel@tonic-gate  * includes an identical phrase in his or her command.
77*0Sstevel@tonic-gate  */
78*0Sstevel@tonic-gate #define ALRT_CMDSTART "## YOUR COMMAND STARTS HERE -- DON'T TOUCH ABOVE!!"
79*0Sstevel@tonic-gate #define ALRT_CMDEND   "## YOUR COMMAND ENDS HERE -- DON'T TOUCH BELOW!!"
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate /**
82*0Sstevel@tonic-gate  ** putalert() - WRITE ALERT TO FILES
83*0Sstevel@tonic-gate  **/
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate int
putalert(char * parent,char * name,FALERT * alertp)86*0Sstevel@tonic-gate putalert(char *parent, char *name, FALERT *alertp)
87*0Sstevel@tonic-gate {
88*0Sstevel@tonic-gate 	char			*path,
89*0Sstevel@tonic-gate 				cur_dir[PATH_MAX + 1],
90*0Sstevel@tonic-gate 				buf[BUFSIZ];
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	int			cur_umask;
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	int fdout, fdin;
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	if (!parent || !*parent || !name || !*name) {
98*0Sstevel@tonic-gate 		errno = EINVAL;
99*0Sstevel@tonic-gate 		return (-1);
100*0Sstevel@tonic-gate 	}
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	if (!alertp->shcmd) {
103*0Sstevel@tonic-gate 		errno = EINVAL;
104*0Sstevel@tonic-gate 		return (-1);
105*0Sstevel@tonic-gate 	}
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	if (STREQU(alertp->shcmd, NAME_NONE))
108*0Sstevel@tonic-gate 		return (delalert(parent, name));
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	/*
111*0Sstevel@tonic-gate 	 * See if the form/printer/print-wheel exists.
112*0Sstevel@tonic-gate 	 */
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate 	if (!(path = makepath(parent, name, (char *)0)))
115*0Sstevel@tonic-gate 		return (-1);
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate 	if (Access(path, F_OK) == -1) {
118*0Sstevel@tonic-gate 		if (errno == ENOENT)
119*0Sstevel@tonic-gate 			errno = ENOTDIR; /* not quite, but what else? */
120*0Sstevel@tonic-gate 		Free (path);
121*0Sstevel@tonic-gate 		return (-1);
122*0Sstevel@tonic-gate 	}
123*0Sstevel@tonic-gate 	Free (path);
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	/*
126*0Sstevel@tonic-gate 	 * First, the shell command file.
127*0Sstevel@tonic-gate 	 */
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	if (!(path = makepath(parent, name, ALERTSHFILE, (char *)0)))
130*0Sstevel@tonic-gate 		return (-1);
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	if ((fdout = open_locked(path, "w", MODE_NOEXEC)) < 0) {
133*0Sstevel@tonic-gate 		Free (path);
134*0Sstevel@tonic-gate 		return (-1);
135*0Sstevel@tonic-gate 	}
136*0Sstevel@tonic-gate 	Free (path);
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	/*
139*0Sstevel@tonic-gate 	 * We use a prototype file to build the shell command,
140*0Sstevel@tonic-gate 	 * so that the alerts are easily customized. The shell
141*0Sstevel@tonic-gate 	 * is expected to handle repeat alerts and failed alerts,
142*0Sstevel@tonic-gate 	 * because the Spooler doesn't. Also, the Spooler runs
143*0Sstevel@tonic-gate 	 * each alert with the UID and GID of the administrator
144*0Sstevel@tonic-gate 	 * who defined the alert. Otherwise, anything goes.
145*0Sstevel@tonic-gate 	 */
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate 	if (!Lp_Bin) {
148*0Sstevel@tonic-gate 		getpaths ();
149*0Sstevel@tonic-gate 		if (!Lp_Bin)
150*0Sstevel@tonic-gate 			return (-1);
151*0Sstevel@tonic-gate 	}
152*0Sstevel@tonic-gate 	if (!(path = makepath(Lp_Bin, ALERTPROTOFILE, (char *)0)))
153*0Sstevel@tonic-gate 		return (-1);
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 	if ((fdin = open_locked(path, "r", 0)) < 0) {
156*0Sstevel@tonic-gate 		Free (path);
157*0Sstevel@tonic-gate 		return (-1);
158*0Sstevel@tonic-gate 	}
159*0Sstevel@tonic-gate 	Free (path);
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 	errno = 0;
162*0Sstevel@tonic-gate 	while (fdgets(buf, BUFSIZ, fdin)) {
163*0Sstevel@tonic-gate 		int			key;
164*0Sstevel@tonic-gate 		char			*cp,
165*0Sstevel@tonic-gate 					*dash;
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 		cp = buf;
168*0Sstevel@tonic-gate 		while ((dash = strchr(cp, '-'))) {
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 		    *dash = 0;
171*0Sstevel@tonic-gate 		    fdputs (cp, fdout);
172*0Sstevel@tonic-gate 		    *(cp = dash) = '-';
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 		    for (key = 0; key < NALRT_KEYS; key++)
175*0Sstevel@tonic-gate 			if (STRNEQU(
176*0Sstevel@tonic-gate 				cp,
177*0Sstevel@tonic-gate 				shell_keys[key].v,
178*0Sstevel@tonic-gate 				shell_keys[key].len
179*0Sstevel@tonic-gate 			)) {
180*0Sstevel@tonic-gate 				register char	*newline =
181*0Sstevel@tonic-gate 						(cp != buf)? "\n" : "";
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 				cp += shell_keys[key].len;
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 				switch (key) {
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 				case ALRT_ENV:
188*0Sstevel@tonic-gate 					fdprintf(fdout, newline);
189*0Sstevel@tonic-gate 					envlist(fdout, environ);
190*0Sstevel@tonic-gate 					break;
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 				case ALRT_PWD:
193*0Sstevel@tonic-gate 					getcwd (cur_dir, PATH_MAX);
194*0Sstevel@tonic-gate 					fdprintf (fdout, "%s", cur_dir);
195*0Sstevel@tonic-gate 					break;
196*0Sstevel@tonic-gate 
197*0Sstevel@tonic-gate 				case ALRT_ULIMIT:
198*0Sstevel@tonic-gate 					fdprintf (fdout, "%ld", ulimit(1, (long)0));
199*0Sstevel@tonic-gate 					break;
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 				case ALRT_UMASK:
202*0Sstevel@tonic-gate 					umask (cur_umask = umask(0));
203*0Sstevel@tonic-gate 					fdprintf (fdout, "%03o", cur_umask);
204*0Sstevel@tonic-gate 					break;
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 				case ALRT_INTERVAL:
207*0Sstevel@tonic-gate 					fdprintf(fdout, "%ld", (long)alertp->W);
208*0Sstevel@tonic-gate 					break;
209*0Sstevel@tonic-gate 
210*0Sstevel@tonic-gate 				case ALRT_CMD:
211*0Sstevel@tonic-gate 					fdprintf(fdout, newline);
212*0Sstevel@tonic-gate 					fdprintf(fdout, "%s\n", ALRT_CMDSTART);
213*0Sstevel@tonic-gate 					fdprintf(fdout, "%s\n", alertp->shcmd);
214*0Sstevel@tonic-gate 					fdprintf(fdout, "%s\n", ALRT_CMDEND);
215*0Sstevel@tonic-gate 					break;
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate 				case ALRT_USER:
218*0Sstevel@tonic-gate 					fdprintf(fdout, "%s", getname());
219*0Sstevel@tonic-gate 					break;
220*0Sstevel@tonic-gate 
221*0Sstevel@tonic-gate 				}
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 				break;
224*0Sstevel@tonic-gate 			}
225*0Sstevel@tonic-gate 		    if (key >= NALRT_KEYS)
226*0Sstevel@tonic-gate 			fdputc(*cp++, fdout);
227*0Sstevel@tonic-gate 
228*0Sstevel@tonic-gate 		}
229*0Sstevel@tonic-gate 		fdputs(cp, fdout);
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 	}
232*0Sstevel@tonic-gate 	if (errno != 0) {
233*0Sstevel@tonic-gate 		int			save_errno = errno;
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 		close(fdin);
236*0Sstevel@tonic-gate 		close(fdout);
237*0Sstevel@tonic-gate 		errno = save_errno;
238*0Sstevel@tonic-gate 		return (-1);
239*0Sstevel@tonic-gate 	}
240*0Sstevel@tonic-gate 	close(fdin);
241*0Sstevel@tonic-gate 	close(fdout);
242*0Sstevel@tonic-gate 
243*0Sstevel@tonic-gate 	/*
244*0Sstevel@tonic-gate 	 * Next, the variables file.
245*0Sstevel@tonic-gate 	 */
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	if (!(path = makepath(parent, name, ALERTVARSFILE, (char *)0)))
248*0Sstevel@tonic-gate 		return (-1);
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	if ((fdout = open_locked(path, "w", MODE_NOREAD)) < 0) {
251*0Sstevel@tonic-gate 		Free (path);
252*0Sstevel@tonic-gate 		return (-1);
253*0Sstevel@tonic-gate 	}
254*0Sstevel@tonic-gate 	Free (path);
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 	fdprintf(fdout, "%d\n", alertp->Q > 0? alertp->Q : 1);
257*0Sstevel@tonic-gate 	fdprintf(fdout, "%d\n", alertp->W >= 0? alertp->W : 0);
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate 	close(fdout);
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 	return (0);
262*0Sstevel@tonic-gate }
263*0Sstevel@tonic-gate 
264*0Sstevel@tonic-gate /**
265*0Sstevel@tonic-gate  ** getalert() - EXTRACT ALERT FROM FILES
266*0Sstevel@tonic-gate  **/
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate FALERT *
getalert(char * parent,char * name)269*0Sstevel@tonic-gate getalert(char *parent, char *name)
270*0Sstevel@tonic-gate {
271*0Sstevel@tonic-gate 	int fd;
272*0Sstevel@tonic-gate 	char *tmp;
273*0Sstevel@tonic-gate 	static FALERT		alert;
274*0Sstevel@tonic-gate 	register char		*path;
275*0Sstevel@tonic-gate 	char			buf[BUFSIZ];
276*0Sstevel@tonic-gate 	int			len;
277*0Sstevel@tonic-gate 
278*0Sstevel@tonic-gate 	if (!parent || !*parent || !name || !*name) {
279*0Sstevel@tonic-gate 		errno = EINVAL;
280*0Sstevel@tonic-gate 		return (0);
281*0Sstevel@tonic-gate 	}
282*0Sstevel@tonic-gate 
283*0Sstevel@tonic-gate 	/*
284*0Sstevel@tonic-gate 	 * See if the form/printer/print-wheel exists.
285*0Sstevel@tonic-gate 	 */
286*0Sstevel@tonic-gate 
287*0Sstevel@tonic-gate 	if (!(path = makepath(parent, name, (char *)0)))
288*0Sstevel@tonic-gate 		return (0);
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 	if (Access(path, F_OK) == -1) {
291*0Sstevel@tonic-gate 		if (errno == ENOENT)
292*0Sstevel@tonic-gate 			errno = ENOTDIR; /* not quite, but what else? */
293*0Sstevel@tonic-gate 		Free (path);
294*0Sstevel@tonic-gate 		return (0);
295*0Sstevel@tonic-gate 	}
296*0Sstevel@tonic-gate 	Free (path);
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate 	/*
299*0Sstevel@tonic-gate 	 * First, the shell command file.
300*0Sstevel@tonic-gate 	 */
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 	if (!(path = makepath(parent, name, ALERTSHFILE, (char *)0)))
303*0Sstevel@tonic-gate 		return (0);
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate 	if ((fd = open_locked(path, "r", 0)) < 0) {
306*0Sstevel@tonic-gate 		Free (path);
307*0Sstevel@tonic-gate 		return (0);
308*0Sstevel@tonic-gate 	}
309*0Sstevel@tonic-gate 	Free (path);
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate 	/*
312*0Sstevel@tonic-gate 	 * Skip over environment setting stuff, while loop, etc.,
313*0Sstevel@tonic-gate 	 * to find the beginning of the command.
314*0Sstevel@tonic-gate 	 */
315*0Sstevel@tonic-gate 	errno = 0;
316*0Sstevel@tonic-gate 	while ((tmp =  fdgets(buf, BUFSIZ, fd)) &&
317*0Sstevel@tonic-gate 		!STRNEQU(buf, ALRT_CMDSTART, sizeof(ALRT_CMDSTART)-1))
318*0Sstevel@tonic-gate 		;
319*0Sstevel@tonic-gate 	if ((tmp == NULL) || (errno != 0)) {
320*0Sstevel@tonic-gate 		int			save_errno = errno;
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate 		close(fd);
323*0Sstevel@tonic-gate 		errno = save_errno;
324*0Sstevel@tonic-gate 		return (0);
325*0Sstevel@tonic-gate 	}
326*0Sstevel@tonic-gate 
327*0Sstevel@tonic-gate 	alert.shcmd = sop_up_rest(fd, ALRT_CMDEND);
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate 	close(fd);
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 	if (!alert.shcmd)
332*0Sstevel@tonic-gate 		return (0);
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate 	/*
335*0Sstevel@tonic-gate 	 * Drop terminating newline.
336*0Sstevel@tonic-gate 	 */
337*0Sstevel@tonic-gate 	if (alert.shcmd[(len = strlen(alert.shcmd)) - 1] == '\n')
338*0Sstevel@tonic-gate 		alert.shcmd[len - 1] = 0;
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 	/*
342*0Sstevel@tonic-gate 	 * Next, the variables file.
343*0Sstevel@tonic-gate 	 */
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 	if (!(path = makepath(parent, name, ALERTVARSFILE, (char *)0)))
346*0Sstevel@tonic-gate 		return (0);
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 	if ((fd = open_locked(path, "r", 0)) < 0) {
349*0Sstevel@tonic-gate 		Free (path);
350*0Sstevel@tonic-gate 		return (0);
351*0Sstevel@tonic-gate 	}
352*0Sstevel@tonic-gate 	Free (path);
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 	errno = 0;
355*0Sstevel@tonic-gate 	(void)fdgets (buf, BUFSIZ, fd);
356*0Sstevel@tonic-gate 	if (errno != 0) {
357*0Sstevel@tonic-gate 		int			save_errno = errno;
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 		close(fd);
360*0Sstevel@tonic-gate 		errno = save_errno;
361*0Sstevel@tonic-gate 		return (0);
362*0Sstevel@tonic-gate 	}
363*0Sstevel@tonic-gate 	alert.Q = atoi(buf);
364*0Sstevel@tonic-gate 
365*0Sstevel@tonic-gate 	(void)fdgets (buf, BUFSIZ, fd);
366*0Sstevel@tonic-gate 	if (errno != 0) {
367*0Sstevel@tonic-gate 		int			save_errno = errno;
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate 		close(fd);
370*0Sstevel@tonic-gate 		errno = save_errno;
371*0Sstevel@tonic-gate 		return (0);
372*0Sstevel@tonic-gate 	}
373*0Sstevel@tonic-gate 	alert.W = atoi(buf);
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 	close(fd);
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 	return (&alert);
378*0Sstevel@tonic-gate }
379*0Sstevel@tonic-gate 
380*0Sstevel@tonic-gate /**
381*0Sstevel@tonic-gate  ** delalert() - DELETE ALERT FILES
382*0Sstevel@tonic-gate  **/
383*0Sstevel@tonic-gate 
384*0Sstevel@tonic-gate int
delalert(char * parent,char * name)385*0Sstevel@tonic-gate delalert(char *parent, char *name)
386*0Sstevel@tonic-gate {
387*0Sstevel@tonic-gate 	char			*path;
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate 
390*0Sstevel@tonic-gate 	if (!parent || !*parent || !name || !*name) {
391*0Sstevel@tonic-gate 		errno = EINVAL;
392*0Sstevel@tonic-gate 		return (-1);
393*0Sstevel@tonic-gate 	}
394*0Sstevel@tonic-gate 
395*0Sstevel@tonic-gate 	/*
396*0Sstevel@tonic-gate 	 * See if the form/printer/print-wheel exists.
397*0Sstevel@tonic-gate 	 */
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate 	if (!(path = makepath(parent, name, (char *)0)))
400*0Sstevel@tonic-gate 		return (-1);
401*0Sstevel@tonic-gate 
402*0Sstevel@tonic-gate 	if (Access(path, F_OK) == -1) {
403*0Sstevel@tonic-gate 		if (errno == ENOENT)
404*0Sstevel@tonic-gate 			errno = ENOTDIR; /* not quite, but what else? */
405*0Sstevel@tonic-gate 		Free (path);
406*0Sstevel@tonic-gate 		return (-1);
407*0Sstevel@tonic-gate 	}
408*0Sstevel@tonic-gate 	Free (path);
409*0Sstevel@tonic-gate 
410*0Sstevel@tonic-gate 	/*
411*0Sstevel@tonic-gate 	 * Remove the two files.
412*0Sstevel@tonic-gate 	 */
413*0Sstevel@tonic-gate 
414*0Sstevel@tonic-gate 	if (!(path = makepath(parent, name, ALERTSHFILE, (char *)0)))
415*0Sstevel@tonic-gate 		return (-1);
416*0Sstevel@tonic-gate 	if (rmfile(path) == -1) {
417*0Sstevel@tonic-gate 		Free (path);
418*0Sstevel@tonic-gate 		return (-1);
419*0Sstevel@tonic-gate 	}
420*0Sstevel@tonic-gate 	Free (path);
421*0Sstevel@tonic-gate 
422*0Sstevel@tonic-gate 	if (!(path = makepath(parent, name, ALERTVARSFILE, (char *)0)))
423*0Sstevel@tonic-gate 		return (-1);
424*0Sstevel@tonic-gate 	if (rmfile(path) == -1) {
425*0Sstevel@tonic-gate 		Free (path);
426*0Sstevel@tonic-gate 		return (-1);
427*0Sstevel@tonic-gate 	}
428*0Sstevel@tonic-gate 	Free (path);
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate 	return (0);
431*0Sstevel@tonic-gate }
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate /**
434*0Sstevel@tonic-gate  ** envlist() - PRINT OUT ENVIRONMENT LIST SAFELY
435*0Sstevel@tonic-gate  **/
436*0Sstevel@tonic-gate 
437*0Sstevel@tonic-gate static void
envlist(int fd,char ** list)438*0Sstevel@tonic-gate envlist(int fd, char **list)
439*0Sstevel@tonic-gate {
440*0Sstevel@tonic-gate 	register char		*env,
441*0Sstevel@tonic-gate 				*value;
442*0Sstevel@tonic-gate 
443*0Sstevel@tonic-gate 	if (!list || !*list)
444*0Sstevel@tonic-gate 		return;
445*0Sstevel@tonic-gate 
446*0Sstevel@tonic-gate 	while ((env = *list++)) {
447*0Sstevel@tonic-gate 		if (!(value = strchr(env, '=')))
448*0Sstevel@tonic-gate 			continue;
449*0Sstevel@tonic-gate 		*value++ = 0;
450*0Sstevel@tonic-gate 		if (!strchr(value, '\''))
451*0Sstevel@tonic-gate 			fdprintf(fd, (char *)gettext("export %s; %s='%s'\n"),
452*0Sstevel@tonic-gate 				env, env, value);
453*0Sstevel@tonic-gate 		*--value = '=';
454*0Sstevel@tonic-gate 	}
455*0Sstevel@tonic-gate }
456*0Sstevel@tonic-gate 
457*0Sstevel@tonic-gate /*
458*0Sstevel@tonic-gate  * printalert() - PRINT ALERT DESCRIPTION
459*0Sstevel@tonic-gate  *
460*0Sstevel@tonic-gate  * This is not used in the scheduler, so we don't need to switch to using
461*0Sstevel@tonic-gate  * file descriptors for scalability.
462*0Sstevel@tonic-gate  */
463*0Sstevel@tonic-gate 
464*0Sstevel@tonic-gate void
printalert(FILE * fp,FALERT * alertp,int isfault)465*0Sstevel@tonic-gate printalert(FILE *fp, FALERT *alertp, int isfault)
466*0Sstevel@tonic-gate {
467*0Sstevel@tonic-gate 	if (!alertp->shcmd) {
468*0Sstevel@tonic-gate 		if (isfault)
469*0Sstevel@tonic-gate 			(void)fprintf (fp, (char *)gettext("On fault: no alert\n"));
470*0Sstevel@tonic-gate 		else
471*0Sstevel@tonic-gate 			(void)fprintf (fp, (char *)gettext("No alert\n"));
472*0Sstevel@tonic-gate 
473*0Sstevel@tonic-gate 	} else {
474*0Sstevel@tonic-gate 		register char	*copy = Strdup(alertp->shcmd),
475*0Sstevel@tonic-gate 				*cp;
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate 		if (isfault)
478*0Sstevel@tonic-gate 			(void)fprintf (fp, (char *)gettext("On fault: "));
479*0Sstevel@tonic-gate 		else
480*0Sstevel@tonic-gate 			if (alertp->Q > 1)
481*0Sstevel@tonic-gate 				(void)fprintf (
482*0Sstevel@tonic-gate 					fp,
483*0Sstevel@tonic-gate 					(char *)gettext("When %d are queued: "),
484*0Sstevel@tonic-gate 					alertp->Q
485*0Sstevel@tonic-gate 				);
486*0Sstevel@tonic-gate 			else
487*0Sstevel@tonic-gate 				(void)fprintf (fp, (char *)gettext("Upon any being queued: "));
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate 		if (copy && (cp = strchr(copy, ' ')))
490*0Sstevel@tonic-gate 			while (*cp == ' ')
491*0Sstevel@tonic-gate 				*cp++ = 0;
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate 		if (
494*0Sstevel@tonic-gate 			copy
495*0Sstevel@tonic-gate 		     && syn_name(cp)
496*0Sstevel@tonic-gate 		     && (
497*0Sstevel@tonic-gate 				STREQU(copy, NAME_WRITE)
498*0Sstevel@tonic-gate 			     || STREQU(copy, NAME_MAIL)
499*0Sstevel@tonic-gate 			)
500*0Sstevel@tonic-gate 		)
501*0Sstevel@tonic-gate 			(void)fprintf (fp, "%s to %s ", copy, cp);
502*0Sstevel@tonic-gate 		else
503*0Sstevel@tonic-gate 			(void)fprintf (fp, (char *)gettext("alert with \"%s\" "), alertp->shcmd);
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate 		if (alertp->W > 0)
506*0Sstevel@tonic-gate 			(void)fprintf (fp, (char *)gettext("every %d minutes\n"), alertp->W);
507*0Sstevel@tonic-gate 		else
508*0Sstevel@tonic-gate 			(void)fprintf (fp, (char *)gettext("once\n"));
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate 		Free (copy);
511*0Sstevel@tonic-gate 	}
512*0Sstevel@tonic-gate 	return;
513*0Sstevel@tonic-gate }
514