container.c: Add minor fixes

This commit is contained in:
Xavier Del Campo Romero 2024-01-27 12:41:46 +01:00
parent 6120d66dd0
commit f0c19df495
Signed by: xavi
GPG Key ID: 84FF3612A9BF43F2
1 changed files with 13 additions and 12 deletions

View File

@ -41,13 +41,19 @@ static int get_file_size(size_t *const sz, FILE *const f)
return -1;
errno = 0;
const unsigned long val = strtoul(szstr, NULL, 10);
char *end;
const unsigned long val = strtoul(szstr, &end, 10);
if (errno)
{
fprintf(stderr, "%s: strtoul(3): %s\n", __func__, strerror(errno));
return -1;
}
else if (*end)
{
fprintf(stderr, "%s: invalid number %s\n", __func__, szstr);
return -1;
}
*sz = val;
return 0;
@ -123,29 +129,24 @@ static const struct container *find_element(const struct container *const list,
static int read_element(const struct container *const list, const size_t n,
FILE *const f, bool *const done)
{
int ret = -1;
long init_off;
const struct container *el = NULL;
size_t sz;
if (!(el = find_element(list, n, f)))
goto end;
else if (get_file_size(&sz, f))
goto end;
if (!(el = find_element(list, n, f))
|| get_file_size(&sz, f))
return -1;
else if ((init_off = ftell(f)) < 0)
{
fprintf(stderr, "%s:%d: fseek failed: %s\n",
__func__, __LINE__, strerror(errno));
goto end;
return -1;
}
else if (read_file_contents(el, f, init_off, sz))
goto end;
return -1;
done[el - list] = true;
ret = 0;
end:
return ret;
return 0;
}
static int read_all_elements(const struct container *const list, const size_t n,