xref: /onnv-gate/usr/src/cmd/fs.d/nfs/lib/sharetab.c (revision 3957)
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
5*3957Sth199096  * Common Development and Distribution License (the "License").
6*3957Sth199096  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*3957Sth199096 
220Sstevel@tonic-gate /*
23*3957Sth199096  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
320Sstevel@tonic-gate  * under license from the Regents of the University of California.
330Sstevel@tonic-gate  */
340Sstevel@tonic-gate 
350Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <stdio.h>
380Sstevel@tonic-gate #include <stdlib.h>
390Sstevel@tonic-gate #include <string.h>
400Sstevel@tonic-gate #include <unistd.h>
41*3957Sth199096 #include <sharefs/share.h>
420Sstevel@tonic-gate #include "sharetab.h"
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate  * Get an entry from the share table.
460Sstevel@tonic-gate  * There should be at least 4 fields:
470Sstevel@tonic-gate  *
480Sstevel@tonic-gate  * 	pathname  resource  fstype  options  [ description ]
490Sstevel@tonic-gate  *
500Sstevel@tonic-gate  * A fifth field (description) is optional.
510Sstevel@tonic-gate  *
520Sstevel@tonic-gate  * Returns:
530Sstevel@tonic-gate  *	> 1  valid entry
540Sstevel@tonic-gate  *	= 0  end of file
550Sstevel@tonic-gate  *	< 0  error
560Sstevel@tonic-gate  */
570Sstevel@tonic-gate int
58*3957Sth199096 getshare(FILE *fd, share_t **shp)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate 	static char *line = NULL;
61*3957Sth199096 	static share_t *sh = NULL;
620Sstevel@tonic-gate 	char *p;
630Sstevel@tonic-gate 	char *lasts;
640Sstevel@tonic-gate 	char *w = " \t";
650Sstevel@tonic-gate 
660Sstevel@tonic-gate 	if (line == NULL) {
670Sstevel@tonic-gate 		line = (char *)malloc(MAXBUFSIZE+1);
680Sstevel@tonic-gate 		if (line == NULL)
690Sstevel@tonic-gate 			return (-1);
700Sstevel@tonic-gate 	}
710Sstevel@tonic-gate 	if (sh == NULL) {
72*3957Sth199096 		sh = (share_t *)malloc(sizeof (*sh));
730Sstevel@tonic-gate 		if (sh == NULL)
740Sstevel@tonic-gate 			return (-1);
750Sstevel@tonic-gate 	}
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	p = fgets(line, MAXBUFSIZE, fd);
780Sstevel@tonic-gate 	if (p == NULL)
790Sstevel@tonic-gate 		return (0);
800Sstevel@tonic-gate 	line[strlen(line) - 1] = '\0';
810Sstevel@tonic-gate 
820Sstevel@tonic-gate 	sh->sh_path = (char *)strtok_r(p, w, &lasts);
830Sstevel@tonic-gate 	if (sh->sh_path == NULL)
840Sstevel@tonic-gate 		return (-3);
850Sstevel@tonic-gate 	sh->sh_res = (char *)strtok_r(NULL, w, &lasts);
860Sstevel@tonic-gate 	if (sh->sh_res == NULL)
870Sstevel@tonic-gate 		return (-3);
880Sstevel@tonic-gate 	sh->sh_fstype = (char *)strtok_r(NULL, w, &lasts);
890Sstevel@tonic-gate 	if (sh->sh_fstype == NULL)
900Sstevel@tonic-gate 		return (-3);
910Sstevel@tonic-gate 	sh->sh_opts = (char *)strtok_r(NULL, w, &lasts);
920Sstevel@tonic-gate 	if (sh->sh_opts == NULL)
930Sstevel@tonic-gate 		return (-3);
940Sstevel@tonic-gate 	sh->sh_descr = (char *)strtok_r(NULL, "", &lasts);
950Sstevel@tonic-gate 	if (sh->sh_descr == NULL)
960Sstevel@tonic-gate 		sh->sh_descr = "";
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	*shp = sh;
990Sstevel@tonic-gate 	return (1);
1000Sstevel@tonic-gate }
1010Sstevel@tonic-gate 
102*3957Sth199096 share_t *
103*3957Sth199096 sharedup(share_t *sh)
1040Sstevel@tonic-gate {
105*3957Sth199096 	share_t *nsh;
1060Sstevel@tonic-gate 
107*3957Sth199096 	nsh = (share_t *)calloc(1, sizeof (*nsh));
1080Sstevel@tonic-gate 	if (nsh == NULL)
1090Sstevel@tonic-gate 		return (NULL);
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate 	if (sh->sh_path) {
1120Sstevel@tonic-gate 		nsh->sh_path = strdup(sh->sh_path);
1130Sstevel@tonic-gate 		if (nsh->sh_path == NULL)
1140Sstevel@tonic-gate 			goto alloc_failed;
1150Sstevel@tonic-gate 	}
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate 	if (sh->sh_res) {
1180Sstevel@tonic-gate 		nsh->sh_res = strdup(sh->sh_res);
1190Sstevel@tonic-gate 		if (nsh->sh_res == NULL)
1200Sstevel@tonic-gate 			goto alloc_failed;
1210Sstevel@tonic-gate 	}
1220Sstevel@tonic-gate 	if (sh->sh_fstype) {
1230Sstevel@tonic-gate 		nsh->sh_fstype = strdup(sh->sh_fstype);
1240Sstevel@tonic-gate 		if (nsh->sh_fstype == NULL)
1250Sstevel@tonic-gate 			goto alloc_failed;
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 	if (sh->sh_opts) {
1280Sstevel@tonic-gate 		nsh->sh_opts = strdup(sh->sh_opts);
1290Sstevel@tonic-gate 		if (nsh->sh_opts == NULL)
1300Sstevel@tonic-gate 			goto alloc_failed;
1310Sstevel@tonic-gate 	}
1320Sstevel@tonic-gate 	if (sh->sh_descr) {
1330Sstevel@tonic-gate 		nsh->sh_descr = strdup(sh->sh_descr);
1340Sstevel@tonic-gate 		if (nsh->sh_descr == NULL)
1350Sstevel@tonic-gate 			goto alloc_failed;
1360Sstevel@tonic-gate 	}
1370Sstevel@tonic-gate 	return (nsh);
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate alloc_failed:
1400Sstevel@tonic-gate 	sharefree(nsh);
1410Sstevel@tonic-gate 	return (NULL);
1420Sstevel@tonic-gate }
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate void
145*3957Sth199096 sharefree(share_t *sh)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate 	if (sh->sh_path != NULL)
1480Sstevel@tonic-gate 		free(sh->sh_path);
1490Sstevel@tonic-gate 	if (sh->sh_res != NULL)
1500Sstevel@tonic-gate 		free(sh->sh_res);
1510Sstevel@tonic-gate 	if (sh->sh_fstype != NULL)
1520Sstevel@tonic-gate 		free(sh->sh_fstype);
1530Sstevel@tonic-gate 	if (sh->sh_opts != NULL)
1540Sstevel@tonic-gate 		free(sh->sh_opts);
1550Sstevel@tonic-gate 	if (sh->sh_descr != NULL)
1560Sstevel@tonic-gate 		free(sh->sh_descr);
1570Sstevel@tonic-gate 	free(sh);
1580Sstevel@tonic-gate }
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate /*
1610Sstevel@tonic-gate  * Return the value after "=" for option "opt"
1620Sstevel@tonic-gate  * in option string "optlist". Caller must
1630Sstevel@tonic-gate  * free returned value.
1640Sstevel@tonic-gate  */
1650Sstevel@tonic-gate char *
166*3957Sth199096 getshareopt(char *optlist, char *opt)
1670Sstevel@tonic-gate {
1680Sstevel@tonic-gate 	char *p, *pe;
1690Sstevel@tonic-gate 	char *b;
1700Sstevel@tonic-gate 	char *bb;
1710Sstevel@tonic-gate 	char *lasts;
1720Sstevel@tonic-gate 	char *val = NULL;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	b = bb = strdup(optlist);
1750Sstevel@tonic-gate 	if (b == NULL)
1760Sstevel@tonic-gate 		return (NULL);
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	while (p = (char *)strtok_r(b, ",", &lasts)) {
1790Sstevel@tonic-gate 		b = NULL;
1800Sstevel@tonic-gate 		if (pe = strchr(p, '=')) {
1810Sstevel@tonic-gate 			*pe = '\0';
1820Sstevel@tonic-gate 			if (strcmp(opt, p) == 0) {
1830Sstevel@tonic-gate 				val = strdup(pe + 1);
1840Sstevel@tonic-gate 				goto done;
1850Sstevel@tonic-gate 			}
1860Sstevel@tonic-gate 		}
1870Sstevel@tonic-gate 		if (strcmp(opt, p) == 0) {
1880Sstevel@tonic-gate 			val = strdup("");
1890Sstevel@tonic-gate 			goto done;
1900Sstevel@tonic-gate 		}
1910Sstevel@tonic-gate 	}
1920Sstevel@tonic-gate done:
1930Sstevel@tonic-gate 	free(bb);
1940Sstevel@tonic-gate 	return (val);
1950Sstevel@tonic-gate }
196