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
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]
24 * Copyright 1995 Sun Microsystems, Inc. All rights reserved.
25 * Use is subject to license terms.
28 #pragma ident "%Z%%M% %I% %E% SMI"
31 * Exported file system table manager. Reads/writes "/etc/xtab".
35 #include <exportent.h>
39 extern char *strtok();
40 extern char *strcpy();
44 static char *TMPFILE
= "/tmp/xtabXXXXXX";
46 static char *skipwhite(char *);
47 static char *skipnonwhite(char *);
56 * Create the tab file if it does not exist already
58 if (access(TABFILE
, F_OK
) < 0) {
59 fd
= open(TABFILE
, O_CREAT
, 0644);
62 if (access(TABFILE
, W_OK
) == 0) {
63 f
= fopen(TABFILE
, "r+");
65 f
= fopen(TABFILE
, "r");
70 if (flock(fileno(f
), LOCK_EX
) < 0) {
88 static char *line
= NULL
;
89 static struct exportent xent
;
94 line
= (char *)malloc(LINESIZE
+ 1);
96 if (fgets(line
, LINESIZE
, f
) == NULL
) {
100 if (line
[len
-1] == '\n') {
103 xent
.xent_dirname
= line
;
104 xent
.xent_options
= NULL
;
105 p
= skipnonwhite(line
);
117 xent
.xent_options
= p
;
122 remexportent(FILE *f
, char *dirname
)
134 fname
= (char *) malloc(strlen(TMPFILE
) + 1);
138 (void)strcpy(fname
, TMPFILE
);
143 if (unlink(fname
) < 0) {
147 f2
= fdopen(fd
, "r+");
152 len
= strlen(dirname
);
154 while (fgets(buf
, sizeof(buf
), f
)) {
155 if (strncmp(buf
, dirname
,
156 len
) != 0 || ! isspace((unsigned char)buf
[len
])) {
157 if (fputs(buf
, f2
) <= 0) {
162 remlen
= strlen(buf
);
163 rempos
= ftell(f
) - remlen
;
167 if (ftruncate(fileno(f
), 0L) < 0) {
172 while (fgets(buf
, sizeof(buf
), f2
)) {
173 if (fputs(buf
, f
) <= 0) {
180 /* nothing removed */
181 (void) fseek(f
, pos
, L_SET
);
183 } else if (pos
<= rempos
) {
184 res
= fseek(f
, pos
, L_SET
);
185 } else if (pos
> rempos
+ remlen
) {
186 res
= fseek(f
, pos
- remlen
, L_SET
);
188 res
= fseek(f
, rempos
, L_SET
);
190 return (res
< 0 ? -1 : 0);
194 addexportent(FILE *f
, char *dirname
, char *options
)
199 if (fseek(f
, 0L, L_XTND
) >= 0 &&
200 fprintf(f
, "%s", dirname
) > 0 &&
201 (options
== NULL
|| fprintf(f
, " -%s", options
) > 0) &&
202 fprintf(f
, "\n") > 0 &&
203 fseek(f
, pos
, L_SET
) >= 0) {
211 getexportopt(struct exportent
*xent
, char *opt
)
213 static char *tokenbuf
= NULL
;
218 if (tokenbuf
== NULL
) {
219 tokenbuf
= (char *)malloc(LINESIZE
);
221 if (xent
->xent_options
== NULL
) {
224 (void)strcpy(tokenbuf
, xent
->xent_options
);
227 while ((tok
= strtok(lp
, ",")) != NULL
) {
229 if (strncmp(opt
, tok
, len
) == 0) {
230 if (tok
[len
] == '=') {
231 return (&tok
[len
+ 1]);
232 } else if (tok
[len
] == 0) {
241 #define iswhite(c) ((c) == ' ' || c == '\t')
246 while (*str
&& iswhite(*str
)) {
253 skipnonwhite(char *str
)
255 while (*str
&& ! iswhite(*str
)) {