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