xref: /titanic_51/usr/src/cmd/fs.d/nfs/lib/sharetab.c (revision dd4eeefdb8e4583c47e28a7f315db6087931ef06)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #pragma ident	"%Z%%M%	%I%	%E% SMI"
36 
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <sharefs/share.h>
42 #include "sharetab.h"
43 
44 /*
45  * Get an entry from the share table.
46  * There should be at least 4 fields:
47  *
48  * 	pathname  resource  fstype  options  [ description ]
49  *
50  * A fifth field (description) is optional.
51  *
52  * Returns:
53  *	> 1  valid entry
54  *	= 0  end of file
55  *	< 0  error
56  */
57 int
58 getshare(FILE *fd, share_t **shp)
59 {
60 	static char *line = NULL;
61 	static share_t *sh = NULL;
62 	char *p;
63 	char *lasts;
64 	char *w = " \t";
65 
66 	if (line == NULL) {
67 		line = (char *)malloc(MAXBUFSIZE+1);
68 		if (line == NULL)
69 			return (-1);
70 	}
71 	if (sh == NULL) {
72 		sh = (share_t *)malloc(sizeof (*sh));
73 		if (sh == NULL)
74 			return (-1);
75 	}
76 
77 	p = fgets(line, MAXBUFSIZE, fd);
78 	if (p == NULL)
79 		return (0);
80 	line[strlen(line) - 1] = '\0';
81 
82 	sh->sh_path = (char *)strtok_r(p, w, &lasts);
83 	if (sh->sh_path == NULL)
84 		return (-3);
85 	sh->sh_res = (char *)strtok_r(NULL, w, &lasts);
86 	if (sh->sh_res == NULL)
87 		return (-3);
88 	sh->sh_fstype = (char *)strtok_r(NULL, w, &lasts);
89 	if (sh->sh_fstype == NULL)
90 		return (-3);
91 	sh->sh_opts = (char *)strtok_r(NULL, w, &lasts);
92 	if (sh->sh_opts == NULL)
93 		return (-3);
94 	sh->sh_descr = (char *)strtok_r(NULL, "", &lasts);
95 	if (sh->sh_descr == NULL)
96 		sh->sh_descr = "";
97 
98 	*shp = sh;
99 	return (1);
100 }
101 
102 share_t *
103 sharedup(share_t *sh)
104 {
105 	share_t *nsh;
106 
107 	nsh = (share_t *)calloc(1, sizeof (*nsh));
108 	if (nsh == NULL)
109 		return (NULL);
110 
111 	if (sh->sh_path) {
112 		nsh->sh_path = strdup(sh->sh_path);
113 		if (nsh->sh_path == NULL)
114 			goto alloc_failed;
115 	}
116 
117 	if (sh->sh_res) {
118 		nsh->sh_res = strdup(sh->sh_res);
119 		if (nsh->sh_res == NULL)
120 			goto alloc_failed;
121 	}
122 	if (sh->sh_fstype) {
123 		nsh->sh_fstype = strdup(sh->sh_fstype);
124 		if (nsh->sh_fstype == NULL)
125 			goto alloc_failed;
126 	}
127 	if (sh->sh_opts) {
128 		nsh->sh_opts = strdup(sh->sh_opts);
129 		if (nsh->sh_opts == NULL)
130 			goto alloc_failed;
131 	}
132 	if (sh->sh_descr) {
133 		nsh->sh_descr = strdup(sh->sh_descr);
134 		if (nsh->sh_descr == NULL)
135 			goto alloc_failed;
136 	}
137 	return (nsh);
138 
139 alloc_failed:
140 	sharefree(nsh);
141 	return (NULL);
142 }
143 
144 void
145 sharefree(share_t *sh)
146 {
147 	if (sh->sh_path != NULL)
148 		free(sh->sh_path);
149 	if (sh->sh_res != NULL)
150 		free(sh->sh_res);
151 	if (sh->sh_fstype != NULL)
152 		free(sh->sh_fstype);
153 	if (sh->sh_opts != NULL)
154 		free(sh->sh_opts);
155 	if (sh->sh_descr != NULL)
156 		free(sh->sh_descr);
157 	free(sh);
158 }
159 
160 /*
161  * Return the value after "=" for option "opt"
162  * in option string "optlist". Caller must
163  * free returned value.
164  */
165 char *
166 getshareopt(char *optlist, char *opt)
167 {
168 	char *p, *pe;
169 	char *b;
170 	char *bb;
171 	char *lasts;
172 	char *val = NULL;
173 
174 	b = bb = strdup(optlist);
175 	if (b == NULL)
176 		return (NULL);
177 
178 	while (p = (char *)strtok_r(b, ",", &lasts)) {
179 		b = NULL;
180 		if (pe = strchr(p, '=')) {
181 			*pe = '\0';
182 			if (strcmp(opt, p) == 0) {
183 				val = strdup(pe + 1);
184 				goto done;
185 			}
186 		}
187 		if (strcmp(opt, p) == 0) {
188 			val = strdup("");
189 			goto done;
190 		}
191 	}
192 done:
193 	free(bb);
194 	return (val);
195 }
196