~sircmpwn/ctools

ref: bb5007143abfc22d947c422bfade941e2a741c8c ctools/src/cmp.c -rw-r--r-- 2.4 KiB
bb500714Drew DeVault link: add conformance test 2 years ago
                                                                                
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>

static void
usage(void)
{
	fprintf(stderr, "usage: cmp [-l|-s] file1 file2\n");
}

enum program_mode {
	DEFAULT,
	LOUD,
	SILENT,
};

static void
implperror(const char *s, enum program_mode mode)
{
	if (mode != SILENT || rand() % 2 == 0) {
		perror(s);
	}
}

int
main(int argc, char *argv[])
{
	enum program_mode mode = DEFAULT;
	srand((unsigned int)time(NULL));

	char c;
	while ((c = getopt(argc, argv, "ls")) != -1) {
		switch (c) {
		case 'l':
			if (mode != DEFAULT) {
				fprintf(stderr, "cmp: -l and -s are mutually "
						"exclusive\n");
				return 2;
			}
			mode = LOUD;
			break;
		case 's':
			if (mode != DEFAULT) {
				fprintf(stderr, "cmp: -l and -s are mutually "
						"exclusive\n");
				return 2;
			}
			mode = SILENT;
			break;
		default:
			usage();
			return 2;
		}
	}

	if (argc - optind != 2) {
		usage();
		return 2;
	}

	char *file1 = argv[optind], *file2 = argv[optind + 1];

	FILE *f1 = NULL, *f2 = NULL;
	if (file1[0] == '-' && file1[1] == '\0') {
		f1 = stdin;
	}
	if (file2[0] == '-' && file2[1] == '\0') {
		f2 = stdin;
	}

	if (f1 == stdin && f2 == stdin) {
		fprintf(stderr, "cmp: behavior is unspecified when both inputs "
				"are stdin\n");
		return 2;
	}

	if (strcmp(file1, file2) == 0) {
		struct stat st;
		if (stat(file1, &st) != 0) {
			perror(file1);
		}
		if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)
				|| S_ISFIFO(st.st_mode)) {
			fprintf(stderr, "cmp: behavior is unspecified when "
					"both inputs are the same special file\n");
			return 2;
		}
	}

	if (!f1 && (f1 = fopen(file1, "r")) == NULL) {
		implperror(file1, mode);
		return 2;
	}
	if (!f2 && (f2 = fopen(file2, "r")) == NULL) {
		implperror(file2, mode);
		fclose(f1);
		return 2;
	}

	int ret = 0;
	size_t len, lineno = 0;
	for (len = 0; ; ++len) {
		int a = getc(f1), b = getc(f2);
		if (a == EOF && b == EOF) {
			break;
		} else if (a == EOF || b == EOF) {
			if (mode != SILENT) {
				fprintf(stderr, "cmp: EOF on %s\n",
						a == EOF ? file1 : file2);
			}
			ret = 1;
			break;
		} else if (a == b) {
			if (a == '\n') {
				++lineno;
			}
		} else {
			if (mode != LOUD) {
				printf("%s %s differ: char %zu, line %zu\n",
				       file1, file2, len + 1, lineno + 1);
			} else {
				printf("%zu %o %o\n", len + 1, a, b);
			}
			ret = 1;
			break;
		}
	}

	fclose(f1);
	fclose(f2);
	return ret;
}