#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <sys/types.h>
#include <ctype.h>
#include <thread.h>
#include <synch.h>
#include "libfsmgt.h"
static char *get_first_column_data(char *line);
static char *retrieve_string(FILE *fp, char *line, int buffersize);
static char *trim_trailing_whitespace(char *line);
void
fileutil_free_string_array(char **arrayp, int num_elements)
{
if (arrayp != NULL) {
int i = 0;
for (i = 0; i < num_elements && arrayp[i] != NULL; i++) {
free(arrayp[i]);
}
free(arrayp);
}
}
char **
fileutil_get_first_column_data(FILE *fp, int *num_elements, int *errp)
{
char line[BUFSIZE];
char *returned_string;
char **return_array = NULL;
*errp = 0;
*num_elements = 0;
while ((returned_string =
retrieve_string(fp, line, BUFSIZE)) != NULL) {
char **tmp_array;
tmp_array = realloc(return_array,
(size_t)(((*num_elements) + 1) * sizeof (char *)));
if (tmp_array == NULL) {
*errp = errno;
fileutil_free_string_array(return_array, *num_elements);
*num_elements = 0;
return (NULL);
}
return_array = tmp_array;
return_array[(*num_elements)] = strdup(returned_string);
if (return_array[(*num_elements)] == NULL) {
*errp = ENOMEM;
fileutil_free_string_array(return_array, *num_elements);
free(returned_string);
*num_elements = 0;
return (NULL);
}
free(returned_string);
*num_elements = *num_elements + 1;
}
return (return_array);
}
char *
fileutil_getfs(FILE *fp)
{
char *s;
static char buff[BUFSIZE];
while (s = fgets(buff, BUFSIZE, fp)) {
while (isspace(*s) || *s != '\0')
++s;
if (*s != '#') {
char *t = s;
while (!isspace(*t) && *t != '\0')
++t;
*t = '\0';
return (s);
}
}
return (NULL);
}
char *
fileutil_getline(FILE *fp, char *line, int linesz)
{
char *share_cmd, *p = line;
*p = '\0';
while (fgets(line, linesz, fp) != NULL) {
share_cmd = fileutil_get_cmd_from_string(line);
if (share_cmd != NULL)
return (share_cmd);
}
return (NULL);
}
char *
fileutil_get_cmd_from_string(char *input_stringp)
{
char *returned_stringp;
char *start_of_commentp;
char *current_string;
if ((input_stringp == NULL) || (strlen(input_stringp) == 0)) {
return (NULL);
}
current_string = strdup(input_stringp);
if (current_string == NULL) {
return (NULL);
}
start_of_commentp = strchr(current_string, '#');
if (start_of_commentp != NULL) {
*start_of_commentp = '\0';
}
returned_stringp = trim_trailing_whitespace(current_string);
free(current_string);
return (returned_stringp);
}
boolean_t
fileutil_add_string_to_array(char ***string_array, char *line, int *count,
int *err)
{
int i;
char **ret_val = NULL;
char **temp_array = NULL;
temp_array = *string_array;
ret_val = calloc(((*count) + 1), sizeof (char *));
if (ret_val != NULL) {
for (i = 0; i < *count; i ++) {
ret_val[i] = temp_array[i];
}
ret_val[*count] = strdup(line);
if (ret_val[*count] != NULL) {
(*count)++;
if (temp_array != NULL) {
free(temp_array);
}
*string_array = ret_val;
} else {
*err = ENOMEM;
free(ret_val);
return (B_FALSE);
}
} else {
*err = ENOMEM;
return (B_FALSE);
}
return (B_TRUE);
}
static char *
get_first_column_data(char *line)
{
return (strtok(line, "\t "));
}
static char *
retrieve_string(FILE *fp, char *line, int buffersize)
{
char *data;
char *returned_string;
while ((returned_string =
fileutil_getline(fp, line, buffersize)) != NULL) {
data = get_first_column_data(returned_string);
if (data != NULL)
return (data);
}
return (NULL);
}
static char *
trim_trailing_whitespace(char *input_string)
{
char *last_nonspace;
char *return_string;
int string_length;
if (input_string == NULL) {
return (NULL);
}
string_length = strlen(input_string);
if (string_length == 0 || *input_string == '\n') {
return (NULL);
}
return_string = strdup(input_string);
if (return_string == NULL) {
return (NULL);
}
last_nonspace = return_string + (string_length - 1);
while (isspace(*last_nonspace)) {
last_nonspace--;
}
*(last_nonspace + 1) = '\0';
return (return_string);
}