From: Will Sowerbutts Date: Sat, 3 Jan 2015 22:20:03 +0000 (+0000) Subject: Library: Make the string functions match the prototypes in stdlib.h X-Git-Url: https://git.ndcode.org/public/gitweb.cgi?a=commitdiff_plain;h=2b95e4eff9140e6ca0c387ef06eae97f153628f3;p=FUZIX.git Library: Make the string functions match the prototypes in stdlib.h Rename single-argument ltoa and ultoa to _ltoa and _ultoa. Rename non-standard ltostr and ultostr to __ltostr and __ultostr, change prototypes in stdlib.h to match expected arguments. New implementations of ltoa, ultoa based on __ltostr, __ultostr. Change vfprintf to call __ltostr and __ultostr. Change _itoa to call _ltoa. Now we have: _ltoa, _ultoa, _itoa: Take a single argument (value). __ltostr, __ultostr: Take two arguments (value, radix). ltoa, ultoa: Take three arguments (value, buffer, radix). Calls either __ltostr or __ultostr and copies the result into the user-provided buffer. --- diff --git a/Library/include/stdlib.h b/Library/include/stdlib.h index 4b6623ba..0f22d68f 100644 --- a/Library/include/stdlib.h +++ b/Library/include/stdlib.h @@ -46,8 +46,8 @@ extern char *_itoa __P((int value)); extern char *_ltoa __P((long value)); extern char *_ultoa __P((unsigned long value)); -extern char *ultostr __P((unsigned long value, char *buf, int radix)); -extern char *ltostr __P((long value, char *buf, int radix)); +extern char *__ultostr __P((unsigned long value, int radix)); +extern char *__ltostr __P((long value, int radix)); extern long strtol __P ((const char * nptr, char ** endptr, int base)); extern unsigned long strtoul __P ((const char * nptr, diff --git a/Library/libs/ltoa.c b/Library/libs/ltoa.c index 98ade5a2..6a7d3844 100644 --- a/Library/libs/ltoa.c +++ b/Library/libs/ltoa.c @@ -4,7 +4,7 @@ */ -char * ultoa(unsigned long val) +char *_ultoa(unsigned long val) { char *p; static char buf[12]; @@ -21,12 +21,12 @@ char * ultoa(unsigned long val) return p; } -char * ltoa(long val) +char *_ltoa(long val) { char *p; int flg = 0; if( val < 0 ) { flg++; val= -val; } - p = ultoa(val); + p = _ultoa(val); if(flg) *--p = '-'; return p; } diff --git a/Library/libs/ltostr.c b/Library/libs/ltostr.c index bb79414f..ba104269 100644 --- a/Library/libs/ltostr.c +++ b/Library/libs/ltostr.c @@ -3,10 +3,12 @@ * under the GNU Library General Public License. */ +#include + static char buf[34]; -char * ultostr(unsigned long val, int radix) +char * __ultostr(unsigned long val, int radix) { register char *p; register int c; @@ -26,12 +28,25 @@ char * ultostr(unsigned long val, int radix) return p; } -char * ltostr(long val, int radix) +char * __ltostr(long val, int radix) { char *p; int flg = 0; if( val < 0 ) { flg++; val= -val; } - p = ultostr(val, radix); + p = __ultostr(val, radix); if(p && flg) *--p = '-'; return p; } + +/* sadly we do not know the size of the user-provided buffer so we cannot write + it backwards, so just copy the result from our own buffer whose size we know */ + +char *ultoa (unsigned long value, char *strP, int radix) +{ + return strcpy(strP, __ultostr(value, radix)); +} + +char *ltoa (long value, char *strP, int radix) +{ + return strcpy(strP, __ltostr(value, radix)); +} diff --git a/Library/libs/vfprintf.c b/Library/libs/vfprintf.c index 2163a73e..138abc41 100644 --- a/Library/libs/vfprintf.c +++ b/Library/libs/vfprintf.c @@ -148,10 +148,9 @@ int vfprintf(FILE * op, char *fmt, va_list ap) case 'd': /* Signed decimal */ case 'i': - ptmp = ltostr((long) ((lval) ? + ptmp = __ltostr((long) ((lval) ? va_arg(ap, long) : - va_arg(ap, short)), - tmp, 10); + va_arg(ap, short)), 10); goto printit; case 'b': /* Unsigned binary */ @@ -181,7 +180,7 @@ int vfprintf(FILE * op, char *fmt, va_list ap) usproc:val = lval ? va_arg(ap, unsigned long) : va_arg(ap, unsigned short); - ptmp = ultostr(val, tmp + 4, radix); + ptmp = __ultostr(val, radix); add = ""; if (hash) { if (radix == 2) diff --git a/Library/libs/xitoa.c b/Library/libs/xitoa.c index 872b135b..a066c10d 100644 --- a/Library/libs/xitoa.c +++ b/Library/libs/xitoa.c @@ -6,6 +6,5 @@ /*********************** xitoa.c ***************************/ char *_itoa(int i) { - static char buf[16]; - return ltostr((long) i, buf, 10); + return _ltoa((long)i); }