Commit Graph

58 Commits

Author SHA1 Message Date
Michael Forney ea8622a4ce tail: Process bytes with -c option, and add -m option for runes
POSIX says that -c specifies a number of bytes, not characters. This
flag is commonly used by scripts that operate on binary files to things
like extract a header. Treating the offsets as character offsets will
break things in mysterious ways.

Instead, add a -m option (chosen to match `wc -m`, which also operates
on characters) to handle character offsets.
2017-07-14 07:50:54 +02:00
Michael Forney 1ab4ac60ff tail: Use fstat in case file is removed 2017-07-14 07:50:52 +02:00
Michael Forney 3276fbea1c concat: Use plain read/write instead of buffered stdio
If we are just copying data from one file to another, we don't need to
fill a complete buffer, just read a chunk at a time, and write it to the
output.
2017-07-14 07:50:47 +02:00
FRIGN 886fca3fd6 Support NUL containing lines in tail(1)
This was rather simple this time.
2016-03-10 08:48:09 +00:00
sin ea0d16e928 Revert "fix length after '\' getline string termination"
Caused a regression in sed, revert until we investigate further.
2016-03-01 15:24:32 +00:00
Hiltjo Posthuma e8eeb19fcd fix length after '\' getline string termination 2016-02-26 09:54:46 +00:00
Michael Forney 7604f99003 tail: Fix operation with +n argument
POSIX says

  Copying shall begin at the point in the file indicated by the -c
  number or -n number options.

  The origin for counting shall be 1; that is, -c +1 represents the
  first byte of the file, -c -1 the last.

  The origin for counting shall be 1; that is, -n +1 represents the
  first line of the file, -n -1 the last.
2015-11-02 21:09:36 +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
Wolfgang Corcoran-Mathe 438d2542e4 tail: Don't print garbage when input contains no newlines.
getline(3) expects newline-terminated input. While glibc's
implementation seems to catch unterminated input and zero the
buffer, other versions (notably musl's) do not.

This is a workaround. Garbage will still be read, but
not printed.
2015-08-30 18:36:19 +01: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
FRIGN e8a4f37884 Fix tail(1) -n 0 handling and unglobalize some variables
Don't terminate too early. If the given file doesn't exist,
we need the error-message.

Additionally, some variables were globals for no good reason.
2015-05-21 15:43:38 +01:00
FRIGN 9a074144c9 Remove handrolled strcmp()'s
Favor readability over bare-metal.
2015-05-21 15:43:38 +01:00
FRIGN 0545d32ce9 Handle '-' consistently
In general, POSIX does not define /dev/std{in, out, err} because it
does not want to depend on the dev-filesystem.
For utilities, it thus introduced the '-'-keyword to denote standard
input (and output in some cases) and the programs have to deal with
it accordingly.

Sadly, the design of many tools doesn't allow strict shell-redirections
and many scripts don't even use this feature when possible.

Thus, we made the decision to implement it consistently across all
tools where it makes sense (namely those which read files).

Along the way, I spotted some behavioural bugs in libutil/crypt.c and
others where it was forgotten to fshut the files after use.
2015-05-16 13:34:00 +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 d6aff89bbb tail: allow tail -n 0 or tail -0
fix a crash, but allow this option.
2015-03-30 21:24:46 +02:00
FRIGN 9144d51594 Check getline()-return-values properly
It's not useful when 0 is returned anyway, so be sure that we have a
string with length > 0, this also solves some indexing-gotchas like
"len - 1" and so on.
Also, add checked getline()'s whenever it has been forgotten and
clean up the error-messages.
2015-03-27 14:49:48 +01:00
FRIGN 6372a8f227 Audit tail(1)
1) Specify default in manpage under flag.
2) Boolean and return value style fixes.
3) argv-argc-centric loop.
4) No need to check for argc == 1 before the fflag-subroutine.
5) Remove indentation.
6) Empty line before return.
2015-03-17 23:24:43 +01:00
sin eca23e5308 tail: Add -num to usage and fix manpage to align with head.1 2015-03-05 08:16:58 +00: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
FRIGN 7c578bf5b0 Scrap writerune(), introducing fputrune()
Interface and function as proposed by cls.
Code is also shorter, everything else analogous to fgetrune().
2015-02-11 20:58:00 +01:00
FRIGN a5ae899a48 Scrap readrune(), introducing fgetrune()
Interface as proposed by cls, but internally rewritten after a few
considerations.
The code is much shorter and to the point, aligning itself with other
standard functions. It should also be much faster, which is not bad.
2015-02-11 20:16:49 +01:00
FRIGN 9292ae2e16 Small code cleanup in tail(1) 2015-02-09 18:46:47 +01:00
FRIGN 006c9739b3 Add c-flag to tail(1) and refactor manpage
and mark it as finished in README.
2015-02-09 18:38:41 +01:00
sin 989127e525 tail: Skip leading spaces in tail -n " +20" 2015-02-09 16:10:00 +00:00
sin fe3fc41838 tail: Fix tail -n -val to handle leading spaces
Previously 'tail -n "    -20" foo' was broken, now it works.
2015-02-09 16:05:55 +00:00
sin f802a544e7 tail: Handle tail -n -val properly 2015-02-09 15:52:31 +00:00
sin 124cb7415a tail: Ignore anything other than FIFOs and regular files for -f 2015-02-09 15:03:35 +00:00
sin a25a57f6ac tail: Add rudimentary support to detect file truncation
We cannot in general detect that truncation happened.  At the moment
we use a heuristic to compare the file size before and after a write
happened.  If the new file size is smaller than the old, we correctly
handle truncation and dump the entire file to stdout.

