diff options
Diffstat (limited to 'libc/stdlib')
-rw-r--r-- | libc/stdlib/Makefile | 9 | ||||
-rw-r--r-- | libc/stdlib/abort.c | 2 | ||||
-rw-r--r-- | libc/stdlib/malloc/alloc.c | 4 | ||||
-rw-r--r-- | libc/stdlib/malloc/malloc.c | 4 | ||||
-rw-r--r-- | libc/stdlib/strto_l.c | 4 | ||||
-rw-r--r-- | libc/stdlib/strto_ll.c | 192 |
6 files changed, 206 insertions, 9 deletions
diff --git a/libc/stdlib/Makefile b/libc/stdlib/Makefile index 4d16a8585..e4b28979c 100644 --- a/libc/stdlib/Makefile +++ b/libc/stdlib/Makefile @@ -29,6 +29,9 @@ DIRS = $(MALLOC) MSRC=strto_l.c MOBJ=strtol.o strtoul.o strto_l.o +MSRC1=strto_ll.c +MOBJ1=strtoll.o strtoull.o strto_ll.o + MSRC2=atexit.c MOBJ2=on_exit.o atexit.o __do_exit.o exit.o @@ -38,7 +41,7 @@ CSRC = abort.c getenv.c mktemp.c qsort.c realpath.c strtod.c \ COBJS=$(patsubst %.c,%.o, $(CSRC)) -OBJS=$(MOBJ) $(MOBJ2) $(COBJS) +OBJS=$(MOBJ) $(MOBJ1) $(MOBJ2) $(COBJS) all: $(OBJS) $(LIBC) @@ -51,6 +54,10 @@ $(MOBJ): $(MSRC) $(CC) $(CFLAGS) -DL_$* $< -c -o $*.o $(STRIPTOOL) -x -R .note -R .comment $*.o +$(MOBJ1): $(MSRC1) + $(CC) $(CFLAGS) -DL_$* $< -c -o $*.o + $(STRIPTOOL) -x -R .note -R .comment $*.o + $(MOBJ2): $(MSRC2) $(CC) $(CFLAGS) -DL_$* $< -c -o $*.o $(STRIPTOOL) -x -R .note -R .comment $*.o diff --git a/libc/stdlib/abort.c b/libc/stdlib/abort.c index 6806a8b55..23510e913 100644 --- a/libc/stdlib/abort.c +++ b/libc/stdlib/abort.c @@ -32,13 +32,11 @@ extern void _exit __P((int __status)) __attribute__ ((__noreturn__)); /* Cause an abnormal program termination with core-dump. */ void abort(void) { -#if FIXME sigset_t sigset; if (sigemptyset(&sigset) == 0 && sigaddset(&sigset, SIGABRT) == 0) { sigprocmask(SIG_UNBLOCK, &sigset, (sigset_t *) NULL); } -#endif if (__cleanup) __cleanup(); diff --git a/libc/stdlib/malloc/alloc.c b/libc/stdlib/malloc/alloc.c index b782f6dcf..22d508ca1 100644 --- a/libc/stdlib/malloc/alloc.c +++ b/libc/stdlib/malloc/alloc.c @@ -12,7 +12,7 @@ void * calloc_dbg(size_t num, size_t size, char * function, char * file, int line) { void * ptr; - fprintf(stderr, "calloc of %d bytes at %s @%s:%d = ", num*size, function, file, line); + fprintf(stderr, "calloc of %ld bytes at %s @%s:%d = ", (long)(num*size), function, file, line); ptr = calloc(num,size); fprintf(stderr, "%p\n", ptr); return ptr; @@ -26,7 +26,7 @@ void * malloc_dbg(size_t len, char * function, char * file, int line) { void * result; - fprintf(stderr, "malloc of %d bytes at %s @%s:%d = ", len, function, file, line); + fprintf(stderr, "malloc of %ld bytes at %s @%s:%d = ", (long)len, function, file, line); result = malloc(len); fprintf(stderr, "%p\n", result); return result; diff --git a/libc/stdlib/malloc/malloc.c b/libc/stdlib/malloc/malloc.c index d7df5d243..a3f50fb3b 100644 --- a/libc/stdlib/malloc/malloc.c +++ b/libc/stdlib/malloc/malloc.c @@ -296,8 +296,8 @@ struct Block_s /* 32-bytes long control structure (if 4-byte aligned) */ /* packed 4-byte attributes */ /* { */ - char bal_free_mem : 8; /* balance of <free_mem> subtree */ - char bal_ptrs : 8; /* balance of <ptrs> subtree */ + signed char bal_free_mem : 8; /* balance of <free_mem> subtree */ + signed char bal_ptrs : 8; /* balance of <ptrs> subtree */ unsigned int used : 1; /* used/free state of the block */ unsigned int broken : 1; /* 1 if previous block can't be merged with it */ /* } */ diff --git a/libc/stdlib/strto_l.c b/libc/stdlib/strto_l.c index a83cf4095..16b29f5d6 100644 --- a/libc/stdlib/strto_l.c +++ b/libc/stdlib/strto_l.c @@ -15,7 +15,7 @@ /* OPTIONS */ /*****************************************************************************/ -/* Set if we want strtod to set errno appropriately. */ +/* Set if we want errno set appropriately. */ /* NOTE: Implies _STRTO_ENDPTR below */ #define _STRTO_ERRNO 0 @@ -107,8 +107,8 @@ unsigned long _strto_l(const char *str, char **endptr, int base, int uflag) goto DONE; } + cutoff_digit = ULONG_MAX % base; cutoff = ULONG_MAX / base; - cutoff_digit = ULONG_MAX - cutoff * base; while (1) { digit = 40; diff --git a/libc/stdlib/strto_ll.c b/libc/stdlib/strto_ll.c new file mode 100644 index 000000000..4b2854b8e --- /dev/null +++ b/libc/stdlib/strto_ll.c @@ -0,0 +1,192 @@ +/* + * Copyright (C) 2000 Manuel Novoa III + * + * Notes: + * + * The primary objective of this implementation was minimal size. + * + * Note: Assumes char layout 0-9.*A-Z.*a-z for ordinals values. + * + * There are a couple of compile-time options below. + * + */ + +/*****************************************************************************/ +/* OPTIONS */ +/*****************************************************************************/ + +/* Set if we want errno set appropriately. */ +/* NOTE: Implies _STRTO_ENDPTR below */ +#define _STRTO_ERRNO 0 + +/* Set if we want support for the endptr arg. */ +/* Implied by _STRTO_ERRNO. */ +#define _STRTO_ENDPTR 1 + +/*****************************************************************************/ +/* Don't change anything that follows. */ +/*****************************************************************************/ + +#if _STRTO_ERRNO +#undef _STRTO_ENDPTR +#define _STRTO_ENDPTR 1 +#endif + +/*****************************************************************************/ + +/* Are there actually any machines where this might fail? */ +#if 'A' > 'a' +#error ordering assumption violated : 'A' > 'a' +#endif + +#include <stdlib.h> +#include <limits.h> +#include <ctype.h> + +#if _STRTO_ERRNO +#include <errno.h> +#endif + +unsigned long long _strto_ll(const char *str, char **endptr, int base, int uflag); + +#if L_strto_ll + +/* + * This is the main work fuction which handles both strtol (uflag = 0) and + * strtoul (uflag = 1). + */ + +unsigned long long _strto_ll(const char *str, char **endptr, int base, int uflag) +{ + unsigned long long number = 0; + unsigned long long cutoff; + char *pos = (char *) str; +#if _STRTO_ENDPTR + char *fail_char = (char *) str; +#endif + int digit, cutoff_digit; + int negative; + + while (isspace(*pos)) { /* skip leading whitespace */ + ++pos; + } + + /* handle optional sign */ + negative = 0; + switch(*pos) { + case '-': negative = 1; /* fall through to increment pos */ + case '+': ++pos; + } + + if ((base == 16) && (*pos == '0')) { /* handle option prefix */ + ++pos; +#if _STRTO_ENDPTR + fail_char = pos; +#endif + if ((*pos == 'x') || (*pos == 'X')) { + ++pos; + } + } + + if (base == 0) { /* dynamic base */ + base = 10; /* default is 10 */ + if (*pos == '0') { + ++pos; + base -= 2; /* now base is 8 (or 16) */ +#if _STRTO_ENDPTR + fail_char = pos; +#endif + if ((*pos == 'x') || (*pos == 'X')) { + base += 8; /* base is 16 */ + ++pos; + } + } + } + + if ((base < 2) || (base > 36)) { /* illegal base */ + goto DONE; + } + + cutoff_digit = ULONG_LONG_MAX % base; + cutoff = ULONG_LONG_MAX / base; + + while (1) { + digit = 40; + if ((*pos >= '0') && (*pos <= '9')) { + digit = (*pos - '0'); + } else if (*pos >= 'a') { + digit = (*pos - 'a' + 10); + } else if (*pos >= 'A') { + digit = (*pos - 'A' + 10); + } else break; + + if (digit >= base) { + break; + } + + ++pos; +#if _STRTO_ENDPTR + fail_char = pos; +#endif + + /* adjust number, with overflow check */ + if ((number > cutoff) + || ((number == cutoff) && (digit > cutoff_digit))) { + number = ULONG_LONG_MAX; + if (uflag) { + negative = 0; /* since unsigned returns ULONG_LONG_MAX */ + } +#if _STRTO_ERRNO + errno = ERANGE; +#endif + } else { + number = number * base + digit; + } + + } + + DONE: +#if _STRTO_ENDPTR + if (endptr) { + *endptr = fail_char; + } +#endif + + if (negative) { + if (!uflag && (number > ((unsigned long long)(-(1+LONG_LONG_MIN)))+1)) { +#if _STRTO_ERRNO + errno = ERANGE; +#endif + return (unsigned long long) LONG_LONG_MIN; + } + return (unsigned long long)(-((long long)number)); + } else { + if (!uflag && (number > (unsigned long long) LONG_LONG_MAX)) { +#if _STRTO_ERRNO + errno = ERANGE; +#endif + return LONG_LONG_MAX; + } + return number; + } +} + +#endif + +#if L_strtoull + +unsigned long long strtoull(const char *str, char **endptr, int base) +{ + return _strto_ll(str, endptr, base, 1); +} + +#endif + +#if L_strtoll + +long long strtoll(const char *str, char **endptr, int base) +{ + return _strto_ll(str, endptr, base, 0); +} + +#endif |