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