xref: /illumos-gate/usr/src/cmd/oamuser/lib/putgrent.c (revision 2a8bcb4e)
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 2003 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 #include <stdio.h>
30 #include <grp.h>
31 #include <unistd.h>
32 
33 /*
34  * putgrent()	function to write a group structure to a file
35  *		supports the use of group names that with + or -
36  */
37 void
putgrent(struct group * grpstr,FILE * to)38 putgrent(struct group *grpstr, FILE *to)
39 {
40 	char **memptr;		/* member vector pointer */
41 
42 	if (grpstr->gr_name[0] == '+' || grpstr->gr_name[0] == '-') {
43 		/*
44 		 * if the groupname starts with either a '+' or '-' then
45 		 * write out what we can as best as we can
46 		 * we assume that fgetgrent() set gr_gid to 0 so
47 		 * write a null entry instead of 0
48 		 * This should not break /etc/nsswitch.conf for any of
49 		 * "group: compat", "group: files", "group: nis"
50 		 *
51 		 */
52 		(void) fprintf(to, "%s:%s:", grpstr->gr_name,
53 			grpstr->gr_passwd != NULL ? grpstr->gr_passwd : "");
54 
55 		if (grpstr->gr_gid == 0) {
56 			(void) fprintf(to, ":");
57 		} else {
58 			(void) fprintf(to, "%ld:", grpstr->gr_gid);
59 		}
60 
61 		memptr = grpstr->gr_mem;
62 
63 		while (memptr != NULL && *memptr != NULL) {
64 			(void) fprintf(to, "%s", *memptr);
65 			memptr++;
66 			if (memptr != NULL && *memptr != NULL)
67 				(void) fprintf(to, ",");
68 		}
69 
70 		(void) fprintf(to, "\n");
71 	} else {
72 		/*
73 		 * otherwise write out all the fields in the group structure
74 		 *
75 		 */
76 		(void) fprintf(to, "%s:%s:%ld:", grpstr->gr_name,
77 			grpstr->gr_passwd, grpstr->gr_gid);
78 
79 		memptr = grpstr->gr_mem;
80 
81 		while (*memptr != NULL) {
82 			(void) fprintf(to, "%s", *memptr);
83 			memptr++;
84 			if (*memptr != NULL)
85 				(void) fprintf(to, ",");
86 		}
87 
88 		(void) fprintf(to, "\n");
89 	}
90 }
91