#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sharefs/share.h>
#include "sharetab.h"
int
getshare(FILE *fd, share_t **shp)
{
static char *line = NULL;
static share_t *sh = NULL;
char *p;
char *lasts;
char *w = " \t";
if (line == NULL) {
line = (char *)malloc(MAXBUFSIZE+1);
if (line == NULL)
return (-1);
}
if (sh == NULL) {
sh = (share_t *)malloc(sizeof (*sh));
if (sh == NULL)
return (-1);
}
p = fgets(line, MAXBUFSIZE, fd);
if (p == NULL)
return (0);
line[strlen(line) - 1] = '\0';
sh->sh_path = (char *)strtok_r(p, w, &lasts);
if (sh->sh_path == NULL)
return (-3);
sh->sh_res = (char *)strtok_r(NULL, w, &lasts);
if (sh->sh_res == NULL)
return (-3);
sh->sh_fstype = (char *)strtok_r(NULL, w, &lasts);
if (sh->sh_fstype == NULL)
return (-3);
sh->sh_opts = (char *)strtok_r(NULL, w, &lasts);
if (sh->sh_opts == NULL)
return (-3);
sh->sh_descr = (char *)strtok_r(NULL, "", &lasts);
if (sh->sh_descr == NULL)
sh->sh_descr = "";
*shp = sh;
return (1);
}
share_t *
sharedup(share_t *sh)
{
share_t *nsh;
nsh = (share_t *)calloc(1, sizeof (*nsh));
if (nsh == NULL)
return (NULL);
if (sh->sh_path) {
nsh->sh_path = strdup(sh->sh_path);
if (nsh->sh_path == NULL)
goto alloc_failed;
}
if (sh->sh_res) {
nsh->sh_res = strdup(sh->sh_res);
if (nsh->sh_res == NULL)
goto alloc_failed;
}
if (sh->sh_fstype) {
nsh->sh_fstype = strdup(sh->sh_fstype);
if (nsh->sh_fstype == NULL)
goto alloc_failed;
}
if (sh->sh_opts) {
nsh->sh_opts = strdup(sh->sh_opts);
if (nsh->sh_opts == NULL)
goto alloc_failed;
}
if (sh->sh_descr) {
nsh->sh_descr = strdup(sh->sh_descr);
if (nsh->sh_descr == NULL)
goto alloc_failed;
}
return (nsh);
alloc_failed:
sharefree(nsh);
return (NULL);
}
void
sharefree(share_t *sh)
{
if (sh->sh_path != NULL)
free(sh->sh_path);
if (sh->sh_res != NULL)
free(sh->sh_res);
if (sh->sh_fstype != NULL)
free(sh->sh_fstype);
if (sh->sh_opts != NULL)
free(sh->sh_opts);
if (sh->sh_descr != NULL)
free(sh->sh_descr);
free(sh);
}
char *
getshareopt(char *optlist, char *opt)
{
char *p, *pe;
char *b;
char *bb;
char *lasts;
char *val = NULL;
b = bb = strdup(optlist);
if (b == NULL)
return (NULL);
while ((p = strtok_r(b, ",", &lasts)) != NULL) {
b = NULL;
if ((pe = strchr(p, '=')) != NULL) {
*pe = '\0';
if (strcmp(opt, p) == 0) {
val = strdup(pe + 1);
goto done;
}
}
if (strcmp(opt, p) == 0) {
val = strdup("");
goto done;
}
}
done:
free(bb);
return (val);
}