1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24*0Sstevel@tonic-gate * Use is subject to license terms.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <stdlib.h>
31*0Sstevel@tonic-gate #include <string.h>
32*0Sstevel@tonic-gate #include <strings.h>
33*0Sstevel@tonic-gate #include <errno.h>
34*0Sstevel@tonic-gate #include <sys/types.h>
35*0Sstevel@tonic-gate #include <ctype.h>
36*0Sstevel@tonic-gate #include <thread.h>
37*0Sstevel@tonic-gate #include <synch.h>
38*0Sstevel@tonic-gate #include "libfsmgt.h"
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gate /*
41*0Sstevel@tonic-gate * Private method declarations
42*0Sstevel@tonic-gate */
43*0Sstevel@tonic-gate static char *get_first_column_data(char *line);
44*0Sstevel@tonic-gate static char *retrieve_string(FILE *fp, char *line, int buffersize);
45*0Sstevel@tonic-gate static char *trim_trailing_whitespace(char *line);
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gate /*
48*0Sstevel@tonic-gate * Public methods
49*0Sstevel@tonic-gate */
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gate void
fileutil_free_string_array(char ** arrayp,int num_elements)52*0Sstevel@tonic-gate fileutil_free_string_array(char **arrayp, int num_elements)
53*0Sstevel@tonic-gate {
54*0Sstevel@tonic-gate if (arrayp != NULL) {
55*0Sstevel@tonic-gate int i = 0;
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gate for (i = 0; i < num_elements && arrayp[i] != NULL; i++) {
58*0Sstevel@tonic-gate free(arrayp[i]);
59*0Sstevel@tonic-gate }
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate free(arrayp);
62*0Sstevel@tonic-gate }
63*0Sstevel@tonic-gate } /* fileutil_free_string_array */
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gate char **
fileutil_get_first_column_data(FILE * fp,int * num_elements,int * errp)66*0Sstevel@tonic-gate fileutil_get_first_column_data(FILE *fp, int *num_elements, int *errp)
67*0Sstevel@tonic-gate {
68*0Sstevel@tonic-gate char line[BUFSIZE];
69*0Sstevel@tonic-gate char *returned_string;
70*0Sstevel@tonic-gate char **return_array = NULL;
71*0Sstevel@tonic-gate
72*0Sstevel@tonic-gate *errp = 0;
73*0Sstevel@tonic-gate *num_elements = 0;
74*0Sstevel@tonic-gate
75*0Sstevel@tonic-gate while ((returned_string =
76*0Sstevel@tonic-gate retrieve_string(fp, line, BUFSIZE)) != NULL) {
77*0Sstevel@tonic-gate
78*0Sstevel@tonic-gate char **tmp_array;
79*0Sstevel@tonic-gate
80*0Sstevel@tonic-gate tmp_array = realloc(return_array,
81*0Sstevel@tonic-gate (size_t)(((*num_elements) + 1) * sizeof (char *)));
82*0Sstevel@tonic-gate if (tmp_array == NULL) {
83*0Sstevel@tonic-gate *errp = errno;
84*0Sstevel@tonic-gate fileutil_free_string_array(return_array, *num_elements);
85*0Sstevel@tonic-gate *num_elements = 0;
86*0Sstevel@tonic-gate return (NULL);
87*0Sstevel@tonic-gate }
88*0Sstevel@tonic-gate return_array = tmp_array;
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate return_array[(*num_elements)] = strdup(returned_string);
91*0Sstevel@tonic-gate if (return_array[(*num_elements)] == NULL) {
92*0Sstevel@tonic-gate *errp = ENOMEM;
93*0Sstevel@tonic-gate fileutil_free_string_array(return_array, *num_elements);
94*0Sstevel@tonic-gate free(returned_string);
95*0Sstevel@tonic-gate *num_elements = 0;
96*0Sstevel@tonic-gate return (NULL);
97*0Sstevel@tonic-gate }
98*0Sstevel@tonic-gate
99*0Sstevel@tonic-gate free(returned_string);
100*0Sstevel@tonic-gate *num_elements = *num_elements + 1;
101*0Sstevel@tonic-gate }
102*0Sstevel@tonic-gate
103*0Sstevel@tonic-gate /*
104*0Sstevel@tonic-gate * Caller must free the space allocated to return_array by calling
105*0Sstevel@tonic-gate * fileutil_free_string_array.
106*0Sstevel@tonic-gate */
107*0Sstevel@tonic-gate return (return_array);
108*0Sstevel@tonic-gate } /* fileutil_get_first_column_data */
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate /*
111*0Sstevel@tonic-gate * Convenience function for retrieving the default fstype from /etc/fstypes.
112*0Sstevel@tonic-gate */
113*0Sstevel@tonic-gate char *
fileutil_getfs(FILE * fp)114*0Sstevel@tonic-gate fileutil_getfs(FILE *fp)
115*0Sstevel@tonic-gate {
116*0Sstevel@tonic-gate char *s;
117*0Sstevel@tonic-gate static char buff[BUFSIZE]; /* line buffer */
118*0Sstevel@tonic-gate
119*0Sstevel@tonic-gate while (s = fgets(buff, BUFSIZE, fp)) {
120*0Sstevel@tonic-gate while (isspace(*s) || *s != '\0') /* skip leading whitespace */
121*0Sstevel@tonic-gate ++s;
122*0Sstevel@tonic-gate if (*s != '#') { /* not a comment */
123*0Sstevel@tonic-gate char *t = s;
124*0Sstevel@tonic-gate while (!isspace(*t) && *t != '\0') /* get the token */
125*0Sstevel@tonic-gate ++t;
126*0Sstevel@tonic-gate *t = '\0'; /* ignore rest of line */
127*0Sstevel@tonic-gate return (s);
128*0Sstevel@tonic-gate }
129*0Sstevel@tonic-gate }
130*0Sstevel@tonic-gate return (NULL); /* that's all, folks! */
131*0Sstevel@tonic-gate } /* fileutil_getfs */
132*0Sstevel@tonic-gate
133*0Sstevel@tonic-gate char *
fileutil_getline(FILE * fp,char * line,int linesz)134*0Sstevel@tonic-gate fileutil_getline(FILE *fp, char *line, int linesz)
135*0Sstevel@tonic-gate {
136*0Sstevel@tonic-gate char *share_cmd, *p = line;
137*0Sstevel@tonic-gate *p = '\0';
138*0Sstevel@tonic-gate
139*0Sstevel@tonic-gate while (fgets(line, linesz, fp) != NULL) {
140*0Sstevel@tonic-gate share_cmd = fileutil_get_cmd_from_string(line);
141*0Sstevel@tonic-gate if (share_cmd != NULL)
142*0Sstevel@tonic-gate return (share_cmd);
143*0Sstevel@tonic-gate }
144*0Sstevel@tonic-gate return (NULL);
145*0Sstevel@tonic-gate } /* fileutil_getline */
146*0Sstevel@tonic-gate
147*0Sstevel@tonic-gate /*
148*0Sstevel@tonic-gate * fileutil_get_cmd_from_string - retieves the command string minus any
149*0Sstevel@tonic-gate * comments from the original string.
150*0Sstevel@tonic-gate *
151*0Sstevel@tonic-gate * Parameters:
152*0Sstevel@tonic-gate * char *input_string - the original string.
153*0Sstevel@tonic-gate */
154*0Sstevel@tonic-gate char *
fileutil_get_cmd_from_string(char * input_stringp)155*0Sstevel@tonic-gate fileutil_get_cmd_from_string(char *input_stringp)
156*0Sstevel@tonic-gate {
157*0Sstevel@tonic-gate /*
158*0Sstevel@tonic-gate * Comments begin with '#'. Strip them off.
159*0Sstevel@tonic-gate */
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate char *returned_stringp;
162*0Sstevel@tonic-gate char *start_of_commentp;
163*0Sstevel@tonic-gate char *current_string;
164*0Sstevel@tonic-gate
165*0Sstevel@tonic-gate if ((input_stringp == NULL) || (strlen(input_stringp) == 0)) {
166*0Sstevel@tonic-gate return (NULL);
167*0Sstevel@tonic-gate }
168*0Sstevel@tonic-gate
169*0Sstevel@tonic-gate current_string = strdup(input_stringp);
170*0Sstevel@tonic-gate
171*0Sstevel@tonic-gate if (current_string == NULL) {
172*0Sstevel@tonic-gate return (NULL);
173*0Sstevel@tonic-gate }
174*0Sstevel@tonic-gate
175*0Sstevel@tonic-gate start_of_commentp = strchr(current_string, '#');
176*0Sstevel@tonic-gate if (start_of_commentp != NULL) {
177*0Sstevel@tonic-gate *start_of_commentp = '\0';
178*0Sstevel@tonic-gate }
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gate returned_stringp = trim_trailing_whitespace(current_string);
181*0Sstevel@tonic-gate free(current_string);
182*0Sstevel@tonic-gate return (returned_stringp);
183*0Sstevel@tonic-gate }
184*0Sstevel@tonic-gate
185*0Sstevel@tonic-gate /*
186*0Sstevel@tonic-gate * NOTE: the caller of this function is responsible for freeing any
187*0Sstevel@tonic-gate * memory allocated by calling fileutil_free_string_array()
188*0Sstevel@tonic-gate *
189*0Sstevel@tonic-gate * fileutil_add_string_to_array - adds one line to the file image
190*0Sstevel@tonic-gate * string array
191*0Sstevel@tonic-gate * Parameters:
192*0Sstevel@tonic-gate * char ***string_array - reference to the string array
193*0Sstevel@tonic-gate * char *line - the line to be added to the temporary dfstab
194*0Sstevel@tonic-gate * int *count - the number of elements in the string array
195*0Sstevel@tonic-gate * int *err - error pointer for returning any errors encountered
196*0Sstevel@tonic-gate *
197*0Sstevel@tonic-gate * Returns:
198*0Sstevel@tonic-gate * B_TRUE on success, B_FALSE on failure.
199*0Sstevel@tonic-gate */
200*0Sstevel@tonic-gate boolean_t
fileutil_add_string_to_array(char *** string_array,char * line,int * count,int * err)201*0Sstevel@tonic-gate fileutil_add_string_to_array(char ***string_array, char *line, int *count,
202*0Sstevel@tonic-gate int *err)
203*0Sstevel@tonic-gate {
204*0Sstevel@tonic-gate int i;
205*0Sstevel@tonic-gate char **ret_val = NULL;
206*0Sstevel@tonic-gate char **temp_array = NULL;
207*0Sstevel@tonic-gate
208*0Sstevel@tonic-gate temp_array = *string_array;
209*0Sstevel@tonic-gate
210*0Sstevel@tonic-gate ret_val = calloc(((*count) + 1), sizeof (char *));
211*0Sstevel@tonic-gate if (ret_val != NULL) {
212*0Sstevel@tonic-gate for (i = 0; i < *count; i ++) {
213*0Sstevel@tonic-gate ret_val[i] = temp_array[i];
214*0Sstevel@tonic-gate }
215*0Sstevel@tonic-gate ret_val[*count] = strdup(line);
216*0Sstevel@tonic-gate if (ret_val[*count] != NULL) {
217*0Sstevel@tonic-gate (*count)++;
218*0Sstevel@tonic-gate if (temp_array != NULL) {
219*0Sstevel@tonic-gate free(temp_array);
220*0Sstevel@tonic-gate }
221*0Sstevel@tonic-gate *string_array = ret_val;
222*0Sstevel@tonic-gate } else {
223*0Sstevel@tonic-gate *err = ENOMEM;
224*0Sstevel@tonic-gate free(ret_val);
225*0Sstevel@tonic-gate return (B_FALSE);
226*0Sstevel@tonic-gate }
227*0Sstevel@tonic-gate } else {
228*0Sstevel@tonic-gate *err = ENOMEM;
229*0Sstevel@tonic-gate return (B_FALSE);
230*0Sstevel@tonic-gate }
231*0Sstevel@tonic-gate return (B_TRUE);
232*0Sstevel@tonic-gate } /* fileutil_add_string_to_array */
233*0Sstevel@tonic-gate
234*0Sstevel@tonic-gate /*
235*0Sstevel@tonic-gate * Private methods
236*0Sstevel@tonic-gate */
237*0Sstevel@tonic-gate static char *
get_first_column_data(char * line)238*0Sstevel@tonic-gate get_first_column_data(char *line)
239*0Sstevel@tonic-gate {
240*0Sstevel@tonic-gate return (strtok(line, "\t "));
241*0Sstevel@tonic-gate } /* get_first_column_data */
242*0Sstevel@tonic-gate
243*0Sstevel@tonic-gate static char *
retrieve_string(FILE * fp,char * line,int buffersize)244*0Sstevel@tonic-gate retrieve_string(FILE *fp, char *line, int buffersize)
245*0Sstevel@tonic-gate {
246*0Sstevel@tonic-gate char *data;
247*0Sstevel@tonic-gate char *returned_string;
248*0Sstevel@tonic-gate
249*0Sstevel@tonic-gate while ((returned_string =
250*0Sstevel@tonic-gate fileutil_getline(fp, line, buffersize)) != NULL) {
251*0Sstevel@tonic-gate
252*0Sstevel@tonic-gate data = get_first_column_data(returned_string);
253*0Sstevel@tonic-gate if (data != NULL)
254*0Sstevel@tonic-gate return (data);
255*0Sstevel@tonic-gate }
256*0Sstevel@tonic-gate
257*0Sstevel@tonic-gate return (NULL);
258*0Sstevel@tonic-gate } /* retrieve_string */
259*0Sstevel@tonic-gate
260*0Sstevel@tonic-gate /*
261*0Sstevel@tonic-gate * trim_trailing_whitespace - helper function to remove trailing
262*0Sstevel@tonic-gate * whitespace from a line
263*0Sstevel@tonic-gate *
264*0Sstevel@tonic-gate * Parameters:
265*0Sstevel@tonic-gate * char *input_stringp - the line to be trimed
266*0Sstevel@tonic-gate */
267*0Sstevel@tonic-gate static char *
trim_trailing_whitespace(char * input_string)268*0Sstevel@tonic-gate trim_trailing_whitespace(char *input_string)
269*0Sstevel@tonic-gate {
270*0Sstevel@tonic-gate char *last_nonspace;
271*0Sstevel@tonic-gate char *return_string;
272*0Sstevel@tonic-gate int string_length;
273*0Sstevel@tonic-gate
274*0Sstevel@tonic-gate
275*0Sstevel@tonic-gate if (input_string == NULL) {
276*0Sstevel@tonic-gate return (NULL);
277*0Sstevel@tonic-gate }
278*0Sstevel@tonic-gate string_length = strlen(input_string);
279*0Sstevel@tonic-gate
280*0Sstevel@tonic-gate if (string_length == 0 || *input_string == '\n') {
281*0Sstevel@tonic-gate return (NULL);
282*0Sstevel@tonic-gate }
283*0Sstevel@tonic-gate
284*0Sstevel@tonic-gate return_string = strdup(input_string);
285*0Sstevel@tonic-gate if (return_string == NULL) {
286*0Sstevel@tonic-gate return (NULL);
287*0Sstevel@tonic-gate }
288*0Sstevel@tonic-gate
289*0Sstevel@tonic-gate /*
290*0Sstevel@tonic-gate * Truncates the last character which will always be '\0'
291*0Sstevel@tonic-gate */
292*0Sstevel@tonic-gate last_nonspace = return_string + (string_length - 1);
293*0Sstevel@tonic-gate
294*0Sstevel@tonic-gate while (isspace(*last_nonspace)) {
295*0Sstevel@tonic-gate last_nonspace--;
296*0Sstevel@tonic-gate }
297*0Sstevel@tonic-gate *(last_nonspace + 1) = '\0';
298*0Sstevel@tonic-gate return (return_string);
299*0Sstevel@tonic-gate }
300