xref: /illumos-gate/usr/src/cmd/fs.d/nfs/lib/sharetab.c (revision 8119dad84d6416f13557b0ba8e2aaf9064cbcfd3)
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 2015 Nexenta Systems, Inc.  All rights reserved.
24  */
25 
26 /*
27  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
32 /*	  All Rights Reserved  	*/
33 
34 /*
35  * Portions of this source code were derived from Berkeley 4.3 BSD
36  * under license from the Regents of the University of California.
37  */
38 
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <sharefs/share.h>
44 #include "sharetab.h"
45 
46 /*
47  * Get an entry from the share table.
48  * There should be at least 4 fields:
49  *
50  * 	pathname  resource  fstype  options  [ description ]
51  *
52  * A fifth field (description) is optional.
53  *
54  * Returns:
55  *	> 1  valid entry
56  *	= 0  end of file
57  *	< 0  error
58  */
59 int
60 getshare(FILE *fd, share_t **shp)
61 {
62 	static char *line = NULL;
63 	static share_t *sh = NULL;
64 	char *p;
65 	char *lasts;
66 	char *w = " \t";
67 
68 	if (line == NULL) {
69 		line = (char *)malloc(MAXBUFSIZE+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, MAXBUFSIZE, fd);
80 	if (p == NULL)
81 		return (0);
82 	line[strlen(line) - 1] = '\0';
83 
84 	sh->sh_path = (char *)strtok_r(p, w, &lasts);
85 	if (sh->sh_path == NULL)
86 		return (-3);
87 	sh->sh_res = (char *)strtok_r(NULL, w, &lasts);
88 	if (sh->sh_res == NULL)
89 		return (-3);
90 	sh->sh_fstype = (char *)strtok_r(NULL, w, &lasts);
91 	if (sh->sh_fstype == NULL)
92 		return (-3);
93 	sh->sh_opts = (char *)strtok_r(NULL, w, &lasts);
94 	if (sh->sh_opts == NULL)
95 		return (-3);
96 	sh->sh_descr = (char *)strtok_r(NULL, "", &lasts);
97 	if (sh->sh_descr == NULL)
98 		sh->sh_descr = "";
99 
100 	*shp = sh;
101 	return (1);
102 }
103 
104 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 	if (sh->sh_path) {
114 		nsh->sh_path = strdup(sh->sh_path);
115 		if (nsh->sh_path == NULL)
116 			goto alloc_failed;
117 	}
118 
119 	if (sh->sh_res) {
120 		nsh->sh_res = strdup(sh->sh_res);
121 		if (nsh->sh_res == NULL)
122 			goto alloc_failed;
123 	}
124 	if (sh->sh_fstype) {
125 		nsh->sh_fstype = strdup(sh->sh_fstype);
126 		if (nsh->sh_fstype == NULL)
127 			goto alloc_failed;
128 	}
129 	if (sh->sh_opts) {
130 		nsh->sh_opts = strdup(sh->sh_opts);
131 		if (nsh->sh_opts == NULL)
132 			goto alloc_failed;
133 	}
134 	if (sh->sh_descr) {
135 		nsh->sh_descr = strdup(sh->sh_descr);
136 		if (nsh->sh_descr == NULL)
137 			goto alloc_failed;
138 	}
139 	return (nsh);
140 
141 alloc_failed:
142 	sharefree(nsh);
143 	return (NULL);
144 }
145 
146 void
147 sharefree(share_t *sh)
148 {
149 	if (sh->sh_path != NULL)
150 		free(sh->sh_path);
151 	if (sh->sh_res != NULL)
152 		free(sh->sh_res);
153 	if (sh->sh_fstype != NULL)
154 		free(sh->sh_fstype);
155 	if (sh->sh_opts != NULL)
156 		free(sh->sh_opts);
157 	if (sh->sh_descr != NULL)
158 		free(sh->sh_descr);
159 	free(sh);
160 }
161 
162 /*
163  * Return the value after "=" for option "opt"
164  * in option string "optlist". Caller must
165  * free returned value.
166  */
167 char *
168 getshareopt(char *optlist, char *opt)
169 {
170 	char *p, *pe;
171 	char *b;
172 	char *bb;
173 	char *lasts;
174 	char *val = NULL;
175 
176 	b = bb = strdup(optlist);
177 	if (b == NULL)
178 		return (NULL);
179 
180 	while ((p = strtok_r(b, ",", &lasts)) != NULL) {
181 		b = NULL;
182 		if ((pe = strchr(p, '=')) != NULL) {
183 			*pe = '\0';
184 			if (strcmp(opt, p) == 0) {
185 				val = strdup(pe + 1);
186 				goto done;
187 			}
188 		}
189 		if (strcmp(opt, p) == 0) {
190 			val = strdup("");
191 			goto done;
192 		}
193 	}
194 done:
195 	free(bb);
196 	return (val);
197 }
198