iucode_tool: add function to parse signed 32-bit integers

Add parse_s32e(), based on parse_u32().  It will be used to parse
microcode revisions in the command line, so it has an extension
that accepts something like 0xfffffffe as an alias for -2.
parent cf52a589
......@@ -250,6 +250,31 @@ static int parse_u32(const char *nptr, char **endptr, int base,
return 0;
}
static int parse_s32e(const char *nptr, char **endptr, int base,
int32_t * const res)
{
long int l;
assert(nptr);
assert(endptr);
assert(res);
errno = 0;
l = strtol(nptr, endptr, base);
if (errno || nptr == *endptr)
return errno ? errno : EINVAL;
if (l > UINT32_MAX || l < INT32_MIN)
return ERANGE;
/*
* Accept UINT32_MAX >= x > INT32_MAX, as if it were
* the binary representation of int32_t read as uint32_t
*/
*res = (int32_t)(l & UINT32_MAX);
return 0;
}
static int is_valid_fd(const int fd)
{
return fcntl(fd, F_GETFD) != -1 || errno != EBADF;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment