/bin/sh and BIG NUMBERS

Peter Pentchev roam at ringlet.net
Mon Apr 7 01:55:11 PDT 2003


On Sun, Apr 06, 2003 at 06:24:50AM +0300, Giorgos Keramidas wrote:
> 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).

intmax_t might be a better choice, if %jd is used :)

G'luck,
Peter

-- 
Peter Pentchev	roam at ringlet.net    roam at sbnd.net    roam at FreeBSD.org
PGP key:	http://people.FreeBSD.org/~roam/roam.key.asc
Key fingerprint	FDBA FD79 C26F 3C51 C95E  DF9E ED18 B68D 1619 4553
If I had finished this sentence,
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20030407/9ddffd94/attachment.bin


More information about the freebsd-hackers mailing list