From 32c96ddd14acadc82e7fccf110367ec3c8320c85 Mon Sep 17 00:00:00 2001 From: Corinna Vinschen Date: Tue, 16 Dec 2014 10:55:17 +0000 Subject: * libc/include/stdlib.h (__itoa): Declare prototype. (__utoa): Ditto. (itoa): Ditto, non-strict-ANSI only. (utoa): Ditto. * libc/stdlib/Makefile.am: Add itoa.c and utoa.c. * libc/stdlib/Makefile.in: Regenerate. * libc/stdlib/itoa.c: New file. * libc/stdlib/utoa.c: New file. --- newlib/ChangeLog | 11 ++++++ newlib/libc/include/stdlib.h | 6 ++++ newlib/libc/stdlib/Makefile.am | 4 +++ newlib/libc/stdlib/Makefile.in | 59 ++++++++++++++++++++------------ newlib/libc/stdlib/itoa.c | 69 ++++++++++++++++++++++++++++++++++++++ newlib/libc/stdlib/utoa.c | 76 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 203 insertions(+), 22 deletions(-) create mode 100644 newlib/libc/stdlib/itoa.c create mode 100644 newlib/libc/stdlib/utoa.c diff --git a/newlib/ChangeLog b/newlib/ChangeLog index 10a77557f..3cba3e9ed 100644 --- a/newlib/ChangeLog +++ b/newlib/ChangeLog @@ -1,3 +1,14 @@ +2014-12-16 Jon Beniston + + * libc/include/stdlib.h (__itoa): Declare prototype. + (__utoa): Ditto. + (itoa): Ditto, non-strict-ANSI only. + (utoa): Ditto. + * libc/stdlib/Makefile.am: Add itoa.c and utoa.c. + * libc/stdlib/Makefile.in: Regenerate. + * libc/stdlib/itoa.c: New file. + * libc/stdlib/utoa.c: New file. + 2014-12-15 Jonathan Roelofs * libc/include/math.h: Add log2l,logbl,nexttowardf, diff --git a/newlib/libc/include/stdlib.h b/newlib/libc/include/stdlib.h index c6562c704..38ac29654 100644 --- a/newlib/libc/include/stdlib.h +++ b/newlib/libc/include/stdlib.h @@ -176,6 +176,12 @@ char * _EXFUN(ecvtbuf,(double, int, int*, int*, char *)); char * _EXFUN(fcvtbuf,(double, int, int*, int*, char *)); char * _EXFUN(ecvtf,(float,int,int *,int *)); char * _EXFUN(dtoa,(double, int, int, int *, int*, char**)); +#endif +char * _EXFUN(__itoa,(int, char *, int)); +char * _EXFUN(__utoa,(unsigned, char *, int)); +#ifndef __STRICT_ANSI__ +char * _EXFUN(itoa,(int, char *, int)); +char * _EXFUN(utoa,(unsigned, char *, int)); int _EXFUN(rand_r,(unsigned *__seed)); double _EXFUN(drand48,(_VOID)); diff --git a/newlib/libc/stdlib/Makefile.am b/newlib/libc/stdlib/Makefile.am index 22fedcc84..e90517f85 100644 --- a/newlib/libc/stdlib/Makefile.am +++ b/newlib/libc/stdlib/Makefile.am @@ -31,6 +31,7 @@ GENERAL_SOURCES = \ gdtoa-hexnan.c \ getenv.c \ getenv_r.c \ + itoa.c \ labs.c \ ldiv.c \ ldtoa.c \ @@ -52,6 +53,7 @@ GENERAL_SOURCES = \ strtod.c \ strtol.c \ strtoul.c \ + utoa.c \ wcstod.c \ wcstol.c \ wcstoul.c \ @@ -257,6 +259,7 @@ CHEWOUT_FILES= \ envlock.def \ exit.def \ getenv.def \ + itoa.def \ labs.def \ ldiv.def \ llabs.def \ @@ -277,6 +280,7 @@ CHEWOUT_FILES= \ strtoll.def \ strtoul.def \ strtoull.def \ + utoa.def \ wcsnrtombs.def \ wcstod.def \ wcstol.def \ diff --git a/newlib/libc/stdlib/Makefile.in b/newlib/libc/stdlib/Makefile.in index 319079dd5..7d014a470 100644 --- a/newlib/libc/stdlib/Makefile.in +++ b/newlib/libc/stdlib/Makefile.in @@ -94,17 +94,18 @@ am__objects_2 = lib_a-__adjust.$(OBJEXT) lib_a-__atexit.$(OBJEXT) \ lib_a-eprintf.$(OBJEXT) lib_a-exit.$(OBJEXT) \ lib_a-gdtoa-gethex.$(OBJEXT) lib_a-gdtoa-hexnan.$(OBJEXT) \ lib_a-getenv.$(OBJEXT) lib_a-getenv_r.$(OBJEXT) \ - lib_a-labs.$(OBJEXT) lib_a-ldiv.$(OBJEXT) \ - lib_a-ldtoa.$(OBJEXT) lib_a-malloc.$(OBJEXT) \ - lib_a-mblen.$(OBJEXT) lib_a-mblen_r.$(OBJEXT) \ - lib_a-mbstowcs.$(OBJEXT) lib_a-mbstowcs_r.$(OBJEXT) \ - lib_a-mbtowc.$(OBJEXT) lib_a-mbtowc_r.$(OBJEXT) \ - lib_a-mlock.$(OBJEXT) lib_a-mprec.$(OBJEXT) \ - lib_a-mstats.$(OBJEXT) lib_a-rand.$(OBJEXT) \ - lib_a-rand_r.$(OBJEXT) lib_a-realloc.$(OBJEXT) \ - lib_a-reallocf.$(OBJEXT) lib_a-sb_charsets.$(OBJEXT) \ - lib_a-strtod.$(OBJEXT) lib_a-strtol.$(OBJEXT) \ - lib_a-strtoul.$(OBJEXT) lib_a-wcstod.$(OBJEXT) \ + lib_a-itoa.$(OBJEXT) lib_a-labs.$(OBJEXT) \ + lib_a-ldiv.$(OBJEXT) lib_a-ldtoa.$(OBJEXT) \ + lib_a-malloc.$(OBJEXT) lib_a-mblen.$(OBJEXT) \ + lib_a-mblen_r.$(OBJEXT) lib_a-mbstowcs.$(OBJEXT) \ + lib_a-mbstowcs_r.$(OBJEXT) lib_a-mbtowc.$(OBJEXT) \ + lib_a-mbtowc_r.$(OBJEXT) lib_a-mlock.$(OBJEXT) \ + lib_a-mprec.$(OBJEXT) lib_a-mstats.$(OBJEXT) \ + lib_a-rand.$(OBJEXT) lib_a-rand_r.$(OBJEXT) \ + lib_a-realloc.$(OBJEXT) lib_a-reallocf.$(OBJEXT) \ + lib_a-sb_charsets.$(OBJEXT) lib_a-strtod.$(OBJEXT) \ + lib_a-strtol.$(OBJEXT) lib_a-strtoul.$(OBJEXT) \ + lib_a-utoa.$(OBJEXT) lib_a-wcstod.$(OBJEXT) \ lib_a-wcstol.$(OBJEXT) lib_a-wcstoul.$(OBJEXT) \ lib_a-wcstombs.$(OBJEXT) lib_a-wcstombs_r.$(OBJEXT) \ lib_a-wctomb.$(OBJEXT) lib_a-wctomb_r.$(OBJEXT) \ @@ -151,13 +152,13 @@ am__objects_9 = __adjust.lo __atexit.lo __call_atexit.lo __exp10.lo \ __ten_mu.lo _Exit.lo abort.lo abs.lo assert.lo atexit.lo \ atof.lo atoff.lo atoi.lo atol.lo calloc.lo div.lo dtoa.lo \ dtoastub.lo environ.lo envlock.lo eprintf.lo exit.lo \ - gdtoa-gethex.lo gdtoa-hexnan.lo getenv.lo getenv_r.lo labs.lo \ - ldiv.lo ldtoa.lo malloc.lo mblen.lo mblen_r.lo mbstowcs.lo \ - mbstowcs_r.lo mbtowc.lo mbtowc_r.lo mlock.lo mprec.lo \ - mstats.lo rand.lo rand_r.lo realloc.lo reallocf.lo \ - sb_charsets.lo strtod.lo strtol.lo strtoul.lo wcstod.lo \ - wcstol.lo wcstoul.lo wcstombs.lo wcstombs_r.lo wctomb.lo \ - wctomb_r.lo $(am__objects_8) + gdtoa-gethex.lo gdtoa-hexnan.lo getenv.lo getenv_r.lo itoa.lo \ + labs.lo ldiv.lo ldtoa.lo malloc.lo mblen.lo mblen_r.lo \ + mbstowcs.lo mbstowcs_r.lo mbtowc.lo mbtowc_r.lo mlock.lo \ + mprec.lo mstats.lo rand.lo rand_r.lo realloc.lo reallocf.lo \ + sb_charsets.lo strtod.lo strtol.lo strtoul.lo utoa.lo \ + wcstod.lo wcstol.lo wcstoul.lo wcstombs.lo wcstombs_r.lo \ + wctomb.lo wctomb_r.lo $(am__objects_8) am__objects_10 = cxa_atexit.lo cxa_finalize.lo drand48.lo ecvtbuf.lo \ efgcvt.lo erand48.lo jrand48.lo lcong48.lo lrand48.lo \ mrand48.lo msize.lo mtrim.lo nrand48.lo rand48.lo seed48.lo \ @@ -355,12 +356,12 @@ GENERAL_SOURCES = __adjust.c __atexit.c __call_atexit.c __exp10.c \ __ten_mu.c _Exit.c abort.c abs.c assert.c atexit.c atof.c \ atoff.c atoi.c atol.c calloc.c div.c dtoa.c dtoastub.c \ environ.c envlock.c eprintf.c exit.c gdtoa-gethex.c \ - gdtoa-hexnan.c getenv.c getenv_r.c labs.c ldiv.c ldtoa.c \ + gdtoa-hexnan.c getenv.c getenv_r.c itoa.o labs.c ldiv.c ldtoa.c \ malloc.c mblen.c mblen_r.c mbstowcs.c mbstowcs_r.c mbtowc.c \ mbtowc_r.c mlock.c mprec.c mstats.c rand.c rand_r.c realloc.c \ - reallocf.c sb_charsets.c strtod.c strtol.c strtoul.c wcstod.c \ - wcstol.c wcstoul.c wcstombs.c wcstombs_r.c wctomb.c wctomb_r.c \ - $(am__append_1) + reallocf.c sb_charsets.c strtod.c strtol.c strtoul.c utoa.c \ + wcstod.c wcstol.c wcstoul.c wcstombs.c wcstombs_r.c wctomb.c \ + wctomb_r.c $(am__append_1) @NEWLIB_NANO_MALLOC_FALSE@MALIGNR = malignr @NEWLIB_NANO_MALLOC_TRUE@MALIGNR = nano-malignr @NEWLIB_NANO_MALLOC_FALSE@MALLOPTR = malloptr @@ -495,6 +496,7 @@ CHEWOUT_FILES = \ envlock.def \ exit.def \ getenv.def \ + itoa.def \ labs.def \ ldiv.def \ llabs.def \ @@ -515,6 +517,7 @@ CHEWOUT_FILES = \ strtoll.def \ strtoul.def \ strtoull.def \ + utoa.def \ wcsnrtombs.def \ wcstod.def \ wcstol.def \ @@ -754,6 +757,12 @@ lib_a-getenv_r.o: getenv_r.c lib_a-getenv_r.obj: getenv_r.c $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-getenv_r.obj `if test -f 'getenv_r.c'; then $(CYGPATH_W) 'getenv_r.c'; else $(CYGPATH_W) '$(srcdir)/getenv_r.c'; fi` +lib_a-itoa.o: itoa.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-itoa.o `test -f 'itoa.c' || echo '$(srcdir)/'`itoa.c + +lib_a-itoa.obj: itoa.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-itoa.obj `if test -f 'itoa.c'; then $(CYGPATH_W) 'itoa.c'; else $(CYGPATH_W) '$(srcdir)/itoa.c'; fi` + lib_a-labs.o: labs.c $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-labs.o `test -f 'labs.c' || echo '$(srcdir)/'`labs.c @@ -880,6 +889,12 @@ lib_a-strtoul.o: strtoul.c lib_a-strtoul.obj: strtoul.c $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-strtoul.obj `if test -f 'strtoul.c'; then $(CYGPATH_W) 'strtoul.c'; else $(CYGPATH_W) '$(srcdir)/strtoul.c'; fi` +lib_a-utoa.o: utoa.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-utoa.o `test -f 'utoa.c' || echo '$(srcdir)/'`utoa.c + +lib_a-utoa.obj: utoa.c + $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-utoa.obj `if test -f 'utoa.c'; then $(CYGPATH_W) 'utoa.c'; else $(CYGPATH_W) '$(srcdir)/utoa.c'; fi` + lib_a-wcstod.o: wcstod.c $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(lib_a_CFLAGS) $(CFLAGS) -c -o lib_a-wcstod.o `test -f 'wcstod.c' || echo '$(srcdir)/'`wcstod.c diff --git a/newlib/libc/stdlib/itoa.c b/newlib/libc/stdlib/itoa.c new file mode 100644 index 000000000..c7c9b8871 --- /dev/null +++ b/newlib/libc/stdlib/itoa.c @@ -0,0 +1,69 @@ +/* +FUNCTION +<>---integer to string + +INDEX + itoa + +ANSI_SYNOPSIS + #include + char *itoa(int <[value]>, char *<[str]>, int <[base]>); + char *__itoa(int <[value]>, char *<[str]>, int <[base]>); + +DESCRIPTION +<> converts the integer [] to a null-terminated string +using the specified base, which must be between 2 and 36, inclusive. +If <[base]> is 10, <[value]> is treated as signed and the string will be +prefixed with '-' if negative. For all other bases, <[value]> is treated as +unsigned. <[str]> should be an array long enough to contain the converted +value, which in the worst case is sizeof(int)*8+1 bytes. + +RETURNS +A pointer to the string, <[str]>, or NULL if <[base]> is invalid. + +PORTABILITY +<> is non-ANSI. + +No supporting OS subroutine calls are required. +*/ + +#include + +char * +_DEFUN (__itoa, (value, str, base), + int value _AND + char *str _AND + int base) +{ + unsigned uvalue; + int i = 0; + + /* Check base is supported. */ + if ((base < 2) || (base > 36)) + { + str[0] = '\0'; + return NULL; + } + + /* Negative numbers are only supported for decimal. + * Cast to unsigned to avoid overflow for maximum negative value. */ + if ((base == 10) && (value < 0)) + { + str[i++] = '-'; + uvalue = (unsigned)-value; + } + else + uvalue = (unsigned)value; + + __utoa (uvalue, &str[i], base); + return str; +} + +char * +_DEFUN (itoa, (value, str, base), + int value _AND + char *str _AND + int base) +{ + return __itoa (value, str, base); +} diff --git a/newlib/libc/stdlib/utoa.c b/newlib/libc/stdlib/utoa.c new file mode 100644 index 000000000..7738c2321 --- /dev/null +++ b/newlib/libc/stdlib/utoa.c @@ -0,0 +1,76 @@ +/* +FUNCTION +<>---unsigned integer to string + +INDEX + utoa + +ANSI_SYNOPSIS + #include + char *utoa(unsigned <[value]>, char *<[str]>, int <[base]>); + char *__utoa(unsigned <[value]>, char *<[str]>, int <[base]>); + +DESCRIPTION +<> converts the unsigned integer [] to a null-terminated string +using the specified base, which must be between 2 and 36, inclusive. +<[str]> should be an array long enough to contain the converted +value, which in the worst case is sizeof(int)*8+1 bytes. + +RETURNS +A pointer to the string, <[str]>, or NULL if <[base]> is invalid. + +PORTABILITY +<> is non-ANSI. + +No supporting OS subroutine calls are required. +*/ + +#include + +char * +_DEFUN (__utoa, (value, str, base), + unsigned value _AND + char *str _AND + int base) +{ + const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; + int i, j; + unsigned remainder; + char c; + + /* Check base is supported. */ + if ((base < 2) || (base > 36)) + { + str[0] = '\0'; + return NULL; + } + + /* Convert to string. Digits are in reverse order. */ + i = 0; + do + { + remainder = value % base; + str[i++] = digits[remainder]; + value = value / base; + } while (value != 0); + str[i] = '\0'; + + /* Reverse string. */ + for (j = 0, i--; j < i; j++, i--) + { + c = str[j]; + str[j] = str[i]; + str[i] = c; + } + + return str; +} + +char * +_DEFUN (utoa, (value, str, base), + unsigned value _AND + char *str _AND + int base) +{ + return __utoa (value, str, base); +} -- cgit v1.2.3