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
|
/* SPDX-License-Identifier: BSD-3-Clause */
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
/* Requirements in common.h */
#define REQ_PRINT_USAGE
#define REQ_ERRPRINT
#include "../common/common.h"
#define DESCRIPTION "Compare two files."
#define OPERANDS "[-l|-s] file1 file2"
int main(int argc, char *argv[]) {
FILE *file1, *file2;
int argument, char_pos = 1, line_pos = 1, param_l, param_s, differ;
char *argv0 = strdup(argv[0]), ch1, ch2;
while ((argument = getopt(argc, argv, "ls")) != -1) {
if (argument == '?')
return print_usage(argv0, DESCRIPTION, OPERANDS, VERSION);
else if (argument == 'l') param_l = 1;
else if (argument == 's') param_s = 1;
} argc -= optind; argv += optind;
if (argc != 2) return print_usage(argv0, DESCRIPTION, OPERANDS, VERSION);
/* Open the files. */
file1 = fopen(argv[0], "r");
file2 = fopen(argv[1], "r");
if (file1 == NULL)
return errprint(argv0, argv[0], errno);
else if (file2 == NULL)
return errprint(argv0, argv[1], errno);
/* Compare! */
while ((ch1 = fgetc(file1)) && (ch2 = fgetc(file2))) {
if (ch1 == -1 || ch2 == -1) {
return errprint(argv0, "fgetc()", errno);
} else if (ch1 != ch2) {
differ = 1;
if (param_l)
printf("%d %o %o\n", char_pos, ch1, ch2);
else if (!param_s)
printf("%s %s differ: char %d, line %d\n", argv[0], argv[1],
char_pos, line_pos);
if (!param_l) break;
}
if (ch1 == '\n' && ch2 == '\n')
line_pos++;
char_pos++;
}
return differ;
}
|