dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / fs.d / nfs / lib / sharetab.c
blob745a454e3d6a0de23824a386f86066fd1660036a
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
27 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
28 * Use is subject to license terms.
31 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
32 /* All Rights Reserved */
35 * Portions of this source code were derived from Berkeley 4.3 BSD
36 * under license from the Regents of the University of California.
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"
47 * Get an entry from the share table.
48 * There should be at least 4 fields:
50 * pathname resource fstype options [ description ]
52 * A fifth field (description) is optional.
54 * Returns:
55 * > 1 valid entry
56 * = 0 end of file
57 * < 0 error
59 int
60 getshare(FILE *fd, share_t **shp)
62 static char *line = NULL;
63 static share_t *sh = NULL;
64 char *p;
65 char *lasts;
66 char *w = " \t";
68 if (line == NULL) {
69 line = (char *)malloc(MAXBUFSIZE+1);
70 if (line == NULL)
71 return (-1);
73 if (sh == NULL) {
74 sh = (share_t *)malloc(sizeof (*sh));
75 if (sh == NULL)
76 return (-1);
79 p = fgets(line, MAXBUFSIZE, fd);
80 if (p == NULL)
81 return (0);
82 line[strlen(line) - 1] = '\0';
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 = "";
100 *shp = sh;
101 return (1);
104 share_t *
105 sharedup(share_t *sh)
107 share_t *nsh;
109 nsh = (share_t *)calloc(1, sizeof (*nsh));
110 if (nsh == NULL)
111 return (NULL);
113 if (sh->sh_path) {
114 nsh->sh_path = strdup(sh->sh_path);
115 if (nsh->sh_path == NULL)
116 goto alloc_failed;
119 if (sh->sh_res) {
120 nsh->sh_res = strdup(sh->sh_res);
121 if (nsh->sh_res == NULL)
122 goto alloc_failed;
124 if (sh->sh_fstype) {
125 nsh->sh_fstype = strdup(sh->sh_fstype);
126 if (nsh->sh_fstype == NULL)
127 goto alloc_failed;
129 if (sh->sh_opts) {
130 nsh->sh_opts = strdup(sh->sh_opts);
131 if (nsh->sh_opts == NULL)
132 goto alloc_failed;
134 if (sh->sh_descr) {
135 nsh->sh_descr = strdup(sh->sh_descr);
136 if (nsh->sh_descr == NULL)
137 goto alloc_failed;
139 return (nsh);
141 alloc_failed:
142 sharefree(nsh);
143 return (NULL);
146 void
147 sharefree(share_t *sh)
149 free(sh->sh_path);
150 free(sh->sh_res);
151 free(sh->sh_fstype);
152 free(sh->sh_opts);
153 free(sh->sh_descr);
154 free(sh);
158 * Return the value after "=" for option "opt"
159 * in option string "optlist". Caller must
160 * free returned value.
162 char *
163 getshareopt(char *optlist, char *opt)
165 char *p, *pe;
166 char *b;
167 char *bb;
168 char *lasts;
169 char *val = NULL;
171 b = bb = strdup(optlist);
172 if (b == NULL)
173 return (NULL);
175 while ((p = strtok_r(b, ",", &lasts)) != NULL) {
176 b = NULL;
177 if ((pe = strchr(p, '=')) != NULL) {
178 *pe = '\0';
179 if (strcmp(opt, p) == 0) {
180 val = strdup(pe + 1);
181 goto done;
184 if (strcmp(opt, p) == 0) {
185 val = strdup("");
186 goto done;
189 done:
190 free(bb);
191 return (val);