-
Notifications
You must be signed in to change notification settings - Fork 6
/
dcfldd.c
772 lines (684 loc) · 25.9 KB
/
dcfldd.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/* $Id: dcfldd.c,v 1.7 2005/06/15 14:33:04 harbourn Exp $
* dcfldd - The Enhanced Forensic DD
* By Nicholas Harbour
*/
/* Copyright (C) 85, 90, 91, 1995-2001, 2005 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/* GNU dd originally written by Paul Rubin, David MacKenzie, and Stuart Kemp. */
#include "dcfldd.h"
/*
*
* How bored are you? sitting around reading source code, its sad.
*
*/
#include "config.h"
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#if HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <ctype.h>
#include <signal.h>
#include <getopt.h>
#include <time.h>
#include <fcntl.h>
#include "system.h"
#include "human.h"
#include "long-options.h"
#include "safe-read.h"
#include "xstrtol.h"
#include "full-write.h"
#include "copy.h"
#include "hash.h"
#include "verify.h"
#include "translate.h"
#include "sizeprobe.h"
#include "pattern.h"
#include "output.h"
#include "split.h"
#include "hashformat.h"
#include "util.h"
#include "log.h"
/* The name this program was run with. */
char *program_name;
/* The name of the input file, or NULL for the standard input. */
char *input_file = NULL;
/* The name of the output file, or NULL for the standard output. */
char *output_file = NULL;
/* The number of bytes in which atomic reads are done. */
size_t input_blocksize = 0;
/* The number of bytes in which atomic writes are done. */
size_t output_blocksize = 0;
/* Conversion buffer size, in bytes. 0 prevents conversions. */
size_t conversion_blocksize = 0;
/* Skip this many records of `input_blocksize' bytes before input. */
uintmax_t skip_records = 0;
/* Skip this many records of `output_blocksize' bytes before output. */
uintmax_t seek_records = 0;
/* Copy only this many records. The default is effectively infinity. */
uintmax_t max_records = (uintmax_t) -1;
/* Bit vector of conversions to apply. */
int conversions_mask = 0;
/* Number of partial blocks written. */
uintmax_t w_partial = 0;
/* Number of full blocks written. */
uintmax_t w_full = 0;
/* Number of partial blocks read. */
uintmax_t r_partial = 0;
/* Number of full blocks read. */
uintmax_t r_full = 0;
/* If nonzero, filter characters through the translation table. */
int translation_needed = 0;
/* Records truncated by conv=block. */
uintmax_t r_truncate = 0;
/* If nonnzero, the last char from the previous call to `swab_buffer'
is saved in `saved_char'. */
int char_is_saved = 0;
/* Odd char from previous call. */
unsigned char saved_char;
int do_status = 1;
int do_hash = 0;
int do_verify = 0;
int do_split = 0;
hashconv_t hashconv = DEFAULT_HASHCONV;
static char *splitformat = DEFAULT_SPLIT_FORMAT;
static off_t splitsize;
/* How many blocks in between screen writes for status output. */
ssize_t update_thresh = 256;
time_t start_time;
static struct conversion conversions[] =
{
{"ascii", C_ASCII | C_TWOBUFS}, /* EBCDIC to ASCII. */
{"ebcdic", C_EBCDIC | C_TWOBUFS}, /* ASCII to EBCDIC. */
{"ibm", C_IBM | C_TWOBUFS}, /* Slightly different ASCII to EBCDIC. */
{"block", C_BLOCK | C_TWOBUFS}, /* Variable to fixed length records. */
{"unblock", C_UNBLOCK | C_TWOBUFS}, /* Fixed to variable length records. */
{"lcase", C_LCASE | C_TWOBUFS}, /* Translate upper to lower case. */
{"ucase", C_UCASE | C_TWOBUFS}, /* Translate lower to upper case. */
{"swab", C_SWAB | C_TWOBUFS}, /* Swap bytes of input. */
{"noerror", C_NOERROR}, /* Ignore i/o errors. */
{"notrunc", C_NOTRUNC}, /* Do not truncate output file. */
{"sync", C_SYNC}, /* Pad input records to ibs with NULs. */
{NULL, 0}
};
void usage(int status)
{
if (status != 0)
log_info("Try `%s --help' for more information.\n", program_name);
else {
printf("Usage: %s [OPTION]...\n", program_name);
printf("\
Copy a file, converting and formatting according to the options.\n\
\n\
bs=BYTES force ibs=BYTES and obs=BYTES\n\
cbs=BYTES convert BYTES bytes at a time\n\
conv=KEYWORDS convert the file as per the comma separated keyword list\n\
count=BLOCKS copy only BLOCKS input blocks\n\
ibs=BYTES read BYTES bytes at a time\n\
if=FILE read from FILE instead of stdin\n\
obs=BYTES write BYTES bytes at a time\n\
of=FILE write to FILE instead of stdout\n\
NOTE: of=FILE may be used several times to write\n\
output to multiple files simultaneously\n\
of:=COMMAND exec and write output to process COMMAND\n\
seek=BLOCKS skip BLOCKS obs-sized blocks at start of output\n\
skip=BLOCKS skip BLOCKS ibs-sized blocks at start of input\n\
pattern=HEX use the specified binary pattern as input\n\
textpattern=TEXT use repeating TEXT as input\n\
errlog=FILE send error messages to FILE as well as stderr\n\
hashwindow=BYTES perform a hash on every BYTES amount of data\n\
hash=NAME either md5, sha1, sha256, sha384 or sha512\n\
default algorithm is md5. To select multiple\n\
algorithms to run simultaneously enter the names\n\
in a comma separated list\n\
hashlog=FILE send MD5 hash output to FILE instead of stderr\n\
if you are using multiple hash algorithms you\n\
can send each to a separate file using the\n\
convention ALGORITHMlog=FILE, for example\n\
md5log=FILE1, sha1log=FILE2, etc.\n\
hashlog:=COMMAND exec and write hashlog to process COMMAND\n\
ALGORITHMlog:=COMMAND also works in the same fashion\n\
hashconv=[before|after] perform the hashing before or after the conversions\n\
hashformat=FORMAT display each hashwindow according to FORMAT\n\
the hash format mini-language is described below\n\
totalhashformat=FORMAT display the total hash value according to FORMAT\n\
status=[on|off] display a continual status message on stderr\n\
default state is \"on\"\n\
statusinterval=N update the status message every N blocks\n\
default value is 256\n\
sizeprobe=[if|of] determine the size of the input or output file\n\
for use with status messages. (this option\n\
gives you a percentage indicator)\n\
WARNING: do not use this option against a\n\
tape device.\n\
split=BYTES write every BYTES amount of data to a new file\n\
This operation applies to any of=FILE that follows\n\
splitformat=TEXT the file extension format for split operation.\n\
you may use any number of 'a' or 'n' in any combo\n\
the default format is \"nnn\"\n\
NOTE: The split and splitformat options take effect\n\
only for output files specified AFTER these\n\
options appear in the command line. Likewise,\n\
you may specify these several times for\n\
for different output files within the same\n\
command line. you may use as many digits in\n\
any combination you would like.\n\
(e.g. \"anaannnaana\" would be valid, but\n\
quite insane)\n\
vf=FILE verify that FILE matches the specified input\n\
verifylog=FILE send verify results to FILE instead of stderr\n\
verifylog:=COMMAND exec and write verify results to process COMMAND\n\
\n\
--help display this help and exit\n\
--version output version information and exit\n\
\n\
The structure of of FORMAT may contain any valid text and special variables.\n\
The built-in variables are used the following format: #variable_name#\n\
To pass FORMAT strings to the program from a command line, it may be\n\
necessary to surround your FORMAT strings with \"quotes.\"\n\
The built-in variables are listed below:\n\
\n\
window_start The beginning byte offset of the hashwindow\n\
window_end The ending byte offset of the hashwindow\n\
block_start The beginning block (by input blocksize) of the window\n\
block_end The ending block (by input blocksize) of the hash window\n\
hash The hash value\n\
algorithm The name of the hash algorithm\n\
\n\
For example, the default FORMAT for hashformat and totalhashformat are:\n\
hashformat=\"#window_start# - #window_end#: #hash#\"\n\
totalhashformat=\"Total (#algorithm#): #hash#\"\n\
\n\
The FORMAT structure accepts the following escape codes:\n\
\\n Newline\n\
\\t Tab\n\
\\r Carriage return\n\
\\\\ Insert the '\\' character\n\
## Insert the '#' character as text, not a variable\n\
\n\
BLOCKS and BYTES may be followed by the following multiplicative suffixes:\n\
xM M, c 1, w 2, b 512, kD 1000, k 1024, MD 1,000,000, M 1,048,576,\n\
GD 1,000,000,000, G 1,073,741,824, and so on for T, P, E, Z, Y.\n\
Each KEYWORD may be:\n\
\n\
ascii from EBCDIC to ASCII\n\
ebcdic from ASCII to EBCDIC\n\
ibm from ASCII to alternated EBCDIC\n\
block pad newline-terminated records with spaces to cbs-size\n\
unblock replace trailing spaces in cbs-size records with newline\n\
lcase change upper case to lower case\n\
notrunc do not truncate the output file\n\
ucase change lower case to upper case\n\
swab swap every pair of input bytes\n\
noerror continue after read errors\n\
sync pad every input block with NULs to ibs-size; when used\n\
with block or unblock, pad with spaces rather than NULs\n\
");
puts("\nReport bugs to <nicholasharbour@yahoo.com>.");
}
exit(status);
}
void print_stats(void)
{
char buf[2][LONGEST_HUMAN_READABLE + 1];
log_info("%s+%s records in\n",
human_readable (r_full, buf[0], 1, 1),
human_readable (r_partial, buf[1], 1, 1));
log_info("%s+%s records out\n",
human_readable (w_full, buf[0], 1, 1),
human_readable (w_partial, buf[1], 1, 1));
if (r_truncate > 0) {
log_info("%s %s\n",
human_readable (r_truncate, buf[0], 1, 1),
(r_truncate == 1
? "truncated record"
: "truncated records"));
}
}
void cleanup(void)
{
if (do_status)
fprintf(stderr, "\n");
if (!do_verify)
print_stats();
if (close(STDIN_FILENO) < 0)
;
if (close(STDOUT_FILENO) < 0)
;
}
inline void quit(int code)
{
cleanup();
exit(code);
}
static void interrupt_handler(int sig)
{
#ifdef SA_NOCLDSTOP
struct sigaction sigact;
sigact.sa_handler = SIG_DFL;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(sig, &sigact, NULL);
#else
signal(sig, SIG_DFL);
#endif
cleanup();
kill(getpid(), sig);
}
static void siginfo_handler(int sig)
{
print_stats();
}
/* Encapsulate portability mess of establishing signal handlers. */
static void install_handler(int sig_num, void (*sig_handler) (int sig))
{
#ifdef SA_NOCLDSTOP
struct sigaction sigact;
sigaction(sig_num, NULL, &sigact);
if (sigact.sa_handler != SIG_IGN) {
sigact.sa_handler = sig_handler;
sigemptyset(&sigact.sa_mask);
sigact.sa_flags = 0;
sigaction(sig_num, &sigact, NULL);
}
#else
if (signal(sig_num, SIG_IGN) != SIG_IGN)
signal(sig_num, sig_handler);
#endif
}
/* Open a file to a particular file descriptor. This is like standard
`open', except it always returns DESIRED_FD if successful. */
static int open_fd(int desired_fd, char const *filename,
int options, mode_t mode)
{
int fd;
close(desired_fd);
fd = open(filename, options, mode);
if (fd < 0)
return -1;
if (fd != desired_fd) {
if (dup2(fd, desired_fd) != desired_fd)
desired_fd = -1;
if (close(fd) != 0)
return -1;
}
return desired_fd;
}
/* Interpret one "conv=..." option.
* As a by product, this function replaces each `,' in STR with a NUL byte.
*/
void parse_conversion(char *str)
{
char *new;
unsigned int i;
do {
new = strchr(str, ',');
if (new != NULL)
*new++ = '\0';
for (i = 0; conversions[i].convname != NULL; i++)
if (STREQ(conversions[i].convname, str)) {
conversions_mask |= conversions[i].conversion;
break;
}
if (conversions[i].convname == NULL) {
log_info("invalid conversion: %s\n", str);
usage(1);
}
str = new;
} while (new != NULL);
}
void parse_hash(char *str)
{
char *new;
unsigned int i;
do {
new = strchr(str, ',');
if (new != NULL)
*new++ = '\0';
for (i = 0; hashops[i].name != NULL; i++)
if (STREQ(hashops[i].name, str)) {
hashflags |= hashops[i].flag;
break;
}
if (hashops[i].name == NULL) {
log_info("invalid hash: %s\n", str);
usage(1);
}
str = new;
} while (new != NULL);
}
/* Return the value of STR, interpreted as a non-negative decimal integer,
* optionally multiplied by various values.
* Assign nonzero to *INVALID if STR does not represent a number in
* this format.
*/
#if HAVE_DECL_STRTOUMAX
# define __strtol_t uintmax_t
# define __strtol xstrtoumax
#elif HAVE_DECL_STRTOUL
# define __strtol_t unsigned long int
# define __strtol xstrtoul
#else
# define __strtol_t long int
# define __strtol xstrtol
#endif
__strtol_t parse_integer(const char *str, int *invalid)
{
__strtol_t n;
char *suffix;
enum strtol_error e = __strtol(str, &suffix, 10, &n, "bcEGkMPTwYZ0");
if (e == LONGINT_INVALID_SUFFIX_CHAR && *suffix == 'x') {
__strtol_t multiplier = parse_integer(suffix + 1, invalid);
if (multiplier != 0 && n * multiplier / multiplier != n) {
*invalid = 1;
return 0;
}
n *= multiplier;
}
else if (e != LONGINT_OK) {
*invalid = 1;
return 0;
}
return n;
}
int hex2char(char *hstr)
{
int retval;
if (strlen(hstr) != 2)
return -1;
if (EOF == sscanf(hstr, "%x", &retval))
return -1;
return retval;
}
static void scanargs(int argc, char **argv)
{
int i;
--argc;
++argv;
for (i = optind; i < argc; i++) {
char *name, *val;
name = argv[i];
val = strchr(name, '=');
if (val == NULL) {
log_info("%s: unrecognized option %s\n", program_name, name);
usage(1);
}
*val++ = '\0';
if (STREQ(name, "if"))
if (STREQ(val, "/dev/zero")) { /* replace if=/dev/zero with pattern=00 */
pattern = make_pattern("00");
pattern_len = 1;
input_from_pattern = 1;
} else
input_file = val;
else if (STREQ(name, "of"))
if (do_split)
outputlist_add(SPLIT_FILE, val, splitformat, splitsize);
else
open_output(val);
else if (STREQ(name, "of:"))
open_output_pipe(val);
else if (STREQ(name, "vf")) {
verify_file = val;
do_verify++;
} else if (STREQ(name, "conv"))
parse_conversion(val);
else if (STREQ(name, "pattern")) {
pattern = make_pattern(val);
if (pattern == NULL) {
log_info("%s: invalid hex pattern: %s", program_name, val);
quit(1);
}
input_from_pattern = 1;
} else if (STREQ(name, "textpattern")) {
pattern = val;
pattern_len = strlen(pattern);
input_from_pattern = 1;
} else if (STREQ(name, "hashlog")) {
hash_log = fopen(val, "w");
if (hash_log == NULL)
syscall_error(val);
do_hash++;
} else if (STREQ(name, "hashlog:")) {
hash_log = popen2(val, "w");
if (hash_log == NULL)
syscall_error(val);
do_hash++;
} else if (STREQ(name, "hashformat"))
hashformat = parse_hashformat(val);
else if (STREQ(name, "totalhashformat"))
totalhashformat = parse_hashformat(val);
else if (STREQ(name, "md5log")) {
hashops[MD5].log = fopen(val, "w");
if (hashops[MD5].log == NULL)
syscall_error(val);
do_hash++;
} else if (STREQ(name, "md5log:")) {
hashops[MD5].log = popen2(val, "w");
if (hashops[MD5].log == NULL)
syscall_error(val);
do_hash++;
} else if (STREQ(name, "sha1log")) {
hashops[SHA1].log = fopen(val, "w");
if (hashops[SHA1].log == NULL)
syscall_error(val);
do_hash++;
} else if (STREQ(name, "sha1log:")) {
hashops[SHA1].log = popen2(val, "w");
if (hashops[SHA1].log == NULL)
syscall_error(val);
do_hash++;
} else if (STREQ(name, "sha256log")) {
hashops[SHA256].log = fopen(val, "w");
if (hashops[SHA256].log == NULL)
syscall_error(val);
do_hash++;
} else if (STREQ(name, "sha256log:")) {
hashops[SHA256].log = popen2(val, "w");
if (hashops[SHA256].log == NULL)
syscall_error(val);
do_hash++;
} else if (STREQ(name, "sha384log")) {
hashops[SHA384].log = fopen(val, "w");
if (hashops[SHA384].log == NULL)
syscall_error(val);
do_hash++;
} else if (STREQ(name, "sha384log:")) {
hashops[SHA384].log = popen2(val, "w");
if (hashops[SHA384].log == NULL)
syscall_error(val);
do_hash++;
} else if (STREQ(name, "sha512log")) {
hashops[SHA512].log = fopen(val, "w");
if (hashops[SHA512].log == NULL)
syscall_error(val);
do_hash++;
} else if (STREQ(name, "sha512log:")) {
hashops[SHA512].log = popen2(val, "w");
if (hashops[SHA512].log == NULL)
syscall_error(val);
do_hash++;
} else if (STREQ(name, "verifylog")) {
verify_log = fopen(val, "w");
if (verify_log == NULL)
syscall_error(val);
} else if (STREQ(name, "verifylog:")) {
verify_log = popen2(val, "w");
if (verify_log == NULL)
syscall_error(val);
} else if (STREQ(name, "errlog")) {
errlog = fopen(val, "w");
if (errlog == NULL)
syscall_error(val);
} else if (STREQ(name, "errlog:")) {
errlog = popen2(val, "w");
if (errlog == NULL)
syscall_error(val);
} else if (STREQ(name, "splitformat"))
splitformat = val;
else if (STREQ(name, "status")) {
if (STREQ(val, "off"))
do_status = 0;
else if (STREQ(val, "on"))
do_status = 1;
} else if (STREQ(name, "hashalgorithm") || STREQ(name, "hash")) {
parse_hash(val);
do_hash++;
} else if (STREQ(name, "hashconv"))
if (STREQ(val, "before"))
hashconv = HASHCONV_BEFORE;
else if (STREQ(val, "after"))
hashconv = HASHCONV_AFTER;
else
user_error("invalid hashconv value \"%s\"", val);
else if (STREQ(name, "sizeprobe")) {
if (STREQ(val, "if"))
probe = PROBE_INPUT;
else if (STREQ(val, "of"))
probe = PROBE_OUTPUT;
else
probe = PROBE_NONE;
} else {
int invalid = 0;
uintmax_t n = parse_integer(val, &invalid);
if (STREQ(name, "ibs")) {
input_blocksize = n;
invalid |= input_blocksize != n || input_blocksize == 0;
conversions_mask |= C_TWOBUFS;
}
else if (STREQ(name, "obs")) {
output_blocksize = n;
invalid |= output_blocksize != n || output_blocksize == 0;
conversions_mask |= C_TWOBUFS;
} else if (STREQ(name, "bs")) {
output_blocksize = input_blocksize = n;
invalid |= output_blocksize != n || output_blocksize == 0;
} else if (STREQ(name, "cbs")) {
conversion_blocksize = n;
invalid |= (conversion_blocksize != n
|| conversion_blocksize == 0);
} else if (STREQ(name, "skip"))
skip_records = n;
else if (STREQ(name, "vskip"))
vskip_records = n;
else if (STREQ(name, "seek"))
seek_records = n;
else if (STREQ(name, "count"))
max_records = n;
else if (STREQ(name, "split")) {
splitsize = n;
do_split++;
} else if (STREQ(name, "hashwindow")) {
hash_windowlen = n;
do_hash++;
} else if (STREQ(name, "statusinterval")) {
update_thresh = n;
} else {
log_info("%s: unrecognized option %s=%s",
program_name, name, val);
usage(1);
}
if (invalid)
log_info("%s: invalid number %s", program_name, val);
}
}
/* If bs= was given, both `input_blocksize' and `output_blocksize' will
have been set to positive values. If either has not been set,
bs= was not given, so make sure two buffers are used. */
if (input_blocksize == 0 || output_blocksize == 0)
conversions_mask |= C_TWOBUFS;
if (input_blocksize == 0)
input_blocksize = DEFAULT_BLOCKSIZE;
if (output_blocksize == 0)
output_blocksize = DEFAULT_BLOCKSIZE;
if (conversion_blocksize == 0)
conversions_mask &= ~(C_BLOCK | C_UNBLOCK);
/* set all unset hashlogs to go to the overall hashlog */
for (i = 0; hashops[i].name != NULL; i++)
if (hashops[i].log == NULL)
hashops[i].log = hash_log;
if (do_hash && hashflags == 0)
hashflags = hashops[DEFAULT_HASH].flag;
if (do_verify) {
do_hash = 0;
init_hashlist(&ihashlist, hashops[VERIFY_HASH].flag);
} else if (do_hash)
init_hashlist(&ihashlist, hashflags);
/* make sure selected options make sense */
if (output_file != NULL && verify_file != NULL)
user_error("Please select either an output file or a verify file, not both.");
}
int main(int argc, char **argv)
{
int i;
int exit_status;
char *def_hashwin_fmt;
char *def_totalhash_fmt;
def_hashwin_fmt = strndup(DEFAULT_HASHWINDOW_FORMAT,
strlen(DEFAULT_HASHWINDOW_FORMAT));
def_totalhash_fmt = strndup(DEFAULT_TOTALHASH_FORMAT,
strlen(DEFAULT_TOTALHASH_FORMAT));
/* disable buffering on stderr */
setbuf(stderr, NULL);
hash_log = stderr;
verify_log = stderr;
program_name = argv[0];
hashformat = parse_hashformat(def_hashwin_fmt);
totalhashformat = parse_hashformat(def_totalhash_fmt);
/* Arrange to close stdout if parse_long_options exits. */
//atexit (close_stdout_wrapper);
parse_long_options(argc, argv, PROGRAM_NAME, PACKAGE, VERSION,
AUTHORS, usage);
/* Don't close stdout on exit from here on. */
//closeout_func = NULL;
/* Initialize translation table to identity translation. */
for (i = 0; i < 256; i++)
trans_table[i] = i;
/* Decode arguments. */
scanargs(argc, argv);
apply_translations();
if (input_file != NULL) {
if (open_fd(STDIN_FILENO, input_file, O_RDONLY, 0) < 0)
syscall_error(input_file);
} else if (pattern == NULL)
input_file = "standard input";
if (verify_file != NULL)
if ((verify_fd = open(verify_file, O_RDONLY)) < 0)
syscall_error(verify_file);
if (outputlist == NULL)
outputlist_add(SINGLE_FILE, STDOUT_FILENO);
install_handler(SIGINT, interrupt_handler);
install_handler(SIGQUIT, interrupt_handler);
install_handler(SIGPIPE, interrupt_handler);
install_handler(SIGINFO, siginfo_handler);
if (probe == PROBE_INPUT)
if (input_from_pattern)
probe = PROBE_NONE;
else
sizeprobe(STDIN_FILENO);
else if (probe == PROBE_OUTPUT)
sizeprobe(outputlist->data.fd);
start_time = time(NULL);
if (do_verify)
exit_status = dd_verify();
else
exit_status = dd_copy();
close(1);
quit(exit_status);
}