xref: /titanic_44/usr/src/cmd/lp/cmd/lpusers.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 /*
23  * Copyright 1997 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.10	*/
32 
33 /* lpusers [-q priority-level] -u (user-list | "")
34    lpusers -d priority-level
35    lpusers -l
36 */
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <stdio.h>
40 #include <sys/types.h>
41 #include <locale.h>
42 
43 #include "lp.h"
44 #include "users.h"
45 #include "msgs.h"
46 
47 #define	WHO_AM_I	I_AM_LPUSERS
48 #include "oam.h"
49 
50 char message[100],
51      reply[100];
52 
53 char	*PRIORITY;
54 
55 int add_user(), del_user();
56 
57 main(argc, argv)
58 int argc;
59 char *argv[];
60 {
61     int mtype, size, c,
62 	list = FALSE, limit = -1, deflt = -1;
63     int fd;
64     char *userlist = 0, *user, **users, *p;
65     short status;
66     struct user_priority *ppri_tbl, *ld_priority_file();
67     extern char *optarg;
68     extern int optind, opterr, optopt, errno;
69 
70     setlocale(LC_ALL, "");
71 
72 #if !defined(TEXT_DOMAIN)
73 #define TEXT_DOMAIN "SYS_TEST"
74 #endif
75     (void) textdomain(TEXT_DOMAIN);
76 
77 
78     if(argc == 1) {
79 usage:
80 	(void) printf(gettext("usage: \n"));
81   	(void) printf(gettext("(assign priority limit to users)\n"));
82 	(void) printf(gettext("\tlpusers -q priority -u user-list\n"));
83 
84   	(void) printf(gettext(
85 		"(assign default priority limit for balance of users)\n"));
86 	(void) printf(gettext("\tlpusers -q priority\n"));
87 
88   	(void) printf(gettext("(put users back to default priority limit)\n"));
89 	(void) printf(gettext("\tlpusers -u user-list\n"));
90 
91   	(void) printf(gettext("(assign default priority)\n"));
92 	(void) printf(gettext("\tlpusers -d priority\n"));
93 
94   	(void) printf(gettext("(examine priority limits, defaults)\n"));
95 	(void) printf(gettext("\tlpusers -l\n"));
96 
97 	exit(argc == 1);
98     }
99 
100     opterr = 0; /* disable printing of errors by getopt */
101     while ((c = getopt(argc, argv, "ld:q:u:")) != -1)
102 	switch(c) {
103 	case 'l':
104 	    if (list)
105 		LP_ERRMSG1(WARNING, E_LP_2MANY, 'l');
106 	    list = TRUE;
107 	    break;
108 	case 'd':
109 	    if (deflt != -1)
110 		LP_ERRMSG1(WARNING, E_LP_2MANY, 'd');
111 	    deflt = (int)strtol(optarg,&p,10);
112 	    if (*p || deflt<PRI_MIN || deflt>PRI_MAX) {
113 		LP_ERRMSG1(ERROR, E_LP_BADPRI, optarg);
114 		exit(1);
115 	    }
116 	    break;
117 	case 'q':
118 	    if (limit != -1)
119 		LP_ERRMSG1(WARNING, E_LP_2MANY, 'q');
120 	    limit = (int)strtol(optarg,&p,10);
121 	    if (*p || limit<PRI_MIN || limit>PRI_MAX) {
122 		LP_ERRMSG1(ERROR, E_LP_BADPRI, optarg);
123 		exit(1);
124 	    }
125 	    break;
126 	case 'u':
127 	    if (userlist)
128 		LP_ERRMSG1(WARNING, E_LP_2MANY, 'u');
129 	    userlist = optarg;
130 	    break;
131 	case '?':
132 	    if (optopt == '?') goto usage;
133 	    (p = "-X")[1] = optopt;
134 	    if (strchr("ldqu", optopt))
135 		LP_ERRMSG1(ERROR, E_LP_OPTARG, p);
136 	    else
137 		LP_ERRMSG1(ERROR, E_LP_OPTION, p);
138 	    exit(1);
139 	}
140 
141     if (optind < argc) {
142 	LP_ERRMSG1(ERROR, E_LP_EXTRA, argv[optind]);
143 	exit(1);
144     }
145 
146     if (((list || deflt != -1) && (limit != -1 || userlist))
147 	|| (list && deflt != -1)) {
148 	LP_ERRMSG(ERROR, E_LP_OPTCOMB);
149 	/* invalid combination of options */
150 	exit(1);
151     }
152 
153     PRIORITY = Lp_Users;
154 
155     /* load existing priorities from file */
156     if (!(ppri_tbl = ld_priority_file(PRIORITY))) {
157 	switch (errno) {
158 	case EBADF:
159 	    LP_ERRMSG1(ERROR, E_LPU_BADFORM, PRIORITY);
160 	    break;
161 	default:
162 	    LP_ERRMSG2(ERROR, E_LPU_BADFILE, PRIORITY, errno);
163 	}
164 	exit(1);
165     }
166 
167     if (list) {
168 	print_tbl(ppri_tbl);
169 	exit (0);
170     } else {
171 	if (userlist) {
172 	    users = getlist(userlist, " \t", ",");
173 	    if (users)
174 		while (user = *users++) {
175 		    if (del_user(ppri_tbl, user) && (limit == -1))
176 			LP_ERRMSG1(WARNING, E_LPU_NOUSER, user);
177 		    if (limit != -1) {
178 			if (add_user(ppri_tbl, user, limit))
179 			    LP_ERRMSG1(WARNING, E_LPU_BADU, user);
180 		    }
181 		}
182 	} else if (deflt != -1)
183 	    ppri_tbl->deflt = deflt;
184 	else
185 	    ppri_tbl->deflt_limit = limit;
186 
187 	if ((fd = open_locked(PRIORITY, "w", LPU_MODE)) < 0) {
188 	    LP_ERRMSG1(ERROR, E_LP_ACCESS, PRIORITY);
189 	    exit(1);
190 	}
191 	output_tbl(fd, ppri_tbl);
192 	close(fd);
193     }
194 
195     if (mopen()) /* error on mopen == no spooler, exit quietly */
196 	exit(0);
197 
198     (void)putmessage (message, S_LOAD_USER_FILE);
199 
200     if (msend(message))
201 	goto Error;
202     if (mrecv(reply, sizeof(reply)) == -1)
203 	goto Error;
204     mtype = getmessage(reply, R_LOAD_USER_FILE, &status);
205     if (mtype != R_LOAD_USER_FILE) {
206 	LP_ERRMSG1 (ERROR, E_LP_BADREPLY, mtype);
207 	goto NoError;
208     }
209 
210     if (status == 0)
211 	goto NoError;
212 
213 Error:	LP_ERRMSG (ERROR, E_LPU_NOLOAD);
214 
215 NoError:(void)mclose ();
216     exit(0);
217 }
218