dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / oamuser / group / add_group.c
blob6bc354a7bd0baff9272c13aba7281ef1805760cf
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.3 */
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <stdio.h>
35 #include <unistd.h>
36 #include <userdefs.h>
38 #define GRPTMP "/etc/gtmp"
39 #define GRPBUFSIZ 5120
41 int
42 add_group(group, gid)
43 char *group; /* name of group to add */
44 gid_t gid; /* gid of group to add */
46 FILE *etcgrp; /* /etc/group file */
47 FILE *etctmp; /* temp file */
48 int o_mask; /* old umask value */
49 int newdone = 0; /* set true when new entry done */
50 struct stat sb; /* stat buf to copy modes */
51 char buf[GRPBUFSIZ];
53 if ((etcgrp = fopen(GROUP, "r")) == NULL) {
54 return (EX_UPDATE);
57 if (fstat(fileno(etcgrp), &sb) < 0) {
58 /* If we can't get mode, take a default */
59 sb.st_mode = S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH;
62 o_mask = umask(077);
63 etctmp = fopen(GRPTMP, "w+");
64 (void) umask(o_mask);
66 if (etctmp == NULL) {
67 fclose(etcgrp);
68 return (EX_UPDATE);
71 if (fchmod(fileno(etctmp), sb.st_mode) != 0 ||
72 fchown(fileno(etctmp), sb.st_uid, sb.st_gid) != 0 ||
73 lockf(fileno(etctmp), F_LOCK, 0) != 0) {
74 fclose(etcgrp);
75 fclose(etctmp);
76 unlink(GRPTMP);
77 return (EX_UPDATE);
80 while (fgets(buf, GRPBUFSIZ, etcgrp) != NULL) {
81 /* Check for NameService reference */
82 if (!newdone && (buf[0] == '+' || buf[0] == '-')) {
83 (void) fprintf(etctmp, "%s::%u:\n", group, gid);
84 newdone = 1;
87 fputs(buf, etctmp);
91 (void) fclose(etcgrp);
93 if (!newdone) {
94 (void) fprintf(etctmp, "%s::%u:\n", group, gid);
97 if (rename(GRPTMP, GROUP) < 0) {
98 fclose(etctmp);
99 unlink(GRPTMP);
100 return (EX_UPDATE);
103 (void) fclose(etctmp);
106 return (EX_SUCCESS);