/bin/sh and BIG NUMBERS
Alex Semenyaka
alexs at ratmir.ru
Mon Apr 7 17:09:38 PDT 2003
On Mon, Apr 07, 2003 at 11:53:09AM +0300, Peter Pentchev wrote:
> >> -int arith(char *);
> >> +long long arith(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'
> intmax_t might be a better choice, if %jd is used :)
Ok, and then one more issue: what is The Right Way to estimate the maximal
length of the resulting number? Since there is the replacement of
fmtstr(p, 12, "%qd", result);
with
fmtstr(p, 21, "%qd", result);
in my patch.
If we are going to handle the max integer type automatically as with intmax_t
and %jd we need to fix that part as well :)
1) Sure I can replace if with
#define ARITH_LEN (int)(2+log10(UMAXINT_MAX))
...
fmtstr(p, ARITH_LEN, "%jd", result);
but then we will need to link libm.so library that does not look like
good idea.
2) Also we can make a some number of the nested #if's like this:
#if UMAXINT_MAX <= UINT_MAX
#define ARITH_LEN 10
#elif UMAXINT_MAX <= ULONGLONG_MAX
#define ARITH_LEN 19
...
That's far from ideal as well.
3) I can allocate a LARGE buffer there but it just the stupid waste of memory.
4) Also we can have special file to be compiled and run during in the building
sh time such as arith_len.c:
---------->------------>-----------
#include <stdio.h>
#include <math.h>
#include <limits.h>
int main(int ac, char *av[])
{
printf("#ifndef ARITH_LEN\n#define ARITH_LEN %d\n#endif\n", (int)(log10(UINT_MA
X)));
exit(0);
}
----------<------------<-----------
and generate file arith_len.h on-the-fly.
OR,
5) we can add corresponding constants to the machine/_stdint.h or
machine/_inttypes.h.
The last way looks like the smartest one for me.
Also Stefan EЯer <se at freebsd.org> wrote me that he did the same job for
/bin/expr 3 years ago and suggested to take a look on his job. He added
64-bit support to expr as well as overflow checks. I do think that it will
be nice to have optional overflow control but optional but nice to have.
Have any objections if I will add new switch for /bin/sh like -O which
will makes /bin/sh to complain to stderr about overflows?
I will prepare the final patch as soon as those two questions will be resolved.
Thank you all for the participating in this discussion, it is very useful for
me!
SY, Alex
More information about the freebsd-hackers
mailing list