xref: /illumos-gate/usr/src/cmd/getent/dogetproject.c (revision 9b9d39d2a32ff806d2431dbcc50968ef1e6d46b2)
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 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <stdio.h>
28 #include <grp.h>
29 #include <stdlib.h>
30 #include <project.h>
31 #include "getent.h"
32 
33 static int
34 putprojent(const struct project *proj, FILE *fp)
35 {
36 	char **names;
37 
38 	if (proj == NULL)
39 		return (1);
40 
41 	if (fprintf(fp, "%s:%ld:%s:",
42 	    proj->pj_name != NULL ? proj->pj_name : "",
43 	    proj->pj_projid,
44 	    proj->pj_comment != NULL ? proj->pj_comment : "") == EOF)
45 		return (1);
46 	names = proj->pj_users;
47 	if (names != NULL) {
48 		if (*names != NULL)
49 			if (fputs(*names++, fp) == EOF)
50 				return (1);
51 		while (*names != NULL)
52 			if (fprintf(fp, ",%s", *names++) == EOF)
53 				return (1);
54 	}
55 	if (putc(':', fp) == EOF)
56 		return (1);
57 	names = proj->pj_groups;
58 	if (names != NULL) {
59 		if (*names != NULL)
60 			if (fputs(*names++, fp) == EOF)
61 				return (1);
62 		while (*names != NULL)
63 			if (fprintf(fp, ",%s", *names++) == EOF)
64 				return (1);
65 	}
66 	if (putc(':', fp) == EOF)
67 		return (1);
68 	if (fprintf(fp, "%s\n",
69 	    proj->pj_attr != NULL ? proj->pj_attr : "") == EOF)
70 		return (1);
71 	return (0);
72 }
73 
74 int
75 dogetproject(const char **list)
76 {
77 	struct project proj;
78 	struct project *pproj;
79 	projid_t projid;
80 	void *buf[PROJECT_BUFSZ];
81 	int rc = EXC_SUCCESS;
82 	char *ptr;
83 
84 	if (list == NULL || *list == NULL) {
85 		setprojent();
86 		while ((pproj = getprojent(&proj, buf, PROJECT_BUFSZ)) != NULL)
87 			(void) putprojent(pproj, stdout);
88 		endprojent();
89 	} else {
90 		for (; *list != NULL; list++) {
91 			projid = strtol(*list, &ptr, 10);
92 			if (ptr == *list)
93 				pproj = getprojbyname(*list, &proj,
94 				    buf, PROJECT_BUFSZ);
95 			else
96 				pproj = getprojbyid(projid, &proj,
97 				    buf, PROJECT_BUFSZ);
98 			if (pproj == NULL)
99 				rc = EXC_NAME_NOT_FOUND;
100 			else
101 				(void) putprojent(pproj, stdout);
102 		}
103 	}
104 
105 	return (rc);
106 }
107