xref: /onnv-gate/usr/src/cmd/lp/lib/users/loadpri.c (revision 320:7e1ecf3ab9be)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
23*320Sceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*320Sceastha #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate # include	<errno.h>
330Sstevel@tonic-gate # include	<stdio.h>
340Sstevel@tonic-gate # include	<stdlib.h>
350Sstevel@tonic-gate 
360Sstevel@tonic-gate # include	"lp.h"
370Sstevel@tonic-gate # include	"users.h"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate static long pri;
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate   Input:  Path name of the user priority file.  It has the following
430Sstevel@tonic-gate 		format:
440Sstevel@tonic-gate 	1 line with a number representing the default priority level.
450Sstevel@tonic-gate 		This must be the first line of the file, and no extra
460Sstevel@tonic-gate 		white space is allowed between the priority value and
470Sstevel@tonic-gate 		the newline.
480Sstevel@tonic-gate 	1 line anywhere in the file with a number representing
490Sstevel@tonic-gate 		the default priority limit.  This number is followed
500Sstevel@tonic-gate 		by a ':', and no extra white space is allowed.
510Sstevel@tonic-gate 	any number of lines with a number followed by a ':', followed
520Sstevel@tonic-gate 		by a white space (blank, tab or newline) separated
530Sstevel@tonic-gate 		list of user names.  No white space is allowed
540Sstevel@tonic-gate 		between the priority value and the colon (:), but any
550Sstevel@tonic-gate 		amount is ok in the UID list.
560Sstevel@tonic-gate 
570Sstevel@tonic-gate   Note:  If the default priority level is missing, a value of 20 will
580Sstevel@tonic-gate 	be used.  If the default limit is missing, zero will be used.
590Sstevel@tonic-gate 	Also, the st_priority_file writes out the priority file in the
600Sstevel@tonic-gate 	same order as the fields occur in the user_priority structure,
610Sstevel@tonic-gate 	but the only order restriction is that the default level is
620Sstevel@tonic-gate 	the first this.  A priority level may occur more than once, and
630Sstevel@tonic-gate 	this function will group them together (but the defaults may
640Sstevel@tonic-gate 	only occur once, however the defaults may occur only once each.
650Sstevel@tonic-gate 
660Sstevel@tonic-gate   Output:  This function returns a pointer to a statically stored
670Sstevel@tonic-gate 	structure containing the priority information.
680Sstevel@tonic-gate 
690Sstevel@tonic-gate    Effect:  The user priority file is read and parsed.  Storage for
700Sstevel@tonic-gate 	the priorities are allocated and loaded.  In case of an error,
710Sstevel@tonic-gate 	it prints out an error message, and returns 0 (NULL).
720Sstevel@tonic-gate */
730Sstevel@tonic-gate 
ld_priority_file(char * path)740Sstevel@tonic-gate struct user_priority * ld_priority_file ( char * path )
750Sstevel@tonic-gate {
760Sstevel@tonic-gate     char				line[BUFSIZ],
770Sstevel@tonic-gate 					*p,
780Sstevel@tonic-gate 					*user,
790Sstevel@tonic-gate 					*next_user();
800Sstevel@tonic-gate     static struct user_priority		pri_tbl;
810Sstevel@tonic-gate     int					line_no	= 1,
820Sstevel@tonic-gate     					opri;
830Sstevel@tonic-gate     int fd;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate     if ((fd = open_locked(path, "r", 0)) < 0) {
860Sstevel@tonic-gate 	if (errno == ENOENT) {
870Sstevel@tonic-gate empty:
880Sstevel@tonic-gate 	    pri_tbl.deflt = LEVEL_DFLT;
890Sstevel@tonic-gate 	    pri_tbl.deflt_limit = LIMIT_DFLT;
900Sstevel@tonic-gate 	    memset ((char *)pri_tbl.users, 0, sizeof(pri_tbl.users));
910Sstevel@tonic-gate 	    return (&pri_tbl);
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate 	return(0);
940Sstevel@tonic-gate     }
950Sstevel@tonic-gate 
960Sstevel@tonic-gate     /* initialize table to empty */
970Sstevel@tonic-gate     pri_tbl.deflt = -1;
980Sstevel@tonic-gate     pri_tbl.deflt_limit = -1;
990Sstevel@tonic-gate     memset ((char *)pri_tbl.users, 0, sizeof(pri_tbl.users));
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate     /* this loop reads the line containing the default priority,
1020Sstevel@tonic-gate        if any, and the first priority limit.  p is left pointing
1030Sstevel@tonic-gate        to the colon (:) in the line with the first limit. */
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate     while (1)
1060Sstevel@tonic-gate     {
1070Sstevel@tonic-gate 	if (!(p = fdgets(line, BUFSIZ, fd)))
1080Sstevel@tonic-gate 	    goto empty;
1090Sstevel@tonic-gate 	p = line;
1100Sstevel@tonic-gate 	pri = strtol(line, &p, 10);
1110Sstevel@tonic-gate 	if (p == line)
1120Sstevel@tonic-gate 	    goto Error;
1130Sstevel@tonic-gate 	if (pri < PRI_MIN || pri > PRI_MAX)
1140Sstevel@tonic-gate 	    goto Error;
1150Sstevel@tonic-gate 	if (line_no == 1 && *p == '\n' && !p[1])
1160Sstevel@tonic-gate 	    pri_tbl.deflt = pri;
1170Sstevel@tonic-gate 	else
1180Sstevel@tonic-gate 	    if (*p == ':')
1190Sstevel@tonic-gate 	    {
1200Sstevel@tonic-gate 		p++;
1210Sstevel@tonic-gate 		break;
1220Sstevel@tonic-gate 	    }
1230Sstevel@tonic-gate 	    else
1240Sstevel@tonic-gate 		goto Error;
1250Sstevel@tonic-gate 	line_no++;
1260Sstevel@tonic-gate     }
1270Sstevel@tonic-gate 
1280Sstevel@tonic-gate     do
1290Sstevel@tonic-gate     {
1300Sstevel@tonic-gate 	/* search list for this priority */
1310Sstevel@tonic-gate 	opri = pri;
1320Sstevel@tonic-gate 	if (!(user = next_user(fd, line, &p)))
1330Sstevel@tonic-gate 	{
1340Sstevel@tonic-gate 	    if (pri_tbl.deflt_limit == -1)
1350Sstevel@tonic-gate 	    {
1360Sstevel@tonic-gate 		pri_tbl.deflt_limit = opri;
1370Sstevel@tonic-gate 		if (pri == -1) break;
1380Sstevel@tonic-gate 		if (!(user = next_user(fd, line, &p))) goto Error;
1390Sstevel@tonic-gate 	    }
1400Sstevel@tonic-gate 	    else
1410Sstevel@tonic-gate 	    {
1420Sstevel@tonic-gate Error:
1430Sstevel@tonic-gate 	        errno = EBADF;
1440Sstevel@tonic-gate 		close(fd);
1450Sstevel@tonic-gate 		return(0);
1460Sstevel@tonic-gate 	    }
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	do
1500Sstevel@tonic-gate 	{
1510Sstevel@tonic-gate 	    add_user (&pri_tbl, user, pri);
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate 	while ((user = next_user(fd, line, &p)));
1540Sstevel@tonic-gate     }
1550Sstevel@tonic-gate     while (pri != -1);
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate     if (pri_tbl.deflt == -1)
1580Sstevel@tonic-gate 	pri_tbl.deflt = LEVEL_DFLT;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate     if (pri_tbl.deflt_limit == -1)
1610Sstevel@tonic-gate 	pri_tbl.deflt_limit = LIMIT_DFLT;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate     close(fd);
1640Sstevel@tonic-gate     return (&pri_tbl);
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate Inputs:  A pointer to a limit structure, and a user.
1690Sstevel@tonic-gate Ouputs:  The limit structure is modified.
1700Sstevel@tonic-gate Effects: Adds <user> to the list of users, if it is not already
1710Sstevel@tonic-gate 	 there.
1720Sstevel@tonic-gate */
1730Sstevel@tonic-gate 
add_user(struct user_priority * ppri_tbl,char * user,int limit)1740Sstevel@tonic-gate int add_user ( struct user_priority * ppri_tbl, char * user, int limit )
1750Sstevel@tonic-gate {
1760Sstevel@tonic-gate     if (limit < PRI_MIN || PRI_MAX < limit)
1770Sstevel@tonic-gate 	return 1;
1780Sstevel@tonic-gate     addlist (&(ppri_tbl->users[limit - PRI_MIN]), user);
1790Sstevel@tonic-gate     return 0;
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate /*
1830Sstevel@tonic-gate Inputs:   The input file to read additional lines, a pointer to
1840Sstevel@tonic-gate 	  a buffer containing the current line, and to read additional
1850Sstevel@tonic-gate 	  lines into, and a pointer to the location pointer (a pointer
1860Sstevel@tonic-gate 	  into buf).
1870Sstevel@tonic-gate Outputs:  The routine returns the next user-id read or 0 if all the
1880Sstevel@tonic-gate 	  users for this priority are read.  The buffer, the location
1890Sstevel@tonic-gate 	  pointer, and the variable pri are modified as a side effect.
1900Sstevel@tonic-gate Effects:  The input buffer is scanned starting at *pp for the next
1910Sstevel@tonic-gate 	  user-id, if the end of the line is reached, the next line is
1920Sstevel@tonic-gate 	  read from the file.  If it scans the next priority value, the
1930Sstevel@tonic-gate 	  variable pri (static to this file), is set to that priority.
1940Sstevel@tonic-gate 	  EOF is indicated by setting this variable to -1, and also
1950Sstevel@tonic-gate 	  returning 0.
1960Sstevel@tonic-gate */
next_user(int fd,char * buf,char ** pp)1970Sstevel@tonic-gate char * next_user (int fd, char * buf, char ** pp )
1980Sstevel@tonic-gate {
1990Sstevel@tonic-gate     long	temp;
2000Sstevel@tonic-gate     char	*p;
201*320Sceastha     static	int beg_line = 0; /* assumes a partial line is in buf to start */
2020Sstevel@tonic-gate 
2030Sstevel@tonic-gate     do
2040Sstevel@tonic-gate     {
2050Sstevel@tonic-gate 	while (**pp == ' ' || **pp == '\n' || **pp == '\t')
2060Sstevel@tonic-gate 	    (*pp)++;
2070Sstevel@tonic-gate 	p = *pp;
2080Sstevel@tonic-gate 	if (*p)
2090Sstevel@tonic-gate 	{
2100Sstevel@tonic-gate 	    if (*p >= '0' && *p <= '9')
2110Sstevel@tonic-gate 	    {
2120Sstevel@tonic-gate 		temp = strtol(p, pp, 10);
2130Sstevel@tonic-gate 		if (beg_line && **pp == ':')
2140Sstevel@tonic-gate 		{
2150Sstevel@tonic-gate 		    (*pp)++;
2160Sstevel@tonic-gate 		    pri = temp;
2170Sstevel@tonic-gate 		    beg_line = 0;
2180Sstevel@tonic-gate 		    return (0);
2190Sstevel@tonic-gate 		}
2200Sstevel@tonic-gate 	    }
2210Sstevel@tonic-gate 
2220Sstevel@tonic-gate 	    for (; **pp && **pp != ' ' && **pp != '\n' && **pp != '\t'; (*pp)++)
2230Sstevel@tonic-gate 		;
2240Sstevel@tonic-gate 	    if (**pp)
2250Sstevel@tonic-gate 		*(*pp)++ = 0;
2260Sstevel@tonic-gate 	    beg_line = 0;
2270Sstevel@tonic-gate 	    return (p);
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 	beg_line = 1;
2300Sstevel@tonic-gate     }
2310Sstevel@tonic-gate     while (*pp = fdgets(buf, BUFSIZ, fd));
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate     pri = -1;
2340Sstevel@tonic-gate     return (0);
2350Sstevel@tonic-gate }
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate /*
2380Sstevel@tonic-gate Inputs:  A pointer to a priority table and a user.
2390Sstevel@tonic-gate Outputs: Zero if user found, else 1, and priority table is modified.
2400Sstevel@tonic-gate Effects: All occurences of <user> in the priority table will be removed.
2410Sstevel@tonic-gate 	 (There should only be one at most.)
2420Sstevel@tonic-gate */
del_user(struct user_priority * ppri_tbl,char * user)2430Sstevel@tonic-gate int del_user ( struct user_priority * ppri_tbl, char * user )
2440Sstevel@tonic-gate {
2450Sstevel@tonic-gate     int		limit;
2460Sstevel@tonic-gate 
2470Sstevel@tonic-gate     for (limit = PRI_MIN; limit <= PRI_MAX; limit++)
2480Sstevel@tonic-gate 	if (searchlist(user, ppri_tbl->users[limit - PRI_MIN]))
2490Sstevel@tonic-gate 	{
2500Sstevel@tonic-gate 	    dellist (&(ppri_tbl->users[limit - PRI_MIN]), user);
2510Sstevel@tonic-gate 	    return (0);
2520Sstevel@tonic-gate 	}
2530Sstevel@tonic-gate     return (1);
2540Sstevel@tonic-gate }
255