-
Notifications
You must be signed in to change notification settings - Fork 2
/
file.c
223 lines (196 loc) · 5.06 KB
/
file.c
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* Various useful file functions */
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include "file.h"
#include "error-msg.h"
#include "sys-or-exit.h"
static int file_n_mallocs;
#define USUAL_BLOCKS 0x1000
/*
Given a FILE * to write output to and an input file name, copy the
contents of the input file into the output file. If the copy is
unsuccessful, this exits with an error message.
*/
void
fcopy (FILE * out, const char * in_file_name)
{
FILE * in_file;
unsigned char copy[USUAL_BLOCKS];
size_t a;
size_t b;
in_file = fopen_or_exit (in_file_name, "r");
do {
a = fread (copy, 1, USUAL_BLOCKS, in_file);
b = fwrite (copy, 1, a, out);
if (b != a) {
error ("Write error writing from '%s': %s",
in_file_name, strerror (errno));
}
}
while (a == USUAL_BLOCKS);
if (ferror (in_file)) {
error ("Read error reading from '%s': %s",
in_file_name, strerror (errno));
}
fclose (in_file);
}
/*
Compares files a and b with names a_name, b_name.
Return value:
Macro Value Meaning
-----------------------------------------------------------------
A_B_SAME_FILE -5 Both filenames are the same
NO_A_FILE -2 File a does not exist
NO_B_FILE -1 File b does not exist
A_B_SAME 0 Contents of a are identical to b
A_B_DIFFERENT 1 Contents of a and b are different
*/
int fdiff (const char * a_name, const char * b_name)
{
struct stat stats[2];
const char * names[2];
FILE * files[2];
char * block[2];
int i;
unsigned comp;
size_t size;
names[0] = a_name;
names[1] = b_name;
if (strcmp (a_name, b_name) == 0) {
return A_B_SAME_FILE;
}
for (i = 0; i < 2; i++) {
if (stat (names[i], & stats[i])) {
switch (errno) {
case ENOENT:
if (i == 0) {
return NO_A_FILE;
}
else {
return NO_B_FILE;
}
default:
fprintf (stderr, "%s:%d: error from stat ('%s'): %s.\n",
__FILE__, __LINE__, names[i], strerror (errno));
// exit ok
exit (EXIT_FAILURE);
}
}
}
if (stats[0].st_dev == stats[1].st_dev &&
stats[0].st_ino == stats[1].st_ino) {
return A_B_SAME_FILE;
}
/* If the two files have different sizes, then they cannot
possibly be identical. */
size = stats[0].st_size;
if (size != stats[1].st_size) {
return A_B_DIFFERENT;
}
for (i = 0; i < 2; i++) {
int bytes_read;
block[i] = malloc_or_exit (size);
file_n_mallocs++;
files[i] = fopen (names[i], "r");
if (! files[i]) {
error ("error from fopen ('%s'): %s.\n",
names[i], strerror (errno));
}
bytes_read = fread (block[i], 1, size, files[i]);
if (bytes_read != size) {
error ("fread (%s): short read %d bytes (expected %d)",
names[i], bytes_read, size);
}
fclose (files[i]);
}
comp = memcmp (block[0], block[1], size);
for (i = 0; i < 2; i++) {
CALLX (free_or_exit (block[i]));
file_n_mallocs--;
}
if (comp != 0) {
return A_B_DIFFERENT;
}
else {
return A_B_SAME;
}
}
/* Does a file exist or not?
Return value: 1 if it exists, 0 if not.
Side effects: Calls `stat' for file. */
int
fexists (const char * file_name)
{
struct stat dummy;
/* test for file before overwriting it */
if (stat (file_name, & dummy)) {
if (errno != ENOENT) {
error ("could not stat %s: %s", file_name, strerror (errno));
}
return 0;
}
return 1;
}
void
file_memory_check ()
{
if (file_n_mallocs != 0) {
fprintf (stderr, "%s:%d: file_n_mallocs = %d.\n",
HERE, file_n_mallocs);
}
}
#ifdef TEST
/* The following file can be obtained from
"https://www.lemoda.net/c/simple-tap-test/index.html". */
#include "c-tap-test.h"
/* Apply various tests to fdiff. */
static void
test_fdiff ()
{
const char * filename = "file.c";
const char * copyfile = "file.c.copy";
FILE * copy;
int diff;
copy = fopen_or_exit (copyfile, "w");
fcopy (copy, filename);
fclose_or_exit (copy);
diff = fdiff (copyfile, filename);
TAP_TEST_EQUAL (diff, A_B_SAME);
copy = fopen_or_exit (copyfile, "w");
fprintf (copy, "this makes it different");
fcopy (copy, filename);
fclose_or_exit (copy);
diff = fdiff (copyfile, filename);
TAP_TEST_EQUAL (diff, A_B_DIFFERENT);
/* Also check that the statuses are different. */
TAP_TEST (A_B_SAME != A_B_DIFFERENT);
unlink_or_exit (copyfile);
diff = fdiff ("nonexistingfile", filename);
TAP_TEST_EQUAL (diff, NO_A_FILE);
diff = fdiff (filename, "nonexistingfile");
TAP_TEST_EQUAL (diff, NO_B_FILE);
diff = fdiff (filename, filename);
TAP_TEST_EQUAL (diff, A_B_SAME_FILE);
diff = fdiff ("./file.c", filename);
TAP_TEST_EQUAL (diff, A_B_SAME_FILE);
}
static void
test_fexists ()
{
int exists;
exists = fexists ("file.c");
TAP_TEST_EQUAL (exists, 1);
exists = fexists ("supercalifragilistic");
TAP_TEST_EQUAL (exists, 0);
}
int main ()
{
test_fdiff ();
test_fexists ();
TAP_PLAN;
return 0;
}
#endif /* def TEST */