xref: /onnv-gate/usr/src/lib/libbc/libc/gen/common/exportent.c (revision 722:636b850d4ee9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*722Smuffin 
230Sstevel@tonic-gate /*
24*722Smuffin  * Copyright 1995 Sun Microsystems, Inc.  All rights reserved.
25*722Smuffin  * Use is subject to license terms.
260Sstevel@tonic-gate  */
270Sstevel@tonic-gate 
28*722Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Exported file system table manager. Reads/writes "/etc/xtab".
320Sstevel@tonic-gate  */
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include <stdio.h>
350Sstevel@tonic-gate #include <exportent.h>
360Sstevel@tonic-gate #include <sys/file.h>
370Sstevel@tonic-gate #include <ctype.h>
380Sstevel@tonic-gate 
390Sstevel@tonic-gate extern char *strtok();
400Sstevel@tonic-gate extern char *strcpy();
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #define LINESIZE 4096
430Sstevel@tonic-gate 
440Sstevel@tonic-gate static char *TMPFILE = "/tmp/xtabXXXXXX";
450Sstevel@tonic-gate 
46*722Smuffin static char *skipwhite(char *);
47*722Smuffin static char *skipnonwhite(char *);
480Sstevel@tonic-gate 
490Sstevel@tonic-gate FILE *
setexportent(void)50*722Smuffin setexportent(void)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate 	FILE *f;
530Sstevel@tonic-gate 	int fd;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 	/*
560Sstevel@tonic-gate 	 * Create the tab file if it does not exist already
570Sstevel@tonic-gate 	 */
580Sstevel@tonic-gate 	if (access(TABFILE, F_OK) < 0) {
590Sstevel@tonic-gate 		fd = open(TABFILE, O_CREAT, 0644);
600Sstevel@tonic-gate 		close(fd);
610Sstevel@tonic-gate 	}
620Sstevel@tonic-gate 	if (access(TABFILE, W_OK) == 0) {
630Sstevel@tonic-gate 		f = fopen(TABFILE, "r+");
640Sstevel@tonic-gate 	} else {
650Sstevel@tonic-gate 		f = fopen(TABFILE, "r");
660Sstevel@tonic-gate 	}
670Sstevel@tonic-gate 	if (f == NULL) {
680Sstevel@tonic-gate 	   	return (NULL);
690Sstevel@tonic-gate 	}
700Sstevel@tonic-gate 	if (flock(fileno(f), LOCK_EX) < 0) {
710Sstevel@tonic-gate 		(void)fclose(f);
720Sstevel@tonic-gate 		return (NULL);
730Sstevel@tonic-gate 	}
740Sstevel@tonic-gate 	return (f);
750Sstevel@tonic-gate }
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 
780Sstevel@tonic-gate void
endexportent(FILE * f)79*722Smuffin endexportent(FILE *f)
800Sstevel@tonic-gate {
81*722Smuffin 	(void) fclose(f);
820Sstevel@tonic-gate }
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 
850Sstevel@tonic-gate struct exportent *
getexportent(FILE * f)86*722Smuffin getexportent(FILE *f)
870Sstevel@tonic-gate {
880Sstevel@tonic-gate 	static char *line = NULL;
890Sstevel@tonic-gate 	static struct exportent xent;
900Sstevel@tonic-gate 	int len;
910Sstevel@tonic-gate 	char *p;
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	if (line == NULL) {
940Sstevel@tonic-gate 		line = (char *)malloc(LINESIZE + 1);
950Sstevel@tonic-gate 	}
960Sstevel@tonic-gate 	if (fgets(line, LINESIZE, f) == NULL) {
970Sstevel@tonic-gate 		return (NULL);
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate 	len = strlen(line);
1000Sstevel@tonic-gate 	if (line[len-1] == '\n') {
1010Sstevel@tonic-gate 		line[len-1] = 0;
1020Sstevel@tonic-gate 	}
1030Sstevel@tonic-gate 	xent.xent_dirname = line;
1040Sstevel@tonic-gate 	xent.xent_options = NULL;
1050Sstevel@tonic-gate 	p = skipnonwhite(line);
1060Sstevel@tonic-gate 	if (*p == 0) {
1070Sstevel@tonic-gate 		return (&xent);
1080Sstevel@tonic-gate 	}
1090Sstevel@tonic-gate 	*p++ = 0;
1100Sstevel@tonic-gate 	p = skipwhite(p);
1110Sstevel@tonic-gate 	if (*p == 0) {
1120Sstevel@tonic-gate 		return (&xent);
1130Sstevel@tonic-gate 	}
1140Sstevel@tonic-gate 	if (*p == '-') {
1150Sstevel@tonic-gate 		p++;
1160Sstevel@tonic-gate 	}
1170Sstevel@tonic-gate 	xent.xent_options = p;
1180Sstevel@tonic-gate 	return (&xent);
1190Sstevel@tonic-gate }
1200Sstevel@tonic-gate 
121*722Smuffin int
remexportent(FILE * f,char * dirname)122*722Smuffin remexportent(FILE *f, char *dirname)
1230Sstevel@tonic-gate {
1240Sstevel@tonic-gate 	char buf[LINESIZE];
1250Sstevel@tonic-gate 	FILE *f2;
1260Sstevel@tonic-gate 	int len;
1270Sstevel@tonic-gate 	char *fname;
1280Sstevel@tonic-gate 	int fd;
1290Sstevel@tonic-gate 	long pos;
1300Sstevel@tonic-gate 	long rempos;
1310Sstevel@tonic-gate 	int remlen;
1320Sstevel@tonic-gate 	int res;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	fname = (char *) malloc(strlen(TMPFILE) + 1);
1350Sstevel@tonic-gate 	pos = ftell(f);
1360Sstevel@tonic-gate 	rempos = 0;
1370Sstevel@tonic-gate 	remlen = 0;
1380Sstevel@tonic-gate 	(void)strcpy(fname, TMPFILE);
1390Sstevel@tonic-gate  	fd = mkstemp(fname);
1400Sstevel@tonic-gate 	if (fd < 0) {
1410Sstevel@tonic-gate 		return (-1);
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate 	if (unlink(fname) < 0) {
1440Sstevel@tonic-gate 		(void)close(fd);
1450Sstevel@tonic-gate 		return (-1);
1460Sstevel@tonic-gate 	}
1470Sstevel@tonic-gate 	f2 = fdopen(fd, "r+");
1480Sstevel@tonic-gate 	if (f2 == NULL) {
1490Sstevel@tonic-gate 		(void)close(fd);
1500Sstevel@tonic-gate 		return (-1);
1510Sstevel@tonic-gate 	}
1520Sstevel@tonic-gate 	len = strlen(dirname);
1530Sstevel@tonic-gate 	rewind(f);
1540Sstevel@tonic-gate 	while (fgets(buf, sizeof(buf), f)) {
1550Sstevel@tonic-gate 		if (strncmp(buf, dirname,
1560Sstevel@tonic-gate 		    len) != 0 || ! isspace((unsigned char)buf[len])) {
1570Sstevel@tonic-gate 			if (fputs(buf, f2) <= 0) {
1580Sstevel@tonic-gate 				(void)fclose(f2);
1590Sstevel@tonic-gate 				return (-1);
1600Sstevel@tonic-gate 			}
1610Sstevel@tonic-gate 		} else {
1620Sstevel@tonic-gate 			remlen = strlen(buf);
1630Sstevel@tonic-gate 			rempos = ftell(f) - remlen;
1640Sstevel@tonic-gate 		}
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 	rewind(f);
1670Sstevel@tonic-gate 	if (ftruncate(fileno(f), 0L) < 0) {
1680Sstevel@tonic-gate 		(void)fclose(f2);
1690Sstevel@tonic-gate 		return (-1);
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate 	rewind(f2);
1720Sstevel@tonic-gate 	while (fgets(buf, sizeof(buf), f2)) {
1730Sstevel@tonic-gate 		if (fputs(buf, f) <= 0) {
1740Sstevel@tonic-gate 			(void)fclose(f2);
1750Sstevel@tonic-gate 			return (-1);
1760Sstevel@tonic-gate 		}
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate 	(void)fclose(f2);
1790Sstevel@tonic-gate 	if (remlen == 0) {
1800Sstevel@tonic-gate 		/* nothing removed */
1810Sstevel@tonic-gate 		(void) fseek(f, pos, L_SET);
1820Sstevel@tonic-gate 		res = -1;
1830Sstevel@tonic-gate 	} else if (pos <= rempos) {
1840Sstevel@tonic-gate 		res = fseek(f, pos, L_SET);
1850Sstevel@tonic-gate 	} else if (pos > rempos + remlen) {
1860Sstevel@tonic-gate 		res = fseek(f, pos - remlen, L_SET);
1870Sstevel@tonic-gate 	} else {
1880Sstevel@tonic-gate 		res = fseek(f, rempos, L_SET);
1890Sstevel@tonic-gate 	}
1900Sstevel@tonic-gate 	return (res < 0 ? -1 : 0);
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate 
193*722Smuffin int
addexportent(FILE * f,char * dirname,char * options)194*722Smuffin addexportent(FILE *f, char *dirname, char *options)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate 	long pos;
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 	pos = ftell(f);
1990Sstevel@tonic-gate 	if (fseek(f, 0L, L_XTND) >= 0 &&
2000Sstevel@tonic-gate 	    fprintf(f, "%s", dirname) > 0 &&
2010Sstevel@tonic-gate 	    (options == NULL || fprintf(f, " -%s", options) > 0) &&
2020Sstevel@tonic-gate 	    fprintf(f, "\n") > 0 &&
2030Sstevel@tonic-gate 	    fseek(f, pos, L_SET) >= 0) {
2040Sstevel@tonic-gate 		return (0);
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 	return (-1);
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate char *
getexportopt(struct exportent * xent,char * opt)211*722Smuffin getexportopt(struct exportent *xent, char *opt)
2120Sstevel@tonic-gate {
2130Sstevel@tonic-gate 	static char *tokenbuf = NULL;
2140Sstevel@tonic-gate 	char *lp;
2150Sstevel@tonic-gate 	char *tok;
2160Sstevel@tonic-gate 	int len;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	if (tokenbuf == NULL) {
2190Sstevel@tonic-gate 		tokenbuf = (char *)malloc(LINESIZE);
2200Sstevel@tonic-gate 	}
2210Sstevel@tonic-gate 	if (xent->xent_options == NULL) {
2220Sstevel@tonic-gate 		return (NULL);
2230Sstevel@tonic-gate 	}
2240Sstevel@tonic-gate 	(void)strcpy(tokenbuf, xent->xent_options);
2250Sstevel@tonic-gate 	lp = tokenbuf;
2260Sstevel@tonic-gate 	len = strlen(opt);
2270Sstevel@tonic-gate 	while ((tok = strtok(lp, ",")) != NULL) {
2280Sstevel@tonic-gate 		lp = NULL;
2290Sstevel@tonic-gate 		if (strncmp(opt, tok, len) == 0) {
2300Sstevel@tonic-gate 			if (tok[len] == '=') {
2310Sstevel@tonic-gate 				return (&tok[len + 1]);
2320Sstevel@tonic-gate 			} else if (tok[len] == 0) {
2330Sstevel@tonic-gate 				return ("");
2340Sstevel@tonic-gate 			}
2350Sstevel@tonic-gate 		}
2360Sstevel@tonic-gate 	}
2370Sstevel@tonic-gate 	return (NULL);
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate #define iswhite(c) 	((c) == ' ' || c == '\t')
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate static char *
skipwhite(char * str)244*722Smuffin skipwhite(char *str)
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate 	while (*str && iswhite(*str)) {
2470Sstevel@tonic-gate 		str++;
2480Sstevel@tonic-gate 	}
2490Sstevel@tonic-gate 	return (str);
2500Sstevel@tonic-gate }
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate static char *
skipnonwhite(char * str)253*722Smuffin skipnonwhite(char *str)
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate 	while (*str && ! iswhite(*str)) {
2560Sstevel@tonic-gate 		str++;
2570Sstevel@tonic-gate 	}
2580Sstevel@tonic-gate 	return (str);
2590Sstevel@tonic-gate }
260