Library: Make the string functions match the prototypes in stdlib.h
authorWill Sowerbutts <will@sowerbutts.com>
Sat, 3 Jan 2015 22:20:03 +0000 (22:20 +0000)
committerWill Sowerbutts <will@sowerbutts.com>
Sat, 3 Jan 2015 23:36:31 +0000 (23:36 +0000)
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.

Library/include/stdlib.h
Library/libs/ltoa.c
Library/libs/ltostr.c
Library/libs/vfprintf.c
Library/libs/xitoa.c

index 4b6623b..0f22d68 100644 (file)
@@ -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,
index 98ade5a..6a7d384 100644 (file)
@@ -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;
 }
index bb79414..ba10426 100644 (file)
@@ -3,10 +3,12 @@
  * under the GNU Library General Public License.
  */
 
+#include <string.h>
+
 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));
+}
index 2163a73..138abc4 100644 (file)
@@ -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)
index 872b135..a066c10 100644 (file)
@@ -6,6 +6,5 @@
 /*********************** xitoa.c ***************************/
 char *_itoa(int i)
 {
-       static char buf[16];
-       return ltostr((long) i, buf, 10);
+    return _ltoa((long)i);
 }