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
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]
23 * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
34 #include <sys/types.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
);
52 fileutil_free_string_array(char **arrayp
, int num_elements
)
57 for (i
= 0; i
< num_elements
&& arrayp
[i
] != NULL
; i
++) {
63 } /* fileutil_free_string_array */
66 fileutil_get_first_column_data(FILE *fp
, int *num_elements
, int *errp
)
69 char *returned_string
;
70 char **return_array
= NULL
;
75 while ((returned_string
=
76 retrieve_string(fp
, line
, BUFSIZE
)) != NULL
) {
80 tmp_array
= realloc(return_array
,
81 (size_t)(((*num_elements
) + 1) * sizeof (char *)));
82 if (tmp_array
== NULL
) {
84 fileutil_free_string_array(return_array
, *num_elements
);
88 return_array
= tmp_array
;
90 return_array
[(*num_elements
)] = strdup(returned_string
);
91 if (return_array
[(*num_elements
)] == NULL
) {
93 fileutil_free_string_array(return_array
, *num_elements
);
94 free(returned_string
);
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.
114 fileutil_getfs(FILE *fp
)
117 static char buff
[BUFSIZE
]; /* line buffer */
119 while (s
= fgets(buff
, BUFSIZE
, fp
)) {
120 while (isspace(*s
) || *s
!= '\0') /* skip leading whitespace */
122 if (*s
!= '#') { /* not a comment */
124 while (!isspace(*t
) && *t
!= '\0') /* get the token */
126 *t
= '\0'; /* ignore rest of line */
130 return (NULL
); /* that's all, folks! */
131 } /* fileutil_getfs */
134 fileutil_getline(FILE *fp
, char *line
, int linesz
)
136 char *share_cmd
, *p
= line
;
139 while (fgets(line
, linesz
, fp
) != NULL
) {
140 share_cmd
= fileutil_get_cmd_from_string(line
);
141 if (share_cmd
!= NULL
)
145 } /* fileutil_getline */
148 * fileutil_get_cmd_from_string - retieves the command string minus any
149 * comments from the original string.
152 * char *input_string - the original string.
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)) {
169 current_string
= strdup(input_stringp
);
171 if (current_string
== 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
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
198 * B_TRUE on success, B_FALSE on failure.
201 fileutil_add_string_to_array(char ***string_array
, char *line
, int *count
,
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
) {
218 if (temp_array
!= NULL
) {
221 *string_array
= ret_val
;
232 } /* fileutil_add_string_to_array */
238 get_first_column_data(char *line
)
240 return (strtok(line
, "\t "));
241 } /* get_first_column_data */
244 retrieve_string(FILE *fp
, char *line
, int buffersize
)
247 char *returned_string
;
249 while ((returned_string
=
250 fileutil_getline(fp
, line
, buffersize
)) != NULL
) {
252 data
= get_first_column_data(returned_string
);
258 } /* retrieve_string */
261 * trim_trailing_whitespace - helper function to remove trailing
262 * whitespace from a line
265 * char *input_stringp - the line to be trimed
268 trim_trailing_whitespace(char *input_string
)
275 if (input_string
== NULL
) {
278 string_length
= strlen(input_string
);
280 if (string_length
== 0 || *input_string
== '\n') {
284 return_string
= strdup(input_string
);
285 if (return_string
== NULL
) {
290 * Truncates the last character which will always be '\0'
292 last_nonspace
= return_string
+ (string_length
- 1);
294 while (isspace(*last_nonspace
)) {
297 *(last_nonspace
+ 1) = '\0';
298 return (return_string
);