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