Commit Graph

33 Commits

Author SHA1 Message Date
Randy Palamar d335c366f7 expr: tonum: handle case where result was previously calculated
As pointed out in a mail to dev expr was segfaulting when multiple
math operations were specified on the command line: eg. 'expr 3 \*
2 + 1'. This happens because the tonum(), introduced in e50d533,
assumed that v->str was always non null. parse() guarantees this
for user input but this is not the case when doop() is called with
the result of a previous calculation. However in that case we know
that v->num is already valid so we can simply return.
2024-01-31 05:00:51 +01:00
Randy Palamar 270ca025ce expr: don't evaluate matched substr as a number
POSIX specifies that if the pattern contains a subexpression then
the first matched subexpression should be returned if it exists.

This fixes things like the following:

./expr 00003 : '\(.*\)'
Before: 3
After: 00003
2024-01-16 09:11:46 +01:00
Randy Palamar e50d533d59 expr: treat expressions as strs until evaluation
Comparison operations (>, <, =, etc.) and matching operations must
operate originally provided string not one that has gone back and
forth through string formatting. This caused operations such as
the following to give incorrect results:

./expr 00003 : '.*'
Before: 1
After: 5

This commit fixes that issue.
2024-01-16 09:11:40 +01:00
Michael Forney ef9f6f359a expr: Allocate arrays with reallocarray instead of VLA
The length of these arrays is user input, and this is the only use of
VLAs in sbase.
2019-04-16 17:44:08 -07:00
Laslo Hunhold e92a1aef54 Handle { NULL } argv[] properly in manual arg-reduction
Thanks izabera for reporting this!
2017-08-05 23:50:39 +02:00
FRIGN d23cc72490 Simplify return & fshut() logic
Get rid of the !!()-constructs and use ret where available (or introduce it).

In some cases, there would be an "abort" on the first fshut-error, but we want
to close all files and report all warnings and then quit, not just the warning
for the first file.
2015-05-26 16:41:43 +01:00
sin 504855ff96 expr: Don't interpret any arguments
expr -1 '<' 1 failed previously.

There is no need to have usage at all, any errors will be flagged
up as necessary further down.
2015-04-21 17:09:20 +01:00
Hiltjo Posthuma ce145a39a7 expr: fix, first parse then enfshut 2015-04-21 09:11:36 +01:00
FRIGN 11e2d472bf Add *fshut() functions to properly flush file streams
This has been a known issue for a long time. Example:

printf "word" > /dev/full

wouldn't report there's not enough space on the device.
This is due to the fact that every libc has internal buffers
for stdout which store fragments of written data until they reach
a certain size or on some callback to flush them all at once to the
kernel.
You can force the libc to flush them with fflush(). In case flushing
fails, you can check the return value of fflush() and report an error.

However, previously, sbase didn't have such checks and without fflush(),
the libc silently flushes the buffers on exit without checking the errors.
No offense, but there's no way for the libc to report errors in the exit-
condition.

GNU coreutils solve this by having onexit-callbacks to handle the flushing
and report issues, but they have obvious deficiencies.
After long discussions on IRC, we came to the conclusion that checking the
return value of every io-function would be a bit too much, and having a
general-purpose fclose-wrapper would be the best way to go.

It turned out that fclose() alone is not enough to detect errors. The right
way to do it is to fflush() + check ferror on the fp and then to a fclose().
This is what fshut does and that's how it's done before each return.
The return value is obviously affected, reporting an error in case a flush
or close failed, but also when reading failed for some reason, the error-
state is caught.

the !!( ... + ...) construction is used to call all functions inside the
brackets and not "terminating" on the first.
We want errors to be reported, but there's no reason to stop flushing buffers
when one other file buffer has issues.
Obviously, functionales come before the flush and ret-logic comes after to
prevent early exits as well without reporting warnings if there are any.

One more advantage of fshut() is that it is even able to report errors
on obscure NFS-setups which the other coreutils are unable to detect,
because they only check the return-value of fflush() and fclose(),
not ferror() as well.
2015-04-05 09:13:56 +01:00
FRIGN 8db1632409 Drop maxdigits in expr(1)
This just begs for off-by-ones. Just drop it and use BUFSIZ.
We'll survive it.
2015-03-23 22:56:26 +01:00
FRIGN f668afa1ff Style fix in expr(1)
(*valp). --> valp->
2015-03-23 22:44:07 +01:00
FRIGN 50ecbd43ad Can't use sizeof on malloc'ed array in expr(1)
Thanks emg for reporting this!
2015-03-23 22:31:36 +01:00
FRIGN d49f6f2044 Audit expr(1)
No bugs found, but I changed intmax_t to long long to make it more
predictable and removed some of the kitchen-sinking.
Don't return structs themselves, as this is not very elegant.
Do it like functions like stat(), which take a pointer to a
struct to fill.
2015-03-22 14:35:15 +01:00
Evan Gates cf5114a133 untypedef expr, find, test, as is existing style in sbase 2015-03-17 20:04:26 +00:00
Evan Gates 07c4f784a6 don't mix declarations and code (and one ** to *[]) 2015-03-06 07:33:58 +00:00
FRIGN 7cc4ef0734 Don't break the coding-style 2015-02-26 19:07:02 +01:00
Evan Gates bf4c5d1fcd expr comments, cleanup, UTF-8 support 2015-02-26 17:26:47 +00:00
FRIGN b47dab2d4d Don't be too pedantic in expr.c 2015-02-20 13:45:57 +01:00
Hiltjo Posthuma 31f0624f3d code-style: minor cleanup and nitpicking 2015-02-20 13:29:38 +01:00
FRIGN 31572c8b0e Clean up #includes 2015-02-14 21:12:23 +01:00
Jakob Kramer 0fcad66c75 make use of en*alloc functions 2015-02-11 01:17:21 +00:00
sin 6f73cd1c97 Relax with caps in usage 2015-01-25 18:31:25 +00:00
FRIGN b98bf41ecc Add mandoc-manpage for expr(1)
and mark it as finished in README.
Upon further checking, expr(1) turns out to be implicitly UTF-8 compliant.
2015-01-25 16:49:46 +01:00
FRIGN 85df198b59 Refactor expr.c
Finish up what has been begun.
2014-11-17 15:05:49 +00:00
FRIGN abdd0671f7 Refactor expr.c 2014-11-17 10:05:19 +00:00
sin 7b8d7fb78e Fix uninitialized use of ret in expr(1) 2014-11-16 14:52:44 +00:00
Hiltjo Posthuma b3ae1a7b4b expr: improvements
- handle divide by zero.
- use eregcomp().
- use emalloc().
- use snprintf() for safety and add a buffer size argument to valstr() just
  to be sure.
- code-style fixes.
2014-11-16 14:37:22 +00:00
sin da715252b3 Add license heading at the top in expr.c 2014-11-16 12:57:54 +00:00
sin 15097f6ab3 Don't use C++ type comments 2014-11-16 12:54:47 +00:00
Evan Gates 6240d26beb new expr using shunting-yard instead of recursive descent (this time with tabs) 2014-11-16 12:52:48 +00:00
sin 41d78c398b Staticise symbols 2014-10-16 12:30:52 +01:00
sin a1784b721d Fix build error
Remove __dead.
2014-09-29 14:10:54 +01:00
sin 618aecd2ec Import expr(1) from OpenBSD
Cleanup + manpage to be done.
2014-09-29 13:44:49 +01:00