complete function definition i.e. atoi

This is a discussion on complete function definition i.e. atoi within the Linux Programming forums, part of the Platform Specific Boards category; I am using MACOSX but since is UNIX (Darwin) based etc. and the forum has not other suitable place I ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    94

    Unhappy complete function definition i.e. atoi

    I am using MACOSX but since is UNIX (Darwin) based etc. and the forum has not other suitable place I am writing my problem here.

    I would like to find the code where complete function definition is placed for different functions i.e. atoi.

    I am able to understand that to use the atoi I need to define the stdlib.h in the top.
    #include <stdlib.h>

    mainly because of this I searched through the shell $ find . -name "stdio.h"

    and I found but... when I go inside I can find only this:
    int atoi(const char *);

    And what actually I would like to find is this:

    [tag]
    Code:
    /* atoi: convert s to integer; version 2 */
    int atoi(char s[])
    {
    int i, n, sign;
    for (i = 0; isspace(s[i]); i++) /* skip white space */
    ;
    sign = (s[i] == '-') ? -1 : 1;
    if (s[i] == '+' || s[i] == '-') /* skip sign */
    i++;
    for (n = 0; isdigit(s[i]); i++)
    n = 10 * n + (s[i] - '0');
    return sign * n;
    }
    [/tag]

    Any help please? How I can achieve this etc.?

    Thanks!

    Btw. I am newbie!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    That's in the library sources. For open source libraries you can download them, but I suspect since MACOSX is not open source, you actually cannot look at the source used for atoi on your system. You only get the stdio header.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    In uClibC, the definition of atoi is:
    Code:
    libc_hidden_proto(atoi)
    int atoi(const char *nptr)
    {
            return (int) strtol(nptr, (char **) NULL, 10);
    }
    libc_hidden_def(atoi)
    then the definition of strtol is obscure, but the bottom line is:
    Code:
    unsigned long attribute_hidden __XL_NPP(_stdlib_strto_l)(register const Wchar * __restrict str,
                                                                                    Wchar ** __restrict endptr, int base,
                                                                                    int sflag __LOCALE_PARAM)
    {
            unsigned long number, cutoff;
    #if _STRTO_ENDPTR
            const Wchar *fail_char;
    #define SET_FAIL(X) fail_char = (X)
    #else
    #define SET_FAIL(X) ((void)(X)) /* Keep side effects. */
    #endif
            unsigned char negative, digit, cutoff_digit;
    
            assert(((unsigned int)sflag) <= 1);
    
            SET_FAIL(str);
    
            while (ISSPACE(*str)) { /* Skip leading whitespace. */
                    ++str;
            }
    
            /* Handle optional sign. */
            negative = 0;
            switch (*str) {
                    case '-': negative = 1; /* Fall through to increment str. */
                    case '+': ++str;
            }
    
            if (!(base & ~0x10)) {          /* Either dynamic (base = 0) or base 16. */
                    base += 10;                             /* Default is 10 (26). */
                    if (*str == '0') {
                            SET_FAIL(++str);
                            base -= 2;                      /* Now base is 8 or 16 (24). */
                            if ((0x20|(*str)) == 'x') { /* WARNING: assumes ascii. */
                                    ++str;
                                    base += base;   /* Base is 16 (16 or 48). */
                            }
                    }
    
                    if (base > 16) {                /* Adjust in case base wasn't dynamic. */
                            base = 16;
                    }
            }
    
            number = 0;
    
            if (((unsigned)(base - 2)) < 35) { /* Legal base. */
                    cutoff_digit = ULONG_MAX % base;
                    cutoff = ULONG_MAX / base;
                    do {
                            digit = ((Wuchar)(*str - '0') <= 9)
                                    ? /* 0..9 */ (*str - '0')
                                    : /* else */ (((Wuchar)(0x20 | *str) >= 'a') /* WARNING: assumes ascii. */
                                       ? /* >= A/a */ ((Wuchar)(0x20 | *str) - ('a' - 10))
                                       : /* else   */ 40 /* bad value */);
    
                            if (digit >= base) {
                                    break;
                            }
    
                            SET_FAIL(++str);
    
                            if ((number > cutoff)
                                    || ((number == cutoff) && (digit > cutoff_digit))) {
                                    number = ULONG_MAX;
                                    negative &= sflag;
                                    SET_ERRNO(ERANGE);
                            } else {
                                    number = number * base + digit;
                            }
                    } while (1);
            }
    
    #if _STRTO_ENDPTR
            if (endptr) {
                    *endptr = (Wchar *) fail_char;
            }
    #endif
    
            {
                    unsigned long tmp = (negative
                                                             ? ((unsigned long)(-(1+LONG_MIN)))+1
                                                             : LONG_MAX);
                    if (sflag && (number > tmp)) {
                            number = tmp;
                            SET_ERRNO(ERANGE);
                    }
            }
    
            return negative ? (unsigned long)(-((long)number)) : number;
    }
    I'm not 100% sure, but I think this is the answer you are looking for. This, however, is the "stripped out" version and is not the exact code that is in GlibC.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    MacOS is BSD-based, so if you can find BSD source you're probably there.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    From all of you I was able to understand just the MK27 post!

    Anyhow I am not able to understand 100% the atoi function that I wrote is taken from K&R could someone please explain me a bit?

    Thanks!

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    Not all of it! I cannot understand the extend of the first for for example! Where it stops does it goes until the end or? Is mainly because we dont have brackets and I am lost ...

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    Mac OS X is partially derived from FreeBSD and NetBSD. You can find FreeBSD's atoi(3) here. Only changes when compared to 4.4BSD are SCM id, additional pair of paranthesis and removal of advertising clause from licence. You can also find strtoll(3) there.
    Last edited by fronty; 03-31-2010 at 01:10 PM.

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    This was a good one fronty! Thanks...

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    Ok I understood the function as well!

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    94
    atoi(str)
    const char *str;
    {
    return((int)strtol(str, (char **)NULL, 10));
    }

    This type of atoi I am not able to understand! Does this means if I will cover pointers in a good way I will be able to understand it?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,313
    Oh dear. You are looking at old syntax. Pretend that you did not see it, and that the code is this:
    Code:
    int atoi(const char *str)
    {
        return((int)strtol(str, (char **)NULL, 10));
    }
    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

  12. #12
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Quote Originally Posted by laserlight View Post
    Oh dear. You are looking at old syntax. Pretend that you did not see it, and that the code is this:
    Code:
    int atoi(const char *str)
    {
        return((int)strtol(str, (char **)NULL, 10));
    }
    Wow! That looks exactly like what I posted.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 04:33 AM
  2. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

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