Commit Graph

42 Commits

Author SHA1 Message Date
Quentin Rameau 6e7743eb56 Cleanup usage() across sbase
Some tools didn't use argv0 for tool name, or usage() at all.
2015-12-21 18:07:25 +00:00
sin 2366164de7 No need for semicolon after ARGEND
This is also the style used in Plan 9.
2015-11-01 10:18:55 +00: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 2fc73e410c xargs: Don't print trailing spaces when -t is set 2015-04-21 18:00:48 +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
Hiltjo Posthuma 8e486d5282 xargs: minor optimizations 2015-03-27 22:48:05 +01:00
Hiltjo Posthuma 67ca2428d5 xargs: fix sysconf(_SC_ARG_MAX)
the minimum space required by environment variables isn't clearly defined by
POSIX afaik.
2015-03-27 22:48:05 +01:00
FRIGN 71adaed519 Add s-, t-, x-flags to and audit xargs(1)
The flexible design already allowed to add these flags trivially.
Drop the -I and -L-flags, which are XSI-extensions.
The audit generally consisted of style-changes, dropping kitchen-
sink functions, updating the usage and using estrtonum instead of
strtol.
2015-03-22 22:57:58 +01:00
Hiltjo Posthuma 066a0306a1 fork: no need to _exit() on the error case 2015-03-10 20:05:18 +01:00
FRIGN a8bd21c0ab Use switch with fork()
Allows dropping a local variable if the explicit PID is not needed
and it makes it clearer what happens.
Also, one should always strive for consistency for cases like these.
2015-03-09 15:01:29 +01:00
FRIGN 6f207dac5f Don't return but _exit after failed exec*() and fork()
Quoting POSIX[0]:
"Care should be taken, also, to call _exit() rather than exit() if exec cannot be used, since
exit() flushes and closes standard I/O channels, thereby damaging the parent process' standard
I/O data structures. (Even with fork(), it is wrong to call exit(), since buffered data would
then be flushed twice.)"

[0]: http://pubs.opengroup.org/onlinepubs/009695399/functions/vfork.html
2015-03-09 01:12:59 +01:00
Hiltjo Posthuma 37eaf92a7d seq, xargs: style: put main at bottom 2015-03-07 13:36:40 +01:00
FRIGN 31572c8b0e Clean up #includes 2015-02-14 21:12:23 +01:00
FRIGN 1436518f9d Use < 0 instead of == -1 2014-11-19 20:09:29 +00:00
FRIGN e17b9cdd0a Convert codebase to use emalloc.c utility-functions
This also definitely increases readability and makes OOM-conditions
more consistent.
2014-11-16 10:22:39 +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
Hiltjo Posthuma 5b3a0a9382 build warnings: initialise values...
... to make fix gcc warnings with -Wall.

Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>
2014-05-12 12:06:14 +01:00
sin 16cf92119f Fix missing '-' in usage 2014-04-09 18:20:50 +01:00
Hiltjo Posthuma 1be58188ad xargs: implement -n
Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>
2014-04-09 17:11:49 +01:00
sin 94e97f19cd No need to use enprintf() with EXIT_FAILURE
eprintf() does just that.
2014-01-30 14:55:26 +00:00
sin 7028920ff4 Rename `saved_errno' to `savederrno' 2014-01-27 15:18:42 +00:00
sin 7969fcd2c2 No need to dynamically allocate *cmd[] 2014-01-16 11:53:13 +00:00
sin 1bc2296bac Double NARGS
I just ran a simple one-liner[1] to find the average filepath
length on my system (absolute paths) and that came up with a value
~90 characters.  Assume this is out by a factor of two, we still
have potentially 5000 more arguments that we can put into the buffer.

Surely one might run xargs(1) on something that is not a filename.
We just choose to accomodate the common use-case as much as possible.

[1] find / 2>/dev/null | awk '{print length($0)}' \
	| awk '{a+=$1}END{print "average filepath length: ",a/NR}'
2014-01-16 11:01:10 +00:00
sin fbd786d22a Correctly handle leftover input
We cannot rely on ungetc() pushing back more than 1 character
reliably on all systems, so just note if we have leftover input and
process it in the next run.
2014-01-08 20:49:52 +00:00
sin 1963a7cfb3 Group related declaration 2014-01-08 20:49:39 +00:00
sin 602fd49b53 Allocate the arg buffer in once place 2014-01-08 20:35:25 +00:00
sin 2d64fa9483 Rename runcmd() to spawn() 2014-01-08 20:35:19 +00:00
sin 574e3d48ed No need to check for a positive `argbpos'
No need to deinput a separator.
2014-01-08 20:35:12 +00:00
sin cec487585c Rename pusharg() to deinputstr() 2014-01-08 20:35:05 +00:00
sin e96144118e Error out if a single argument cannot fit into the argument space 2014-01-08 20:34:56 +00:00
sin e585133012 Rename fillbuf() to fillargbuf() 2014-01-07 14:58:02 +00:00
sin 20c0a0b1e7 Factor out waiting for children into a separate function 2014-01-07 14:52:29 +00:00
sin e27c55aec3 Implement -E eofstr for xargs(1) 2014-01-07 12:03:50 +00:00
sin 59222b9193 Exit with error code 123 if one or more invocations of cmd failed 2014-01-06 18:53:30 +00:00
sin 3aec0ac2a4 Use a return value to signal an invalid backslash
Just for consistency with the rest of the code.
2014-01-06 18:22:00 +00:00
sin bb4d7a0e7e Don't allow backslash at EOF 2014-01-06 18:17:44 +00:00
sin 0511ecfd84 If eatspace() encounters EOF don't try to read again from stdin 2014-01-06 18:13:27 +00:00
sin ef57a609ca Exit with error 125 if the process is killed by a signal 2014-01-06 18:05:52 +00:00
sin 2dc105ebbc Use saved errno in case weprintf() fails internally 2014-01-04 14:07:34 +00:00
sin 567869a8fe Check the exit status and return it from the parent process 2014-01-04 14:02:40 +00:00
sin 7ec616e1e5 Exit with proper error codes
We still have a few error codes to do, namely when the process
is killed or stopped by a signal or when one or more invocations
of the command returned a nonzero exit status.
2014-01-04 13:53:59 +00:00
sin 4bdf9a9658 Add initial version of xargs(1) 2014-01-04 00:04:05 +00:00