/bin/sh and BIG NUMBERS

Giorgos Keramidas keramida at ceid.upatras.gr
Sat Apr 5 19:44:35 PST 2003


On 2003-04-05 07:06, Alex Semenyaka <alexs at ratmir.ru> wrote:
> I found that /bin/sh cannot handle numbers those do not fit to integer
> type.  That is not too bad. Too bad that it just silently warps them
> in arithmetical operations:
>
> alexs at snark> /bin/sh -c 'echo $((10000000000-1))'
> 2147483646
>
> That was not a problem 5 years ago... But now we have a lot of 64-bits
> values.  So those old scripts which perfectly worked for a long time
> now can give wrong results, and you will not be able even to notice
> it, there is no any diagnostics or such. The simplest way to fix it is
> to switch internal /bin/sh arithmetics from 32 to 64-bits (you know,
> approach "640K ought to be enough for anybody").  I've did the patch
> for this (below), please, look at it. Any comments or suggestions?
>
> diff -u -U1 -r ../sh.old/arith.h ./arith.h
> --- ../sh.old/arith.h	Fri Jul 19 08:38:51 2002
> +++ ./arith.h	Sat Apr  5 06:26:48 2003
> @@ -36,3 +36,3 @@
>
> -int arith(char *);
> +long long arith(char *);
>  int expcmd(int , char **);
>
> [snip rest of long-long using patch]

Nice idea, but we should probably ask the -standards people if we
can/should make this use uint64_t and %jd instead of `long long'
(using %qd is deprecated and %lld is advised in printf(3) anyway).

: giorgos at gothmog[06:19]/tmp/lala$ cat lala.c
: #include <limits.h>
: #include <stdint.h>
: #include <stdio.h>
:
: int
: main(void)
: {
:         uint64_t foo;
:
:         foo = UINT_MAX;
:         printf("%jd\n", foo);
:         return (0);
: }
: giorgos at gothmog[06:19]/tmp/lala$ make WARNS=5
: Warning: Object directory not changed from original /tmp/lala
: cc -O -pipe   -Wsystem-headers -Werror -Wall -Wno-format-y2k \
:  -W -Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith \
:  -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch -Wshadow \
:  -Wcast-align -Wuninitialized -Wformat=2 -Wno-format-extra-args \
:  -Werror  -c lala.c
: cc -O -pipe   -Wsystem-headers -Werror -Wall -Wno-format-y2k \
:  -W -Wstrict-prototypes -Wmissing-prototypes -Wpointer-arith \
:  -Wreturn-type -Wcast-qual -Wwrite-strings -Wswitch -Wshadow \
:  -Wcast-align -Wuninitialized -Wformat=2 -Wno-format-extra-args \
:  -Werror   -o lala lala.o
: giorgos at gothmog[06:19]/tmp/lala$ ./lala
: 4294967295
: giorgos at gothmog[06:19]/tmp/lala$



More information about the freebsd-hackers mailing list