RetroBSD
http://retrobsd.org/

expr(1) arithmetic operation is broken?
http://retrobsd.org/viewtopic.php?f=7&t=37519
Page 1 of 1

Author:  mutoh [ Mon Feb 01, 2016 6:53 am ]
Post subject:  expr(1) arithmetic operation is broken?

Today, I want to use expr(1), but I can't get correct result.

For example
Code:
# uname -a
2.11BSD pic32 2.11BSD 2.11 BSD Unix for PIC32, revision G488 build 2:      Compiled 2016-02-01 by mutoh@mutoubushinokonpyuta.local:      /Users/mutoh/work/BSD/retrobsd/sys/pic32/max32  pic32
# expr 1 + 1
00 00 9c 00 00 00 04 00 00 00 40 00 00 00 80 00


I think to fix this problem, use patch below:
Code:
% git diff src/cmd/expr.y
diff --git a/src/cmd/expr.y b/src/cmd/expr.y
index 0b81aca..32c892f 100644
--- a/src/cmd/expr.y
+++ b/src/cmd/expr.y
@@ -132,7 +132,7 @@ char *arith(op, r1, r2) char *r1, *r2;
     case REM: i1 = i1 % i2; break;
     }
     rv = malloc(16);
-    sprintf(rv, "%D", i1);
+    sprintf(rv, "%ld", i1);
     return rv;
 }
 

Author:  mutoh [ Tue Mar 15, 2016 5:01 am ]
Post subject:  Re: expr(1) arithmetic operation is broken?

I find what is the real problem.

At many implementation of printf, it have format string "%D" which equal to "%ld".
This is undocumented function.

So, real fix is below:
Quote:
diff --git a/src/libc/stdio/doprnt.c b/src/libc/stdio/doprnt.c
index b1885bb..6ac58ea 100644
--- a/src/libc/stdio/doprnt.c
+++ b/src/libc/stdio/doprnt.c
@@ -171,20 +171,8 @@ reswitch: switch (c = *fmt++) {
break;

case 'D':
- s = va_arg (ap, const unsigned char*);
- if (! width)
- width = 16;
- if (sharpflag)
- padding = ':';
- while (width--) {
- c = *s++;
- PUTC (mkhex (c >> 4));
- PUTC (mkhex (c));
- if (width)
- PUTC (padding);
- }
- break;
-
+ lflag=1;
+ /*FALLTHROUGH*/
case 'd':
case 'i':
ul = lflag ? va_arg (ap, long) : va_arg (ap, int);


How about this fix?

Author:  wiz [ Tue Mar 15, 2016 5:56 am ]
Post subject:  Re: expr(1) arithmetic operation is broken?

Hi mutoh,

Nice piece of work :).

I wonder what 'D' is useful for?

And how to leave the feature available but make expr work correctly.

Serge is a pretty clever guy. I bet he had that feature in for some pretty good reason?

The feature doesn't seem to be in the LiteBSD sources.

Lots of fun :).

Wiz

Author:  Pito [ Tue Mar 15, 2016 7:21 am ]
Post subject:  Re: expr(1) arithmetic operation is broken?

Years back there has been a significant :) effort provided in order to implement double precision floating point into retrobsd. We messed with printf too. Not sure the latest builds do include the fixes.
So be careful when using fp math, for example.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/