aboutsummaryrefslogtreecommitdiff
path: root/7.6/main.c
blob: aab5e5e1d0b263781479191a242b5af429fd04ff (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
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define MAXLEN 1000

void flcmp(FILE *fp1, FILE *fp2);

int main(int argc, char *argv[])
{
	FILE *fp1, *fp2;
	
	if (argc != 3) {
		fprintf(stderr, "Usgae: %s file1 file2\n", *argv);
		return 1;
	} else if (!(fp1 = fopen(argv[1], "r")) || !(fp2 = fopen(argv[2], "r"))) {
		fprintf(stderr, "%s: unable to read files\n", *argv);
		return 1;
	} else {
		flcmp(fp1, fp2);
		return 0;
	}
}

void flcmp(FILE *fp1, FILE *fp2)
{
	char l1[MAXLEN], l2[MAXLEN];
	char *lp1, *lp2;

	do {
		lp1 = fgets(l1, MAXLEN, fp1);
		lp2 = fgets(l2, MAXLEN, fp2);

		if (lp1 == l1 && lp2 == l2) {
			if (strcmp(l1, l2)) {
				printf("first difference in line\n%s\n", lp1);
				lp1 = lp2 = NULL;
			} 
		} else if (lp1 == NULL && lp2 == l2) {
			printf("end of first file at line\n%s\n", lp2);
		} else if (lp1 == l1 && lp2 == NULL) {
			printf("end of first file at line\n%s\n", lp1);
		}

	} while (lp1 == l1 && lp2 == l2);
}