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
35*3957Sth199096 #pragma ident "%Z%%M% %I% %E% SMI"
360Sstevel@tonic-gate
370Sstevel@tonic-gate #include <stdio.h>
380Sstevel@tonic-gate #include <string.h>
39*3957Sth199096 #include <sys/types.h>
40*3957Sth199096 #include <sys/types32.h>
41*3957Sth199096 #include <sharefs/share.h>
420Sstevel@tonic-gate #include "sharetab.h"
430Sstevel@tonic-gate
44*3957Sth199096 static share_t *sharedup();
45*3957Sth199096 static void sharefree();
460Sstevel@tonic-gate
470Sstevel@tonic-gate /*
480Sstevel@tonic-gate * Get an entry from the share table.
490Sstevel@tonic-gate * There should be at least 4 fields:
500Sstevel@tonic-gate *
510Sstevel@tonic-gate * pathname resource fstype options [ description ]
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * A fifth field (description) is optional.
540Sstevel@tonic-gate *
550Sstevel@tonic-gate * Returns:
560Sstevel@tonic-gate * > 1 valid entry
570Sstevel@tonic-gate * = 0 end of file
580Sstevel@tonic-gate * < 0 error
590Sstevel@tonic-gate */
600Sstevel@tonic-gate int
getshare(FILE * fd,share_t ** shp)61*3957Sth199096 getshare(FILE *fd, share_t **shp)
620Sstevel@tonic-gate {
630Sstevel@tonic-gate static char *line = NULL;
64*3957Sth199096 static share_t *sh = NULL;
650Sstevel@tonic-gate register char *p;
660Sstevel@tonic-gate char *w = " \t";
670Sstevel@tonic-gate
680Sstevel@tonic-gate if (line == NULL) {
69*3957Sth199096 line = (char *)malloc(BUFSIZ+1);
700Sstevel@tonic-gate if (line == NULL)
710Sstevel@tonic-gate return (-1);
720Sstevel@tonic-gate }
730Sstevel@tonic-gate if (sh == NULL) {
74*3957Sth199096 sh = (share_t *)malloc(sizeof (*sh));
750Sstevel@tonic-gate if (sh == NULL)
760Sstevel@tonic-gate return (-1);
770Sstevel@tonic-gate }
780Sstevel@tonic-gate
790Sstevel@tonic-gate p = fgets(line, BUFSIZ, fd);
800Sstevel@tonic-gate if (p == NULL)
810Sstevel@tonic-gate return (0);
820Sstevel@tonic-gate line[strlen(line) - 1] = '\0';
830Sstevel@tonic-gate
840Sstevel@tonic-gate sh->sh_path = strtok(p, w);
850Sstevel@tonic-gate if (sh->sh_path == NULL)
860Sstevel@tonic-gate return (-1);
870Sstevel@tonic-gate sh->sh_res = strtok(NULL, w);
880Sstevel@tonic-gate if (sh->sh_res == NULL)
890Sstevel@tonic-gate return (-1);
900Sstevel@tonic-gate sh->sh_fstype = strtok(NULL, w);
910Sstevel@tonic-gate if (sh->sh_fstype == NULL)
920Sstevel@tonic-gate return (-1);
930Sstevel@tonic-gate sh->sh_opts = strtok(NULL, w);
940Sstevel@tonic-gate if (sh->sh_opts == NULL)
950Sstevel@tonic-gate return (-1);
960Sstevel@tonic-gate sh->sh_descr = strtok(NULL, "");
970Sstevel@tonic-gate if (sh->sh_descr == NULL)
980Sstevel@tonic-gate sh->sh_descr = "";
990Sstevel@tonic-gate
1000Sstevel@tonic-gate *shp = sh;
1010Sstevel@tonic-gate return (1);
1020Sstevel@tonic-gate }
1030Sstevel@tonic-gate
104*3957Sth199096 static share_t *
sharedup(share_t * sh)105*3957Sth199096 sharedup(share_t *sh)
1060Sstevel@tonic-gate {
107*3957Sth199096 share_t *nsh;
1080Sstevel@tonic-gate
109*3957Sth199096 nsh = (share_t *)calloc(1, sizeof (*nsh));
1100Sstevel@tonic-gate if (nsh == NULL)
1110Sstevel@tonic-gate return (NULL);
112*3957Sth199096
1130Sstevel@tonic-gate nsh->sh_path = strdup(sh->sh_path);
1140Sstevel@tonic-gate if (nsh->sh_path == NULL)
1150Sstevel@tonic-gate goto alloc_failed;
1160Sstevel@tonic-gate nsh->sh_res = strdup(sh->sh_res);
1170Sstevel@tonic-gate if (nsh->sh_res == NULL)
1180Sstevel@tonic-gate goto alloc_failed;
1190Sstevel@tonic-gate nsh->sh_fstype = strdup(sh->sh_fstype);
1200Sstevel@tonic-gate if (nsh->sh_fstype == NULL)
1210Sstevel@tonic-gate goto alloc_failed;
1220Sstevel@tonic-gate nsh->sh_opts = strdup(sh->sh_opts);
1230Sstevel@tonic-gate if (nsh->sh_opts == NULL)
1240Sstevel@tonic-gate goto alloc_failed;
1250Sstevel@tonic-gate nsh->sh_descr = strdup(sh->sh_descr);
1260Sstevel@tonic-gate if (nsh->sh_descr == NULL)
1270Sstevel@tonic-gate goto alloc_failed;
1280Sstevel@tonic-gate return (nsh);
1290Sstevel@tonic-gate
1300Sstevel@tonic-gate alloc_failed:
1310Sstevel@tonic-gate sharefree(nsh);
1320Sstevel@tonic-gate return (NULL);
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate static void
sharefree(share_t * sh)136*3957Sth199096 sharefree(share_t *sh)
1370Sstevel@tonic-gate {
1380Sstevel@tonic-gate if (sh == NULL)
1390Sstevel@tonic-gate return;
1400Sstevel@tonic-gate if (sh->sh_path != NULL)
1410Sstevel@tonic-gate free(sh->sh_path);
1420Sstevel@tonic-gate if (sh->sh_res != NULL)
1430Sstevel@tonic-gate free(sh->sh_res);
1440Sstevel@tonic-gate if (sh->sh_fstype != NULL)
1450Sstevel@tonic-gate free(sh->sh_fstype);
1460Sstevel@tonic-gate if (sh->sh_opts != NULL)
1470Sstevel@tonic-gate free(sh->sh_opts);
1480Sstevel@tonic-gate if (sh->sh_descr != NULL)
1490Sstevel@tonic-gate free(sh->sh_descr);
1500Sstevel@tonic-gate free(sh);
1510Sstevel@tonic-gate }
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate /*
1540Sstevel@tonic-gate * Return the value after "=" for option "opt"
1550Sstevel@tonic-gate * in option string "optlist".
1560Sstevel@tonic-gate */
1570Sstevel@tonic-gate char *
getshareopt(char * optlist,char * opt)158*3957Sth199096 getshareopt(char *optlist, char *opt)
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate char *p, *pe;
1610Sstevel@tonic-gate char *b;
1620Sstevel@tonic-gate static char *bb;
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate if (bb)
1650Sstevel@tonic-gate free(bb);
1660Sstevel@tonic-gate b = bb = strdup(optlist);
1670Sstevel@tonic-gate if (b == NULL)
1680Sstevel@tonic-gate return (NULL);
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate while (p = strtok(b, ",")) {
1710Sstevel@tonic-gate b = NULL;
1720Sstevel@tonic-gate if (pe = strchr(p, '=')) {
1730Sstevel@tonic-gate *pe = '\0';
1740Sstevel@tonic-gate if (strcmp(opt, p) == 0)
1750Sstevel@tonic-gate return (pe + 1);
1760Sstevel@tonic-gate }
1770Sstevel@tonic-gate if (strcmp(opt, p) == 0)
1780Sstevel@tonic-gate return ("");
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate return (NULL);
1820Sstevel@tonic-gate }
183