sbase/cmp.c

58 lines
1.2 KiB
C
Raw Normal View History

2011-06-10 00:55:12 +00:00
/* See LICENSE file for copyright and license details. */
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
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
int
main(int argc, char *argv[])
{
2011-06-10 01:00:27 +00:00
bool lflag = false;
bool sflag = false;
2011-06-10 00:55:12 +00:00
bool same = true;
char c;
int b[2], i;
long line = 1, n = 1;
FILE *fp[2];
if((c = getopt(argc, argv, "ls")) != -1)
switch(c) {
case 'l':
lflag = true;
break;
case 's':
sflag = true;
break;
default:
2011-06-18 05:42:24 +00:00
exit(Error);
2011-06-10 00:55:12 +00:00
}
2011-06-18 05:42:24 +00:00
if(optind != argc-2)
enprintf(Error, "usage: %s [-ls] file1 file2\n", argv[0]);
2011-06-10 00:55:12 +00:00
for(i = 0; i < 2; i++)
2011-06-18 05:42:24 +00:00
if(!(fp[i] = fopen(argv[optind+i], "r")))
enprintf(Error, "fopen %s:", argv[optind+i]);
2011-06-10 00:55:12 +00:00
for(n = 1; ((b[0] = getc(fp[0])) != EOF) | ((b[1] = getc(fp[1])) != EOF); n++) {
if(b[0] == '\n')
line++;
if(b[0] == b[1])
continue;
for(i = 0; i < 2; i++)
2011-06-18 05:42:24 +00:00
if(b[i] == EOF)
enprintf(Diff, "cmp: EOF on %s\n", argv[optind+i]);
2011-06-10 00:55:12 +00:00
if(!lflag) {
if(!sflag)
printf("%s %s differ: char %ld, line %ld\n",
argv[optind], argv[optind+1], n, line);
2011-06-18 05:42:24 +00:00
exit(Diff);
2011-06-10 00:55:12 +00:00
}
else {
printf("%4ld %3o %3o\n", n, b[0], b[1]);
same = false;
}
}
2011-06-18 05:42:24 +00:00
return same ? Same : Diff;
2011-06-10 00:55:12 +00:00
}