2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <string.h>
2N/A#include <strings.h>
2N/A#include <errno.h>
2N/A#include <sys/types.h>
2N/A#include <ctype.h>
2N/A#include <thread.h>
2N/A#include <synch.h>
2N/A#include "libfsmgt.h"
2N/A
2N/A/*
2N/A * Private method declarations
2N/A */
2N/Astatic char *get_first_column_data(char *line);
2N/Astatic char *retrieve_string(FILE *fp, char *line, int buffersize);
2N/Astatic char *trim_trailing_whitespace(char *line);
2N/A
2N/A/*
2N/A * Public methods
2N/A */
2N/A
2N/Avoid
2N/Afileutil_free_string_array(char **arrayp, int num_elements)
2N/A{
2N/A if (arrayp != NULL) {
2N/A int i = 0;
2N/A
2N/A for (i = 0; i < num_elements && arrayp[i] != NULL; i++) {
2N/A free(arrayp[i]);
2N/A }
2N/A
2N/A free(arrayp);
2N/A }
2N/A} /* fileutil_free_string_array */
2N/A
2N/Achar **
2N/Afileutil_get_first_column_data(FILE *fp, int *num_elements, int *errp)
2N/A{
2N/A char line[BUFSIZE];
2N/A char *returned_string;
2N/A char **return_array = NULL;
2N/A
2N/A *errp = 0;
2N/A *num_elements = 0;
2N/A
2N/A while ((returned_string =
2N/A retrieve_string(fp, line, BUFSIZE)) != NULL) {
2N/A
2N/A char **tmp_array;
2N/A
2N/A tmp_array = realloc(return_array,
2N/A (size_t)(((*num_elements) + 1) * sizeof (char *)));
2N/A if (tmp_array == NULL) {
2N/A *errp = errno;
2N/A fileutil_free_string_array(return_array, *num_elements);
2N/A *num_elements = 0;
2N/A return (NULL);
2N/A }
2N/A return_array = tmp_array;
2N/A
2N/A return_array[(*num_elements)] = strdup(returned_string);
2N/A if (return_array[(*num_elements)] == NULL) {
2N/A *errp = ENOMEM;
2N/A fileutil_free_string_array(return_array, *num_elements);
2N/A free(returned_string);
2N/A *num_elements = 0;
2N/A return (NULL);
2N/A }
2N/A
2N/A free(returned_string);
2N/A *num_elements = *num_elements + 1;
2N/A }
2N/A
2N/A /*
2N/A * Caller must free the space allocated to return_array by calling
2N/A * fileutil_free_string_array.
2N/A */
2N/A return (return_array);
2N/A} /* fileutil_get_first_column_data */
2N/A
2N/A/*
2N/A * Convenience function for retrieving the default fstype from /etc/fstypes.
2N/A */
2N/Achar *
2N/Afileutil_getfs(FILE *fp)
2N/A{
2N/A char *s;
2N/A static char buff[BUFSIZE]; /* line buffer */
2N/A
2N/A while (s = fgets(buff, BUFSIZE, fp)) {
2N/A while (isspace(*s) || *s != '\0') /* skip leading whitespace */
2N/A ++s;
2N/A if (*s != '#') { /* not a comment */
2N/A char *t = s;
2N/A while (!isspace(*t) && *t != '\0') /* get the token */
2N/A ++t;
2N/A *t = '\0'; /* ignore rest of line */
2N/A return (s);
2N/A }
2N/A }
2N/A return (NULL); /* that's all, folks! */
2N/A} /* fileutil_getfs */
2N/A
2N/Achar *
2N/Afileutil_getline(FILE *fp, char *line, int linesz)
2N/A{
2N/A char *share_cmd, *p = line;
2N/A *p = '\0';
2N/A
2N/A while (fgets(line, linesz, fp) != NULL) {
2N/A share_cmd = fileutil_get_cmd_from_string(line);
2N/A if (share_cmd != NULL)
2N/A return (share_cmd);
2N/A }
2N/A return (NULL);
2N/A} /* fileutil_getline */
2N/A
2N/A/*
2N/A * fileutil_get_cmd_from_string - retieves the command string minus any
2N/A * comments from the original string.
2N/A *
2N/A * Parameters:
2N/A * char *input_string - the original string.
2N/A */
2N/Achar *
2N/Afileutil_get_cmd_from_string(char *input_stringp)
2N/A{
2N/A /*
2N/A * Comments begin with '#'. Strip them off.
2N/A */
2N/A
2N/A char *returned_stringp;
2N/A char *start_of_commentp;
2N/A char *current_string;
2N/A
2N/A if ((input_stringp == NULL) || (strlen(input_stringp) == 0)) {
2N/A return (NULL);
2N/A }
2N/A
2N/A current_string = strdup(input_stringp);
2N/A
2N/A if (current_string == NULL) {
2N/A return (NULL);
2N/A }
2N/A
2N/A start_of_commentp = strchr(current_string, '#');
2N/A if (start_of_commentp != NULL) {
2N/A *start_of_commentp = '\0';
2N/A }
2N/A
2N/A returned_stringp = trim_trailing_whitespace(current_string);
2N/A free(current_string);
2N/A return (returned_stringp);
2N/A}
2N/A
2N/A/*
2N/A * NOTE: the caller of this function is responsible for freeing any
2N/A * memory allocated by calling fileutil_free_string_array()
2N/A *
2N/A * fileutil_add_string_to_array - adds one line to the file image
2N/A * string array
2N/A * Parameters:
2N/A * char ***string_array - reference to the string array
2N/A * char *line - the line to be added to the temporary dfstab
2N/A * int *count - the number of elements in the string array
2N/A * int *err - error pointer for returning any errors encountered
2N/A *
2N/A * Returns:
2N/A * B_TRUE on success, B_FALSE on failure.
2N/A */
2N/Aboolean_t
2N/Afileutil_add_string_to_array(char ***string_array, char *line, int *count,
2N/A int *err)
2N/A{
2N/A int i;
2N/A char **ret_val = NULL;
2N/A char **temp_array = NULL;
2N/A
2N/A temp_array = *string_array;
2N/A
2N/A ret_val = calloc(((*count) + 1), sizeof (char *));
2N/A if (ret_val != NULL) {
2N/A for (i = 0; i < *count; i ++) {
2N/A ret_val[i] = temp_array[i];
2N/A }
2N/A ret_val[*count] = strdup(line);
2N/A if (ret_val[*count] != NULL) {
2N/A (*count)++;
2N/A if (temp_array != NULL) {
2N/A free(temp_array);
2N/A }
2N/A *string_array = ret_val;
2N/A } else {
2N/A *err = ENOMEM;
2N/A free(ret_val);
2N/A return (B_FALSE);
2N/A }
2N/A } else {
2N/A *err = ENOMEM;
2N/A return (B_FALSE);
2N/A }
2N/A return (B_TRUE);
2N/A} /* fileutil_add_string_to_array */
2N/A
2N/A/*
2N/A * Private methods
2N/A */
2N/Astatic char *
2N/Aget_first_column_data(char *line)
2N/A{
2N/A return (strtok(line, "\t "));
2N/A} /* get_first_column_data */
2N/A
2N/Astatic char *
2N/Aretrieve_string(FILE *fp, char *line, int buffersize)
2N/A{
2N/A char *data;
2N/A char *returned_string;
2N/A
2N/A while ((returned_string =
2N/A fileutil_getline(fp, line, buffersize)) != NULL) {
2N/A
2N/A data = get_first_column_data(returned_string);
2N/A if (data != NULL)
2N/A return (data);
2N/A }
2N/A
2N/A return (NULL);
2N/A} /* retrieve_string */
2N/A
2N/A/*
2N/A * trim_trailing_whitespace - helper function to remove trailing
2N/A * whitespace from a line
2N/A *
2N/A * Parameters:
2N/A * char *input_stringp - the line to be trimed
2N/A */
2N/Astatic char *
2N/Atrim_trailing_whitespace(char *input_string)
2N/A{
2N/A char *last_nonspace;
2N/A char *return_string;
2N/A int string_length;
2N/A
2N/A
2N/A if (input_string == NULL) {
2N/A return (NULL);
2N/A }
2N/A string_length = strlen(input_string);
2N/A
2N/A if (string_length == 0 || *input_string == '\n') {
2N/A return (NULL);
2N/A }
2N/A
2N/A return_string = strdup(input_string);
2N/A if (return_string == NULL) {
2N/A return (NULL);
2N/A }
2N/A
2N/A /*
2N/A * Truncates the last character which will always be '\0'
2N/A */
2N/A last_nonspace = return_string + (string_length - 1);
2N/A
2N/A while (isspace(*last_nonspace)) {
2N/A last_nonspace--;
2N/A }
2N/A *(last_nonspace + 1) = '\0';
2N/A return (return_string);
2N/A}