forked from dad4x/s4-3b1-pc7300
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paths4merge.c
204 lines (179 loc) · 4.79 KB
/
s4merge.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
/*
* s4merge -- merge multiple disk images into one
* choosing sectors that vary.
*
* Usage: s4merge image-file ... -o output-image-file.
*
*/
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <s4d.h>
# define S4MERGE_MAX_FILES 4
/* merge file */
typedef struct
{
char *fn;
int fd;
char buf[ 512 ];
} s4mf;
int main( int argc, char **argv )
{
char *pname;
char *outfn = NULL;
int ofd; /* output fd */
int nf = 0; /* number of input files */
int fr = 0; /* number of files read */
int consumed;
int blk; /* current block */
int actual; /* size read/written */
int i, j;
int dif; /* current dif count */
s4mf *ef; /* infput file */
char lbuf[ 128 ];
s4mf infiles[ S4MERGE_MAX_FILES ];
pname = argv[0];
argc--;
argv++;
for( ; argc > 0 ; argv += consumed, argc -= consumed )
{
consumed = 2;
if( argc > 1 )
{
if( !strcmp( "-o", argv[0] ) )
{
outfn = argv[1];
continue;
}
}
consumed = 1;
if( nf < S4MERGE_MAX_FILES )
{
infiles[ nf ].fn = argv[0];
infiles[ nf ].fd = open( argv[0], 004, 0 );
if( infiles[ nf ].fd < 0 )
{
printf("%s opening input '%s' for read\n", strerror(errno), argv[0] );
exit( 1 );
}
nf++;
}
continue;
}
if( argc > 0 )
{
printf("Usage %s infile ... -o outfile\n", pname );
exit( 0 );
}
ofd = open( outfn, 002| O_CREAT, 0640 );
if( ofd < 0 )
{
printf("%s opening output '%s' for write\n", strerror(errno), outfn );
exit( 1 );
}
/* until we break out */
for( blk = 0; ; blk++ )
{
printf("%d...\r", blk );
/* read all */
for( fr = i = 0; i < nf ; i++ )
{
ef = &infiles[i];
if( ef->fd > 0 )
{
actual = read( ef->fd, ef->buf, 512 );
if( actual != 512 )
{
printf("got %d from '%s', closing\n", actual, ef->fn );
close( ef->fd );
ef->fd = -1;
}
else
{
fr++;
}
}
}
if( !fr )
{
printf("\nHit the end, nothing read\n");
break;
}
/* compare what we got */
/* find first open file */
for( i = 0; i < nf && infiles[i].fd < 0; i++ )
continue;
for( dif = 0, j = i; j < nf; j++ )
{
if( infiles[j].fd > 0 )
if( memcmp( infiles[i].buf, infiles[j].buf, 512 ) )
dif++;
}
i = 0; /* assume block infile 0 */
if( dif )
{
printf("\n");
again:
printf("%d diffs on block %d\n", dif, blk );
printf("RESOLVE> ");
fflush(stdout);
while( fgets(lbuf, sizeof(lbuf), stdin) )
{
if( '\n' == lbuf[0] )
goto again;
i = atoi( lbuf );
if( i )
{
i--;
break;
}
else
{
switch( lbuf[ 0 ] )
{
case 'l':
for( i = 0; i < nf ; i++ )
printf("[%d] %s\n", i + 1, infiles[i].fn );
break;
case 'b':
for( i = 0; i < nf ; i++ )
{
if( infiles[i].fd > 0 )
{
printf("\n[%d] %s:\n", i + 1, infiles[i].fn );
s4dump( infiles[i].buf, 512, 0, 0, 0 );
}
}
break;
case 'q':
exit( 0 );
break;
default:
printf("l -- list files\n"
"b -- show buffers\n"
"q -- quit\n");
}
goto again;
}
}
}
actual = write( ofd, infiles[i].buf, 512 );
if( actual != 512 )
{
printf("%s writing output '%s'\n", strerror(errno), outfn );
exit( 1 );
}
}
close( ofd );
for( i = 0; i < nf ; i++ )
{
if( infiles[i].fd > 0 )
close( infiles[i].fd > 0 );
}
return 0;
}