Since your return_val
is an unsigned int
, you should probably be using strtoul()
which has been standard since C89 and is therefore supported by MSVC (whereas strtoll()
has only been standard since C99 and is not supported by MSVC).
Your testing of the error conditions is not adequate. You need to set errno
to zero before calling the conversion function; you also need to detect whether an error was reported, which is trickier than it seems.
Section §7.20.1.4 'The strtol, strtoll, strtoul, and strtoull functions' of the C99 standard says:
Returns
The strtol
, strtoll
, strtoul
, and strtoull
functions return the converted
value, if any. If no conversion could be performed, zero is returned. If the correct value
is outside the range of representable values, LONG_MIN, LONG_MAX, LLONG_MIN,
LLONG_MAX, ULONG_MAX, or ULLONG_MAX is returned (according to the return type
and sign of the value, if any), and the value of the macro ERANGE is stored in errno
.
You also have to read the look at the value stored in the endptr
parameter to the conversion functions to tell that no conversion was performed (as opposed to a valid zero was converted).
If the subject sequence is empty or does not have the expected form, no conversion is
performed; the value of nptr
is stored in the object pointed to by endptr
, provided
that endptr
is not a null pointer.
So, you must write code more like this (omitting the test against EINVAL because the standard does not mention these functions setting errno
to EINVAL):
unsigned int return_val=0;
if (index + 1 <= argc - 1)
{
char *end;
unsigned long ul;
errno = 0;
ul = strtoul(argv[index+1], &end, 10);
if ((ul == 0 && end == argv[index+1]) ||
(ul == ULONG_MAX && errno == ERANGE) ||
(ul > UINT_MAX))
{
fprintf(stderr, "Could not parse argument %s for switch %s!\n",
argv[index], argv[index+1]);
return 0;
}
retval = (unsigned int)ul;
}
Note that this is simpler than the test for a signed integer conversion which must take into account the negative <type>_MIN
limit as well as the <type>_MAX
limit.
Also note that you really should record the result in an unsigned long
and then check whether it fits within your designated range, which may be limited to UINT_MAX (which can be less than ULONG_MAX in a Unix-like 64-bit environment).