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