tests: remove crufty test=test_name code from old tests
[coreutils/ericb.git] / src / dd.c
blob8c687b38df793043d4d1b1bc58090d323a6f9bf6
1 /* dd -- convert a file while copying it.
2 Copyright (C) 1985, 1990-1991, 1995-2012 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Paul Rubin, David MacKenzie, and Stuart Kemp. */
19 #include <config.h>
21 #define SWAB_ALIGN_OFFSET 2
23 #include <sys/types.h>
24 #include <signal.h>
25 #include <getopt.h>
27 #include "system.h"
28 #include "close-stream.h"
29 #include "error.h"
30 #include "fd-reopen.h"
31 #include "gethrxtime.h"
32 #include "human.h"
33 #include "long-options.h"
34 #include "quote.h"
35 #include "quotearg.h"
36 #include "xstrtol.h"
37 #include "xtime.h"
39 /* The official name of this program (e.g., no 'g' prefix). */
40 #define PROGRAM_NAME "dd"
42 #define AUTHORS \
43 proper_name ("Paul Rubin"), \
44 proper_name ("David MacKenzie"), \
45 proper_name ("Stuart Kemp")
47 /* Use SA_NOCLDSTOP as a proxy for whether the sigaction machinery is
48 present. */
49 #ifndef SA_NOCLDSTOP
50 # define SA_NOCLDSTOP 0
51 # define sigprocmask(How, Set, Oset) /* empty */
52 # define sigset_t int
53 # if ! HAVE_SIGINTERRUPT
54 # define siginterrupt(sig, flag) /* empty */
55 # endif
56 #endif
58 /* NonStop circa 2011 lacks SA_RESETHAND; see Bug#9076. */
59 #ifndef SA_RESETHAND
60 # define SA_RESETHAND 0
61 #endif
63 #ifndef SIGINFO
64 # define SIGINFO SIGUSR1
65 #endif
67 /* This may belong in GNULIB's fcntl module instead.
68 Define O_CIO to 0 if it is not supported by this OS. */
69 #ifndef O_CIO
70 # define O_CIO 0
71 #endif
73 /* On AIX 5.1 and AIX 5.2, O_NOCACHE is defined via <fcntl.h>
74 and would interfere with our use of that name, below. */
75 #undef O_NOCACHE
77 #if ! HAVE_FDATASYNC
78 # define fdatasync(fd) (errno = ENOSYS, -1)
79 #endif
81 #define output_char(c) \
82 do \
83 { \
84 obuf[oc++] = (c); \
85 if (oc >= output_blocksize) \
86 write_output (); \
87 } \
88 while (0)
90 /* Default input and output blocksize. */
91 #define DEFAULT_BLOCKSIZE 512
93 /* How many bytes to add to the input and output block sizes before invoking
94 malloc. See dd_copy for details. INPUT_BLOCK_SLOP must be no less than
95 OUTPUT_BLOCK_SLOP. */
96 #define INPUT_BLOCK_SLOP (2 * SWAB_ALIGN_OFFSET + 2 * page_size - 1)
97 #define OUTPUT_BLOCK_SLOP (page_size - 1)
99 /* Maximum blocksize for the given SLOP.
100 Keep it smaller than SIZE_MAX - SLOP, so that we can
101 allocate buffers that size. Keep it smaller than SSIZE_MAX, for
102 the benefit of system calls like "read". And keep it smaller than
103 OFF_T_MAX, for the benefit of the large-offset seek code. */
104 #define MAX_BLOCKSIZE(slop) MIN (SIZE_MAX - (slop), MIN (SSIZE_MAX, OFF_T_MAX))
106 /* Conversions bit masks. */
107 enum
109 C_ASCII = 01,
111 C_EBCDIC = 02,
112 C_IBM = 04,
113 C_BLOCK = 010,
114 C_UNBLOCK = 020,
115 C_LCASE = 040,
116 C_UCASE = 0100,
117 C_SWAB = 0200,
118 C_NOERROR = 0400,
119 C_NOTRUNC = 01000,
120 C_SYNC = 02000,
122 /* Use separate input and output buffers, and combine partial
123 input blocks. */
124 C_TWOBUFS = 04000,
126 C_NOCREAT = 010000,
127 C_EXCL = 020000,
128 C_FDATASYNC = 040000,
129 C_FSYNC = 0100000
132 /* Status bit masks. */
133 enum
135 STATUS_NOXFER = 01
138 /* The name of the input file, or NULL for the standard input. */
139 static char const *input_file = NULL;
141 /* The name of the output file, or NULL for the standard output. */
142 static char const *output_file = NULL;
144 /* The page size on this host. */
145 static size_t page_size;
147 /* The number of bytes in which atomic reads are done. */
148 static size_t input_blocksize = 0;
150 /* The number of bytes in which atomic writes are done. */
151 static size_t output_blocksize = 0;
153 /* Conversion buffer size, in bytes. 0 prevents conversions. */
154 static size_t conversion_blocksize = 0;
156 /* Skip this many records of 'input_blocksize' bytes before input. */
157 static uintmax_t skip_records = 0;
159 /* Skip this many records of 'output_blocksize' bytes before output. */
160 static uintmax_t seek_records = 0;
162 /* Copy only this many records. The default is effectively infinity. */
163 static uintmax_t max_records = (uintmax_t) -1;
165 /* Bit vector of conversions to apply. */
166 static int conversions_mask = 0;
168 /* Open flags for the input and output files. */
169 static int input_flags = 0;
170 static int output_flags = 0;
172 /* Status flags for what is printed to stderr. */
173 static int status_flags = 0;
175 /* If nonzero, filter characters through the translation table. */
176 static bool translation_needed = false;
178 /* Number of partial blocks written. */
179 static uintmax_t w_partial = 0;
181 /* Number of full blocks written. */
182 static uintmax_t w_full = 0;
184 /* Number of partial blocks read. */
185 static uintmax_t r_partial = 0;
187 /* Number of full blocks read. */
188 static uintmax_t r_full = 0;
190 /* Number of bytes written. */
191 static uintmax_t w_bytes = 0;
193 /* Time that dd started. */
194 static xtime_t start_time;
196 /* True if input is seekable. */
197 static bool input_seekable;
199 /* Error number corresponding to initial attempt to lseek input.
200 If ESPIPE, do not issue any more diagnostics about it. */
201 static int input_seek_errno;
203 /* File offset of the input, in bytes, along with a flag recording
204 whether it overflowed. */
205 static uintmax_t input_offset;
206 static bool input_offset_overflow;
208 /* True if a partial read should be diagnosed. */
209 static bool warn_partial_read;
211 /* Records truncated by conv=block. */
212 static uintmax_t r_truncate = 0;
214 /* Output representation of newline and space characters.
215 They change if we're converting to EBCDIC. */
216 static char newline_character = '\n';
217 static char space_character = ' ';
219 /* Output buffer. */
220 static char *obuf;
222 /* Current index into 'obuf'. */
223 static size_t oc = 0;
225 /* Index into current line, for 'conv=block' and 'conv=unblock'. */
226 static size_t col = 0;
228 /* The set of signals that are caught. */
229 static sigset_t caught_signals;
231 /* If nonzero, the value of the pending fatal signal. */
232 static sig_atomic_t volatile interrupt_signal;
234 /* A count of the number of pending info signals that have been received. */
235 static sig_atomic_t volatile info_signal_count;
237 /* Whether to discard cache for input or output. */
238 static bool i_nocache, o_nocache;
240 /* Function used for read (to handle iflag=fullblock parameter). */
241 static ssize_t (*iread_fnc) (int fd, char *buf, size_t size);
243 /* A longest symbol in the struct symbol_values tables below. */
244 #define LONGEST_SYMBOL "fdatasync"
246 /* A symbol and the corresponding integer value. */
247 struct symbol_value
249 char symbol[sizeof LONGEST_SYMBOL];
250 int value;
253 /* Conversion symbols, for conv="...". */
254 static struct symbol_value const conversions[] =
256 {"ascii", C_ASCII | C_TWOBUFS}, /* EBCDIC to ASCII. */
257 {"ebcdic", C_EBCDIC | C_TWOBUFS}, /* ASCII to EBCDIC. */
258 {"ibm", C_IBM | C_TWOBUFS}, /* Slightly different ASCII to EBCDIC. */
259 {"block", C_BLOCK | C_TWOBUFS}, /* Variable to fixed length records. */
260 {"unblock", C_UNBLOCK | C_TWOBUFS}, /* Fixed to variable length records. */
261 {"lcase", C_LCASE | C_TWOBUFS}, /* Translate upper to lower case. */
262 {"ucase", C_UCASE | C_TWOBUFS}, /* Translate lower to upper case. */
263 {"swab", C_SWAB | C_TWOBUFS}, /* Swap bytes of input. */
264 {"noerror", C_NOERROR}, /* Ignore i/o errors. */
265 {"nocreat", C_NOCREAT}, /* Do not create output file. */
266 {"excl", C_EXCL}, /* Fail if the output file already exists. */
267 {"notrunc", C_NOTRUNC}, /* Do not truncate output file. */
268 {"sync", C_SYNC}, /* Pad input records to ibs with NULs. */
269 {"fdatasync", C_FDATASYNC}, /* Synchronize output data before finishing. */
270 {"fsync", C_FSYNC}, /* Also synchronize output metadata. */
271 {"", 0}
274 #define FFS_MASK(x) ((x) ^ ((x) & ((x) - 1)))
275 enum
277 /* Compute a value that's bitwise disjoint from the union
278 of all O_ values. */
279 v = ~(0
280 | O_APPEND
281 | O_BINARY
282 | O_CIO
283 | O_DIRECT
284 | O_DIRECTORY
285 | O_DSYNC
286 | O_NOATIME
287 | O_NOCTTY
288 | O_NOFOLLOW
289 | O_NOLINKS
290 | O_NONBLOCK
291 | O_SYNC
292 | O_TEXT
295 /* Use its lowest bits for private flags. */
296 O_FULLBLOCK = FFS_MASK (v),
297 v2 = v ^ O_FULLBLOCK,
299 O_NOCACHE = FFS_MASK (v2)
302 /* Ensure that we got something. */
303 verify (O_FULLBLOCK != 0);
304 verify (O_NOCACHE != 0);
306 #define MULTIPLE_BITS_SET(i) (((i) & ((i) - 1)) != 0)
308 /* Ensure that this is a single-bit value. */
309 verify ( ! MULTIPLE_BITS_SET (O_FULLBLOCK));
310 verify ( ! MULTIPLE_BITS_SET (O_NOCACHE));
312 /* Flags, for iflag="..." and oflag="...". */
313 static struct symbol_value const flags[] =
315 {"append", O_APPEND},
316 {"binary", O_BINARY},
317 {"cio", O_CIO},
318 {"direct", O_DIRECT},
319 {"directory", O_DIRECTORY},
320 {"dsync", O_DSYNC},
321 {"noatime", O_NOATIME},
322 {"nocache", O_NOCACHE}, /* Discard cache. */
323 {"noctty", O_NOCTTY},
324 {"nofollow", HAVE_WORKING_O_NOFOLLOW ? O_NOFOLLOW : 0},
325 {"nolinks", O_NOLINKS},
326 {"nonblock", O_NONBLOCK},
327 {"sync", O_SYNC},
328 {"text", O_TEXT},
329 {"fullblock", O_FULLBLOCK}, /* Accumulate full blocks from input. */
330 {"", 0}
333 /* Status, for status="...". */
334 static struct symbol_value const statuses[] =
336 {"noxfer", STATUS_NOXFER},
337 {"", 0}
340 /* Translation table formed by applying successive transformations. */
341 static unsigned char trans_table[256];
343 static char const ascii_to_ebcdic[] =
345 '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
346 '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
347 '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
348 '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
349 '\100', '\117', '\177', '\173', '\133', '\154', '\120', '\175',
350 '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
351 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
352 '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
353 '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
354 '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
355 '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
356 '\347', '\350', '\351', '\112', '\340', '\132', '\137', '\155',
357 '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
358 '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
359 '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
360 '\247', '\250', '\251', '\300', '\152', '\320', '\241', '\007',
361 '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
362 '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
363 '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
364 '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
365 '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
366 '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
367 '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
368 '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
369 '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
370 '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',
371 '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',
372 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
373 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
374 '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
375 '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
376 '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
379 static char const ascii_to_ibm[] =
381 '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
382 '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
383 '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
384 '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
385 '\100', '\132', '\177', '\173', '\133', '\154', '\120', '\175',
386 '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
387 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
388 '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
389 '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
390 '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
391 '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
392 '\347', '\350', '\351', '\255', '\340', '\275', '\137', '\155',
393 '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
394 '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
395 '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
396 '\247', '\250', '\251', '\300', '\117', '\320', '\241', '\007',
397 '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
398 '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
399 '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
400 '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
401 '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
402 '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
403 '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
404 '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
405 '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
406 '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',
407 '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',
408 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
409 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
410 '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
411 '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
412 '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
415 static char const ebcdic_to_ascii[] =
417 '\000', '\001', '\002', '\003', '\234', '\011', '\206', '\177',
418 '\227', '\215', '\216', '\013', '\014', '\015', '\016', '\017',
419 '\020', '\021', '\022', '\023', '\235', '\205', '\010', '\207',
420 '\030', '\031', '\222', '\217', '\034', '\035', '\036', '\037',
421 '\200', '\201', '\202', '\203', '\204', '\012', '\027', '\033',
422 '\210', '\211', '\212', '\213', '\214', '\005', '\006', '\007',
423 '\220', '\221', '\026', '\223', '\224', '\225', '\226', '\004',
424 '\230', '\231', '\232', '\233', '\024', '\025', '\236', '\032',
425 '\040', '\240', '\241', '\242', '\243', '\244', '\245', '\246',
426 '\247', '\250', '\133', '\056', '\074', '\050', '\053', '\041',
427 '\046', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
428 '\260', '\261', '\135', '\044', '\052', '\051', '\073', '\136',
429 '\055', '\057', '\262', '\263', '\264', '\265', '\266', '\267',
430 '\270', '\271', '\174', '\054', '\045', '\137', '\076', '\077',
431 '\272', '\273', '\274', '\275', '\276', '\277', '\300', '\301',
432 '\302', '\140', '\072', '\043', '\100', '\047', '\075', '\042',
433 '\303', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
434 '\150', '\151', '\304', '\305', '\306', '\307', '\310', '\311',
435 '\312', '\152', '\153', '\154', '\155', '\156', '\157', '\160',
436 '\161', '\162', '\313', '\314', '\315', '\316', '\317', '\320',
437 '\321', '\176', '\163', '\164', '\165', '\166', '\167', '\170',
438 '\171', '\172', '\322', '\323', '\324', '\325', '\326', '\327',
439 '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
440 '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
441 '\173', '\101', '\102', '\103', '\104', '\105', '\106', '\107',
442 '\110', '\111', '\350', '\351', '\352', '\353', '\354', '\355',
443 '\175', '\112', '\113', '\114', '\115', '\116', '\117', '\120',
444 '\121', '\122', '\356', '\357', '\360', '\361', '\362', '\363',
445 '\134', '\237', '\123', '\124', '\125', '\126', '\127', '\130',
446 '\131', '\132', '\364', '\365', '\366', '\367', '\370', '\371',
447 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
448 '\070', '\071', '\372', '\373', '\374', '\375', '\376', '\377'
451 /* True if we need to close the standard output *stream*. */
452 static bool close_stdout_required = true;
454 /* The only reason to close the standard output *stream* is if
455 parse_long_options fails (as it does for --help or --version).
456 In any other case, dd uses only the STDOUT_FILENO file descriptor,
457 and the "cleanup" function calls "close (STDOUT_FILENO)".
458 Closing the file descriptor and then letting the usual atexit-run
459 close_stdout function call "fclose (stdout)" would result in a
460 harmless failure of the close syscall (with errno EBADF).
461 This function serves solely to avoid the unnecessary close_stdout
462 call, once parse_long_options has succeeded.
463 Meanwhile, we guarantee that the standard error stream is flushed,
464 by inlining the last half of close_stdout as needed. */
465 static void
466 maybe_close_stdout (void)
468 if (close_stdout_required)
469 close_stdout ();
470 else if (close_stream (stderr) != 0)
471 _exit (EXIT_FAILURE);
474 void
475 usage (int status)
477 if (status != EXIT_SUCCESS)
478 emit_try_help ();
479 else
481 printf (_("\
482 Usage: %s [OPERAND]...\n\
483 or: %s OPTION\n\
485 program_name, program_name);
486 fputs (_("\
487 Copy a file, converting and formatting according to the operands.\n\
489 bs=BYTES read and write up to BYTES bytes at a time\n\
490 cbs=BYTES convert BYTES bytes at a time\n\
491 conv=CONVS convert the file as per the comma separated symbol list\n\
492 count=BLOCKS copy only BLOCKS input blocks\n\
493 ibs=BYTES read up to BYTES bytes at a time (default: 512)\n\
494 "), stdout);
495 fputs (_("\
496 if=FILE read from FILE instead of stdin\n\
497 iflag=FLAGS read as per the comma separated symbol list\n\
498 obs=BYTES write BYTES bytes at a time (default: 512)\n\
499 of=FILE write to FILE instead of stdout\n\
500 oflag=FLAGS write as per the comma separated symbol list\n\
501 seek=BLOCKS skip BLOCKS obs-sized blocks at start of output\n\
502 skip=BLOCKS skip BLOCKS ibs-sized blocks at start of input\n\
503 status=noxfer suppress transfer statistics\n\
504 "), stdout);
505 fputs (_("\
507 BLOCKS and BYTES may be followed by the following multiplicative suffixes:\n\
508 c =1, w =2, b =512, kB =1000, K =1024, MB =1000*1000, M =1024*1024, xM =M\n\
509 GB =1000*1000*1000, G =1024*1024*1024, and so on for T, P, E, Z, Y.\n\
511 Each CONV symbol may be:\n\
513 "), stdout);
514 fputs (_("\
515 ascii from EBCDIC to ASCII\n\
516 ebcdic from ASCII to EBCDIC\n\
517 ibm from ASCII to alternate EBCDIC\n\
518 block pad newline-terminated records with spaces to cbs-size\n\
519 unblock replace trailing spaces in cbs-size records with newline\n\
520 lcase change upper case to lower case\n\
521 ucase change lower case to upper case\n\
522 swab swap every pair of input bytes\n\
523 sync pad every input block with NULs to ibs-size; when used\n\
524 with block or unblock, pad with spaces rather than NULs\n\
525 "), stdout);
526 fputs (_("\
527 excl fail if the output file already exists\n\
528 nocreat do not create the output file\n\
529 notrunc do not truncate the output file\n\
530 noerror continue after read errors\n\
531 fdatasync physically write output file data before finishing\n\
532 fsync likewise, but also write metadata\n\
533 "), stdout);
534 fputs (_("\
536 Each FLAG symbol may be:\n\
538 append append mode (makes sense only for output; conv=notrunc suggested)\n\
539 "), stdout);
540 if (O_CIO)
541 fputs (_(" cio use concurrent I/O for data\n"), stdout);
542 if (O_DIRECT)
543 fputs (_(" direct use direct I/O for data\n"), stdout);
544 if (O_DIRECTORY)
545 fputs (_(" directory fail unless a directory\n"), stdout);
546 if (O_DSYNC)
547 fputs (_(" dsync use synchronized I/O for data\n"), stdout);
548 if (O_SYNC)
549 fputs (_(" sync likewise, but also for metadata\n"), stdout);
550 fputs (_(" fullblock accumulate full blocks of input (iflag only)\n"),
551 stdout);
552 if (O_NONBLOCK)
553 fputs (_(" nonblock use non-blocking I/O\n"), stdout);
554 if (O_NOATIME)
555 fputs (_(" noatime do not update access time\n"), stdout);
556 #if HAVE_POSIX_FADVISE
557 if (O_NOCACHE)
558 fputs (_(" nocache discard cached data\n"), stdout);
559 #endif
560 if (O_NOCTTY)
561 fputs (_(" noctty do not assign controlling terminal from file\n"),
562 stdout);
563 if (HAVE_WORKING_O_NOFOLLOW)
564 fputs (_(" nofollow do not follow symlinks\n"), stdout);
565 if (O_NOLINKS)
566 fputs (_(" nolinks fail if multiply-linked\n"), stdout);
567 if (O_BINARY)
568 fputs (_(" binary use binary I/O for data\n"), stdout);
569 if (O_TEXT)
570 fputs (_(" text use text I/O for data\n"), stdout);
573 char const *siginfo_name = (SIGINFO == SIGUSR1 ? "USR1" : "INFO");
574 printf (_("\
576 Sending a %s signal to a running 'dd' process makes it\n\
577 print I/O statistics to standard error and then resume copying.\n\
579 $ dd if=/dev/zero of=/dev/null& pid=$!\n\
580 $ kill -%s $pid; sleep 1; kill $pid\n\
581 18335302+0 records in\n\
582 18335302+0 records out\n\
583 9387674624 bytes (9.4 GB) copied, 34.6279 seconds, 271 MB/s\n\
585 Options are:\n\
588 siginfo_name, siginfo_name);
591 fputs (HELP_OPTION_DESCRIPTION, stdout);
592 fputs (VERSION_OPTION_DESCRIPTION, stdout);
593 emit_ancillary_info ();
595 exit (status);
598 static void
599 translate_charset (char const *new_trans)
601 int i;
603 for (i = 0; i < 256; i++)
604 trans_table[i] = new_trans[trans_table[i]];
605 translation_needed = true;
608 /* Return true if I has more than one bit set. I must be nonnegative. */
610 static inline bool
611 multiple_bits_set (int i)
613 return MULTIPLE_BITS_SET (i);
616 /* Print transfer statistics. */
618 static void
619 print_stats (void)
621 xtime_t now = gethrxtime ();
622 char hbuf[LONGEST_HUMAN_READABLE + 1];
623 int human_opts =
624 (human_autoscale | human_round_to_nearest
625 | human_space_before_unit | human_SI | human_B);
626 double delta_s;
627 char const *bytes_per_second;
629 fprintf (stderr,
630 _("%"PRIuMAX"+%"PRIuMAX" records in\n"
631 "%"PRIuMAX"+%"PRIuMAX" records out\n"),
632 r_full, r_partial, w_full, w_partial);
634 if (r_truncate != 0)
635 fprintf (stderr,
636 ngettext ("%"PRIuMAX" truncated record\n",
637 "%"PRIuMAX" truncated records\n",
638 select_plural (r_truncate)),
639 r_truncate);
641 if (status_flags & STATUS_NOXFER)
642 return;
644 /* Use integer arithmetic to compute the transfer rate,
645 since that makes it easy to use SI abbreviations. */
647 fprintf (stderr,
648 ngettext ("%"PRIuMAX" byte (%s) copied",
649 "%"PRIuMAX" bytes (%s) copied",
650 select_plural (w_bytes)),
651 w_bytes,
652 human_readable (w_bytes, hbuf, human_opts, 1, 1));
654 if (start_time < now)
656 double XTIME_PRECISIONe0 = XTIME_PRECISION;
657 uintmax_t delta_xtime = now;
658 delta_xtime -= start_time;
659 delta_s = delta_xtime / XTIME_PRECISIONe0;
660 bytes_per_second = human_readable (w_bytes, hbuf, human_opts,
661 XTIME_PRECISION, delta_xtime);
663 else
665 delta_s = 0;
666 bytes_per_second = _("Infinity B");
669 /* TRANSLATORS: The two instances of "s" in this string are the SI
670 symbol "s" (meaning second), and should not be translated.
672 This format used to be:
674 ngettext (", %g second, %s/s\n", ", %g seconds, %s/s\n", delta_s == 1)
676 but that was incorrect for languages like Polish. To fix this
677 bug we now use SI symbols even though they're a bit more
678 confusing in English. */
679 fprintf (stderr, _(", %g s, %s/s\n"), delta_s, bytes_per_second);
682 /* An ordinary signal was received; arrange for the program to exit. */
684 static void
685 interrupt_handler (int sig)
687 if (! SA_RESETHAND)
688 signal (sig, SIG_DFL);
689 interrupt_signal = sig;
692 /* An info signal was received; arrange for the program to print status. */
694 static void
695 siginfo_handler (int sig)
697 if (! SA_NOCLDSTOP)
698 signal (sig, siginfo_handler);
699 info_signal_count++;
702 /* Install the signal handlers. */
704 static void
705 install_signal_handlers (void)
707 bool catch_siginfo = ! (SIGINFO == SIGUSR1 && getenv ("POSIXLY_CORRECT"));
709 #if SA_NOCLDSTOP
711 struct sigaction act;
712 sigemptyset (&caught_signals);
713 if (catch_siginfo)
715 sigaction (SIGINFO, NULL, &act);
716 if (act.sa_handler != SIG_IGN)
717 sigaddset (&caught_signals, SIGINFO);
719 sigaction (SIGINT, NULL, &act);
720 if (act.sa_handler != SIG_IGN)
721 sigaddset (&caught_signals, SIGINT);
722 act.sa_mask = caught_signals;
724 if (sigismember (&caught_signals, SIGINFO))
726 act.sa_handler = siginfo_handler;
727 act.sa_flags = 0;
728 sigaction (SIGINFO, &act, NULL);
731 if (sigismember (&caught_signals, SIGINT))
733 act.sa_handler = interrupt_handler;
734 act.sa_flags = SA_NODEFER | SA_RESETHAND;
735 sigaction (SIGINT, &act, NULL);
738 #else
740 if (catch_siginfo && signal (SIGINFO, SIG_IGN) != SIG_IGN)
742 signal (SIGINFO, siginfo_handler);
743 siginterrupt (SIGINFO, 1);
745 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
747 signal (SIGINT, interrupt_handler);
748 siginterrupt (SIGINT, 1);
750 #endif
753 static void
754 cleanup (void)
756 if (close (STDIN_FILENO) < 0)
757 error (EXIT_FAILURE, errno,
758 _("closing input file %s"), quote (input_file));
760 /* Don't remove this call to close, even though close_stdout
761 closes standard output. This close is necessary when cleanup
762 is called as part of a signal handler. */
763 if (close (STDOUT_FILENO) < 0)
764 error (EXIT_FAILURE, errno,
765 _("closing output file %s"), quote (output_file));
768 /* Process any pending signals. If signals are caught, this function
769 should be called periodically. Ideally there should never be an
770 unbounded amount of time when signals are not being processed. */
772 static void
773 process_signals (void)
775 while (interrupt_signal || info_signal_count)
777 int interrupt;
778 int infos;
779 sigset_t oldset;
781 sigprocmask (SIG_BLOCK, &caught_signals, &oldset);
783 /* Reload interrupt_signal and info_signal_count, in case a new
784 signal was handled before sigprocmask took effect. */
785 interrupt = interrupt_signal;
786 infos = info_signal_count;
788 if (infos)
789 info_signal_count = infos - 1;
791 sigprocmask (SIG_SETMASK, &oldset, NULL);
793 if (interrupt)
794 cleanup ();
795 print_stats ();
796 if (interrupt)
797 raise (interrupt);
801 static void ATTRIBUTE_NORETURN
802 quit (int code)
804 cleanup ();
805 print_stats ();
806 process_signals ();
807 exit (code);
810 /* Return LEN rounded down to a multiple of PAGE_SIZE
811 while storing the remainder internally per FD.
812 Pass LEN == 0 to get the current remainder. */
814 static off_t
815 cache_round (int fd, off_t len)
817 static off_t i_pending, o_pending;
818 off_t *pending = (fd == STDIN_FILENO ? &i_pending : &o_pending);
820 if (len)
822 off_t c_pending = *pending + len;
823 *pending = c_pending % page_size;
824 if (c_pending > *pending)
825 len = c_pending - *pending;
826 else
827 len = 0;
829 else
830 len = *pending;
832 return len;
835 /* Discard the cache from the current offset of either
836 STDIN_FILENO or STDOUT_FILENO.
837 Return true on success. */
839 static bool
840 invalidate_cache (int fd, off_t len)
842 int adv_ret = -1;
844 /* Minimize syscalls. */
845 off_t clen = cache_round (fd, len);
846 if (len && !clen)
847 return true; /* Don't advise this time. */
848 if (!len && !clen && max_records)
849 return true; /* Nothing pending. */
850 off_t pending = len ? cache_round (fd, 0) : 0;
852 if (fd == STDIN_FILENO)
854 if (input_seekable)
856 /* Note we're being careful here to only invalidate what
857 we've read, so as not to dump any read ahead cache. */
858 #if HAVE_POSIX_FADVISE
859 adv_ret = posix_fadvise (fd, input_offset - clen - pending, clen,
860 POSIX_FADV_DONTNEED);
861 #else
862 errno = ENOTSUP;
863 #endif
865 else
866 errno = ESPIPE;
868 else if (fd == STDOUT_FILENO)
870 static off_t output_offset = -2;
872 if (output_offset != -1)
874 if (0 > output_offset)
876 output_offset = lseek (fd, 0, SEEK_CUR);
877 output_offset -= clen + pending;
879 if (0 <= output_offset)
881 #if HAVE_POSIX_FADVISE
882 adv_ret = posix_fadvise (fd, output_offset, clen,
883 POSIX_FADV_DONTNEED);
884 #else
885 errno = ENOTSUP;
886 #endif
887 output_offset += clen + pending;
892 return adv_ret != -1 ? true : false;
895 /* Read from FD into the buffer BUF of size SIZE, processing any
896 signals that arrive before bytes are read. Return the number of
897 bytes read if successful, -1 (setting errno) on failure. */
899 static ssize_t
900 iread (int fd, char *buf, size_t size)
902 ssize_t nread;
906 process_signals ();
907 nread = read (fd, buf, size);
909 while (nread < 0 && errno == EINTR);
911 if (0 < nread && warn_partial_read)
913 static ssize_t prev_nread;
915 if (0 < prev_nread && prev_nread < size)
917 uintmax_t prev = prev_nread;
918 error (0, 0, ngettext (("warning: partial read (%"PRIuMAX" byte); "
919 "suggest iflag=fullblock"),
920 ("warning: partial read (%"PRIuMAX" bytes); "
921 "suggest iflag=fullblock"),
922 select_plural (prev)),
923 prev);
924 warn_partial_read = false;
927 prev_nread = nread;
930 return nread;
933 /* Wrapper around iread function to accumulate full blocks. */
934 static ssize_t
935 iread_fullblock (int fd, char *buf, size_t size)
937 ssize_t nread = 0;
939 while (0 < size)
941 ssize_t ncurr = iread (fd, buf, size);
942 if (ncurr < 0)
943 return ncurr;
944 if (ncurr == 0)
945 break;
946 nread += ncurr;
947 buf += ncurr;
948 size -= ncurr;
951 return nread;
954 /* Write to FD the buffer BUF of size SIZE, processing any signals
955 that arrive. Return the number of bytes written, setting errno if
956 this is less than SIZE. Keep trying if there are partial
957 writes. */
959 static size_t
960 iwrite (int fd, char const *buf, size_t size)
962 size_t total_written = 0;
964 if ((output_flags & O_DIRECT) && size < output_blocksize)
966 int old_flags = fcntl (STDOUT_FILENO, F_GETFL);
967 if (fcntl (STDOUT_FILENO, F_SETFL, old_flags & ~O_DIRECT) != 0)
968 error (0, errno, _("failed to turn off O_DIRECT: %s"),
969 quote (output_file));
971 /* Since we have just turned off O_DIRECT for the final write,
972 here we try to preserve some of its semantics. First, use
973 posix_fadvise to tell the system not to pollute the buffer
974 cache with this data. Don't bother to diagnose lseek or
975 posix_fadvise failure. */
976 invalidate_cache (STDOUT_FILENO, 0);
978 /* Attempt to ensure that that final block is committed
979 to disk as quickly as possible. */
980 conversions_mask |= C_FSYNC;
983 while (total_written < size)
985 ssize_t nwritten;
986 process_signals ();
987 nwritten = write (fd, buf + total_written, size - total_written);
988 if (nwritten < 0)
990 if (errno != EINTR)
991 break;
993 else if (nwritten == 0)
995 /* Some buggy drivers return 0 when one tries to write beyond
996 a device's end. (Example: Linux kernel 1.2.13 on /dev/fd0.)
997 Set errno to ENOSPC so they get a sensible diagnostic. */
998 errno = ENOSPC;
999 break;
1001 else
1002 total_written += nwritten;
1005 if (o_nocache && total_written)
1006 invalidate_cache (fd, total_written);
1008 return total_written;
1011 /* Write, then empty, the output buffer 'obuf'. */
1013 static void
1014 write_output (void)
1016 size_t nwritten = iwrite (STDOUT_FILENO, obuf, output_blocksize);
1017 w_bytes += nwritten;
1018 if (nwritten != output_blocksize)
1020 error (0, errno, _("writing to %s"), quote (output_file));
1021 if (nwritten != 0)
1022 w_partial++;
1023 quit (EXIT_FAILURE);
1025 else
1026 w_full++;
1027 oc = 0;
1030 /* Return true if STR is of the form "PATTERN" or "PATTERNDELIM...". */
1032 static bool _GL_ATTRIBUTE_PURE
1033 operand_matches (char const *str, char const *pattern, char delim)
1035 while (*pattern)
1036 if (*str++ != *pattern++)
1037 return false;
1038 return !*str || *str == delim;
1041 /* Interpret one "conv=..." or similar operand STR according to the
1042 symbols in TABLE, returning the flags specified. If the operand
1043 cannot be parsed, use ERROR_MSGID to generate a diagnostic. */
1045 static int
1046 parse_symbols (char const *str, struct symbol_value const *table,
1047 char const *error_msgid)
1049 int value = 0;
1051 while (true)
1053 char const *strcomma = strchr (str, ',');
1054 struct symbol_value const *entry;
1056 for (entry = table;
1057 ! (operand_matches (str, entry->symbol, ',') && entry->value);
1058 entry++)
1060 if (! entry->symbol[0])
1062 size_t slen = strcomma ? strcomma - str : strlen (str);
1063 error (0, 0, "%s: %s", _(error_msgid),
1064 quotearg_n_style_mem (0, locale_quoting_style, str, slen));
1065 usage (EXIT_FAILURE);
1069 value |= entry->value;
1070 if (!strcomma)
1071 break;
1072 str = strcomma + 1;
1075 return value;
1078 /* Return the value of STR, interpreted as a non-negative decimal integer,
1079 optionally multiplied by various values.
1080 Set *INVALID if STR does not represent a number in this format. */
1082 static uintmax_t
1083 parse_integer (const char *str, bool *invalid)
1085 uintmax_t n;
1086 char *suffix;
1087 enum strtol_error e = xstrtoumax (str, &suffix, 10, &n, "bcEGkKMPTwYZ0");
1089 if (e == LONGINT_INVALID_SUFFIX_CHAR && *suffix == 'x')
1091 uintmax_t multiplier = parse_integer (suffix + 1, invalid);
1093 if (multiplier != 0 && n * multiplier / multiplier != n)
1095 *invalid = true;
1096 return 0;
1099 n *= multiplier;
1101 else if (e != LONGINT_OK)
1103 *invalid = true;
1104 return 0;
1107 return n;
1110 /* OPERAND is of the form "X=...". Return true if X is NAME. */
1112 static bool _GL_ATTRIBUTE_PURE
1113 operand_is (char const *operand, char const *name)
1115 return operand_matches (operand, name, '=');
1118 static void
1119 scanargs (int argc, char *const *argv)
1121 int i;
1122 size_t blocksize = 0;
1124 for (i = optind; i < argc; i++)
1126 char const *name = argv[i];
1127 char const *val = strchr (name, '=');
1129 if (val == NULL)
1131 error (0, 0, _("unrecognized operand %s"), quote (name));
1132 usage (EXIT_FAILURE);
1134 val++;
1136 if (operand_is (name, "if"))
1137 input_file = val;
1138 else if (operand_is (name, "of"))
1139 output_file = val;
1140 else if (operand_is (name, "conv"))
1141 conversions_mask |= parse_symbols (val, conversions,
1142 N_("invalid conversion"));
1143 else if (operand_is (name, "iflag"))
1144 input_flags |= parse_symbols (val, flags,
1145 N_("invalid input flag"));
1146 else if (operand_is (name, "oflag"))
1147 output_flags |= parse_symbols (val, flags,
1148 N_("invalid output flag"));
1149 else if (operand_is (name, "status"))
1150 status_flags |= parse_symbols (val, statuses,
1151 N_("invalid status flag"));
1152 else
1154 bool invalid = false;
1155 uintmax_t n = parse_integer (val, &invalid);
1157 if (operand_is (name, "ibs"))
1159 invalid |= ! (0 < n && n <= MAX_BLOCKSIZE (INPUT_BLOCK_SLOP));
1160 input_blocksize = n;
1162 else if (operand_is (name, "obs"))
1164 invalid |= ! (0 < n && n <= MAX_BLOCKSIZE (OUTPUT_BLOCK_SLOP));
1165 output_blocksize = n;
1167 else if (operand_is (name, "bs"))
1169 invalid |= ! (0 < n && n <= MAX_BLOCKSIZE (INPUT_BLOCK_SLOP));
1170 blocksize = n;
1172 else if (operand_is (name, "cbs"))
1174 invalid |= ! (0 < n && n <= SIZE_MAX);
1175 conversion_blocksize = n;
1177 else if (operand_is (name, "skip"))
1178 skip_records = n;
1179 else if (operand_is (name, "seek"))
1180 seek_records = n;
1181 else if (operand_is (name, "count"))
1182 max_records = n;
1183 else
1185 error (0, 0, _("unrecognized operand %s"), quote (name));
1186 usage (EXIT_FAILURE);
1189 if (invalid)
1190 error (EXIT_FAILURE, 0, _("invalid number %s"), quote (val));
1194 if (blocksize)
1195 input_blocksize = output_blocksize = blocksize;
1196 else
1198 /* POSIX says dd aggregates partial reads into
1199 output_blocksize if bs= is not specified. */
1200 conversions_mask |= C_TWOBUFS;
1203 if (input_blocksize == 0)
1204 input_blocksize = DEFAULT_BLOCKSIZE;
1205 if (output_blocksize == 0)
1206 output_blocksize = DEFAULT_BLOCKSIZE;
1207 if (conversion_blocksize == 0)
1208 conversions_mask &= ~(C_BLOCK | C_UNBLOCK);
1210 if (input_flags & (O_DSYNC | O_SYNC))
1211 input_flags |= O_RSYNC;
1213 if (output_flags & O_FULLBLOCK)
1215 error (0, 0, "%s: %s", _("invalid output flag"), "'fullblock'");
1216 usage (EXIT_FAILURE);
1219 /* Warn about partial reads if bs=SIZE is given and iflag=fullblock
1220 is not, and if counting or skipping bytes or using direct I/O.
1221 This helps to avoid confusion with miscounts, and to avoid issues
1222 with direct I/O on GNU/Linux. */
1223 warn_partial_read =
1224 (! (conversions_mask & C_TWOBUFS) && ! (input_flags & O_FULLBLOCK)
1225 && (skip_records
1226 || (0 < max_records && max_records < (uintmax_t) -1)
1227 || (input_flags | output_flags) & O_DIRECT));
1229 iread_fnc = ((input_flags & O_FULLBLOCK)
1230 ? iread_fullblock
1231 : iread);
1232 input_flags &= ~O_FULLBLOCK;
1234 if (multiple_bits_set (conversions_mask & (C_ASCII | C_EBCDIC | C_IBM)))
1235 error (EXIT_FAILURE, 0, _("cannot combine any two of {ascii,ebcdic,ibm}"));
1236 if (multiple_bits_set (conversions_mask & (C_BLOCK | C_UNBLOCK)))
1237 error (EXIT_FAILURE, 0, _("cannot combine block and unblock"));
1238 if (multiple_bits_set (conversions_mask & (C_LCASE | C_UCASE)))
1239 error (EXIT_FAILURE, 0, _("cannot combine lcase and ucase"));
1240 if (multiple_bits_set (conversions_mask & (C_EXCL | C_NOCREAT)))
1241 error (EXIT_FAILURE, 0, _("cannot combine excl and nocreat"));
1242 if (multiple_bits_set (input_flags & (O_DIRECT | O_NOCACHE))
1243 || multiple_bits_set (output_flags & (O_DIRECT | O_NOCACHE)))
1244 error (EXIT_FAILURE, 0, _("cannot combine direct and nocache"));
1246 if (input_flags & O_NOCACHE)
1248 i_nocache = true;
1249 input_flags &= ~O_NOCACHE;
1251 if (output_flags & O_NOCACHE)
1253 o_nocache = true;
1254 output_flags &= ~O_NOCACHE;
1258 /* Fix up translation table. */
1260 static void
1261 apply_translations (void)
1263 int i;
1265 if (conversions_mask & C_ASCII)
1266 translate_charset (ebcdic_to_ascii);
1268 if (conversions_mask & C_UCASE)
1270 for (i = 0; i < 256; i++)
1271 trans_table[i] = toupper (trans_table[i]);
1272 translation_needed = true;
1274 else if (conversions_mask & C_LCASE)
1276 for (i = 0; i < 256; i++)
1277 trans_table[i] = tolower (trans_table[i]);
1278 translation_needed = true;
1281 if (conversions_mask & C_EBCDIC)
1283 translate_charset (ascii_to_ebcdic);
1284 newline_character = ascii_to_ebcdic['\n'];
1285 space_character = ascii_to_ebcdic[' '];
1287 else if (conversions_mask & C_IBM)
1289 translate_charset (ascii_to_ibm);
1290 newline_character = ascii_to_ibm['\n'];
1291 space_character = ascii_to_ibm[' '];
1295 /* Apply the character-set translations specified by the user
1296 to the NREAD bytes in BUF. */
1298 static void
1299 translate_buffer (char *buf, size_t nread)
1301 char *cp;
1302 size_t i;
1304 for (i = nread, cp = buf; i; i--, cp++)
1305 *cp = trans_table[to_uchar (*cp)];
1308 /* If true, the last char from the previous call to 'swab_buffer'
1309 is saved in 'saved_char'. */
1310 static bool char_is_saved = false;
1312 /* Odd char from previous call. */
1313 static char saved_char;
1315 /* Swap NREAD bytes in BUF, plus possibly an initial char from the
1316 previous call. If NREAD is odd, save the last char for the
1317 next call. Return the new start of the BUF buffer. */
1319 static char *
1320 swab_buffer (char *buf, size_t *nread)
1322 char *bufstart = buf;
1323 char *cp;
1324 size_t i;
1326 /* Is a char left from last time? */
1327 if (char_is_saved)
1329 *--bufstart = saved_char;
1330 (*nread)++;
1331 char_is_saved = false;
1334 if (*nread & 1)
1336 /* An odd number of chars are in the buffer. */
1337 saved_char = bufstart[--*nread];
1338 char_is_saved = true;
1341 /* Do the byte-swapping by moving every second character two
1342 positions toward the end, working from the end of the buffer
1343 toward the beginning. This way we only move half of the data. */
1345 cp = bufstart + *nread; /* Start one char past the last. */
1346 for (i = *nread / 2; i; i--, cp -= 2)
1347 *cp = *(cp - 2);
1349 return ++bufstart;
1352 /* Add OFFSET to the input offset, setting the overflow flag if
1353 necessary. */
1355 static void
1356 advance_input_offset (uintmax_t offset)
1358 input_offset += offset;
1359 if (input_offset < offset)
1360 input_offset_overflow = true;
1363 /* This is a wrapper for lseek. It detects and warns about a kernel
1364 bug that makes lseek a no-op for tape devices, even though the kernel
1365 lseek return value suggests that the function succeeded.
1367 The parameters are the same as those of the lseek function, but
1368 with the addition of FILENAME, the name of the file associated with
1369 descriptor FDESC. The file name is used solely in the warning that's
1370 printed when the bug is detected. Return the same value that lseek
1371 would have returned, but when the lseek bug is detected, return -1
1372 to indicate that lseek failed.
1374 The offending behavior has been confirmed with an Exabyte SCSI tape
1375 drive accessed via /dev/nst0 on both Linux 2.2.17 and 2.4.16 kernels. */
1377 #ifdef __linux__
1379 # include <sys/mtio.h>
1381 # define MT_SAME_POSITION(P, Q) \
1382 ((P).mt_resid == (Q).mt_resid \
1383 && (P).mt_fileno == (Q).mt_fileno \
1384 && (P).mt_blkno == (Q).mt_blkno)
1386 static off_t
1387 skip_via_lseek (char const *filename, int fdesc, off_t offset, int whence)
1389 struct mtget s1;
1390 struct mtget s2;
1391 bool got_original_tape_position = (ioctl (fdesc, MTIOCGET, &s1) == 0);
1392 /* known bad device type */
1393 /* && s.mt_type == MT_ISSCSI2 */
1395 off_t new_position = lseek (fdesc, offset, whence);
1396 if (0 <= new_position
1397 && got_original_tape_position
1398 && ioctl (fdesc, MTIOCGET, &s2) == 0
1399 && MT_SAME_POSITION (s1, s2))
1401 error (0, 0, _("warning: working around lseek kernel bug for file (%s)\n\
1402 of mt_type=0x%0lx -- see <sys/mtio.h> for the list of types"),
1403 filename, s2.mt_type);
1404 errno = 0;
1405 new_position = -1;
1408 return new_position;
1410 #else
1411 # define skip_via_lseek(Filename, Fd, Offset, Whence) lseek (Fd, Offset, Whence)
1412 #endif
1414 /* Throw away RECORDS blocks of BLOCKSIZE bytes on file descriptor FDESC,
1415 which is open with read permission for FILE. Store up to BLOCKSIZE
1416 bytes of the data at a time in BUF, if necessary. RECORDS must be
1417 nonzero. If fdesc is STDIN_FILENO, advance the input offset.
1418 Return the number of records remaining, i.e., that were not skipped
1419 because EOF was reached. */
1421 static uintmax_t
1422 skip (int fdesc, char const *file, uintmax_t records, size_t blocksize,
1423 char *buf)
1425 uintmax_t offset = records * blocksize;
1427 /* Try lseek and if an error indicates it was an inappropriate operation --
1428 or if the file offset is not representable as an off_t --
1429 fall back on using read. */
1431 errno = 0;
1432 if (records <= OFF_T_MAX / blocksize
1433 && 0 <= skip_via_lseek (file, fdesc, offset, SEEK_CUR))
1435 if (fdesc == STDIN_FILENO)
1437 struct stat st;
1438 if (fstat (STDIN_FILENO, &st) != 0)
1439 error (EXIT_FAILURE, errno, _("cannot fstat %s"), quote (file));
1440 if (S_ISREG (st.st_mode) && st.st_size < (input_offset + offset))
1442 /* When skipping past EOF, return the number of _full_ blocks
1443 * that are not skipped, and set offset to EOF, so the caller
1444 * can determine the requested skip was not satisfied. */
1445 records = ( offset - st.st_size ) / blocksize;
1446 offset = st.st_size - input_offset;
1448 else
1449 records = 0;
1450 advance_input_offset (offset);
1452 else
1453 records = 0;
1454 return records;
1456 else
1458 int lseek_errno = errno;
1460 /* The seek request may have failed above if it was too big
1461 (> device size, > max file size, etc.)
1462 Or it may not have been done at all (> OFF_T_MAX).
1463 Therefore try to seek to the end of the file,
1464 to avoid redundant reading. */
1465 if ((skip_via_lseek (file, fdesc, 0, SEEK_END)) >= 0)
1467 /* File is seekable, and we're at the end of it, and
1468 size <= OFF_T_MAX. So there's no point using read to advance. */
1470 if (!lseek_errno)
1472 /* The original seek was not attempted as offset > OFF_T_MAX.
1473 We should error for write as can't get to the desired
1474 location, even if OFF_T_MAX < max file size.
1475 For read we're not going to read any data anyway,
1476 so we should error for consistency.
1477 It would be nice to not error for /dev/{zero,null}
1478 for any offset, but that's not a significant issue. */
1479 lseek_errno = EOVERFLOW;
1482 if (fdesc == STDIN_FILENO)
1483 error (0, lseek_errno, _("%s: cannot skip"), quote (file));
1484 else
1485 error (0, lseek_errno, _("%s: cannot seek"), quote (file));
1486 /* If the file has a specific size and we've asked
1487 to skip/seek beyond the max allowable, then quit. */
1488 quit (EXIT_FAILURE);
1490 /* else file_size && offset > OFF_T_MAX or file ! seekable */
1494 ssize_t nread = iread_fnc (fdesc, buf, blocksize);
1495 if (nread < 0)
1497 if (fdesc == STDIN_FILENO)
1499 error (0, errno, _("reading %s"), quote (file));
1500 if (conversions_mask & C_NOERROR)
1502 print_stats ();
1503 continue;
1506 else
1507 error (0, lseek_errno, _("%s: cannot seek"), quote (file));
1508 quit (EXIT_FAILURE);
1511 if (nread == 0)
1512 break;
1513 if (fdesc == STDIN_FILENO)
1514 advance_input_offset (nread);
1516 while (--records != 0);
1518 return records;
1522 /* Advance the input by NBYTES if possible, after a read error.
1523 The input file offset may or may not have advanced after the failed
1524 read; adjust it to point just after the bad record regardless.
1525 Return true if successful, or if the input is already known to not
1526 be seekable. */
1528 static bool
1529 advance_input_after_read_error (size_t nbytes)
1531 if (! input_seekable)
1533 if (input_seek_errno == ESPIPE)
1534 return true;
1535 errno = input_seek_errno;
1537 else
1539 off_t offset;
1540 advance_input_offset (nbytes);
1541 input_offset_overflow |= (OFF_T_MAX < input_offset);
1542 if (input_offset_overflow)
1544 error (0, 0, _("offset overflow while reading file %s"),
1545 quote (input_file));
1546 return false;
1548 offset = lseek (STDIN_FILENO, 0, SEEK_CUR);
1549 if (0 <= offset)
1551 off_t diff;
1552 if (offset == input_offset)
1553 return true;
1554 diff = input_offset - offset;
1555 if (! (0 <= diff && diff <= nbytes))
1556 error (0, 0, _("warning: invalid file offset after failed read"));
1557 if (0 <= skip_via_lseek (input_file, STDIN_FILENO, diff, SEEK_CUR))
1558 return true;
1559 if (errno == 0)
1560 error (0, 0, _("cannot work around kernel bug after all"));
1564 error (0, errno, _("%s: cannot seek"), quote (input_file));
1565 return false;
1568 /* Copy NREAD bytes of BUF, with no conversions. */
1570 static void
1571 copy_simple (char const *buf, size_t nread)
1573 const char *start = buf; /* First uncopied char in BUF. */
1577 size_t nfree = MIN (nread, output_blocksize - oc);
1579 memcpy (obuf + oc, start, nfree);
1581 nread -= nfree; /* Update the number of bytes left to copy. */
1582 start += nfree;
1583 oc += nfree;
1584 if (oc >= output_blocksize)
1585 write_output ();
1587 while (nread != 0);
1590 /* Copy NREAD bytes of BUF, doing conv=block
1591 (pad newline-terminated records to 'conversion_blocksize',
1592 replacing the newline with trailing spaces). */
1594 static void
1595 copy_with_block (char const *buf, size_t nread)
1597 size_t i;
1599 for (i = nread; i; i--, buf++)
1601 if (*buf == newline_character)
1603 if (col < conversion_blocksize)
1605 size_t j;
1606 for (j = col; j < conversion_blocksize; j++)
1607 output_char (space_character);
1609 col = 0;
1611 else
1613 if (col == conversion_blocksize)
1614 r_truncate++;
1615 else if (col < conversion_blocksize)
1616 output_char (*buf);
1617 col++;
1622 /* Copy NREAD bytes of BUF, doing conv=unblock
1623 (replace trailing spaces in 'conversion_blocksize'-sized records
1624 with a newline). */
1626 static void
1627 copy_with_unblock (char const *buf, size_t nread)
1629 size_t i;
1630 char c;
1631 static size_t pending_spaces = 0;
1633 for (i = 0; i < nread; i++)
1635 c = buf[i];
1637 if (col++ >= conversion_blocksize)
1639 col = pending_spaces = 0; /* Wipe out any pending spaces. */
1640 i--; /* Push the char back; get it later. */
1641 output_char (newline_character);
1643 else if (c == space_character)
1644 pending_spaces++;
1645 else
1647 /* 'c' is the character after a run of spaces that were not
1648 at the end of the conversion buffer. Output them. */
1649 while (pending_spaces)
1651 output_char (space_character);
1652 --pending_spaces;
1654 output_char (c);
1659 /* Set the file descriptor flags for FD that correspond to the nonzero bits
1660 in ADD_FLAGS. The file's name is NAME. */
1662 static void
1663 set_fd_flags (int fd, int add_flags, char const *name)
1665 /* Ignore file creation flags that are no-ops on file descriptors. */
1666 add_flags &= ~ (O_NOCTTY | O_NOFOLLOW);
1668 if (add_flags)
1670 int old_flags = fcntl (fd, F_GETFL);
1671 int new_flags = old_flags | add_flags;
1672 bool ok = true;
1673 if (old_flags < 0)
1674 ok = false;
1675 else if (old_flags != new_flags)
1677 if (new_flags & (O_DIRECTORY | O_NOLINKS))
1679 /* NEW_FLAGS contains at least one file creation flag that
1680 requires some checking of the open file descriptor. */
1681 struct stat st;
1682 if (fstat (fd, &st) != 0)
1683 ok = false;
1684 else if ((new_flags & O_DIRECTORY) && ! S_ISDIR (st.st_mode))
1686 errno = ENOTDIR;
1687 ok = false;
1689 else if ((new_flags & O_NOLINKS) && 1 < st.st_nlink)
1691 errno = EMLINK;
1692 ok = false;
1694 new_flags &= ~ (O_DIRECTORY | O_NOLINKS);
1697 if (ok && old_flags != new_flags
1698 && fcntl (fd, F_SETFL, new_flags) == -1)
1699 ok = false;
1702 if (!ok)
1703 error (EXIT_FAILURE, errno, _("setting flags for %s"), quote (name));
1707 static char *
1708 human_size (size_t n)
1710 static char hbuf[LONGEST_HUMAN_READABLE + 1];
1711 int human_opts =
1712 (human_autoscale | human_round_to_nearest | human_base_1024
1713 | human_space_before_unit | human_SI | human_B);
1714 return human_readable (n, hbuf, human_opts, 1, 1);
1717 /* The main loop. */
1719 static int
1720 dd_copy (void)
1722 char *ibuf, *bufstart; /* Input buffer. */
1723 /* These are declared static so that even though we don't free the
1724 buffers, valgrind will recognize that there is no "real" leak. */
1725 static char *real_buf; /* real buffer address before alignment */
1726 static char *real_obuf;
1727 ssize_t nread; /* Bytes read in the current block. */
1729 /* If nonzero, then the previously read block was partial and
1730 PARTREAD was its size. */
1731 size_t partread = 0;
1733 int exit_status = EXIT_SUCCESS;
1734 size_t n_bytes_read;
1736 /* Leave at least one extra byte at the beginning and end of 'ibuf'
1737 for conv=swab, but keep the buffer address even. But some peculiar
1738 device drivers work only with word-aligned buffers, so leave an
1739 extra two bytes. */
1741 /* Some devices require alignment on a sector or page boundary
1742 (e.g. character disk devices). Align the input buffer to a
1743 page boundary to cover all bases. Note that due to the swab
1744 algorithm, we must have at least one byte in the page before
1745 the input buffer; thus we allocate 2 pages of slop in the
1746 real buffer. 8k above the blocksize shouldn't bother anyone.
1748 The page alignment is necessary on any Linux kernel that supports
1749 either the SGI raw I/O patch or Steven Tweedies raw I/O patch.
1750 It is necessary when accessing raw (i.e. character special) disk
1751 devices on Unixware or other SVR4-derived system. */
1753 real_buf = malloc (input_blocksize + INPUT_BLOCK_SLOP);
1754 if (!real_buf)
1755 error (EXIT_FAILURE, 0,
1756 _("memory exhausted by input buffer of size %zu bytes (%s)"),
1757 input_blocksize, human_size (input_blocksize));
1759 ibuf = real_buf;
1760 ibuf += SWAB_ALIGN_OFFSET; /* allow space for swab */
1762 ibuf = ptr_align (ibuf, page_size);
1764 if (conversions_mask & C_TWOBUFS)
1766 /* Page-align the output buffer, too. */
1767 real_obuf = malloc (output_blocksize + OUTPUT_BLOCK_SLOP);
1768 if (!real_obuf)
1769 error (EXIT_FAILURE, 0,
1770 _("memory exhausted by output buffer of size %zu bytes (%s)"),
1771 output_blocksize, human_size (output_blocksize));
1772 obuf = ptr_align (real_obuf, page_size);
1774 else
1776 real_obuf = NULL;
1777 obuf = ibuf;
1780 if (skip_records != 0)
1782 uintmax_t us_bytes = input_offset + (skip_records * input_blocksize);
1783 uintmax_t us_blocks = skip (STDIN_FILENO, input_file,
1784 skip_records, input_blocksize, ibuf);
1785 us_bytes -= input_offset;
1787 /* POSIX doesn't say what to do when dd detects it has been
1788 asked to skip past EOF, so I assume it's non-fatal.
1789 There are 3 reasons why there might be unskipped blocks/bytes:
1790 1. file is too small
1791 2. pipe has not enough data
1792 3. partial reads */
1793 if (us_blocks || (!input_offset_overflow && us_bytes))
1795 error (0, 0,
1796 _("%s: cannot skip to specified offset"), quote (input_file));
1800 if (seek_records != 0)
1802 uintmax_t write_records = skip (STDOUT_FILENO, output_file,
1803 seek_records, output_blocksize, obuf);
1805 if (write_records != 0)
1807 memset (obuf, 0, output_blocksize);
1810 if (iwrite (STDOUT_FILENO, obuf, output_blocksize)
1811 != output_blocksize)
1813 error (0, errno, _("writing to %s"), quote (output_file));
1814 quit (EXIT_FAILURE);
1816 while (--write_records != 0);
1820 if (max_records == 0)
1821 return exit_status;
1823 while (1)
1825 if (r_partial + r_full >= max_records)
1826 break;
1828 /* Zero the buffer before reading, so that if we get a read error,
1829 whatever data we are able to read is followed by zeros.
1830 This minimizes data loss. */
1831 if ((conversions_mask & C_SYNC) && (conversions_mask & C_NOERROR))
1832 memset (ibuf,
1833 (conversions_mask & (C_BLOCK | C_UNBLOCK)) ? ' ' : '\0',
1834 input_blocksize);
1836 nread = iread_fnc (STDIN_FILENO, ibuf, input_blocksize);
1838 if (nread >= 0 && i_nocache)
1839 invalidate_cache (STDIN_FILENO, nread);
1841 if (nread == 0)
1842 break; /* EOF. */
1844 if (nread < 0)
1846 error (0, errno, _("reading %s"), quote (input_file));
1847 if (conversions_mask & C_NOERROR)
1849 print_stats ();
1850 size_t bad_portion = input_blocksize - partread;
1852 /* We already know this data is not cached,
1853 but call this so that correct offsets are maintained. */
1854 invalidate_cache (STDIN_FILENO, bad_portion);
1856 /* Seek past the bad block if possible. */
1857 if (!advance_input_after_read_error (bad_portion))
1859 exit_status = EXIT_FAILURE;
1861 /* Suppress duplicate diagnostics. */
1862 input_seekable = false;
1863 input_seek_errno = ESPIPE;
1865 if ((conversions_mask & C_SYNC) && !partread)
1866 /* Replace the missing input with null bytes and
1867 proceed normally. */
1868 nread = 0;
1869 else
1870 continue;
1872 else
1874 /* Write any partial block. */
1875 exit_status = EXIT_FAILURE;
1876 break;
1880 n_bytes_read = nread;
1881 advance_input_offset (nread);
1883 if (n_bytes_read < input_blocksize)
1885 r_partial++;
1886 partread = n_bytes_read;
1887 if (conversions_mask & C_SYNC)
1889 if (!(conversions_mask & C_NOERROR))
1890 /* If C_NOERROR, we zeroed the block before reading. */
1891 memset (ibuf + n_bytes_read,
1892 (conversions_mask & (C_BLOCK | C_UNBLOCK)) ? ' ' : '\0',
1893 input_blocksize - n_bytes_read);
1894 n_bytes_read = input_blocksize;
1897 else
1899 r_full++;
1900 partread = 0;
1903 if (ibuf == obuf) /* If not C_TWOBUFS. */
1905 size_t nwritten = iwrite (STDOUT_FILENO, obuf, n_bytes_read);
1906 w_bytes += nwritten;
1907 if (nwritten != n_bytes_read)
1909 error (0, errno, _("writing %s"), quote (output_file));
1910 return EXIT_FAILURE;
1912 else if (n_bytes_read == input_blocksize)
1913 w_full++;
1914 else
1915 w_partial++;
1916 continue;
1919 /* Do any translations on the whole buffer at once. */
1921 if (translation_needed)
1922 translate_buffer (ibuf, n_bytes_read);
1924 if (conversions_mask & C_SWAB)
1925 bufstart = swab_buffer (ibuf, &n_bytes_read);
1926 else
1927 bufstart = ibuf;
1929 if (conversions_mask & C_BLOCK)
1930 copy_with_block (bufstart, n_bytes_read);
1931 else if (conversions_mask & C_UNBLOCK)
1932 copy_with_unblock (bufstart, n_bytes_read);
1933 else
1934 copy_simple (bufstart, n_bytes_read);
1937 /* If we have a char left as a result of conv=swab, output it. */
1938 if (char_is_saved)
1940 if (conversions_mask & C_BLOCK)
1941 copy_with_block (&saved_char, 1);
1942 else if (conversions_mask & C_UNBLOCK)
1943 copy_with_unblock (&saved_char, 1);
1944 else
1945 output_char (saved_char);
1948 if ((conversions_mask & C_BLOCK) && col > 0)
1950 /* If the final input line didn't end with a '\n', pad
1951 the output block to 'conversion_blocksize' chars. */
1952 size_t i;
1953 for (i = col; i < conversion_blocksize; i++)
1954 output_char (space_character);
1957 if (col && (conversions_mask & C_UNBLOCK))
1959 /* If there was any output, add a final '\n'. */
1960 output_char (newline_character);
1963 /* Write out the last block. */
1964 if (oc != 0)
1966 size_t nwritten = iwrite (STDOUT_FILENO, obuf, oc);
1967 w_bytes += nwritten;
1968 if (nwritten != 0)
1969 w_partial++;
1970 if (nwritten != oc)
1972 error (0, errno, _("writing %s"), quote (output_file));
1973 return EXIT_FAILURE;
1977 if ((conversions_mask & C_FDATASYNC) && fdatasync (STDOUT_FILENO) != 0)
1979 if (errno != ENOSYS && errno != EINVAL)
1981 error (0, errno, _("fdatasync failed for %s"), quote (output_file));
1982 exit_status = EXIT_FAILURE;
1984 conversions_mask |= C_FSYNC;
1987 if (conversions_mask & C_FSYNC)
1988 while (fsync (STDOUT_FILENO) != 0)
1989 if (errno != EINTR)
1991 error (0, errno, _("fsync failed for %s"), quote (output_file));
1992 return EXIT_FAILURE;
1995 return exit_status;
1999 main (int argc, char **argv)
2001 int i;
2002 int exit_status;
2003 off_t offset;
2005 install_signal_handlers ();
2007 initialize_main (&argc, &argv);
2008 set_program_name (argv[0]);
2009 setlocale (LC_ALL, "");
2010 bindtextdomain (PACKAGE, LOCALEDIR);
2011 textdomain (PACKAGE);
2013 /* Arrange to close stdout if parse_long_options exits. */
2014 atexit (maybe_close_stdout);
2016 page_size = getpagesize ();
2018 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, Version,
2019 usage, AUTHORS, (char const *) NULL);
2020 close_stdout_required = false;
2022 if (getopt_long (argc, argv, "", NULL, NULL) != -1)
2023 usage (EXIT_FAILURE);
2025 /* Initialize translation table to identity translation. */
2026 for (i = 0; i < 256; i++)
2027 trans_table[i] = i;
2029 /* Decode arguments. */
2030 scanargs (argc, argv);
2032 apply_translations ();
2034 if (input_file == NULL)
2036 input_file = _("standard input");
2037 set_fd_flags (STDIN_FILENO, input_flags, input_file);
2039 else
2041 if (fd_reopen (STDIN_FILENO, input_file, O_RDONLY | input_flags, 0) < 0)
2042 error (EXIT_FAILURE, errno, _("opening %s"), quote (input_file));
2045 offset = lseek (STDIN_FILENO, 0, SEEK_CUR);
2046 input_seekable = (0 <= offset);
2047 input_offset = MAX (0, offset);
2048 input_seek_errno = errno;
2050 if (output_file == NULL)
2052 output_file = _("standard output");
2053 set_fd_flags (STDOUT_FILENO, output_flags, output_file);
2055 else
2057 mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
2058 int opts
2059 = (output_flags
2060 | (conversions_mask & C_NOCREAT ? 0 : O_CREAT)
2061 | (conversions_mask & C_EXCL ? O_EXCL : 0)
2062 | (seek_records || (conversions_mask & C_NOTRUNC) ? 0 : O_TRUNC));
2064 /* Open the output file with *read* access only if we might
2065 need to read to satisfy a 'seek=' request. If we can't read
2066 the file, go ahead with write-only access; it might work. */
2067 if ((! seek_records
2068 || fd_reopen (STDOUT_FILENO, output_file, O_RDWR | opts, perms) < 0)
2069 && (fd_reopen (STDOUT_FILENO, output_file, O_WRONLY | opts, perms)
2070 < 0))
2071 error (EXIT_FAILURE, errno, _("opening %s"), quote (output_file));
2073 if (seek_records != 0 && !(conversions_mask & C_NOTRUNC))
2075 uintmax_t size = seek_records * output_blocksize;
2076 unsigned long int obs = output_blocksize;
2078 if (OFF_T_MAX / output_blocksize < seek_records)
2079 error (EXIT_FAILURE, 0,
2080 _("offset too large: "
2081 "cannot truncate to a length of seek=%"PRIuMAX""
2082 " (%lu-byte) blocks"),
2083 seek_records, obs);
2085 if (ftruncate (STDOUT_FILENO, size) != 0)
2087 /* Complain only when ftruncate fails on a regular file, a
2088 directory, or a shared memory object, as POSIX 1003.1-2004
2089 specifies ftruncate's behavior only for these file types.
2090 For example, do not complain when Linux kernel 2.4 ftruncate
2091 fails on /dev/fd0. */
2092 int ftruncate_errno = errno;
2093 struct stat stdout_stat;
2094 if (fstat (STDOUT_FILENO, &stdout_stat) != 0)
2095 error (EXIT_FAILURE, errno, _("cannot fstat %s"),
2096 quote (output_file));
2097 if (S_ISREG (stdout_stat.st_mode)
2098 || S_ISDIR (stdout_stat.st_mode)
2099 || S_TYPEISSHM (&stdout_stat))
2100 error (EXIT_FAILURE, ftruncate_errno,
2101 _("failed to truncate to %"PRIuMAX" bytes"
2102 " in output file %s"),
2103 size, quote (output_file));
2108 start_time = gethrxtime ();
2110 exit_status = dd_copy ();
2112 if (max_records == 0)
2114 /* Special case to invalidate cache to end of file. */
2115 if (i_nocache && !invalidate_cache (STDIN_FILENO, 0))
2117 error (0, errno, _("failed to discard cache for: %s"),
2118 quote (input_file));
2119 exit_status = EXIT_FAILURE;
2121 if (o_nocache && !invalidate_cache (STDOUT_FILENO, 0))
2123 error (0, errno, _("failed to discard cache for: %s"),
2124 quote (output_file));
2125 exit_status = EXIT_FAILURE;
2128 else if (max_records != (uintmax_t) -1)
2130 /* Invalidate any pending region less that page size,
2131 in case the kernel might round up. */
2132 if (i_nocache)
2133 invalidate_cache (STDIN_FILENO, 0);
2134 if (o_nocache)
2135 invalidate_cache (STDOUT_FILENO, 0);
2138 quit (exit_status);