xref: /titanic_50/usr/src/cmd/rexd/sharetab.c (revision a237e38e9161f0acd6451439d4a7dd597e66291d)
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
5*a237e38eSth199096  * Common Development and Distribution License (the "License").
6*a237e38eSth199096  * 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  */
21*a237e38eSth199096 
227c478bd9Sstevel@tonic-gate /*
23*a237e38eSth199096  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley 4.3 BSD
327c478bd9Sstevel@tonic-gate  * under license from the Regents of the University of California.
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
35*a237e38eSth199096 #pragma ident	"%Z%%M%	%I%	%E% SMI"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
39*a237e38eSth199096 #include <sys/types.h>
40*a237e38eSth199096 #include <sys/types32.h>
41*a237e38eSth199096 #include <sharefs/share.h>
427c478bd9Sstevel@tonic-gate #include "sharetab.h"
437c478bd9Sstevel@tonic-gate 
44*a237e38eSth199096 static share_t	*sharedup();
457c478bd9Sstevel@tonic-gate static void	sharefree();
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * Get an entry from the share table.
497c478bd9Sstevel@tonic-gate  * There should be at least 4 fields:
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  * 	pathname  resource  fstype  options  [ description ]
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  * A fifth field (description) is optional.
547c478bd9Sstevel@tonic-gate  *
557c478bd9Sstevel@tonic-gate  * Returns:
567c478bd9Sstevel@tonic-gate  *	> 1  valid entry
577c478bd9Sstevel@tonic-gate  *	= 0  end of file
587c478bd9Sstevel@tonic-gate  *	< 0  error
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate int
getshare(FILE * fd,share_t ** shp)61*a237e38eSth199096 getshare(FILE *fd, share_t **shp)
627c478bd9Sstevel@tonic-gate {
637c478bd9Sstevel@tonic-gate 	static char *line = NULL;
64*a237e38eSth199096 	static share_t *sh = NULL;
657c478bd9Sstevel@tonic-gate 	register char *p;
667c478bd9Sstevel@tonic-gate 	char *w = " \t";
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	if (line == NULL) {
697c478bd9Sstevel@tonic-gate 		line = (char *)malloc(BUFSIZ+1);
707c478bd9Sstevel@tonic-gate 		if (line == NULL)
717c478bd9Sstevel@tonic-gate 			return (-1);
727c478bd9Sstevel@tonic-gate 	}
737c478bd9Sstevel@tonic-gate 	if (sh == NULL) {
74*a237e38eSth199096 		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, BUFSIZ, 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 = strtok(p, w);
857c478bd9Sstevel@tonic-gate 	if (sh->sh_path == NULL)
867c478bd9Sstevel@tonic-gate 		return (-1);
877c478bd9Sstevel@tonic-gate 	sh->sh_res = strtok(NULL, w);
887c478bd9Sstevel@tonic-gate 	if (sh->sh_res == NULL)
897c478bd9Sstevel@tonic-gate 		return (-1);
907c478bd9Sstevel@tonic-gate 	sh->sh_fstype = strtok(NULL, w);
917c478bd9Sstevel@tonic-gate 	if (sh->sh_fstype == NULL)
927c478bd9Sstevel@tonic-gate 		return (-1);
937c478bd9Sstevel@tonic-gate 	sh->sh_opts = strtok(NULL, w);
947c478bd9Sstevel@tonic-gate 	if (sh->sh_opts == NULL)
957c478bd9Sstevel@tonic-gate 		return (-1);
967c478bd9Sstevel@tonic-gate 	sh->sh_descr = strtok(NULL, "");
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 
104*a237e38eSth199096 static share_t *
sharedup(share_t * sh)105*a237e38eSth199096 sharedup(share_t *sh)
1067c478bd9Sstevel@tonic-gate {
107*a237e38eSth199096 	share_t *nsh;
1087c478bd9Sstevel@tonic-gate 
109*a237e38eSth199096 	nsh = (share_t *)calloc(1, sizeof (*nsh));
1107c478bd9Sstevel@tonic-gate 	if (nsh == NULL)
1117c478bd9Sstevel@tonic-gate 		return (NULL);
112*a237e38eSth199096 
1137c478bd9Sstevel@tonic-gate 	nsh->sh_path = strdup(sh->sh_path);
1147c478bd9Sstevel@tonic-gate 	if (nsh->sh_path == NULL)
1157c478bd9Sstevel@tonic-gate 		goto alloc_failed;
1167c478bd9Sstevel@tonic-gate 	nsh->sh_res = strdup(sh->sh_res);
1177c478bd9Sstevel@tonic-gate 	if (nsh->sh_res == NULL)
1187c478bd9Sstevel@tonic-gate 		goto alloc_failed;
1197c478bd9Sstevel@tonic-gate 	nsh->sh_fstype = strdup(sh->sh_fstype);
1207c478bd9Sstevel@tonic-gate 	if (nsh->sh_fstype == NULL)
1217c478bd9Sstevel@tonic-gate 		goto alloc_failed;
1227c478bd9Sstevel@tonic-gate 	nsh->sh_opts = strdup(sh->sh_opts);
1237c478bd9Sstevel@tonic-gate 	if (nsh->sh_opts == NULL)
1247c478bd9Sstevel@tonic-gate 		goto alloc_failed;
1257c478bd9Sstevel@tonic-gate 	nsh->sh_descr = strdup(sh->sh_descr);
1267c478bd9Sstevel@tonic-gate 	if (nsh->sh_descr == NULL)
1277c478bd9Sstevel@tonic-gate 		goto alloc_failed;
1287c478bd9Sstevel@tonic-gate 	return (nsh);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate alloc_failed:
1317c478bd9Sstevel@tonic-gate 	sharefree(nsh);
1327c478bd9Sstevel@tonic-gate 	return (NULL);
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate static void
sharefree(share_t * sh)136*a237e38eSth199096 sharefree(share_t *sh)
1377c478bd9Sstevel@tonic-gate {
1387c478bd9Sstevel@tonic-gate 	if (sh == NULL)
1397c478bd9Sstevel@tonic-gate 		return;
1407c478bd9Sstevel@tonic-gate 	if (sh->sh_path != NULL)
1417c478bd9Sstevel@tonic-gate 		free(sh->sh_path);
1427c478bd9Sstevel@tonic-gate 	if (sh->sh_res != NULL)
1437c478bd9Sstevel@tonic-gate 		free(sh->sh_res);
1447c478bd9Sstevel@tonic-gate 	if (sh->sh_fstype != NULL)
1457c478bd9Sstevel@tonic-gate 		free(sh->sh_fstype);
1467c478bd9Sstevel@tonic-gate 	if (sh->sh_opts != NULL)
1477c478bd9Sstevel@tonic-gate 		free(sh->sh_opts);
1487c478bd9Sstevel@tonic-gate 	if (sh->sh_descr != NULL)
1497c478bd9Sstevel@tonic-gate 		free(sh->sh_descr);
1507c478bd9Sstevel@tonic-gate 	free(sh);
1517c478bd9Sstevel@tonic-gate }
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  * Return the value after "=" for option "opt"
1557c478bd9Sstevel@tonic-gate  * in option string "optlist".
1567c478bd9Sstevel@tonic-gate  */
1577c478bd9Sstevel@tonic-gate char *
getshareopt(char * optlist,char * opt)158*a237e38eSth199096 getshareopt(char *optlist, char *opt)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	char *p, *pe;
1617c478bd9Sstevel@tonic-gate 	char *b;
1627c478bd9Sstevel@tonic-gate 	static char *bb;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if (bb)
1657c478bd9Sstevel@tonic-gate 		free(bb);
1667c478bd9Sstevel@tonic-gate 	b = bb = strdup(optlist);
1677c478bd9Sstevel@tonic-gate 	if (b == NULL)
1687c478bd9Sstevel@tonic-gate 		return (NULL);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	while (p = strtok(b, ",")) {
1717c478bd9Sstevel@tonic-gate 		b = NULL;
1727c478bd9Sstevel@tonic-gate 		if (pe = strchr(p, '=')) {
1737c478bd9Sstevel@tonic-gate 			*pe = '\0';
1747c478bd9Sstevel@tonic-gate 			if (strcmp(opt, p) == 0)
1757c478bd9Sstevel@tonic-gate 				return (pe + 1);
1767c478bd9Sstevel@tonic-gate 		}
1777c478bd9Sstevel@tonic-gate 		if (strcmp(opt, p) == 0)
1787c478bd9Sstevel@tonic-gate 			return ("");
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	return (NULL);
1827c478bd9Sstevel@tonic-gate }
183