The main problem I am facing here is that strtoll() is flagged as an error in VC 2010 (error C3861: 'strtoll': identifier not found). Will it do the same thing if I replace it with strtol()?
unsigned int get_uintval_from_arg(int argc, int index, char **argv,
unsigned int lower_bound, unsigned int upper_bound)
{
unsigned int return_val=0;
if (index + 1 <= argc - 1)
{
return_val=(unsigned int)strtoll(argv[index+1],NULL,10);
if (errno == EINVAL || errno== ERANGE)
{
fprintf(stderr, "Could not parse argument %s for switch %s!\n",
argv[index], argv[index+1]);
return 0;
}
}
// ....... I will post the remaining part of the code if necessary
.......
}
Since your
return_valis anunsigned int, you should probably be usingstrtoul()which has been standard since C89 and is therefore supported by MSVC (whereasstrtoll()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
errnoto 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:
You also have to read the look at the value stored in the
endptrparameter to the conversion functions to tell that no conversion was performed (as opposed to a valid zero was converted).So, you must write code more like this (omitting the test against EINVAL because the standard does not mention these functions setting
errnoto EINVAL):Note that this is simpler than the test for a signed integer conversion which must take into account the negative
<type>_MINlimit as well as the<type>_MAXlimit.Also note that you really should record the result in an
unsigned longand 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).