xref: /titanic_53/usr/src/cmd/bnu/grades.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*7c478bd9Sstevel@tonic-gate 
25*7c478bd9Sstevel@tonic-gate /*
26*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2000 by Sun Microsystems, Inc.
27*7c478bd9Sstevel@tonic-gate  * All rights reserved.
28*7c478bd9Sstevel@tonic-gate  */
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #ident	"%Z%%M%	%I%	%E% SMI"	/* from SVR4 bnu:grades.c 1.7 */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include "uucp.h"
33*7c478bd9Sstevel@tonic-gate #include <grp.h>
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #define G_EXT	0
36*7c478bd9Sstevel@tonic-gate #define	G_INT	1
37*7c478bd9Sstevel@tonic-gate #define	G_RES	2
38*7c478bd9Sstevel@tonic-gate #define	G_ACT	3
39*7c478bd9Sstevel@tonic-gate #define	G_IDF	4
40*7c478bd9Sstevel@tonic-gate #define	G_MAX	512	/* max number of fields in the Grades file line */
41*7c478bd9Sstevel@tonic-gate #define	SMBUF	128
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #define	TYPE	0
44*7c478bd9Sstevel@tonic-gate #define FILE1	1
45*7c478bd9Sstevel@tonic-gate #define	FILE2	2
46*7c478bd9Sstevel@tonic-gate #define	USER	3
47*7c478bd9Sstevel@tonic-gate #define	OPTS	4
48*7c478bd9Sstevel@tonic-gate #define	FILE3	5
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate extern int rdfulline(), jsize(), gdirf(), gnamef();
51*7c478bd9Sstevel@tonic-gate extern void wfcommit();
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate static void	mailAdmin();		/* Send mail to administrator. */
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate /*
56*7c478bd9Sstevel@tonic-gate  * chkgrp - checks to see the group has permission
57*7c478bd9Sstevel@tonic-gate  *		to use a service grade queue.
58*7c478bd9Sstevel@tonic-gate  *
59*7c478bd9Sstevel@tonic-gate  * returns
60*7c478bd9Sstevel@tonic-gate  *
61*7c478bd9Sstevel@tonic-gate  *	SUCCESS - if the group has permissions
62*7c478bd9Sstevel@tonic-gate  *	FAIL - if group does not
63*7c478bd9Sstevel@tonic-gate  *
64*7c478bd9Sstevel@tonic-gate  */
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate static int
67*7c478bd9Sstevel@tonic-gate chkgrp(carray,na)
68*7c478bd9Sstevel@tonic-gate char **carray;
69*7c478bd9Sstevel@tonic-gate int na;
70*7c478bd9Sstevel@tonic-gate {
71*7c478bd9Sstevel@tonic-gate 	struct group *grp;
72*7c478bd9Sstevel@tonic-gate 	int i;
73*7c478bd9Sstevel@tonic-gate 	gid_t gid;
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	gid = getgid();
76*7c478bd9Sstevel@tonic-gate 	grp = getgrgid(gid);
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate 	for (i = G_IDF; i < na; i++)
79*7c478bd9Sstevel@tonic-gate 			if (EQUALS(carray[i], grp->gr_name))
80*7c478bd9Sstevel@tonic-gate 				return(SUCCESS);
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	return(FAIL);
83*7c478bd9Sstevel@tonic-gate }
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate /*
86*7c478bd9Sstevel@tonic-gate  * chkusr - checks the permission fields of the Grades file
87*7c478bd9Sstevel@tonic-gate  *	    to determine if the user can queue to a particular service grade.
88*7c478bd9Sstevel@tonic-gate  *
89*7c478bd9Sstevel@tonic-gate  * returns
90*7c478bd9Sstevel@tonic-gate  *
91*7c478bd9Sstevel@tonic-gate  *	SUCCESS - if the user can queue to the service grade.
92*7c478bd9Sstevel@tonic-gate  *	FAIL - if the user can not queue to this service grade.
93*7c478bd9Sstevel@tonic-gate  *
94*7c478bd9Sstevel@tonic-gate  */
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate static int
97*7c478bd9Sstevel@tonic-gate chkusr(carray, na)
98*7c478bd9Sstevel@tonic-gate char **carray;
99*7c478bd9Sstevel@tonic-gate int na;
100*7c478bd9Sstevel@tonic-gate {
101*7c478bd9Sstevel@tonic-gate 	int i;
102*7c478bd9Sstevel@tonic-gate 
103*7c478bd9Sstevel@tonic-gate 	/*
104*7c478bd9Sstevel@tonic-gate 	 * start at the point where the users are supposed to be in the
105*7c478bd9Sstevel@tonic-gate 	 * Grades file. Loop thru until the end of the user list is
106*7c478bd9Sstevel@tonic-gate 	 * found or the user name is found. If the user name is found then
107*7c478bd9Sstevel@tonic-gate 	 * return TRUE. If the end of the list is found, return FAIL.
108*7c478bd9Sstevel@tonic-gate 	 */
109*7c478bd9Sstevel@tonic-gate 
110*7c478bd9Sstevel@tonic-gate 	DEBUG(9, "User (%s)\n", User);
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	/* check for any user and return if so */
113*7c478bd9Sstevel@tonic-gate 
114*7c478bd9Sstevel@tonic-gate 	if (EQUALS(carray[G_IDF], "Any"))
115*7c478bd9Sstevel@tonic-gate 		return(SUCCESS);
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	DEBUG(9, "Members of administrator defined service grade (%s)\n", carray[G_EXT]);
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	for (i = G_IDF; i < na; i++) {
120*7c478bd9Sstevel@tonic-gate 		DEBUG(9, "%s\n", carray[i]);
121*7c478bd9Sstevel@tonic-gate 		if (EQUALS(User, carray[i]))
122*7c478bd9Sstevel@tonic-gate 			return(SUCCESS);
123*7c478bd9Sstevel@tonic-gate 	}
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	return(FAIL);
126*7c478bd9Sstevel@tonic-gate }
127*7c478bd9Sstevel@tonic-gate 
128*7c478bd9Sstevel@tonic-gate /*
129*7c478bd9Sstevel@tonic-gate  *	fgrade - finds the appropiate queue to queue a job into
130*7c478bd9Sstevel@tonic-gate  *
131*7c478bd9Sstevel@tonic-gate  *	returns
132*7c478bd9Sstevel@tonic-gate  *		SUCCESS	-> found a queue
133*7c478bd9Sstevel@tonic-gate  *		FAIL	-> can't find a queue
134*7c478bd9Sstevel@tonic-gate  */
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate int
137*7c478bd9Sstevel@tonic-gate fgrade(scfile)
138*7c478bd9Sstevel@tonic-gate struct cs_struct *scfile;
139*7c478bd9Sstevel@tonic-gate {
140*7c478bd9Sstevel@tonic-gate 	char fdgrade();
141*7c478bd9Sstevel@tonic-gate 	FILE *cfd;
142*7c478bd9Sstevel@tonic-gate 	char line[BUFSIZ];
143*7c478bd9Sstevel@tonic-gate 	char *carray[G_MAX];
144*7c478bd9Sstevel@tonic-gate 	long climit;
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate 	/* Check for the default service grade first */
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	if (strcmp(scfile->sgrade, "default") == 0) {
149*7c478bd9Sstevel@tonic-gate 		scfile->grade = fdgrade();
150*7c478bd9Sstevel@tonic-gate 		return(SUCCESS);
151*7c478bd9Sstevel@tonic-gate 	}
152*7c478bd9Sstevel@tonic-gate 
153*7c478bd9Sstevel@tonic-gate 	/* open grades file to begin a linear for the grade requested */
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	cfd = fopen(GRADES, "r");
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	/* loop until the file is empty or we find the grade we want */
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	while (rdfulline(cfd, line, BUFSIZ) != 0) {
160*7c478bd9Sstevel@tonic-gate 		(void) getargs(line, carray, G_MAX);
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 		/* check to see if this is the grade we want */
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate 		if (!EQUALS(scfile->sgrade, carray[G_EXT]))
165*7c478bd9Sstevel@tonic-gate 			continue;
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 		if (jsize(scfile, carray[G_RES], &climit) != FAIL) {
168*7c478bd9Sstevel@tonic-gate 			(void) fclose(cfd);
169*7c478bd9Sstevel@tonic-gate 			scfile->grade = *carray[G_INT];
170*7c478bd9Sstevel@tonic-gate 			return(SUCCESS);
171*7c478bd9Sstevel@tonic-gate 		}
172*7c478bd9Sstevel@tonic-gate 	}
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate 	(void) fclose(cfd);
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("Job size (%ld bytes)"
177*7c478bd9Sstevel@tonic-gate 	    " exceeds maximum number of bytes (%ld bytes)"
178*7c478bd9Sstevel@tonic-gate 	    " allowed into this service grade (%s).\n"
179*7c478bd9Sstevel@tonic-gate 	    "Job queued to default grade.\n"),
180*7c478bd9Sstevel@tonic-gate 	    scfile->jsize, climit, scfile->sgrade);
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate 	scfile->grade = fdgrade();
183*7c478bd9Sstevel@tonic-gate 	return(SUCCESS);
184*7c478bd9Sstevel@tonic-gate }
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate /*
187*7c478bd9Sstevel@tonic-gate  *	fdgrade - finds the default queue for this system
188*7c478bd9Sstevel@tonic-gate  *
189*7c478bd9Sstevel@tonic-gate  *	returns
190*7c478bd9Sstevel@tonic-gate  *		a one char name for the default queue
191*7c478bd9Sstevel@tonic-gate  *
192*7c478bd9Sstevel@tonic-gate  */
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate char
195*7c478bd9Sstevel@tonic-gate fdgrade()
196*7c478bd9Sstevel@tonic-gate {
197*7c478bd9Sstevel@tonic-gate 	FILE *cfd;
198*7c478bd9Sstevel@tonic-gate 	char line[BUFSIZ];
199*7c478bd9Sstevel@tonic-gate 	char *carray[G_MAX];
200*7c478bd9Sstevel@tonic-gate 
201*7c478bd9Sstevel@tonic-gate 	/* Check for the default grade first */
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 		cfd = fopen(GRADES, "r");
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 		/* loop until the end of the file is read */
206*7c478bd9Sstevel@tonic-gate 
207*7c478bd9Sstevel@tonic-gate 		for (; rdfulline(cfd, line, BUFSIZ) != 0;) {
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate 			/* parse the fields of this line */
210*7c478bd9Sstevel@tonic-gate 
211*7c478bd9Sstevel@tonic-gate 			(void) getargs(line, carray, G_MAX);
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate 			/* check to see if the administrator has defined
214*7c478bd9Sstevel@tonic-gate 			 * a default grade for the machine.
215*7c478bd9Sstevel@tonic-gate 			 */
216*7c478bd9Sstevel@tonic-gate 
217*7c478bd9Sstevel@tonic-gate 			if (strcmp(carray[G_EXT], "default") != 0)
218*7c478bd9Sstevel@tonic-gate 				continue;
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 			/* default must be defined in the file
221*7c478bd9Sstevel@tonic-gate 			 *  close the file, get the queue name, and return.
222*7c478bd9Sstevel@tonic-gate 			 */
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 			(void) fclose(cfd);
225*7c478bd9Sstevel@tonic-gate 			return(*carray[G_INT]);
226*7c478bd9Sstevel@tonic-gate 		}
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 		/* no default defined in this file. close file.
229*7c478bd9Sstevel@tonic-gate 		 * get our default queue and return.
230*7c478bd9Sstevel@tonic-gate 		 */
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 		(void) fclose(cfd);
233*7c478bd9Sstevel@tonic-gate 		return(D_QUEUE);
234*7c478bd9Sstevel@tonic-gate }
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate /*
237*7c478bd9Sstevel@tonic-gate  * job_size - determines the size of a job
238*7c478bd9Sstevel@tonic-gate  *
239*7c478bd9Sstevel@tonic-gate  * returns
240*7c478bd9Sstevel@tonic-gate  *
241*7c478bd9Sstevel@tonic-gate  *	SUCCESS - if the size of the job can be determined
242*7c478bd9Sstevel@tonic-gate  *	FAIL	- otherwise
243*7c478bd9Sstevel@tonic-gate  */
244*7c478bd9Sstevel@tonic-gate 
245*7c478bd9Sstevel@tonic-gate int
246*7c478bd9Sstevel@tonic-gate job_size(scfile)
247*7c478bd9Sstevel@tonic-gate struct cs_struct *scfile;
248*7c478bd9Sstevel@tonic-gate {
249*7c478bd9Sstevel@tonic-gate 	extern int Dfileused;
250*7c478bd9Sstevel@tonic-gate 	struct stat s;
251*7c478bd9Sstevel@tonic-gate 	FILE *fp;
252*7c478bd9Sstevel@tonic-gate 	char line[BUFSIZ];
253*7c478bd9Sstevel@tonic-gate 	char *carray[G_MAX];
254*7c478bd9Sstevel@tonic-gate 	int na;
255*7c478bd9Sstevel@tonic-gate 	int nodfile = FALSE;
256*7c478bd9Sstevel@tonic-gate 	int ret;
257*7c478bd9Sstevel@tonic-gate 
258*7c478bd9Sstevel@tonic-gate 	scfile->jsize = 0;
259*7c478bd9Sstevel@tonic-gate 
260*7c478bd9Sstevel@tonic-gate 	fp = fopen(scfile->file, "r");
261*7c478bd9Sstevel@tonic-gate 
262*7c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
263*7c478bd9Sstevel@tonic-gate 		toCorrupt(scfile->file);
264*7c478bd9Sstevel@tonic-gate 		errent(Ct_OPEN, scfile->file, errno, __FILE__,  __LINE__);
265*7c478bd9Sstevel@tonic-gate 	}
266*7c478bd9Sstevel@tonic-gate 
267*7c478bd9Sstevel@tonic-gate 	while (fgets(line, BUFSIZ, fp) != NULL) {
268*7c478bd9Sstevel@tonic-gate 		na = getargs(line, carray, G_MAX);
269*7c478bd9Sstevel@tonic-gate 
270*7c478bd9Sstevel@tonic-gate 		if (na < 6) {
271*7c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
272*7c478bd9Sstevel@tonic-gate 			toCorrupt(scfile->file);
273*7c478bd9Sstevel@tonic-gate 			errent("BAD NUMBER OF ARGUMENTS", scfile->file, 0,
274*7c478bd9Sstevel@tonic-gate 				__FILE__, __LINE__);
275*7c478bd9Sstevel@tonic-gate 		}
276*7c478bd9Sstevel@tonic-gate 
277*7c478bd9Sstevel@tonic-gate 		/* if the type of a transfer is not a push
278*7c478bd9Sstevel@tonic-gate 		 * then don't try to determine the size of
279*7c478bd9Sstevel@tonic-gate 		 * the data file, because you can't.
280*7c478bd9Sstevel@tonic-gate 		 */
281*7c478bd9Sstevel@tonic-gate 
282*7c478bd9Sstevel@tonic-gate 		if (*carray[TYPE] == 'R')
283*7c478bd9Sstevel@tonic-gate 			continue;
284*7c478bd9Sstevel@tonic-gate 
285*7c478bd9Sstevel@tonic-gate 		/* find the data dile that is to be transferred */
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 		if ((ret = stat(carray[FILE3], &s)) != 0) {
288*7c478bd9Sstevel@tonic-gate 			if (errno == ENOENT) {
289*7c478bd9Sstevel@tonic-gate 				nodfile = TRUE;
290*7c478bd9Sstevel@tonic-gate 				ret = stat(carray[FILE1], &s);
291*7c478bd9Sstevel@tonic-gate 			}
292*7c478bd9Sstevel@tonic-gate 		}
293*7c478bd9Sstevel@tonic-gate 		else
294*7c478bd9Sstevel@tonic-gate 			Dfileused = TRUE;
295*7c478bd9Sstevel@tonic-gate 
296*7c478bd9Sstevel@tonic-gate 		/*
297*7c478bd9Sstevel@tonic-gate 		 * check to see if the return code from stat was 0
298*7c478bd9Sstevel@tonic-gate 		 * if return code was not 0, write message to error
299*7c478bd9Sstevel@tonic-gate 		 * log and quit. Otherwise, add size of file to job
300*7c478bd9Sstevel@tonic-gate 		 * size and continue looping.
301*7c478bd9Sstevel@tonic-gate 		 */
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate 		if (ret != 0) {
304*7c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
305*7c478bd9Sstevel@tonic-gate 			errent(Ct_STAT, nodfile ?
306*7c478bd9Sstevel@tonic-gate 				carray[FILE1] : carray[FILE3], errno,
307*7c478bd9Sstevel@tonic-gate 				__FILE__, __LINE__);
308*7c478bd9Sstevel@tonic-gate 		}
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate 		nodfile = FALSE;
311*7c478bd9Sstevel@tonic-gate 		scfile->jsize += s.st_size;
312*7c478bd9Sstevel@tonic-gate 	}
313*7c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
314*7c478bd9Sstevel@tonic-gate 	return(SUCCESS);
315*7c478bd9Sstevel@tonic-gate }
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate /*
318*7c478bd9Sstevel@tonic-gate  * jsize - determines whether if a job is small enough to
319*7c478bd9Sstevel@tonic-gate  * 	   be placed in the appropiate queue.
320*7c478bd9Sstevel@tonic-gate  *
321*7c478bd9Sstevel@tonic-gate  * returns
322*7c478bd9Sstevel@tonic-gate  *
323*7c478bd9Sstevel@tonic-gate  *	SUCCESS - if the size of the job is less than or
324*7c478bd9Sstevel@tonic-gate  *		  equal to the number of bytes in the restriction
325*7c478bd9Sstevel@tonic-gate  *		  of the GRADES file.
326*7c478bd9Sstevel@tonic-gate  *
327*7c478bd9Sstevel@tonic-gate  *	FAIL	- otherwise
328*7c478bd9Sstevel@tonic-gate  */
329*7c478bd9Sstevel@tonic-gate 
330*7c478bd9Sstevel@tonic-gate int
331*7c478bd9Sstevel@tonic-gate jsize(scfile, climit, nlimit)
332*7c478bd9Sstevel@tonic-gate struct cs_struct *scfile;
333*7c478bd9Sstevel@tonic-gate char *climit;
334*7c478bd9Sstevel@tonic-gate long *nlimit;
335*7c478bd9Sstevel@tonic-gate {
336*7c478bd9Sstevel@tonic-gate #define ONE_K (1024)
337*7c478bd9Sstevel@tonic-gate #define ONE_MEG ((1024)*(1024))
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate 	static void lcase();
340*7c478bd9Sstevel@tonic-gate 	char rest[SMBUF];
341*7c478bd9Sstevel@tonic-gate 	char msg[BUFSIZ], *p;
342*7c478bd9Sstevel@tonic-gate 
343*7c478bd9Sstevel@tonic-gate 	if (EQUALS(climit, "Any"))
344*7c478bd9Sstevel@tonic-gate 		return(SUCCESS);
345*7c478bd9Sstevel@tonic-gate 
346*7c478bd9Sstevel@tonic-gate 	lcase(climit, rest, SMBUF);
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate 	if (!(p = strchr(rest, 'k')) && (!(p = strchr(rest, 'm')))) {
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate 		for(p = climit; *p; ++p) {
351*7c478bd9Sstevel@tonic-gate 			if (isdigit(*p))
352*7c478bd9Sstevel@tonic-gate 				continue;
353*7c478bd9Sstevel@tonic-gate 
354*7c478bd9Sstevel@tonic-gate 			/* corrupt restriction field in the Grades file.
355*7c478bd9Sstevel@tonic-gate 			 * report it to the uucp administrator.
356*7c478bd9Sstevel@tonic-gate 			 */
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate 			snprintf(msg, sizeof (msg),
359*7c478bd9Sstevel@tonic-gate 			    gettext("Error encountered in the"
360*7c478bd9Sstevel@tonic-gate 			    " restrictions field of the Grades file."
361*7c478bd9Sstevel@tonic-gate 			    "  Field contents (%s)."), climit);
362*7c478bd9Sstevel@tonic-gate 			mailAdmin(msg);
363*7c478bd9Sstevel@tonic-gate 			return(SUCCESS);
364*7c478bd9Sstevel@tonic-gate 		}
365*7c478bd9Sstevel@tonic-gate 
366*7c478bd9Sstevel@tonic-gate 		*nlimit = atol(climit);
367*7c478bd9Sstevel@tonic-gate 	}
368*7c478bd9Sstevel@tonic-gate 	else if (*p == 'k') {
369*7c478bd9Sstevel@tonic-gate 		*p = '\0';
370*7c478bd9Sstevel@tonic-gate 		*nlimit = (long) (atof(rest) * ONE_K);
371*7c478bd9Sstevel@tonic-gate 	}
372*7c478bd9Sstevel@tonic-gate 	else {
373*7c478bd9Sstevel@tonic-gate 		*p = '\0';
374*7c478bd9Sstevel@tonic-gate 		*nlimit = (long) (atof(rest) * ONE_MEG);
375*7c478bd9Sstevel@tonic-gate 	}
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate 	if (scfile->jsize <= *nlimit)
378*7c478bd9Sstevel@tonic-gate 		return(SUCCESS);
379*7c478bd9Sstevel@tonic-gate 	else
380*7c478bd9Sstevel@tonic-gate 		return(FAIL);
381*7c478bd9Sstevel@tonic-gate }
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate static void
384*7c478bd9Sstevel@tonic-gate lcase(s, t, lim)
385*7c478bd9Sstevel@tonic-gate char s[], t[];
386*7c478bd9Sstevel@tonic-gate int lim;
387*7c478bd9Sstevel@tonic-gate {
388*7c478bd9Sstevel@tonic-gate 	char *p;
389*7c478bd9Sstevel@tonic-gate 	int i;
390*7c478bd9Sstevel@tonic-gate 
391*7c478bd9Sstevel@tonic-gate 
392*7c478bd9Sstevel@tonic-gate 	p = s;
393*7c478bd9Sstevel@tonic-gate 
394*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < lim-1 && *p; i++)
395*7c478bd9Sstevel@tonic-gate 		if (isupper(*p))
396*7c478bd9Sstevel@tonic-gate 			t[i] = tolower(*p++);
397*7c478bd9Sstevel@tonic-gate 		else
398*7c478bd9Sstevel@tonic-gate 			t[i] = *p++;
399*7c478bd9Sstevel@tonic-gate 
400*7c478bd9Sstevel@tonic-gate 	t[i] = '\0';
401*7c478bd9Sstevel@tonic-gate 	return;
402*7c478bd9Sstevel@tonic-gate }
403*7c478bd9Sstevel@tonic-gate 
404*7c478bd9Sstevel@tonic-gate /*
405*7c478bd9Sstevel@tonic-gate  * mailAdmin - mail a message to the uucp administrator.
406*7c478bd9Sstevel@tonic-gate  *
407*7c478bd9Sstevel@tonic-gate  * returns:
408*7c478bd9Sstevel@tonic-gate  *
409*7c478bd9Sstevel@tonic-gate  *	nothing
410*7c478bd9Sstevel@tonic-gate  */
411*7c478bd9Sstevel@tonic-gate 
412*7c478bd9Sstevel@tonic-gate static void
413*7c478bd9Sstevel@tonic-gate mailAdmin (msg)
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate char *	msg;
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate {
418*7c478bd9Sstevel@tonic-gate 	char	cmd[BUFSIZ];		/* Place to build mail command. */
419*7c478bd9Sstevel@tonic-gate 	FILE *	mail;			/* Channel to write mail on. */
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate 	(void) sprintf(cmd, "%s %s %s", PATH, MAIL, "uucp");
422*7c478bd9Sstevel@tonic-gate 	if ((mail = popen(cmd, "w")) != (FILE *) NULL)
423*7c478bd9Sstevel@tonic-gate 	{
424*7c478bd9Sstevel@tonic-gate 		(void) fprintf(mail, "To: uucp\nSubject: %s\n\n%s\n",
425*7c478bd9Sstevel@tonic-gate 		    gettext("Grades file problem"), msg);
426*7c478bd9Sstevel@tonic-gate 		(void) pclose(mail);
427*7c478bd9Sstevel@tonic-gate 	}
428*7c478bd9Sstevel@tonic-gate 
429*7c478bd9Sstevel@tonic-gate 	/*
430*7c478bd9Sstevel@tonic-gate 	 * Ignore popen failure.  There is not much that we can do if
431*7c478bd9Sstevel@tonic-gate 	 * it fails, since we are already trying to notify the administrator
432*7c478bd9Sstevel@tonic-gate 	 * of a problem.
433*7c478bd9Sstevel@tonic-gate 	 */
434*7c478bd9Sstevel@tonic-gate 	return;
435*7c478bd9Sstevel@tonic-gate }
436*7c478bd9Sstevel@tonic-gate 
437*7c478bd9Sstevel@tonic-gate /*
438*7c478bd9Sstevel@tonic-gate  * putdfiles - moves any and all of the D. to the spool directory for
439*7c478bd9Sstevel@tonic-gate  * 	       a C. file.
440*7c478bd9Sstevel@tonic-gate  *
441*7c478bd9Sstevel@tonic-gate  * returns
442*7c478bd9Sstevel@tonic-gate  *
443*7c478bd9Sstevel@tonic-gate  *	nothing
444*7c478bd9Sstevel@tonic-gate  */
445*7c478bd9Sstevel@tonic-gate 
446*7c478bd9Sstevel@tonic-gate void
447*7c478bd9Sstevel@tonic-gate putdfiles(scfile)
448*7c478bd9Sstevel@tonic-gate struct cs_struct scfile;
449*7c478bd9Sstevel@tonic-gate {
450*7c478bd9Sstevel@tonic-gate 	FILE *fp;
451*7c478bd9Sstevel@tonic-gate 	char line[BUFSIZ];
452*7c478bd9Sstevel@tonic-gate 	char *carray[G_MAX];
453*7c478bd9Sstevel@tonic-gate 	int na;
454*7c478bd9Sstevel@tonic-gate 	struct stat s;
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate 	fp = fopen(scfile.file, "r");
457*7c478bd9Sstevel@tonic-gate 
458*7c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
459*7c478bd9Sstevel@tonic-gate 		toCorrupt(scfile.file);
460*7c478bd9Sstevel@tonic-gate 		errent(Ct_OPEN, scfile.file, errno, __FILE__, __LINE__);
461*7c478bd9Sstevel@tonic-gate 	}
462*7c478bd9Sstevel@tonic-gate 
463*7c478bd9Sstevel@tonic-gate 	while (fgets(line, BUFSIZ, fp) != NULL) {
464*7c478bd9Sstevel@tonic-gate 
465*7c478bd9Sstevel@tonic-gate 		na = getargs(line, carray, G_MAX);
466*7c478bd9Sstevel@tonic-gate 		if (na < 6) {
467*7c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
468*7c478bd9Sstevel@tonic-gate 			toCorrupt(scfile.file);
469*7c478bd9Sstevel@tonic-gate 			errent("BAD NUMBER OF ARGUMENTS", scfile.file, 0,
470*7c478bd9Sstevel@tonic-gate 				__FILE__, __LINE__);
471*7c478bd9Sstevel@tonic-gate 		}
472*7c478bd9Sstevel@tonic-gate 
473*7c478bd9Sstevel@tonic-gate 		if (*carray[TYPE] == 'R')
474*7c478bd9Sstevel@tonic-gate 			continue;
475*7c478bd9Sstevel@tonic-gate 
476*7c478bd9Sstevel@tonic-gate 	    	/* move D. file to the spool area */
477*7c478bd9Sstevel@tonic-gate 
478*7c478bd9Sstevel@tonic-gate 		if (stat(carray[FILE3], &s) != -1)
479*7c478bd9Sstevel@tonic-gate 			wfcommit(carray[FILE3], carray[FILE3], scfile.sys);
480*7c478bd9Sstevel@tonic-gate 	}
481*7c478bd9Sstevel@tonic-gate 
482*7c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
483*7c478bd9Sstevel@tonic-gate 	return;
484*7c478bd9Sstevel@tonic-gate }
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate /*
487*7c478bd9Sstevel@tonic-gate  * reads a line from a file and takes care of comment lines
488*7c478bd9Sstevel@tonic-gate  * and continuations (\) in last column.
489*7c478bd9Sstevel@tonic-gate  *
490*7c478bd9Sstevel@tonic-gate  * return:
491*7c478bd9Sstevel@tonic-gate  *	the number of chars that are placed in line.
492*7c478bd9Sstevel@tonic-gate  */
493*7c478bd9Sstevel@tonic-gate 
494*7c478bd9Sstevel@tonic-gate int
495*7c478bd9Sstevel@tonic-gate rdfulline(fd, line, lim)
496*7c478bd9Sstevel@tonic-gate FILE *fd;
497*7c478bd9Sstevel@tonic-gate char *line;
498*7c478bd9Sstevel@tonic-gate int lim;
499*7c478bd9Sstevel@tonic-gate {
500*7c478bd9Sstevel@tonic-gate 	register char *p, *c;
501*7c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];
502*7c478bd9Sstevel@tonic-gate 	size_t blr, btox;
503*7c478bd9Sstevel@tonic-gate 
504*7c478bd9Sstevel@tonic-gate 	p = line;
505*7c478bd9Sstevel@tonic-gate 	for (;fgets(buf, BUFSIZ, fd) != NULL;) {
506*7c478bd9Sstevel@tonic-gate 		/* check to see if it is a comment */
507*7c478bd9Sstevel@tonic-gate 
508*7c478bd9Sstevel@tonic-gate 		if (buf[0] == '#')
509*7c478bd9Sstevel@tonic-gate 			continue;
510*7c478bd9Sstevel@tonic-gate 
511*7c478bd9Sstevel@tonic-gate 		/* remove trailing white space */
512*7c478bd9Sstevel@tonic-gate 		c = &buf[strlen(buf)-1];
513*7c478bd9Sstevel@tonic-gate 		while (c>=buf && (*c == '\n' || *c == '\t' || *c == ' ') )
514*7c478bd9Sstevel@tonic-gate 			*c-- = NULLCHAR;
515*7c478bd9Sstevel@tonic-gate 
516*7c478bd9Sstevel@tonic-gate 		if (buf[0] == '\n' || buf[0] == NULLCHAR)
517*7c478bd9Sstevel@tonic-gate 			continue;
518*7c478bd9Sstevel@tonic-gate 
519*7c478bd9Sstevel@tonic-gate 		blr = lim - 1 - (p - line);
520*7c478bd9Sstevel@tonic-gate 		btox = blr < strlen(buf) ? blr : strlen(buf);
521*7c478bd9Sstevel@tonic-gate 
522*7c478bd9Sstevel@tonic-gate 		if (btox <= 0)
523*7c478bd9Sstevel@tonic-gate 			break;
524*7c478bd9Sstevel@tonic-gate 
525*7c478bd9Sstevel@tonic-gate 		(void) strncpy(p, buf, btox);
526*7c478bd9Sstevel@tonic-gate 		p += btox - 1;
527*7c478bd9Sstevel@tonic-gate 
528*7c478bd9Sstevel@tonic-gate 		if ( *(p-1) == '\\')
529*7c478bd9Sstevel@tonic-gate 			p--;
530*7c478bd9Sstevel@tonic-gate 		else
531*7c478bd9Sstevel@tonic-gate 			break;
532*7c478bd9Sstevel@tonic-gate 	}
533*7c478bd9Sstevel@tonic-gate 
534*7c478bd9Sstevel@tonic-gate 	*++p = '\0';
535*7c478bd9Sstevel@tonic-gate 	return(p-line-1);
536*7c478bd9Sstevel@tonic-gate }
537*7c478bd9Sstevel@tonic-gate 
538*7c478bd9Sstevel@tonic-gate /*	upermit - checks to determine if the user has permissions
539*7c478bd9Sstevel@tonic-gate  *	to use administrator defined service grade.
540*7c478bd9Sstevel@tonic-gate  *
541*7c478bd9Sstevel@tonic-gate  *	returns
542*7c478bd9Sstevel@tonic-gate  *		SUCCESS -> if the user can queue to this service grade.
543*7c478bd9Sstevel@tonic-gate  *		FAIL -> if the user cannot queue to this service grade.
544*7c478bd9Sstevel@tonic-gate  */
545*7c478bd9Sstevel@tonic-gate 
546*7c478bd9Sstevel@tonic-gate int
547*7c478bd9Sstevel@tonic-gate upermit(carray, na)
548*7c478bd9Sstevel@tonic-gate char **carray;
549*7c478bd9Sstevel@tonic-gate int na;
550*7c478bd9Sstevel@tonic-gate {
551*7c478bd9Sstevel@tonic-gate #define G_USR "user"
552*7c478bd9Sstevel@tonic-gate #define G_NUSR "non-user"
553*7c478bd9Sstevel@tonic-gate #define G_GRP "group"
554*7c478bd9Sstevel@tonic-gate #define G_NGRP "non-group"
555*7c478bd9Sstevel@tonic-gate 
556*7c478bd9Sstevel@tonic-gate 	static void lcase();
557*7c478bd9Sstevel@tonic-gate 	char actn[SMBUF];
558*7c478bd9Sstevel@tonic-gate 	char ufld[SMBUF];
559*7c478bd9Sstevel@tonic-gate 	char msg[BUFSIZ];
560*7c478bd9Sstevel@tonic-gate 
561*7c478bd9Sstevel@tonic-gate 	(void) strcpy(actn, carray[G_ACT]);
562*7c478bd9Sstevel@tonic-gate 
563*7c478bd9Sstevel@tonic-gate 	lcase(actn, ufld, SMBUF);
564*7c478bd9Sstevel@tonic-gate 
565*7c478bd9Sstevel@tonic-gate 	if (EQUALS(ufld, G_USR))
566*7c478bd9Sstevel@tonic-gate 		return(chkusr(carray,na));
567*7c478bd9Sstevel@tonic-gate 
568*7c478bd9Sstevel@tonic-gate 	if (EQUALS(ufld, G_NUSR))
569*7c478bd9Sstevel@tonic-gate 		return((chkusr(carray, na) != SUCCESS) ? SUCCESS : FAIL);
570*7c478bd9Sstevel@tonic-gate 
571*7c478bd9Sstevel@tonic-gate 	if (EQUALS(ufld, G_GRP))
572*7c478bd9Sstevel@tonic-gate 		return(chkgrp(carray, na));
573*7c478bd9Sstevel@tonic-gate 
574*7c478bd9Sstevel@tonic-gate 	if (EQUALS(ufld, G_NGRP))
575*7c478bd9Sstevel@tonic-gate 		return((chkgrp(carray, na) != SUCCESS) ? SUCCESS : FAIL);
576*7c478bd9Sstevel@tonic-gate 
577*7c478bd9Sstevel@tonic-gate 	(void) snprintf(msg, sizeof (msg),
578*7c478bd9Sstevel@tonic-gate 	    gettext("Error encountered in action field of"
579*7c478bd9Sstevel@tonic-gate 	    " the Grades file. Field contents (%s)."), carray[G_ACT]);
580*7c478bd9Sstevel@tonic-gate 	mailAdmin(msg);
581*7c478bd9Sstevel@tonic-gate 	return(FAIL);
582*7c478bd9Sstevel@tonic-gate }
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate /*
585*7c478bd9Sstevel@tonic-gate  *	vergrd - verify if the grade name is a valid administrator
586*7c478bd9Sstevel@tonic-gate  *		 defined service grade name and if the user has the
587*7c478bd9Sstevel@tonic-gate  *		 appropiate permission to use this grade.
588*7c478bd9Sstevel@tonic-gate  *
589*7c478bd9Sstevel@tonic-gate  *	returns
590*7c478bd9Sstevel@tonic-gate  *		SUCCESS	-> grade is valid and user is
591*7c478bd9Sstevel@tonic-gate  *			   permitted to use this grade.
592*7c478bd9Sstevel@tonic-gate  *		FAIL	-> otherwise
593*7c478bd9Sstevel@tonic-gate  *
594*7c478bd9Sstevel@tonic-gate  */
595*7c478bd9Sstevel@tonic-gate 
596*7c478bd9Sstevel@tonic-gate int
597*7c478bd9Sstevel@tonic-gate vergrd(grade)
598*7c478bd9Sstevel@tonic-gate char *grade;
599*7c478bd9Sstevel@tonic-gate {
600*7c478bd9Sstevel@tonic-gate 	FILE *cfd;
601*7c478bd9Sstevel@tonic-gate 	char line[BUFSIZ];
602*7c478bd9Sstevel@tonic-gate 	char *carray[G_MAX];
603*7c478bd9Sstevel@tonic-gate 	int na;
604*7c478bd9Sstevel@tonic-gate 
605*7c478bd9Sstevel@tonic-gate 	/* Check for the default grade first */
606*7c478bd9Sstevel@tonic-gate 
607*7c478bd9Sstevel@tonic-gate 	if (EQUALS(grade, "default"))
608*7c478bd9Sstevel@tonic-gate 		return(SUCCESS);
609*7c478bd9Sstevel@tonic-gate 
610*7c478bd9Sstevel@tonic-gate 	/* open grades file to begin a linear for the grade requested */
611*7c478bd9Sstevel@tonic-gate 
612*7c478bd9Sstevel@tonic-gate 	cfd = fopen(GRADES, "r");
613*7c478bd9Sstevel@tonic-gate 
614*7c478bd9Sstevel@tonic-gate 	/* loop until the file is empty or we find the grade we want */
615*7c478bd9Sstevel@tonic-gate 
616*7c478bd9Sstevel@tonic-gate 	while (rdfulline(cfd, line, BUFSIZ) != 0) {
617*7c478bd9Sstevel@tonic-gate 		na = getargs(line, carray, G_MAX);
618*7c478bd9Sstevel@tonic-gate 
619*7c478bd9Sstevel@tonic-gate 		/* check to see if this is the grade we want */
620*7c478bd9Sstevel@tonic-gate 
621*7c478bd9Sstevel@tonic-gate 		if (!EQUALS(grade, carray[G_EXT]))
622*7c478bd9Sstevel@tonic-gate 			continue;
623*7c478bd9Sstevel@tonic-gate 
624*7c478bd9Sstevel@tonic-gate 		/* check for the permission on this grade */
625*7c478bd9Sstevel@tonic-gate 
626*7c478bd9Sstevel@tonic-gate 		if (upermit(carray, na) != FAIL) {
627*7c478bd9Sstevel@tonic-gate 			(void) fclose(cfd);
628*7c478bd9Sstevel@tonic-gate 			return(SUCCESS);
629*7c478bd9Sstevel@tonic-gate 		}
630*7c478bd9Sstevel@tonic-gate 		else {
631*7c478bd9Sstevel@tonic-gate 			(void) fclose(cfd);
632*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("User does not have"
633*7c478bd9Sstevel@tonic-gate 			    " permission to use this service grade (%s).\n"
634*7c478bd9Sstevel@tonic-gate 			    "Job has not been queued.\n"
635*7c478bd9Sstevel@tonic-gate 			    "Use (uuglist) to find which service grades"
636*7c478bd9Sstevel@tonic-gate 			    " you can queue to.\n"), grade);
637*7c478bd9Sstevel@tonic-gate 			return(FAIL);
638*7c478bd9Sstevel@tonic-gate 		}
639*7c478bd9Sstevel@tonic-gate 	}
640*7c478bd9Sstevel@tonic-gate 
641*7c478bd9Sstevel@tonic-gate 	(void) fclose(cfd);
642*7c478bd9Sstevel@tonic-gate 
643*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext(
644*7c478bd9Sstevel@tonic-gate 	    "Service grade (%s) does not exist on this machine."
645*7c478bd9Sstevel@tonic-gate 	    "  Job not queued.\n"
646*7c478bd9Sstevel@tonic-gate 	    "Use (uuglist) to find which service grades are available on"
647*7c478bd9Sstevel@tonic-gate 	    " this machine.\n"), grade);
648*7c478bd9Sstevel@tonic-gate 	return(FAIL);
649*7c478bd9Sstevel@tonic-gate }
650*7c478bd9Sstevel@tonic-gate 
651*7c478bd9Sstevel@tonic-gate /*
652*7c478bd9Sstevel@tonic-gate  * wfremove - removes a C. file from the Workspace directory and all of its
653*7c478bd9Sstevel@tonic-gate  * D. files.
654*7c478bd9Sstevel@tonic-gate  */
655*7c478bd9Sstevel@tonic-gate 
656*7c478bd9Sstevel@tonic-gate void
657*7c478bd9Sstevel@tonic-gate wfremove(file)
658*7c478bd9Sstevel@tonic-gate char *file;
659*7c478bd9Sstevel@tonic-gate {
660*7c478bd9Sstevel@tonic-gate 	FILE *fp;
661*7c478bd9Sstevel@tonic-gate 	char line[BUFSIZ];
662*7c478bd9Sstevel@tonic-gate 	char *carray[G_MAX];
663*7c478bd9Sstevel@tonic-gate 	int na;
664*7c478bd9Sstevel@tonic-gate 	struct stat s;
665*7c478bd9Sstevel@tonic-gate 
666*7c478bd9Sstevel@tonic-gate 	fp = fopen(file, "r");
667*7c478bd9Sstevel@tonic-gate 
668*7c478bd9Sstevel@tonic-gate 	if (fp == NULL) {
669*7c478bd9Sstevel@tonic-gate 		toCorrupt(file);
670*7c478bd9Sstevel@tonic-gate 		errent(Ct_OPEN, file, errno, __FILE__, __LINE__);
671*7c478bd9Sstevel@tonic-gate 	}
672*7c478bd9Sstevel@tonic-gate 
673*7c478bd9Sstevel@tonic-gate 	while (fgets(line, BUFSIZ, fp) != NULL) {
674*7c478bd9Sstevel@tonic-gate 		na = getargs(line, carray, G_MAX);
675*7c478bd9Sstevel@tonic-gate 
676*7c478bd9Sstevel@tonic-gate 		if (na < 6) {
677*7c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
678*7c478bd9Sstevel@tonic-gate 			toCorrupt(file);
679*7c478bd9Sstevel@tonic-gate 			errent("BAD NUMBER OF ARGUMENTS", file, 0,
680*7c478bd9Sstevel@tonic-gate 				__FILE__, __LINE__);
681*7c478bd9Sstevel@tonic-gate 		}
682*7c478bd9Sstevel@tonic-gate 
683*7c478bd9Sstevel@tonic-gate 		if (*carray[TYPE] == 'R')
684*7c478bd9Sstevel@tonic-gate 			continue;
685*7c478bd9Sstevel@tonic-gate 
686*7c478bd9Sstevel@tonic-gate 	    	/* remove D. file */
687*7c478bd9Sstevel@tonic-gate 
688*7c478bd9Sstevel@tonic-gate 	    	DEBUG(4, "Removing data file (%s)\n", carray[FILE3]);
689*7c478bd9Sstevel@tonic-gate 
690*7c478bd9Sstevel@tonic-gate 		if ((stat(carray[FILE3], &s) != -1) && (unlink(carray[FILE3]) != 0)) {
691*7c478bd9Sstevel@tonic-gate 			(void) fclose(fp);
692*7c478bd9Sstevel@tonic-gate 			toCorrupt(file);
693*7c478bd9Sstevel@tonic-gate 			toCorrupt(carray[FILE3]);
694*7c478bd9Sstevel@tonic-gate 			errent(Ct_UNLINK, carray[FILE3], errno, __FILE__,
695*7c478bd9Sstevel@tonic-gate 				__LINE__);
696*7c478bd9Sstevel@tonic-gate 		}
697*7c478bd9Sstevel@tonic-gate 	}
698*7c478bd9Sstevel@tonic-gate 
699*7c478bd9Sstevel@tonic-gate 	(void) fclose(fp);
700*7c478bd9Sstevel@tonic-gate 
701*7c478bd9Sstevel@tonic-gate 	DEBUG(4, "Removing work file (%s)\n", file);
702*7c478bd9Sstevel@tonic-gate 
703*7c478bd9Sstevel@tonic-gate 	if (unlink(file) != 0) {
704*7c478bd9Sstevel@tonic-gate 		toCorrupt(file);
705*7c478bd9Sstevel@tonic-gate 		errent(Ct_UNLINK, file, errno, __FILE__, __LINE__);
706*7c478bd9Sstevel@tonic-gate 	}
707*7c478bd9Sstevel@tonic-gate 	return;
708*7c478bd9Sstevel@tonic-gate }
709*7c478bd9Sstevel@tonic-gate 
710*7c478bd9Sstevel@tonic-gate /*
711*7c478bd9Sstevel@tonic-gate  * findgrade - finds the highest priority job grade that is not locked
712*7c478bd9Sstevel@tonic-gate  * and that has jobs.
713*7c478bd9Sstevel@tonic-gate  *
714*7c478bd9Sstevel@tonic-gate  * job grade name is null, if no job grade is found.
715*7c478bd9Sstevel@tonic-gate  */
716*7c478bd9Sstevel@tonic-gate 
717*7c478bd9Sstevel@tonic-gate void
718*7c478bd9Sstevel@tonic-gate findgrade(dir, jobgrade)
719*7c478bd9Sstevel@tonic-gate char *dir, *jobgrade;
720*7c478bd9Sstevel@tonic-gate {
721*7c478bd9Sstevel@tonic-gate 	char prevgrade[MAXBASENAME+1], curgrade[MAXBASENAME+1],
722*7c478bd9Sstevel@tonic-gate 	     gradedir[MAXBASENAME+1];
723*7c478bd9Sstevel@tonic-gate 	char lockname[MAXFULLNAME];
724*7c478bd9Sstevel@tonic-gate 	char Cfile[MAXBASENAME+1];
725*7c478bd9Sstevel@tonic-gate 	DIR *p, *q;
726*7c478bd9Sstevel@tonic-gate 
727*7c478bd9Sstevel@tonic-gate 	*prevgrade = NULLCHAR;
728*7c478bd9Sstevel@tonic-gate 	p = opendir(dir);
729*7c478bd9Sstevel@tonic-gate 	ASSERT(p != NULL, Ct_OPEN, dir, errno);
730*7c478bd9Sstevel@tonic-gate 
731*7c478bd9Sstevel@tonic-gate 	while (gdirf(p, gradedir, dir) == TRUE) {
732*7c478bd9Sstevel@tonic-gate 		(void) sprintf(lockname, "%s.%.*s.%s", LOCKPRE, SYSNSIZE,
733*7c478bd9Sstevel@tonic-gate 		    Rmtname, gradedir);
734*7c478bd9Sstevel@tonic-gate 		if (cklock(lockname) == FAIL)
735*7c478bd9Sstevel@tonic-gate 			continue;
736*7c478bd9Sstevel@tonic-gate 		q = opendir(gradedir);
737*7c478bd9Sstevel@tonic-gate 		ASSERT(q != NULL, Ct_OPEN, gradedir, errno);
738*7c478bd9Sstevel@tonic-gate 		while (gnamef(q, Cfile) == TRUE) {
739*7c478bd9Sstevel@tonic-gate 			if (Cfile[0] == CMDPRE) {
740*7c478bd9Sstevel@tonic-gate 				if (*prevgrade == NULLCHAR) {
741*7c478bd9Sstevel@tonic-gate 					(void) strcpy(prevgrade, gradedir);
742*7c478bd9Sstevel@tonic-gate 					break;
743*7c478bd9Sstevel@tonic-gate 				}
744*7c478bd9Sstevel@tonic-gate 				(void) strcpy(curgrade, gradedir);
745*7c478bd9Sstevel@tonic-gate 				if (strcmp(curgrade, prevgrade) < 0)
746*7c478bd9Sstevel@tonic-gate 					(void) strcpy(prevgrade, curgrade);
747*7c478bd9Sstevel@tonic-gate 			}
748*7c478bd9Sstevel@tonic-gate 		}
749*7c478bd9Sstevel@tonic-gate 		closedir(q);
750*7c478bd9Sstevel@tonic-gate 	}
751*7c478bd9Sstevel@tonic-gate 	closedir(p);
752*7c478bd9Sstevel@tonic-gate 	(void) strncpy(jobgrade, prevgrade, MAXBASENAME);
753*7c478bd9Sstevel@tonic-gate 	jobgrade[MAXBASENAME] = NULLCHAR;
754*7c478bd9Sstevel@tonic-gate 	return;
755*7c478bd9Sstevel@tonic-gate }
756