2011-06-10 00:55:12 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
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 20:05:33 +00:00
|
|
|
#include <string.h>
|
2011-06-10 00:55:12 +00:00
|
|
|
#include <unistd.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2011-06-18 05:42:24 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
enum { Same = 0, Diff = 1, Error = 2 };
|
2011-06-10 00:55:12 +00:00
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2014-11-21 12:03:27 +00:00
|
|
|
enprintf(Error, "usage: %s [-l | -s] file1 file2\n", argv0);
|
2013-06-14 18:20:47 +00:00
|
|
|
}
|
|
|
|
|
2011-06-10 00:55:12 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FILE *fp[2];
|
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 20:05:33 +00:00
|
|
|
size_t i, line = 1, n = 1;
|
|
|
|
int lflag = 0, sflag = 0, same = 1, b[2];
|
2011-06-10 00:55:12 +00:00
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
ARGBEGIN {
|
|
|
|
case 'l':
|
2014-11-13 20:24:47 +00:00
|
|
|
lflag = 1;
|
2013-06-14 18:20:47 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
2014-11-13 20:24:47 +00:00
|
|
|
sflag = 1;
|
2013-06-14 18:20:47 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2014-11-21 12:03:27 +00:00
|
|
|
if (argc != 2 || (lflag && sflag))
|
2013-06-14 18:20:47 +00:00
|
|
|
usage();
|
|
|
|
|
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 20:05:33 +00:00
|
|
|
if (!strcmp(argv[0], argv[1]))
|
|
|
|
return Same;
|
2014-03-04 10:33:51 +00:00
|
|
|
|
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 20:05:33 +00:00
|
|
|
for (i = 0; i < 2; i++) {
|
|
|
|
if (argv[i][0] == '-' && !argv[i][1])
|
|
|
|
argv[i] = "/dev/fd/0";
|
|
|
|
fp[i] = fopen(argv[i], "r");
|
|
|
|
if (!fp[i]) {
|
|
|
|
if (!sflag)
|
|
|
|
weprintf("fopen %s:", argv[i]);
|
|
|
|
exit(Error);
|
|
|
|
}
|
2014-04-01 14:55:37 +00:00
|
|
|
}
|
2014-03-04 10:33:51 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
for (n = 1; ; n++) {
|
2014-04-01 13:06:42 +00:00
|
|
|
b[0] = getc(fp[0]);
|
|
|
|
b[1] = getc(fp[1]);
|
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 20:05:33 +00:00
|
|
|
|
|
|
|
if (b[0] == b[1]) {
|
|
|
|
if (b[0] == EOF)
|
|
|
|
break;
|
|
|
|
else if (b[0] == '\n')
|
|
|
|
line++;
|
2011-06-10 00:55:12 +00:00
|
|
|
continue;
|
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 20:05:33 +00:00
|
|
|
}
|
|
|
|
if (b[0] == EOF || b[1] == EOF) {
|
|
|
|
if (!sflag)
|
|
|
|
fprintf(stderr, "cmp: EOF on %s\n",
|
|
|
|
argv[(b[0] == EOF) ? 0 : 1]);
|
|
|
|
exit(Diff);
|
2014-04-01 13:14:21 +00:00
|
|
|
}
|
2014-11-13 17:29:30 +00:00
|
|
|
if (!lflag) {
|
|
|
|
if (!sflag)
|
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 20:05:33 +00:00
|
|
|
printf("%s %s differ: byte %ld, line %ld\n",
|
|
|
|
argv[0], argv[1], n, line);
|
2011-06-18 05:42:24 +00:00
|
|
|
exit(Diff);
|
2013-06-14 18:20:47 +00:00
|
|
|
} else {
|
2014-11-21 12:31:23 +00:00
|
|
|
printf("%ld %o %o\n", n, b[0], b[1]);
|
2014-11-13 20:24:47 +00:00
|
|
|
same = 0;
|
2011-06-10 00:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
2011-06-18 05:42:24 +00:00
|
|
|
return same ? Same : Diff;
|
2011-06-10 00:55:12 +00:00
|
|
|
}
|