Skip to content

Commit

Permalink
[libc] Use __divmod in ltoa, ultoa, ltostr, ultostr and dd
Browse files Browse the repository at this point in the history
  • Loading branch information
ghaerr committed Sep 15, 2024
1 parent bb6bf04 commit 8dc2155
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 33 deletions.
28 changes: 0 additions & 28 deletions elkscmd/file_utils/cat.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,6 @@

static char readbuf[BUFSIZ]; /* use disk block size for stack limit and efficiency*/

#define TEST 0
#if TEST
void test(void)
{
printf(" p: '%p'\n", 0x18AF);
printf(" lp: '%lp'\n", 0x02d018AFL);
printf("04X: '%04X'\n", 0x2ab);
printf("04x: '%04x'\n", 0x2ab);
printf(" 4x: '%4x'\n", 0x2ab);
printf("04d: '%04d'\n", 0x200);
printf(" 4d: '%4d'\n", 0x200);
printf("05d: '%05d'\n", -20);
printf(" 5d: '%5d'\n", -20);
printf("+5d: '%5d'\n", -20);
printf("+5d: '%5d'\n", 20);
printf(",ld: '%,ld'\n", -123456789L);
printf(" lx: '%lx'\n", 0x87654321L);
printf(" lo: '%lo'\n", 0xFFFFFFFFL);
printf(" s: '%s'\n", "thisisatest");
printf(" 6s: '%6s'\n", "thisisatest");
printf("20s: '%20s'\n", "thisisatest");
}
#endif

static int copyfd(int fd)
{
int n;
Expand All @@ -50,10 +26,6 @@ int main(int argc, char **argv)
{
int i, fd;

#if TEST
test();
exit(0);
#endif
if (argc <= 1) {
if (copyfd(STDIN_FILENO)) {
perror("stdin");
Expand Down
9 changes: 9 additions & 0 deletions elkscmd/file_utils/dd.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,18 @@ char *ultoa_r(char *buf, unsigned long i)
char *b = buf + 34 - 1;

*b = '\0';
#ifdef _M_I86
do {
unsigned int c;
c = 10;
i = __divmod(i, &c);
*--b = '0' + c;
} while (i);
#else
do {
*--b = '0' + (i % 10);
} while ((i /= 10) != 0);
#endif
return b;
}
static void eprintf(const char *s, ...)
Expand Down
2 changes: 1 addition & 1 deletion elkscmd/lib/tiny_vfprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
*/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <arch/divmod.h>

static unsigned char bufout[80];

Expand Down
25 changes: 25 additions & 0 deletions elkscmd/test/other/test_float.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,5 +271,30 @@ int main(int argc, char **argv) {
printf("%s\n", ultostr(-0x7fffffff, 10));
printf("%s\n", ultostr(0x80000000, 10));
printf("%s\n", ultostr(0xffffffff, 10));

printf("-25 %s\n", itoa(-25));
printf("32767 %s\n", uitoa(32767));
printf(" 100000 %s\n", ultoa(100000L));
printf("-100000 %s\n", ltoa(-100000L));
printf(" 300000 %s\n", ultostr(300000L, 10));
printf("-300000 %s\n", ltostr(-300000L, 10));
printf(" p: '%p'\n", 0x18AF);
printf(" lp: '%lp'\n", 0x02d018AFL);
printf("04X: '%04X'\n", 0x2ab);
printf("04x: '%04x'\n", 0x2ab);
printf(" 4x: '%4x'\n", 0x2ab);
printf("04d: '%04d'\n", 0x200);
printf(" 4d: '%4d'\n", 0x200);
printf("05d: '%05d'\n", -20);
printf(" 5d: '%5d'\n", -20);
printf("+5d: '%5d'\n", -20);
printf("+5d: '%5d'\n", 20);
printf(",ld: '%,ld'\n", -123456789L);
printf(" lx: '%lx'\n", 0x87654321L);
printf(" lo: '%lo'\n", 0xFFFFFFFFL);
printf(" s: '%s'\n", "thisisatest");
printf(" 6s: '%6s'\n", "thisisatest");
printf("20s: '%20s'\n", "thisisatest");

return 0;
}
1 change: 1 addition & 0 deletions libc/include/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <features.h>
#include <sys/types.h>
#include <malloc.h>
#include <arch/divmod.h>

/* Don't overwrite user definitions of NULL */
#ifndef NULL
Expand Down
9 changes: 9 additions & 0 deletions libc/misc/ltoa.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,18 @@ char *ltoa(long val)
unsigned long u = (val < 0)? 0u - val: val;

*b = '\0';
#ifdef _M_I86
do {
unsigned int c;
c = 10;
u = __divmod(u, &c);
*--b = '0' + c;
} while (u);
#else
do {
*--b = '0' + (u % 10);
} while ((u /= 10) != 0);
#endif
if (val < 0)
*--b = '-';
return b;
Expand Down
9 changes: 9 additions & 0 deletions libc/misc/ultoa.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,17 @@ char *ultoa(unsigned long i)
char *b = buf + sizeof(buf) - 1;

*b = '\0';
#ifdef _M_I86
do {
unsigned int c;
c = 10;
i = __divmod(i, &c);
*--b = '0' + c;
} while (i);
#else
do {
*--b = '0' + (i % 10);
} while ((i /= 10) != 0);
#endif
return b;
}
11 changes: 9 additions & 2 deletions libc/misc/ultostr.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,18 @@ char *ultostr(unsigned long val, int radix)

*p = '\0';
do {
int c = val % radix;
#ifdef _M_I86
unsigned int c;
c = radix;
val = __divmod(val, &c);
#else
unsigned int c = val % radix;
val = val / radix;
#endif
if (c > 9)
*--p = 'a' - 10 + c;
else
*--p = '0' + c;
} while ((val /= radix) != 0);
} while (val);
return p;
}
4 changes: 2 additions & 2 deletions libc/stdio/vfprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <arch/divmod.h>

#ifndef __HAS_NO_FLOATS__
#include <sys/weaken.h>
Expand Down Expand Up @@ -260,7 +260,7 @@ vfprintf(FILE *op, const char *fmt, va_list ap)
p = buf + sizeof(buf) - 1;
*p = '\0';
for (i = 0;;) {
#if 1
#ifdef _M_I86
c = radix;
v = __divmod(v, &c); /* remainder returned in c */
#else
Expand Down

0 comments on commit 8dc2155

Please sign in to comment.