Function To Test If there string is an int

This is a discussion on Function To Test If there string is an int within the Linux Programming forums, part of the Platform Specific Boards category; I am writing a kernel module and I am wondering what system call function would I use to compare if ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    96

    Function To Test If there string is an int

    I am writing a kernel module and I am wondering what system call function would I use to compare if the user entered string is an int or not?

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    See the manpg of strtol(), atoi(), atol(), or strtoul() routine.

  3. #3
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    There's also isdigit() in ctype.h - if every character is a digit, it's an integer.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by itCbitC View Post
    See the manpg of strtol(), atoi(), atol(), or strtoul() routine.
    In the kernel?
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by brewbuck View Post
    In the kernel?
    Whoops! missed the part about the kernel module and coming from a closed source OS I haven't the faintest idea how to even begin writing one.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    Quote Originally Posted by brewbuck
    In the kernel?
    It seems possible to me, at least in theory.

    But I guess that this is in practice and concerns Linux. Moved to Linux Programming forum.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by laserlight View Post
    It seems possible to me, at least in theory.

    But I guess that this is in practice and concerns Linux. Moved to Linux Programming forum.
    There's nothing stopping you from implementing atoi() yourself, you just don't get it from the C library because the C library isn't available in the kernel.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    Quote Originally Posted by brewbuck
    There's nothing stopping you from implementing atoi() yourself, you just don't get it from the C library because the C library isn't available in the kernel.
    I know that, with respect to Linux. However, I gather from your response that it is impossible to implement an operating system for which the C library, or at least parts of it, is available in the kernel? Why is that so?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    Quote Originally Posted by laserlight View Post
    I know that, with respect to Linux. However, I gather from your response that it is impossible to implement an operating system for which the C library, or at least parts of it, is available in the kernel? Why is that so?
    It's not impossible, and in fact if you linked the kernel with the C library just to get a function like atoi(), it would probably work (not that I recommend it).

    A kernel is really one of the most non-standard, non-portable things you can write. It doesn't seem to make much sense to try to impose a concept like a standard library on top of it.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,936
    Quote Originally Posted by brewbuck
    It's not impossible, and in fact if you linked the kernel with the C library just to get a function like atoi(), it would probably work (not that I recommend it).

    A kernel is really one of the most non-standard, non-portable things you can write. It doesn't seem to make much sense to try to impose a concept like a standard library on top of it.
    Right, so we were actually just saying the same thing. That said, I note that sean's suggestion also relies on the standard library, so NuNn's question has not been answered yet (other than with a "write your own function" suggestion).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Kernel API String Conversion Functions

    Call simple_strtol and check the end pointer.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. getting a headache
    By sreetvert83 in forum C++ Programming
    Replies: 41
    Last Post: 09-30-2005, 05:20 AM
  5. A Simple (?) Problem
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 10-12-2001, 04:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21