PDA

View Full Version : complete function definition i.e. atoi



c_lady
03-31-2010, 11:07 AM
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:




/* 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;
}



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

Thanks!

Btw. I am newbie!

MK27
03-31-2010, 11:21 AM
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.

Kennedy
03-31-2010, 11:40 AM
In uClibC, the definition of atoi is:
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:
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.

tabstop
03-31-2010, 11:43 AM
MacOS is BSD-based, so if you can find BSD source you're probably there.

c_lady
03-31-2010, 11:57 AM
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!

c_lady
03-31-2010, 11:58 AM
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 ... :(

fronty
03-31-2010, 12:02 PM
Mac OS X is partially derived from FreeBSD and NetBSD. You can find FreeBSD's atoi(3) here (http://svn.freebsd.org/viewvc/base/head/lib/libc/stdlib/atoi.c?r1=88681&view=log). 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.

c_lady
03-31-2010, 12:07 PM
This was a good one fronty! Thanks...

c_lady
03-31-2010, 12:09 PM
Ok I understood the function as well!

c_lady
03-31-2010, 12:16 PM
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?

laserlight
03-31-2010, 12:21 PM
Oh dear. You are looking at old syntax. Pretend that you did not see it, and that the code is this:

int atoi(const char *str)
{
return((int)strtol(str, (char **)NULL, 10));
}

Kennedy
03-31-2010, 12:28 PM
Oh dear. You are looking at old syntax. Pretend that you did not see it, and that the code is this:

int atoi(const char *str)
{
return((int)strtol(str, (char **)NULL, 10));
}

Wow! That looks exactly like what I posted.