xref: /illumos-gate/usr/src/cmd/fs.d/nfs/lib/sharetab.c (revision 54d34259)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a237e38eSth  * Common Development and Distribution License (the "License").
6a237e38eSth  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21a237e38eSth 
22*54d34259SMarcel Telka /*
23*54d34259SMarcel Telka  * Copyright 2015 Nexenta Systems, Inc.  All rights reserved.
24*54d34259SMarcel Telka  */
25*54d34259SMarcel Telka 
267c478bd9Sstevel@tonic-gate /*
27a237e38eSth  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
287c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
327c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate /*
357c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
367c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #include <stdio.h>
407c478bd9Sstevel@tonic-gate #include <stdlib.h>
417c478bd9Sstevel@tonic-gate #include <string.h>
427c478bd9Sstevel@tonic-gate #include <unistd.h>
43a237e38eSth #include <sharefs/share.h>
447c478bd9Sstevel@tonic-gate #include "sharetab.h"
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  * Get an entry from the share table.
487c478bd9Sstevel@tonic-gate  * There should be at least 4 fields:
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * 	pathname  resource  fstype  options  [ description ]
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * A fifth field (description) is optional.
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  * Returns:
557c478bd9Sstevel@tonic-gate  *	> 1  valid entry
567c478bd9Sstevel@tonic-gate  *	= 0  end of file
577c478bd9Sstevel@tonic-gate  *	< 0  error
587c478bd9Sstevel@tonic-gate  */
597c478bd9Sstevel@tonic-gate int
getshare(FILE * fd,share_t ** shp)60a237e38eSth getshare(FILE *fd, share_t **shp)
617c478bd9Sstevel@tonic-gate {
627c478bd9Sstevel@tonic-gate 	static char *line = NULL;
63a237e38eSth 	static share_t *sh = NULL;
647c478bd9Sstevel@tonic-gate 	char *p;
657c478bd9Sstevel@tonic-gate 	char *lasts;
667c478bd9Sstevel@tonic-gate 	char *w = " \t";
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	if (line == NULL) {
697c478bd9Sstevel@tonic-gate 		line = (char *)malloc(MAXBUFSIZE+1);
707c478bd9Sstevel@tonic-gate 		if (line == NULL)
717c478bd9Sstevel@tonic-gate 			return (-1);
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 	if (sh == NULL) {
74a237e38eSth 		sh = (share_t *)malloc(sizeof (*sh));
757c478bd9Sstevel@tonic-gate 		if (sh == NULL)
767c478bd9Sstevel@tonic-gate 			return (-1);
777c478bd9Sstevel@tonic-gate 	}
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	p = fgets(line, MAXBUFSIZE, fd);
807c478bd9Sstevel@tonic-gate 	if (p == NULL)
817c478bd9Sstevel@tonic-gate 		return (0);
827c478bd9Sstevel@tonic-gate 	line[strlen(line) - 1] = '\0';
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	sh->sh_path = (char *)strtok_r(p, w, &lasts);
857c478bd9Sstevel@tonic-gate 	if (sh->sh_path == NULL)
867c478bd9Sstevel@tonic-gate 		return (-3);
877c478bd9Sstevel@tonic-gate 	sh->sh_res = (char *)strtok_r(NULL, w, &lasts);
887c478bd9Sstevel@tonic-gate 	if (sh->sh_res == NULL)
897c478bd9Sstevel@tonic-gate 		return (-3);
907c478bd9Sstevel@tonic-gate 	sh->sh_fstype = (char *)strtok_r(NULL, w, &lasts);
917c478bd9Sstevel@tonic-gate 	if (sh->sh_fstype == NULL)
927c478bd9Sstevel@tonic-gate 		return (-3);
937c478bd9Sstevel@tonic-gate 	sh->sh_opts = (char *)strtok_r(NULL, w, &lasts);
947c478bd9Sstevel@tonic-gate 	if (sh->sh_opts == NULL)
957c478bd9Sstevel@tonic-gate 		return (-3);
967c478bd9Sstevel@tonic-gate 	sh->sh_descr = (char *)strtok_r(NULL, "", &lasts);
977c478bd9Sstevel@tonic-gate 	if (sh->sh_descr == NULL)
987c478bd9Sstevel@tonic-gate 		sh->sh_descr = "";
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	*shp = sh;
1017c478bd9Sstevel@tonic-gate 	return (1);
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
104a237e38eSth share_t *
sharedup(share_t * sh)105a237e38eSth sharedup(share_t *sh)
1067c478bd9Sstevel@tonic-gate {
107a237e38eSth 	share_t *nsh;
1087c478bd9Sstevel@tonic-gate 
109a237e38eSth 	nsh = (share_t *)calloc(1, sizeof (*nsh));
1107c478bd9Sstevel@tonic-gate 	if (nsh == NULL)
1117c478bd9Sstevel@tonic-gate 		return (NULL);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 	if (sh->sh_path) {
1147c478bd9Sstevel@tonic-gate 		nsh->sh_path = strdup(sh->sh_path);
1157c478bd9Sstevel@tonic-gate 		if (nsh->sh_path == NULL)
1167c478bd9Sstevel@tonic-gate 			goto alloc_failed;
1177c478bd9Sstevel@tonic-gate 	}
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	if (sh->sh_res) {
1207c478bd9Sstevel@tonic-gate 		nsh->sh_res = strdup(sh->sh_res);
1217c478bd9Sstevel@tonic-gate 		if (nsh->sh_res == NULL)
1227c478bd9Sstevel@tonic-gate 			goto alloc_failed;
1237c478bd9Sstevel@tonic-gate 	}
1247c478bd9Sstevel@tonic-gate 	if (sh->sh_fstype) {
1257c478bd9Sstevel@tonic-gate 		nsh->sh_fstype = strdup(sh->sh_fstype);
1267c478bd9Sstevel@tonic-gate 		if (nsh->sh_fstype == NULL)
1277c478bd9Sstevel@tonic-gate 			goto alloc_failed;
1287c478bd9Sstevel@tonic-gate 	}
1297c478bd9Sstevel@tonic-gate 	if (sh->sh_opts) {
1307c478bd9Sstevel@tonic-gate 		nsh->sh_opts = strdup(sh->sh_opts);
1317c478bd9Sstevel@tonic-gate 		if (nsh->sh_opts == NULL)
1327c478bd9Sstevel@tonic-gate 			goto alloc_failed;
1337c478bd9Sstevel@tonic-gate 	}
1347c478bd9Sstevel@tonic-gate 	if (sh->sh_descr) {
1357c478bd9Sstevel@tonic-gate 		nsh->sh_descr = strdup(sh->sh_descr);
1367c478bd9Sstevel@tonic-gate 		if (nsh->sh_descr == NULL)
1377c478bd9Sstevel@tonic-gate 			goto alloc_failed;
1387c478bd9Sstevel@tonic-gate 	}
1397c478bd9Sstevel@tonic-gate 	return (nsh);
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate alloc_failed:
1427c478bd9Sstevel@tonic-gate 	sharefree(nsh);
1437c478bd9Sstevel@tonic-gate 	return (NULL);
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate void
sharefree(share_t * sh)147a237e38eSth sharefree(share_t *sh)
1487c478bd9Sstevel@tonic-gate {
1497c478bd9Sstevel@tonic-gate 	if (sh->sh_path != NULL)
1507c478bd9Sstevel@tonic-gate 		free(sh->sh_path);
1517c478bd9Sstevel@tonic-gate 	if (sh->sh_res != NULL)
1527c478bd9Sstevel@tonic-gate 		free(sh->sh_res);
1537c478bd9Sstevel@tonic-gate 	if (sh->sh_fstype != NULL)
1547c478bd9Sstevel@tonic-gate 		free(sh->sh_fstype);
1557c478bd9Sstevel@tonic-gate 	if (sh->sh_opts != NULL)
1567c478bd9Sstevel@tonic-gate 		free(sh->sh_opts);
1577c478bd9Sstevel@tonic-gate 	if (sh->sh_descr != NULL)
1587c478bd9Sstevel@tonic-gate 		free(sh->sh_descr);
1597c478bd9Sstevel@tonic-gate 	free(sh);
1607c478bd9Sstevel@tonic-gate }
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate  * Return the value after "=" for option "opt"
1647c478bd9Sstevel@tonic-gate  * in option string "optlist". Caller must
1657c478bd9Sstevel@tonic-gate  * free returned value.
1667c478bd9Sstevel@tonic-gate  */
1677c478bd9Sstevel@tonic-gate char *
getshareopt(char * optlist,char * opt)168a237e38eSth getshareopt(char *optlist, char *opt)
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate 	char *p, *pe;
1717c478bd9Sstevel@tonic-gate 	char *b;
1727c478bd9Sstevel@tonic-gate 	char *bb;
1737c478bd9Sstevel@tonic-gate 	char *lasts;
1747c478bd9Sstevel@tonic-gate 	char *val = NULL;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	b = bb = strdup(optlist);
1777c478bd9Sstevel@tonic-gate 	if (b == NULL)
1787c478bd9Sstevel@tonic-gate 		return (NULL);
1797c478bd9Sstevel@tonic-gate 
180*54d34259SMarcel Telka 	while ((p = strtok_r(b, ",", &lasts)) != NULL) {
1817c478bd9Sstevel@tonic-gate 		b = NULL;
182*54d34259SMarcel Telka 		if ((pe = strchr(p, '=')) != NULL) {
1837c478bd9Sstevel@tonic-gate 			*pe = '\0';
1847c478bd9Sstevel@tonic-gate 			if (strcmp(opt, p) == 0) {
1857c478bd9Sstevel@tonic-gate 				val = strdup(pe + 1);
1867c478bd9Sstevel@tonic-gate 				goto done;
1877c478bd9Sstevel@tonic-gate 			}
1887c478bd9Sstevel@tonic-gate 		}
1897c478bd9Sstevel@tonic-gate 		if (strcmp(opt, p) == 0) {
1907c478bd9Sstevel@tonic-gate 			val = strdup("");
1917c478bd9Sstevel@tonic-gate 			goto done;
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate done:
1957c478bd9Sstevel@tonic-gate 	free(bb);
1967c478bd9Sstevel@tonic-gate 	return (val);
1977c478bd9Sstevel@tonic-gate }
198