8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libbc / libc / gen / common / mntent.c
blob8a0b8ac37506d8d70c0d3f35c5107aadc02d8a60
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 1990 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <stdio.h>
30 #include <ctype.h>
31 #include <mntent.h>
32 #include <sys/file.h>
33 #include <malloc.h>
35 static int mntprtent(FILE *, struct mntent *);
37 static struct mntent *mntp;
39 struct mntent *
40 _mnt(void)
43 if (mntp == 0)
44 mntp = (struct mntent *)calloc(1, sizeof (struct mntent));
45 return (mntp);
48 static char *
49 mntstr(char **p)
51 unsigned char *cp = (unsigned char *) *p;
52 unsigned char *retstr;
54 while (*cp && isspace(*cp))
55 cp++;
56 retstr = cp;
57 while (*cp && !isspace(*cp))
58 cp++;
59 if (*cp) {
60 *cp = '\0';
61 cp++;
63 *p = (char *) cp;
64 return ((char *)retstr);
67 static int
68 mntdigit(char **p)
70 int value = 0;
71 unsigned char *cp = (unsigned char *) *p;
73 while (*cp && isspace(*cp))
74 cp++;
75 for (; *cp && isdigit(*cp); cp++) {
76 value *= 10;
77 value += *cp - '0';
79 while (*cp && !isspace(*cp))
80 cp++;
81 if (*cp) {
82 *cp = '\0';
83 cp++;
85 *p = (char *) cp;
86 return (value);
89 static int
90 mnttabscan(FILE *mnttabp, struct mntent *mnt)
92 static char *line = NULL;
93 char *cp;
95 if (line == NULL)
96 line = (char *)malloc(BUFSIZ+1);
97 do {
98 cp = fgets(line, BUFSIZ, mnttabp);
99 if (cp == NULL) {
100 return (EOF);
102 } while (*cp == '#');
103 mnt->mnt_fsname = mntstr(&cp);
104 if (*cp == '\0')
105 return (1);
106 mnt->mnt_dir = mntstr(&cp);
107 if (*cp == '\0')
108 return (2);
109 mnt->mnt_type = mntstr(&cp);
110 if (*cp == '\0')
111 return (3);
112 mnt->mnt_opts = mntstr(&cp);
113 if (*cp == '\0')
114 return (4);
115 mnt->mnt_freq = mntdigit(&cp);
116 if (*cp == '\0')
117 return (5);
118 mnt->mnt_passno = mntdigit(&cp);
119 return (6);
122 FILE *
123 setmntent(char *fname, char *flag)
125 FILE *mnttabp;
127 if ((mnttabp = fopen(fname, flag)) == NULL) {
128 return (NULL);
130 for (; *flag ; flag++) {
131 if (*flag == 'w' || *flag == 'a' || *flag == '+') {
132 if (flock(fileno(mnttabp), LOCK_EX) < 0) {
133 fclose(mnttabp);
134 return (NULL);
136 break;
139 return (mnttabp);
143 endmntent(FILE *mnttabp)
146 if (mnttabp) {
147 fclose(mnttabp);
149 return (1);
152 struct mntent *
153 getmntent(FILE *mnttabp)
155 int nfields;
157 if (mnttabp == 0)
158 return ((struct mntent *)0);
159 if (_mnt() == 0)
160 return ((struct mntent *)0);
161 nfields = mnttabscan(mnttabp, mntp);
162 if (nfields == EOF || nfields != 6)
163 return ((struct mntent *)0);
164 return (mntp);
168 addmntent(FILE *mnttabp, struct mntent *mnt)
170 if (fseek(mnttabp, 0L, 2) < 0)
171 return (1);
172 if (mnt == (struct mntent *)0)
173 return (1);
174 if (mnt->mnt_fsname == NULL || mnt->mnt_dir == NULL ||
175 mnt->mnt_type == NULL || mnt->mnt_opts == NULL)
176 return (1);
178 mntprtent(mnttabp, mnt);
179 return (0);
182 static char *
183 mntopt(char **p)
185 unsigned char *cp = (unsigned char *) *p;
186 unsigned char *retstr;
188 while (*cp && isspace(*cp))
189 cp++;
190 retstr = cp;
191 while (*cp && *cp != ',')
192 cp++;
193 if (*cp) {
194 *cp = '\0';
195 cp++;
197 *p = (char *) cp;
198 return ((char *)retstr);
201 char *
202 hasmntopt(struct mntent *mnt, char *opt)
204 char *f, *opts;
205 static char *tmpopts;
207 if (tmpopts == 0) {
208 tmpopts = (char *)calloc(256, sizeof (char));
209 if (tmpopts == 0)
210 return (0);
212 strcpy(tmpopts, mnt->mnt_opts);
213 opts = tmpopts;
214 f = mntopt(&opts);
215 for (; *f; f = mntopt(&opts)) {
216 if (strncmp(opt, f, strlen(opt)) == 0)
217 return (f - tmpopts + mnt->mnt_opts);
219 return (NULL);
222 static int
223 mntprtent(FILE *mnttabp, struct mntent *mnt)
225 fprintf(mnttabp, "%s %s %s %s %d %d\n",
226 mnt->mnt_fsname,
227 mnt->mnt_dir,
228 mnt->mnt_type,
229 mnt->mnt_opts,
230 mnt->mnt_freq,
231 mnt->mnt_passno);
232 return (0);