From 86a056c70ae2ec5d605182cb30af46267c686b92 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 20 Jul 2015 02:45:57 +0900 Subject: [PATCH] [bugfix] some paths in no-libc mode depend on lib --- extlib/benz/file.c | 22 ++++++++++++---------- extlib/benz/number.c | 19 ++++--------------- extlib/benz/read.c | 8 ++++++-- 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/extlib/benz/file.c b/extlib/benz/file.c index 3a4f6228..888dbff0 100644 --- a/extlib/benz/file.c +++ b/extlib/benz/file.c @@ -353,24 +353,26 @@ int xvfprintf(pic_state *pic, xFILE *stream, const char *fmt, va_list ap) { break; } #else +# define fabs(x) ((x) >= 0 ? (x) : -(x)) case 'f': { - double dval, dint; - dval = modf(va_arg(ap, double), &dint); - if (dint < 0 || dval < 0) { /* either may be zero */ + double dval = va_arg(ap, double); + long lval; + if (dval < 0) { + dval = -dval; xputc(pic, '-', stream); cnt++; } - cnt += print_int(pic, stream, (long)fabs(dint), 10); + lval = (long)dval; + cnt += print_int(pic, stream, lval, 10); xputc(pic, '.', stream); cnt++; - if ((ival = fabs(fabs(dval) * 1e4) + 0.5) == 0) { + dval -= lval; + if ((ival = fabs(dval) * 1e4 + 0.5) == 0) { cnt += xfputs(pic, "0000", stream); } else { - int i; - for (i = 0; i < 3 - (int)log10(ival); ++i) { - xputc(pic, '0', stream); - cnt++; - } + if (ival < 1000) xputc(pic, '0', stream); cnt++; + if (ival < 100) xputc(pic, '0', stream); cnt++; + if (ival < 10) xputc(pic, '0', stream); cnt++; cnt += print_int(pic, stream, ival, 10); } break; diff --git a/extlib/benz/number.c b/extlib/benz/number.c index bd723f9f..42f20640 100644 --- a/extlib/benz/number.c +++ b/extlib/benz/number.c @@ -117,11 +117,6 @@ DEFINE_AOP(div, pic_div(pic, pic_int_value(1), argv[0]), do { pic_errorf(pic, "/: at least one argument required"); } while (0)) -/** - * Returns the length of string representing val. - * radix is between 2 and 36 (inclusive). - * No error checks are performed in this function. - */ static int number_string_length(int val, int radix) { @@ -141,12 +136,6 @@ number_string_length(int val, int radix) return count; } -/** - * Returns the string representing val. - * radix is between 2 and 36 (inclusive). - * This function overwrites buffer and stores the result. - * No error checks are performed in this function. It is caller's responsibility to avoid buffer-overrun. - */ static void number_string(int val, int radix, int length, char *buffer) { const char digits[37] = "0123456789abcdefghijklmnopqrstuvwxyz"; @@ -216,7 +205,7 @@ pic_number_string_to_number(pic_state *pic) int radix = 10; long num; char *eptr; - double flo; + pic_value flo; pic_get_args(pic, "z|i", &str, &radix); @@ -227,9 +216,9 @@ pic_number_string_to_number(pic_state *pic) : pic_float_value(num); } - flo = strtod(str, &eptr); - if (*eptr == '\0') { - return pic_float_value(flo); + flo = pic_read_cstr(pic, str); + if (pic_int_p(flo) || pic_float_p(flo)) { + return flo; } pic_errorf(pic, "invalid string given: %s", str); diff --git a/extlib/benz/read.c b/extlib/benz/read.c index 926204b2..6051ecf7 100644 --- a/extlib/benz/read.c +++ b/extlib/benz/read.c @@ -282,14 +282,18 @@ read_unsigned(pic_state *pic, struct pic_port *port, int c) } #else case '.': { - double f, g; + double f, g, h; next(pic, port); g = 0, e = 0; while (isdigit(c = peek(pic, port))) { g = g * 10 + (next(pic, port) - '0'); e++; } - f = u + g * pow(10, -e); + h = 1.0; + while (e-- > 0) { + h /= 10; + } + f = u + g * h; exp = read_suffix(pic, port); if (exp >= 0) {