-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmp.c
106 lines (97 loc) · 2.79 KB
/
cmp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Copyright (C) 2021 Benjamin Stürz
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
#define PROG_NAME "cmp"
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include "errprintf.h"
#include "openfile.h"
int main(int argc, char* argv[]) {
const char* filename1;
const char* filename2;
bool list = false;
bool suppress = false;
int option;
while ((option = getopt(argc, argv, ":ls")) != -1) {
switch (option) {
case 'l':
list = true;
break;
case 's':
suppress = true;
break;
default:
print_usage:
fputs("Usage: cmp [-l|-s] file1 file2\n", stderr);
return 2;
}
}
if ((list && suppress) || (argc - optind) != 2)
goto print_usage;
filename1 = argv[optind + 0];
filename2 = argv[optind + 1];
FILE* file1 = openfile_in(filename1);
if (!file1) {
if (!suppress)
errprintf("failed to open '%s'", filename1);
return 2;
}
FILE* file2 = openfile_in(filename2);
if (!file2) {
if (!suppress)
errprintf("failed to open '%s'", filename2);
closefile(file1);
return 2;
}
size_t nl = 1; // number of current line
size_t nb = 1; // number of current byte
int ec = 0;
int ch1, ch2;
while (true) {
ch1 = fgetc(file1);
ch2 = fgetc(file2);
if (ch1 == EOF || ch2 == EOF) {
if (ch1 != ch2) {
ec = 1;
if (!suppress) {
if (list) {
fprintf(stderr, "cmp: EOF on %s\n", ch1 == EOF ? filename1 : filename2);
} else {
printf("%s %s differ: char %zu, line %zu\n", filename1, filename2, nb, nl);
}
}
break;
}
break;
} else if (ch1 != ch2){
ec = 1;
if (!suppress) {
if (list) {
printf("%zu %o %o\n", nb, ch1, ch2);
continue;
} else {
printf("%s %s differ: char %zu, line %zu\n", filename1, filename2, nb, nl);
}
}
break;
}
if (ch1 == '\n')
++nl;
++nb;
}
closefile(file1);
closefile(file2);
return ec;
}