xref: /titanic_52/usr/src/cmd/rexd/sharetab.c (revision a49a392f179e40c74ea8903bf2793b2aa49efdf1)
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 <string.h>
39 #include <sys/types.h>
40 #include <sys/types32.h>
41 #include <sharefs/share.h>
42 #include "sharetab.h"
43 
44 static share_t	*sharedup();
45 static void	sharefree();
46 
47 /*
48  * Get an entry from the share table.
49  * There should be at least 4 fields:
50  *
51  * 	pathname  resource  fstype  options  [ description ]
52  *
53  * A fifth field (description) is optional.
54  *
55  * Returns:
56  *	> 1  valid entry
57  *	= 0  end of file
58  *	< 0  error
59  */
60 int
61 getshare(FILE *fd, share_t **shp)
62 {
63 	static char *line = NULL;
64 	static share_t *sh = NULL;
65 	register char *p;
66 	char *w = " \t";
67 
68 	if (line == NULL) {
69 		line = (char *)malloc(BUFSIZ+1);
70 		if (line == NULL)
71 			return (-1);
72 	}
73 	if (sh == NULL) {
74 		sh = (share_t *)malloc(sizeof (*sh));
75 		if (sh == NULL)
76 			return (-1);
77 	}
78 
79 	p = fgets(line, BUFSIZ, fd);
80 	if (p == NULL)
81 		return (0);
82 	line[strlen(line) - 1] = '\0';
83 
84 	sh->sh_path = strtok(p, w);
85 	if (sh->sh_path == NULL)
86 		return (-1);
87 	sh->sh_res = strtok(NULL, w);
88 	if (sh->sh_res == NULL)
89 		return (-1);
90 	sh->sh_fstype = strtok(NULL, w);
91 	if (sh->sh_fstype == NULL)
92 		return (-1);
93 	sh->sh_opts = strtok(NULL, w);
94 	if (sh->sh_opts == NULL)
95 		return (-1);
96 	sh->sh_descr = strtok(NULL, "");
97 	if (sh->sh_descr == NULL)
98 		sh->sh_descr = "";
99 
100 	*shp = sh;
101 	return (1);
102 }
103 
104 static share_t *
105 sharedup(share_t *sh)
106 {
107 	share_t *nsh;
108 
109 	nsh = (share_t *)calloc(1, sizeof (*nsh));
110 	if (nsh == NULL)
111 		return (NULL);
112 
113 	nsh->sh_path = strdup(sh->sh_path);
114 	if (nsh->sh_path == NULL)
115 		goto alloc_failed;
116 	nsh->sh_res = strdup(sh->sh_res);
117 	if (nsh->sh_res == NULL)
118 		goto alloc_failed;
119 	nsh->sh_fstype = strdup(sh->sh_fstype);
120 	if (nsh->sh_fstype == NULL)
121 		goto alloc_failed;
122 	nsh->sh_opts = strdup(sh->sh_opts);
123 	if (nsh->sh_opts == NULL)
124 		goto alloc_failed;
125 	nsh->sh_descr = strdup(sh->sh_descr);
126 	if (nsh->sh_descr == NULL)
127 		goto alloc_failed;
128 	return (nsh);
129 
130 alloc_failed:
131 	sharefree(nsh);
132 	return (NULL);
133 }
134 
135 static void
136 sharefree(share_t *sh)
137 {
138 	if (sh == NULL)
139 		return;
140 	if (sh->sh_path != NULL)
141 		free(sh->sh_path);
142 	if (sh->sh_res != NULL)
143 		free(sh->sh_res);
144 	if (sh->sh_fstype != NULL)
145 		free(sh->sh_fstype);
146 	if (sh->sh_opts != NULL)
147 		free(sh->sh_opts);
148 	if (sh->sh_descr != NULL)
149 		free(sh->sh_descr);
150 	free(sh);
151 }
152 
153 /*
154  * Return the value after "=" for option "opt"
155  * in option string "optlist".
156  */
157 char *
158 getshareopt(char *optlist, char *opt)
159 {
160 	char *p, *pe;
161 	char *b;
162 	static char *bb;
163 
164 	if (bb)
165 		free(bb);
166 	b = bb = strdup(optlist);
167 	if (b == NULL)
168 		return (NULL);
169 
170 	while (p = strtok(b, ",")) {
171 		b = NULL;
172 		if (pe = strchr(p, '=')) {
173 			*pe = '\0';
174 			if (strcmp(opt, p) == 0)
175 				return (pe + 1);
176 		}
177 		if (strcmp(opt, p) == 0)
178 			return ("");
179 	}
180 
181 	return (NULL);
182 }
183