xref: /titanic_41/usr/src/cmd/rexd/sharetab.c (revision 1d4b38e0077763e7c9b20768eacb841957e787bc)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1989 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 #ident	"%Z%%M%	%I%	%E% SMI"
36 
37 #include <stdio.h>
38 #include <string.h>
39 #include "sharetab.h"
40 
41 static struct share *	sharedup();
42 static void		sharefree();
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(fd, shp)
59 	FILE *fd;
60 	struct share **shp;
61 {
62 	static char *line = NULL;
63 	static struct share *sh = NULL;
64 	register char *p;
65 	char *w = " \t";
66 
67 	if (line == NULL) {
68 		line = (char *) malloc(BUFSIZ+1);
69 		if (line == NULL)
70 			return (-1);
71 	}
72 	if (sh == NULL) {
73 		sh = (struct share *) malloc(sizeof(*sh));
74 		if (sh == NULL)
75 			return (-1);
76 	}
77 
78 	p = fgets(line, BUFSIZ, fd);
79 	if (p == NULL)
80 		return (0);
81 	line[strlen(line) - 1] = '\0';
82 
83 	sh->sh_path = strtok(p, w);
84 	if (sh->sh_path == NULL)
85 		return (-1);
86 	sh->sh_res = strtok(NULL, w);
87 	if (sh->sh_res == NULL)
88 		return (-1);
89 	sh->sh_fstype = strtok(NULL, w);
90 	if (sh->sh_fstype == NULL)
91 		return (-1);
92 	sh->sh_opts = strtok(NULL, w);
93 	if (sh->sh_opts == NULL)
94 		return (-1);
95 	sh->sh_descr = strtok(NULL, "");
96 	if (sh->sh_descr == NULL)
97 		sh->sh_descr = "";
98 
99 	*shp = sh;
100 	return (1);
101 }
102 
103 /*
104  * Append an entry to the sharetab file.
105  */
106 int
107 putshare(fd, sh)
108 	FILE *fd;
109 	struct share *sh;
110 {
111 	int r;
112 
113 	if (fseek(fd, 0L, 2) < 0)
114 		return (-1);
115 
116 	r = fprintf(fd, "%s\t%s\t%s\t%s\t%s\n",
117 		sh->sh_path,
118 		sh->sh_res,
119 		sh->sh_fstype,
120 		sh->sh_opts,
121 		sh->sh_descr);
122 	return (r);
123 }
124 
125 /*
126  * The entry corresponding to path is removed from the
127  * sharetab file.  The file is assumed to be locked.
128  * Read the entries into a linked list of share structures
129  * minus the entry to be removed.  Then truncate the sharetab
130  * file and write almost all of it back to the file from the
131  * linked list.
132  * Note: The file is assumed to be locked.
133  */
134 int
135 remshare(fd, path)
136 	FILE *fd;
137 	char *path;
138 {
139 	struct share *sh_tmp;
140 	struct shl {			/* the linked list */
141 		struct shl   *shl_next;
142 		struct share *shl_sh;
143 	};
144 	struct shl *shl_head = NULL;
145 	struct shl *shl, *prev, *next;
146 	int res, remcnt;
147 
148 	rewind(fd);
149 	remcnt = 0;
150 	while ((res = getshare(fd, &sh_tmp)) > 0) {
151 		if (strcmp(path, sh_tmp->sh_path) == 0 ||
152 		    strcmp(path, sh_tmp->sh_res)  == 0) {
153 			remcnt++;
154 		} else {
155 			prev = shl;
156 			shl = (struct shl *) malloc(sizeof(*shl));
157 			if (shl == NULL) {
158 				res = -1;
159 				goto dealloc;
160 			}
161 			if (shl_head == NULL)
162 				shl_head = shl;
163 			else
164 				prev->shl_next = shl;
165 			shl->shl_next = NULL;
166 			shl->shl_sh = sharedup(sh_tmp);
167 			if (shl->shl_sh == NULL) {
168 				res = -1;
169 				goto dealloc;
170 			}
171 		}
172 	}
173 	if (res < 0)
174 		goto dealloc;
175 	if (remcnt == 0) {
176 		res = 1;	/* nothing removed */
177 		goto dealloc;
178 	}
179 
180 	if (ftruncate(fileno(fd), 0) < 0) {
181 		res = -1;
182 		goto dealloc;
183 	}
184 
185 	for (shl = shl_head ; shl ; shl = shl->shl_next)
186 		putshare(fd, shl->shl_sh);
187 	res = 1;
188 
189 dealloc:
190 	for (shl = shl_head ; shl ; shl = next) {
191 		sharefree(shl->shl_sh);
192 		next = shl->shl_next;
193 		free(shl);
194 	}
195 	return (res);
196 }
197 
198 static struct share *
199 sharedup(sh)
200 	struct share *sh;
201 {
202 	struct share *nsh;
203 
204 	nsh = (struct share *) malloc(sizeof(*nsh));
205 	if (nsh == NULL)
206 		return (NULL);
207 	nsh->sh_path = strdup(sh->sh_path);
208 	if (nsh->sh_path == NULL)
209 		goto alloc_failed;
210 	nsh->sh_res = strdup(sh->sh_res);
211 	if (nsh->sh_res == NULL)
212 		goto alloc_failed;
213 	nsh->sh_fstype = strdup(sh->sh_fstype);
214 	if (nsh->sh_fstype == NULL)
215 		goto alloc_failed;
216 	nsh->sh_opts = strdup(sh->sh_opts);
217 	if (nsh->sh_opts == NULL)
218 		goto alloc_failed;
219 	nsh->sh_descr = strdup(sh->sh_descr);
220 	if (nsh->sh_descr == NULL)
221 		goto alloc_failed;
222 	return (nsh);
223 
224 alloc_failed:
225 	sharefree(nsh);
226 	return (NULL);
227 }
228 
229 static void
230 sharefree(sh)
231 	struct share *sh;
232 {
233 	if (sh == NULL)
234 		return;
235 	if (sh->sh_path != NULL)
236 		free(sh->sh_path);
237 	if (sh->sh_res != NULL)
238 		free(sh->sh_res);
239 	if (sh->sh_fstype != NULL)
240 		free(sh->sh_fstype);
241 	if (sh->sh_opts != NULL)
242 		free(sh->sh_opts);
243 	if (sh->sh_descr != NULL)
244 		free(sh->sh_descr);
245 	free(sh);
246 }
247 
248 /*
249  * Return the value after "=" for option "opt"
250  * in option string "optlist".
251  */
252 char *
253 getshareopt(optlist, opt)
254 	char *optlist, *opt;
255 {
256 	char *p, *pe;
257 	char *b;
258 	static char *bb;
259 
260 	if (bb)
261 		free(bb);
262 	b = bb = strdup(optlist);
263 	if (b == NULL)
264 		return (NULL);
265 
266 	while (p = strtok(b, ",")) {
267 		b = NULL;
268 		if (pe = strchr(p, '=')) {
269 			*pe = '\0';
270 			if (strcmp(opt, p) == 0)
271 				return (pe + 1);
272 		}
273 		if (strcmp(opt, p) == 0)
274 			return ("");
275 	}
276 
277 	return (NULL);
278 }
279