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