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