Skip to content

Commit

Permalink
zoomsh: fset command YAZ-916
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdickmeiss committed Oct 8, 2018
1 parent 525ba8b commit f3a5e77
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions zoom/zoomsh.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,60 @@ static int is_command(const char *cmd_str, const char *this_str, int this_len)
return 1;
}

static int cmd_fset(struct zoom_sh *sh, const char **args)
{
WRBUF key, fname = 0;
FILE *inf = 0;
int ret = -1;
long fsize = 0;
char *b = 0;

if (!(key = next_token_new_wrbuf(args)))
{
printf("missing argument for set\n");
return 1;
}
fname = next_token_new_wrbuf(args);
if (!fname)
{
printf("Missing arg for fset %s\n", wrbuf_cstr(key));
}
else if ((inf = fopen(wrbuf_cstr(fname), "rb")) == 0)
{
printf("Could not open %s\n", wrbuf_cstr(fname));
}
else if (fseek(inf, 0L, SEEK_END) == -1 || (fsize = ftell(inf)) == -1L)
{
printf("Couldn't seek in %s\n", wrbuf_cstr(fname));
}
else if (fseek(inf, 0L, SEEK_SET) == -1)
{
printf("Couldn't seek in %s\n", wrbuf_cstr(fname));
}
else if ((b = malloc(fsize + 1)) == 0)
{
printf("Couldn't allocate %ld bytes for %s\n",
fsize, wrbuf_cstr(fname));
}
else if (fsize > 0 && fread(b, 1, fsize, inf) != fsize)
{
printf("Couldn't read %ld bytes for %s\n",
fsize, wrbuf_cstr(fname));
}
else
{
ZOOM_options_setl(sh->options, wrbuf_cstr(key), b, fsize);
ret = 0;
}
if (inf)
fclose(inf);
if (fname)
wrbuf_destroy(fname);
wrbuf_destroy(key);
free(b);
return ret;
}

static int cmd_set(struct zoom_sh *sh, const char **args)
{
WRBUF key;
Expand All @@ -157,6 +211,7 @@ static int cmd_set(struct zoom_sh *sh, const char **args)
return 0;
}


static int cmd_get(struct zoom_sh *sh, const char **args)
{
WRBUF key;
Expand Down Expand Up @@ -856,6 +911,8 @@ static int cmd_parse(struct zoom_sh *sh, const char **buf)
return -1;
else if (is_command("set", cmd_str, cmd_len))
ret = cmd_set(sh, buf);
else if (is_command("fset", cmd_str, cmd_len))
ret = cmd_fset(sh, buf);
else if (is_command("get", cmd_str, cmd_len))
ret = cmd_get(sh, buf);
else if (is_command("rget", cmd_str, cmd_len))
Expand Down

0 comments on commit f3a5e77

Please sign in to comment.