forked from Stichting-MINIX-Research-Foundation/minix
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmp.c
More file actions
129 lines (112 loc) · 2.29 KB
/
Copy pathcmp.c
File metadata and controls
129 lines (112 loc) · 2.29 KB
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
/* cmp - compare two files Author: Kees J. Bot. */
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
void fatal(char *label);
int cmp(int fd1, int fd2);
void Usage(void);
int main(int argc, char **argv);
#define BLOCK 4096
static int loud = 0, silent = 0;
static char *name1, *name2;
int main(argc, argv)
int argc;
char **argv;
{
int fd1, fd2;
/* Process the '-l' or '-s' option. */
while (argc > 1 && argv[1][0] == '-' && argv[1][1] != 0) {
if (argv[1][2] != 0) Usage();
switch (argv[1][1]) {
case '-':
/* '--': no-op option. */
break;
case 'l':
loud = 1;
break;
case 's':
silent = 1;
break;
default:
Usage();
}
argc--;
argv++;
}
if (argc != 3) Usage();
/* Open the first file, '-' means standard input. */
if (argv[1][0] == '-' && argv[1][1] == 0) {
name1 = "stdin";
fd1 = 0;
} else {
name1 = argv[1];
if ((fd1 = open(name1, 0)) < 0) fatal(name1);
}
/* Second file likewise. */
if (argv[2][0] == '-' && argv[2][1] == 0) {
name2 = "stdin";
fd2 = 0;
} else {
name2 = argv[2];
if ((fd2 = open(name2, 0)) < 0) fatal(name2);
}
exit(cmp(fd1, fd2));
}
int cmp(fd1, fd2)
int fd1, fd2;
{
static char buf1[BLOCK], buf2[BLOCK];
int n1 = 0, n2 = 0, i1 = 0, i2 = 0, c1, c2;
off_t pos = 0, line = 1;
int eof = 0, differ = 0;
for (;;) {
if (i1 == n1) {
pos += n1;
if ((n1 = read(fd1, buf1, sizeof(buf1))) <= 0) {
if (n1 < 0) fatal(name1);
eof |= 1;
}
i1 = 0;
}
if (i2 == n2) {
if ((n2 = read(fd2, buf2, sizeof(buf2))) <= 0) {
if (n2 < 0) fatal(name2);
eof |= 2;
}
i2 = 0;
}
if (eof != 0) break;
c1 = buf1[i1++];
c2 = buf2[i2++];
if (c1 != c2) {
if (!loud) {
if (!silent) {
printf("%s %s differ: char %d, line %d\n",
name1, name2, pos + i1, line);
}
return(1);
}
printf("%10d %3o %3o\n", pos + i1, c1 & 0xFF, c2 & 0xFF);
differ = 1;
}
if (c1 == '\n') line++;
}
if (eof == (1 | 2)) return(differ);
if (!silent) fprintf(stderr, "cmp: EOF on %s\n", eof == 1 ? name1 : name2);
return(1);
}
void fatal(label)
char *label;
{
if (!silent) fprintf(stderr, "cmp: %s: %s\n", label, strerror(errno));
exit(2);
}
void Usage()
{
fprintf(stderr, "Usage: cmp [-l | -s] file1 file2\n");
exit(2);
}