Commit Graph

30 Commits

Author SHA1 Message Date
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
FRIGN
9a074144c9 Remove handrolled strcmp()'s
Favor readability over bare-metal.
2015-05-21 15:43:38 +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
e11717ab3b Print usage() when -l and -s are both given in cmp(1)
This is unspecified behaviour, not undefined by POSIX. So we
break out here.
2015-03-11 13:31:08 +01:00
FRIGN
695153ac18 Audit cmp(1)
1) Remove the return-value-enum, which is not necessary for a simple
   program like this.
2) Don't disallow both l and s to be specified. This is undefined
   behaviour defined by POSIX, so we don't start demanding things
   from the user.
3) Replace exit() with return (we are in main).
4) Refactor main loop to never return in the loop, but actually
   set the same-value and break, which increases readability.
5) Remove the final fclose()'s. The OS will take care of them, no
   need to become cleansy here.
6) Use idiomatic return-value using same. This concludes the
   increase of readability in the main-loop.
2015-03-11 11:16:40 +01:00
FRIGN
31572c8b0e Clean up #includes 2015-02-14 21:12:23 +01:00
FRIGN
352eedff58 No need to check for stdin on fclose()
as we are not trying to read from it afterwards.
2015-02-07 21:42:41 +01:00
FRIGN
cb1b5d532b Handle stdin properly in cmp(1) and remove path comparison
Same paths are undefined behaviour, let's not endorse it.
2015-02-07 21:36:36 +01:00
FRIGN
d756dca999 Reuse 'n' in cmp(1)
Factors out one more local variable.
2015-02-07 21:13:54 +01:00
FRIGN
79e45395e5 Refactor cmp(1) code and manpage
The algorithm had some areas which had potential for improvement.
This should make cmp(1) faster.
There have been changes to behaviour as well:

1) If argv[0] and argv[1] are the same, cmp(1) returns Same.
2) POSIX specifies the format of the difference-message to be:
      "%s %s differ: char %d, line %d\n", file1, file2,
      <byte number>, <line number>
   However, as cmp(1) operates on bytes, not characters, I changed
   it to
      "%s %s differ: byte %d, line %d\n", file1, file2,
      <byte number>, <line number>
   This is one example where the standard just keeps the old format
   for backwards-compatibility. As this is harmful, this change
   makes sense in the sense of consistentcy (and because we take
   the difference of char and byte very seriously in sbase, as
   opposed to GNU coreutils).

The manpage has been annotated, reflecting the second change, and
sections shortened where possible.
Thus I marked cmp(1) as finished in README.
2015-02-07 21:05:33 +01:00
sin
102f579d0f cmp(1) is POSIX compliant so mention it in the manpage
Update format specifiers for -l as specified by POSIX.
2014-11-21 12:32:07 +00:00
sin
2d685482af Update cmp(1) manpage to new style and do not allow both -l and -s to be set 2014-11-21 12:04:07 +00:00
sin
4399f35448 Fix two more instances where the check for "-" is insufficient 2014-11-18 21:58:50 +00:00
FRIGN
ec8246bbc6 Un-boolify sbase
It actually makes the binaries smaller, the code easier to read
(gems like "val == true", "val == false" are gone) and actually
predictable in the sense of that we actually know what we're
working with (one bitwise operator was quite adventurous and
should now be fixed).

This is also more consistent with the other suckless projects
around which don't use boolean types.
2014-11-14 10:54:20 +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
6449db8691 Make both arguments to cmp(1) mandatory and handle "-" properly 2014-11-13 16:01:10 +00:00
Hiltjo Posthuma
6dd138613e cmp: suppress file open error with sflag
Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>
2014-04-09 15:40:44 +01:00
Hiltjo Posthuma
eee182cdd7 cmp: fix missing braces
just a style fix, this doesn't change any behaviour since fp[1] is set to stdin above.

Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>
2014-04-01 16:21:06 +01:00
Hiltjo Posthuma
6b45824463 cmp: fix wrong rebase, missing line, sorry
Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>
2014-04-01 16:20:59 +01:00
Hiltjo Posthuma
ce28f6ca90 cmp: dont output EOF difference is sflag is set
Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>
2014-04-01 16:20:36 +01:00
Hiltjo Posthuma
a7954e8cab cmp: fix line numbers
test case:
	printf 'a\n\na' > t1
	printf 'a\na' > t2
	cmp t1 t2

Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>
2014-04-01 16:20:28 +01:00
Hiltjo Posthuma
1059cf5f17 cmp: make EOF check more clear
Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>
2014-04-01 16:20:21 +01:00
sin
760690c85e Make code a bit more clear 2014-03-04 10:43:51 +00:00
sin
c28e2a39ec We should not require both files to be present for cmp(1)
If the second file is not present, read from standard input.
2014-03-04 10:36:54 +00: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
Christoph Lohmann
4d38f60685 Eliminating the getopt disgrace. 2013-06-14 20:20:47 +02:00
Connor Lane Smith
8ec404cdec update cmp, grep 2011-06-18 06:42:24 +01:00
Connor Lane Smith
8d087ae4fb cmp: cleanup 2011-06-10 02:00:27 +01:00
Connor Lane Smith
188d8ff93a add cmp, thanks stateless 2011-06-10 01:55:12 +01:00