geniconvtbl: put full cpp path in its argv
[unleashed.git] / usr / src / lib / libfsmgt / common / fileutil.c
blobbd13da8b83ddcf1d9199b5ae7d43f946f9eadf99
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <strings.h>
33 #include <errno.h>
34 #include <sys/types.h>
35 #include <ctype.h>
36 #include <thread.h>
37 #include <synch.h>
38 #include "libfsmgt.h"
41 * Private method declarations
43 static char *get_first_column_data(char *line);
44 static char *retrieve_string(FILE *fp, char *line, int buffersize);
45 static char *trim_trailing_whitespace(char *line);
48 * Public methods
51 void
52 fileutil_free_string_array(char **arrayp, int num_elements)
54 if (arrayp != NULL) {
55 int i = 0;
57 for (i = 0; i < num_elements && arrayp[i] != NULL; i++) {
58 free(arrayp[i]);
61 free(arrayp);
63 } /* fileutil_free_string_array */
65 char **
66 fileutil_get_first_column_data(FILE *fp, int *num_elements, int *errp)
68 char line[BUFSIZE];
69 char *returned_string;
70 char **return_array = NULL;
72 *errp = 0;
73 *num_elements = 0;
75 while ((returned_string =
76 retrieve_string(fp, line, BUFSIZE)) != NULL) {
78 char **tmp_array;
80 tmp_array = realloc(return_array,
81 (size_t)(((*num_elements) + 1) * sizeof (char *)));
82 if (tmp_array == NULL) {
83 *errp = errno;
84 fileutil_free_string_array(return_array, *num_elements);
85 *num_elements = 0;
86 return (NULL);
88 return_array = tmp_array;
90 return_array[(*num_elements)] = strdup(returned_string);
91 if (return_array[(*num_elements)] == NULL) {
92 *errp = ENOMEM;
93 fileutil_free_string_array(return_array, *num_elements);
94 free(returned_string);
95 *num_elements = 0;
96 return (NULL);
99 free(returned_string);
100 *num_elements = *num_elements + 1;
104 * Caller must free the space allocated to return_array by calling
105 * fileutil_free_string_array.
107 return (return_array);
108 } /* fileutil_get_first_column_data */
111 * Convenience function for retrieving the default fstype from /etc/fstypes.
113 char *
114 fileutil_getfs(FILE *fp)
116 char *s;
117 static char buff[BUFSIZE]; /* line buffer */
119 while (s = fgets(buff, BUFSIZE, fp)) {
120 while (isspace(*s) || *s != '\0') /* skip leading whitespace */
121 ++s;
122 if (*s != '#') { /* not a comment */
123 char *t = s;
124 while (!isspace(*t) && *t != '\0') /* get the token */
125 ++t;
126 *t = '\0'; /* ignore rest of line */
127 return (s);
130 return (NULL); /* that's all, folks! */
131 } /* fileutil_getfs */
133 char *
134 fileutil_getline(FILE *fp, char *line, int linesz)
136 char *share_cmd, *p = line;
137 *p = '\0';
139 while (fgets(line, linesz, fp) != NULL) {
140 share_cmd = fileutil_get_cmd_from_string(line);
141 if (share_cmd != NULL)
142 return (share_cmd);
144 return (NULL);
145 } /* fileutil_getline */
148 * fileutil_get_cmd_from_string - retieves the command string minus any
149 * comments from the original string.
151 * Parameters:
152 * char *input_string - the original string.
154 char *
155 fileutil_get_cmd_from_string(char *input_stringp)
158 * Comments begin with '#'. Strip them off.
161 char *returned_stringp;
162 char *start_of_commentp;
163 char *current_string;
165 if ((input_stringp == NULL) || (strlen(input_stringp) == 0)) {
166 return (NULL);
169 current_string = strdup(input_stringp);
171 if (current_string == NULL) {
172 return (NULL);
175 start_of_commentp = strchr(current_string, '#');
176 if (start_of_commentp != NULL) {
177 *start_of_commentp = '\0';
180 returned_stringp = trim_trailing_whitespace(current_string);
181 free(current_string);
182 return (returned_stringp);
186 * NOTE: the caller of this function is responsible for freeing any
187 * memory allocated by calling fileutil_free_string_array()
189 * fileutil_add_string_to_array - adds one line to the file image
190 * string array
191 * Parameters:
192 * char ***string_array - reference to the string array
193 * char *line - the line to be added to the temporary dfstab
194 * int *count - the number of elements in the string array
195 * int *err - error pointer for returning any errors encountered
197 * Returns:
198 * B_TRUE on success, B_FALSE on failure.
200 boolean_t
201 fileutil_add_string_to_array(char ***string_array, char *line, int *count,
202 int *err)
204 int i;
205 char **ret_val = NULL;
206 char **temp_array = NULL;
208 temp_array = *string_array;
210 ret_val = calloc(((*count) + 1), sizeof (char *));
211 if (ret_val != NULL) {
212 for (i = 0; i < *count; i ++) {
213 ret_val[i] = temp_array[i];
215 ret_val[*count] = strdup(line);
216 if (ret_val[*count] != NULL) {
217 (*count)++;
218 if (temp_array != NULL) {
219 free(temp_array);
221 *string_array = ret_val;
222 } else {
223 *err = ENOMEM;
224 free(ret_val);
225 return (B_FALSE);
227 } else {
228 *err = ENOMEM;
229 return (B_FALSE);
231 return (B_TRUE);
232 } /* fileutil_add_string_to_array */
235 * Private methods
237 static char *
238 get_first_column_data(char *line)
240 return (strtok(line, "\t "));
241 } /* get_first_column_data */
243 static char *
244 retrieve_string(FILE *fp, char *line, int buffersize)
246 char *data;
247 char *returned_string;
249 while ((returned_string =
250 fileutil_getline(fp, line, buffersize)) != NULL) {
252 data = get_first_column_data(returned_string);
253 if (data != NULL)
254 return (data);
257 return (NULL);
258 } /* retrieve_string */
261 * trim_trailing_whitespace - helper function to remove trailing
262 * whitespace from a line
264 * Parameters:
265 * char *input_stringp - the line to be trimed
267 static char *
268 trim_trailing_whitespace(char *input_string)
270 char *last_nonspace;
271 char *return_string;
272 int string_length;
275 if (input_string == NULL) {
276 return (NULL);
278 string_length = strlen(input_string);
280 if (string_length == 0 || *input_string == '\n') {
281 return (NULL);
284 return_string = strdup(input_string);
285 if (return_string == NULL) {
286 return (NULL);
290 * Truncates the last character which will always be '\0'
292 last_nonspace = return_string + (string_length - 1);
294 while (isspace(*last_nonspace)) {
295 last_nonspace--;
297 *(last_nonspace + 1) = '\0';
298 return (return_string);