xref: /titanic_44/usr/src/cmd/oamuser/user/val_lprj.c (revision 6927f468b0af7710df000f6b16f6ee413e1e3007)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include	<sys/types.h>
30 #include	<stdio.h>
31 #include	<strings.h>
32 #include	<sys/param.h>
33 #include	<users.h>
34 #include	<userdefs.h>
35 #include	<project.h>
36 #include	<errno.h>
37 #include	"messages.h"
38 
39 
40 static projid_t projlist[NPROJECTS_MAX + 1];
41 static int nproj_max = NPROJECTS_MAX;
42 
43 /* Validate a list of projects */
44 int **
45 valid_lproject(char *list)
46 {
47 	int n_invalid = 0;
48 	int i = 0;
49 	int j;
50 	char *ptr;
51 	struct project projent;
52 	int warning;
53 	char mybuf[PROJECT_BUFSZ];
54 
55 	if (!list || !*list)
56 		return ((int **)NULL);
57 
58 	while (ptr = strtok(((i || n_invalid) ? NULL : list), ",")) {
59 
60 		switch (valid_project(ptr, &projent, mybuf, sizeof (mybuf),
61 		    &warning)) {
62 		case INVALID:
63 			errmsg(M_INVALID, ptr, "project id");
64 			n_invalid++;
65 			break;
66 		case TOOBIG:
67 			errmsg(M_TOOBIG, "projid", ptr);
68 			n_invalid++;
69 			break;
70 		case UNIQUE:
71 			errmsg(M_PROJ_NOTUSED, ptr);
72 			n_invalid++;
73 			break;
74 		case NOTUNIQUE:
75 			if (!i)
76 				/* ignore respecified primary  */
77 				projlist[i++] = projent.pj_projid;
78 			else {
79 				/* Keep out duplicates */
80 				for (j = 0; j < i; j++)
81 					if (projent.pj_projid == projlist[j])
82 						break;
83 
84 				if (j == i)
85 					/* Not a duplicate */
86 					projlist[i++] = projent.pj_projid;
87 			}
88 			break;
89 		}
90 		if (warning)
91 			warningmsg(warning, ptr);
92 
93 		if (i >= nproj_max) {
94 			errmsg(M_MAXPROJECTS, nproj_max);
95 			break;
96 		}
97 	}
98 
99 	/* Terminate the list */
100 	projlist[i] = -1;
101 
102 	if (n_invalid)
103 		exit(EX_BADARG);
104 
105 	return ((int **)projlist);
106 }
107