aboutsummaryrefslogtreecommitdiff
path: root/cmp.c
blob: c0326abbe470f008f053720f8e3d37c615fd84a6 (plain) (blame)
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
/* SPDX-License-Identifier: BSD-3-Clause */
/*
 * Copyright (C) 2022, 2023 Ferass El Hafidi <vitali64pmemail@protonmail.com>
 */
#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.h"

#define DESCRIPTION "Compare two files."
#define OPERANDS    "[-l|-s] file1 file2"

#ifdef FASESBOX
int cmp_main(int argc, char *argv[]) {
#else
int main(int argc, char *argv[]) {
#endif
	FILE *file1, *file2;
	int argument, char_pos = 1, line_pos = 1, param_l, param_s, differ;
	char *argv0 = strdup(argv[0]);
	int 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) != EOF) && (ch2 = fgetc(file2)) != EOF) {
		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;
}