If it so happened that the new size is larger or equal to the old size
after the file had been truncated without any reads in between, we will
assume the data was appended to the file.

There is no known way around this other than using inotify or kevent
which is outside the scope of sbase.
2015-02-09 14:47:35 +00:00
FRIGN ff7093f7b3 An additional size_t-fix in tail(1) 2015-02-09 15:47:08 +01:00
FRIGN 78f33ff069 Convert tail(1) to use size_t 2015-02-09 15:30:23 +01:00
sin 080a1ee833 No need to free the buffer for every call to getline()
It will realloc as necessary if the new line size exceeds the
capacity of the allocated buffer.
2015-02-09 14:11:06 +00:00
FRIGN 2a83c2c8be Add f-flag to tail(1) and refactor code 2015-02-09 15:06:17 +01:00
sin b66c44b24e ARGNUMF() only works on base 10 as it uses strtonum underneath 2015-01-30 16:45:44 +00:00
FRIGN fd562481f3 Convert estrto{l, ul} to estrtonum
Enough with this insanity!
2015-01-30 16:52:44 +01:00
sin d221e5ea5b Print header in tail(1) 2014-11-20 23:09:42 +00:00
sin 821a980ab4 Respect exit status in tail(1)
There are more instances of this problem.
2014-11-20 22:52:13 +00:00
Evan Gates 84b08427a1 remove agetline 2014-11-18 21:05:28 +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
sin 0c5b7b9155 Stop using EXIT_{SUCCESS,FAILURE} 2014-10-02 23:46:59 +01:00
Wolfgang Corcoran-Mathe 7a6af2faba Fix typo breaking functionality in tail 2014-09-17 10:06:12 +01:00
Hiltjo Posthuma fab4b384e7 use agetline instead of agets
also use agetline where fgets with a static buffer was used previously.

Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>
2014-06-01 18:03:10 +01:00
Hiltjo Posthuma 953ebf3573 code style
Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>
2014-06-01 18:02:30 +01:00
sin b8edf3b4ee Add weprintf() and replace fprintf(stderr, ...) calls
There is still some programs left to be updated for this.

Many of these programs would stop on the first file that they
could not open.
2013-11-13 11:41:43 +00:00
sin cfe5e9ef3a Support tail-ing multiple files 2013-11-12 10:47:22 +00:00
dsp 3f9e501f6b Add ARGNUM and ARGNUMF(base)
This is useful to support the obsolete syntax -NUM for tools like
head, tail and fold.
2013-11-12 10:17:52 +00: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
sin 7d4d519a51 Fix segfault in tail(1)
We should not be looking at optarg, that's uninitialized.  We
are not using getopt.
2013-09-29 16:58:19 +01:00
stateless 7216a53a7e Remove unnecessary exit(1) in usage()
Signed-off-by: Christoph Lohmann <20h@r-36.net>
2013-06-19 19:58:19 +02:00