LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 12-23-2008, 11:09 AM   #1
unkie888
Member
 
Registered: Aug 2007
Posts: 67

Rep: Reputation: 24
Question C programming linux - strtol does not set errorno on failure


According to the man page, strtol should set the errno:

Quote:
Since strtol() can legitimately return 0, LONG_MAX, or LONG_MIN (LLONG_MAX or LLONG_MIN for strtoll()) on both success and failure,
the calling program should set errno to 0 before the call, and then determine if an error occurred by checking whether errno has a
non-zero value after the call.

However, it appears not to:

Code:
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

main(argc, argv)
char **argv;
{
    char *val = "turdburger";
    int ival;

    errno = 0;

    ival = strtol(val,(char **)NULL, 10);

    if ( errno )
    {
      printf("FAIL - (%d)(%s)\n",errno,strerror(errno));
    }
    else
    {
      printf("apparently it worked %s=%d \n",val,ival);
    }
}


[root]# ./a.out
apparently it worked turdburger=0

[root]# uname -a
Linux xxx 2.6.22.14-72.fc6 #1 SMP Wed Nov 21 14:10:25 EST 2007 x86_64 x86_64 x86_64 GNU/Linux
 
Old 12-23-2008, 12:33 PM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,136

Rep: Reputation: 230Reputation: 230Reputation: 230
Hi

But before that:

Quote:
The strtol() function converts the initial part of the string in nptr
to a long integer value according to the given base, which must be
between 2 and 36 inclusive, or be the special value 0.
This means it only uses the digit chacacters at the left of the string. It stops at the first non-digit, and returns the number. It's zero for no digits at all. The only error it returns, is if the number is too big or too low for "long" variables.
 
Old 12-24-2008, 06:06 AM   #3
unkie888
Member
 
Registered: Aug 2007
Posts: 67

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by Guttorm View Post
Hi

But before that:



This means it only uses the digit chacacters at the left of the string. It stops at the first non-digit, and returns the number. It's zero for no digits at all. The only error it returns, is if the number is too big or too low for "long" variables.
Is there anyway in C to check weather a string is indeed an integer value?
 
Old 12-24-2008, 07:18 AM   #4
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,136

Rep: Reputation: 230Reputation: 230Reputation: 230
You mean to check if the string contains digits only? I don't think there is one, but it's easy to make one - looping thru the characters and check each with isdigit.


Another way:

Code:
if (strspn(s,"0123456789") == strlen(s)) {
  /* s contains only digits */
}
 
Old 12-26-2008, 09:29 AM   #5
unkie888
Member
 
Registered: Aug 2007
Posts: 67

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by Guttorm View Post

Code:
if (strspn(s,"0123456789") == strlen(s)) {
  /* s contains only digits */
}

I really liked that strspn tip.
 
  


Reply

Tags
gcc, programming


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
semtimedop() returns -1 with errorno 27(E2BIG) ugsimd Linux - General 0 10-29-2008 11:24 PM
how to set the login failure times? CharlieCai Linux - Software 10 06-15-2006 07:23 AM
ERRORNO 6 Error mounting ext3 with noatime nikhil_sabnis Linux - General 0 05-02-2006 12:26 PM
Failure to set terminal on 5.x install abyss AIX 7 04-23-2004 08:41 AM


All times are GMT -5. The time now is 01:33 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration