A
arnuld
OJECTIVE: Why does perror() says SUCESSS ?
This code converts a string like "1234" into an unsingned long integer. I
wrote it following the discussion here on clc of which I have lost the
url from google http interface to newsgroup.
perror() reports error in else condition many times, I can't understand
why:
int convert_string_to_ulongint(const char* str, unsigned long* ulp)
{
int ret;
unsigned long int num;
char* p;
if(NULL == str || '\0' == *str || NULL == ulp)
{
printf("IN: %s @ %d: Invalid Args\n", __func__, __LINE__);
return VAL_ERR;
}
p = NULL;
errno = 0;
num = strtoul(str, &p, 10);
/* Error check */
if(ERANGE == errno)
{
if((0 == num) && (0 == strcmp(str, p)))
{
printf("IN: %s @%d: strtoul() could not perform conversion\n",
__func__, __LINE__);
}
else if(ULONG_MAX == num)
{
printf("IN: %s @%dp: strtoul() overflow error\n", __func__,
__LINE__);
}
else
{
printf("IN: %s @%d: strange output from strtoul()\n", __func__,
__LINE__);
}
perror("*ERROR ERANGE*");
ret = -1;
}
else if((0 == errno) && ('\0' == *p))
{
printf("IN: %s @%d: Successful Conversion by strtoul()\n",
__func__, __LINE__);
*ulp = num;
ret = 1;
}
else
{
printf("IN: %s @%d: strange conversions and errno values
\n",
__func__, __LINE__);
printf("num = %lu, ULONG_MAX = %lu\n", num, ULONG_MAX);
perror("*ERROR in ELSE");
ret = -1;
}
This code converts a string like "1234" into an unsingned long integer. I
wrote it following the discussion here on clc of which I have lost the
url from google http interface to newsgroup.
perror() reports error in else condition many times, I can't understand
why:
int convert_string_to_ulongint(const char* str, unsigned long* ulp)
{
int ret;
unsigned long int num;
char* p;
if(NULL == str || '\0' == *str || NULL == ulp)
{
printf("IN: %s @ %d: Invalid Args\n", __func__, __LINE__);
return VAL_ERR;
}
p = NULL;
errno = 0;
num = strtoul(str, &p, 10);
/* Error check */
if(ERANGE == errno)
{
if((0 == num) && (0 == strcmp(str, p)))
{
printf("IN: %s @%d: strtoul() could not perform conversion\n",
__func__, __LINE__);
}
else if(ULONG_MAX == num)
{
printf("IN: %s @%dp: strtoul() overflow error\n", __func__,
__LINE__);
}
else
{
printf("IN: %s @%d: strange output from strtoul()\n", __func__,
__LINE__);
}
perror("*ERROR ERANGE*");
ret = -1;
}
else if((0 == errno) && ('\0' == *p))
{
printf("IN: %s @%d: Successful Conversion by strtoul()\n",
__func__, __LINE__);
*ulp = num;
ret = 1;
}
else
{
printf("IN: %s @%d: strange conversions and errno values
__func__, __LINE__);
printf("num = %lu, ULONG_MAX = %lu\n", num, ULONG_MAX);
perror("*ERROR in ELSE");
ret = -1;
}