Commit Graph

24 Commits

Author SHA1 Message Date
Mattias Andrée 1d885159f2 basename: support --
POSIX-2017 clarifies that -- and normal option parsing must be supported.
See EXAMPLES in basename(1p).

Signed-off-by: Mattias Andrée <>
2018-07-07 12:16:11 +01: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 9016d288f1 Do not use arg.h for tools which take no flags
We've already seen the issue with echo(1): Before we changed it to
ignore "--", the command

$ echo --

did not work as expected. Given POSIX mandated this and makes most
sense, in the interest of consistency the other tools need to be
streamlined for that as well.
Looking at yes(1) for instance, there's no reason to skip "--" in
the argument list.
We do not have long options like GNU does and there's no reason to
tinker with that here.

The majority of tools changed are ones taking lists of arguments
or only a single one. There's no reason why dirname should "fail"
on "--". In the end, this is a valid name.

The practice of hand-holding the user was established with the GNU
coreutils. "--help" and "--version" long-options are a disgrace to
what could've been done properly with manpages.
2015-04-25 11:43:14 +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
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-

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
sin 804b62f7a2 Fix broken sbase-box due to multiple definitions of usage 2015-02-28 18:33:33 +00:00
FRIGN e7c33c4af3 Audit basename(1)
1) be stricter which number of arguments is accepted (1 or 2)
2) basename already returns a pointer to "." is argv[0] is ""
3) No need to check for *p != '/', because basename() only returns
   a string beginning with '/' which has length 1, so if strlen(p)
   == 1, the only way for suffix to be "evaluated" is for off to
   be > 0, being equal to suffix being "", but "" != "/".
4) don't calculate strlen twice for each string. Store it in a
   ssize_t and check if it's > 0.
2015-02-28 14:48:44 +01:00
FRIGN 31572c8b0e Clean up #includes 2015-02-14 21:12:23 +01:00
Hiltjo Posthuma a986e4bbd5 basename: make usage line the same as the man page 2015-02-01 19:26:49 +01:00
sin 1059782af8 Fix comparison >= 0 is always true in basename(1) 2014-12-08 12:14:39 +00:00
Brandon Mulcahy 757cf0651a Fix basename suffix treatment
Explicitly use "." instead of the result of basename(3) when argv[0] is
an empty string in order to avoid a segfault.

Skip suffix treatment if the result of basename(3) is "/", per POSIX.

Fix the suffix check, which was previously checking for a match at any
location in the string.
2014-12-04 08:00:03 +00:00
FRIGN eee98ed3a4 Fix coding style
It was about damn time. Consistency is very important in such a
big codebase.
2014-11-13 18:08:43 +00:00
sin a7ad11ed5e Fix overflow in basename(1) 2014-11-13 14:56:25 +00:00
sin 0c5b7b9155 Stop using EXIT_{SUCCESS,FAILURE} 2014-10-02 23:46:59 +01:00
Hiltjo Posthuma 953ebf3573 code style
Signed-off-by: Hiltjo Posthuma <>
2014-06-01 18:02:30 +01:00
sin b5a511dacf Exit with EXIT_SUCCESS/EXIT_FAILURE instead of 0 and 1
Fixed for consistency purposes.
2013-10-07 16:44:22 +01:00
Christoph Lohmann 52d39e35c2 The style inquisition on basename. 2013-03-05 21:35:55 +01:00
Connor Lane Smith fcb8821246 revert to per-cmd usage() 2012-05-15 13:32:56 +01:00
Connor Lane Smith 146cca114e new ARGBEGIN & usage() function 2012-05-14 21:28:41 +01:00
Christoph Lohmann 3863ccdf98 Testing before rage. Suffix is fixed in basename. 2012-04-23 16:34:34 +02:00
Christoph Lohmann 3817f78f87 Moving back to the stone age. 2012-04-23 16:32:41 +02:00
Christoph Lohmann f75d7a47ff Adding ARGBEGIN for basename. 2012-04-23 15:50:47 +02:00
Connor Lane Smith c367d4d05f add dirname 2011-05-24 21:39:20 +01:00
Connor Lane Smith 026e63c005 simplify basename 2011-05-24 12:56:38 +01:00
Connor Lane Smith 8e26716a5a initial commit 2011-05-23 02:36:34 +01:00