8987 bootadm: add bootfile fallback to unix
[unleashed.git] / usr / src / cmd / oamuser / group / del_group.c
blob1495798dd1f8c6ec0355e8fd33a8d5a0ccc01d7f
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 #pragma ident "%Z%%M% %I% %E% SMI"
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <stdio.h>
36 #include <ctype.h>
37 #include <grp.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <userdefs.h>
41 #include <stdlib.h>
42 #include "users.h"
43 #include "messages.h"
45 /* lint error-killers: */
46 int errmsg(int, int);
48 /* Delete a group from the GROUP file */
49 int
50 del_group(char *group)
52 int deleted;
53 FILE *e_fptr, *t_fptr;
54 struct group *grpstruct;
55 char tname[] = "/etc/gtmp.XXXXXX";
56 int fd;
57 struct stat sbuf;
58 boolean_t haserr;
59 int line = 1;
61 if ((e_fptr = fopen(GROUP, "r")) == NULL)
62 return (EX_UPDATE);
64 if (fstat(fileno(e_fptr), &sbuf) != 0)
65 return (EX_UPDATE);
67 if ((fd = mkstemp(tname)) == -1)
68 return (EX_UPDATE);
70 if ((t_fptr = fdopen(fd, "w")) == NULL) {
71 (void) close(fd);
72 (void) unlink(tname);
73 return (EX_UPDATE);
77 * Get ownership and permissions correct
80 if (fchmod(fd, sbuf.st_mode) != 0 ||
81 fchown(fd, sbuf.st_uid, sbuf.st_gid) != 0) {
82 (void) fclose(t_fptr);
83 (void) unlink(tname);
84 return (EX_UPDATE);
87 /* loop thru GROUP looking for the one to delete */
88 deleted = 0;
90 while ((grpstruct = fgetgrent(e_fptr)) != NULL) {
92 /* check to see if group is one to delete */
93 if (strcmp(grpstruct->gr_name, group) == 0)
94 deleted = 1;
95 else
96 putgrent(grpstruct, t_fptr);
97 line++;
100 haserr = !feof(e_fptr);
102 if (haserr)
103 errmsg(M_SYNTAX, line);
105 (void) fclose(e_fptr);
107 if (fclose(t_fptr) != 0 || haserr) {
108 /* GROUP file contains bad entries or write failed. */
109 (void) unlink(tname);
110 return (EX_UPDATE);
113 /* If deleted, update GROUP file */
114 if (deleted) {
115 if (rename(tname, GROUP) != 0) {
116 (void) unlink(tname);
117 return (EX_UPDATE);
119 return (EX_SUCCESS);
120 } else {
121 (void) unlink(tname);
122 return (EX_NAME_NOT_EXIST);