maint: revert "build: update gnulib submodule to latest"
[coreutils/ericb.git] / src / dd.c
blob0824f6c0abd7dd67b4d0c401795c5b3c3ab636af
1 /* dd -- convert a file while copying it.
2 Copyright (C) 1985, 1990-1991, 1995-2011 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 fprintf (stderr, _("Try `%s --help' for more information.\n"),
479 program_name);
480 else
482 printf (_("\
483 Usage: %s [OPERAND]...\n\
484 or: %s OPTION\n\
486 program_name, program_name);
487 fputs (_("\
488 Copy a file, converting and formatting according to the operands.\n\
490 bs=BYTES read and write up to BYTES bytes at a time\n\
491 cbs=BYTES convert BYTES bytes at a time\n\
492 conv=CONVS convert the file as per the comma separated symbol list\n\
493 count=BLOCKS copy only BLOCKS input blocks\n\
494 ibs=BYTES read up to BYTES bytes at a time (default: 512)\n\
495 "), stdout);
496 fputs (_("\
497 if=FILE read from FILE instead of stdin\n\
498 iflag=FLAGS read as per the comma separated symbol list\n\
499 obs=BYTES write BYTES bytes at a time (default: 512)\n\
500 of=FILE write to FILE instead of stdout\n\
501 oflag=FLAGS write as per the comma separated symbol list\n\
502 seek=BLOCKS skip BLOCKS obs-sized blocks at start of output\n\
503 skip=BLOCKS skip BLOCKS ibs-sized blocks at start of input\n\
504 status=noxfer suppress transfer statistics\n\
505 "), stdout);
506 fputs (_("\
508 BLOCKS and BYTES may be followed by the following multiplicative suffixes:\n\
509 c =1, w =2, b =512, kB =1000, K =1024, MB =1000*1000, M =1024*1024, xM =M\n\
510 GB =1000*1000*1000, G =1024*1024*1024, and so on for T, P, E, Z, Y.\n\
512 Each CONV symbol may be:\n\
514 "), stdout);
515 fputs (_("\
516 ascii from EBCDIC to ASCII\n\
517 ebcdic from ASCII to EBCDIC\n\
518 ibm from ASCII to alternate EBCDIC\n\
519 block pad newline-terminated records with spaces to cbs-size\n\
520 unblock replace trailing spaces in cbs-size records with newline\n\
521 lcase change upper case to lower case\n\
522 ucase change lower case to upper case\n\
523 swab swap every pair of input bytes\n\
524 sync pad every input block with NULs to ibs-size; when used\n\
525 with block or unblock, pad with spaces rather than NULs\n\
526 "), stdout);
527 fputs (_("\
528 excl fail if the output file already exists\n\
529 nocreat do not create the output file\n\
530 notrunc do not truncate the output file\n\
531 noerror continue after read errors\n\
532 fdatasync physically write output file data before finishing\n\
533 fsync likewise, but also write metadata\n\
534 "), stdout);
535 fputs (_("\
537 Each FLAG symbol may be:\n\
539 append append mode (makes sense only for output; conv=notrunc suggested)\n\
540 "), stdout);
541 if (O_CIO)
542 fputs (_(" cio use concurrent I/O for data\n"), stdout);
543 if (O_DIRECT)
544 fputs (_(" direct use direct I/O for data\n"), stdout);
545 if (O_DIRECTORY)
546 fputs (_(" directory fail unless a directory\n"), stdout);
547 if (O_DSYNC)
548 fputs (_(" dsync use synchronized I/O for data\n"), stdout);
549 if (O_SYNC)
550 fputs (_(" sync likewise, but also for metadata\n"), stdout);
551 fputs (_(" fullblock accumulate full blocks of input (iflag only)\n"),
552 stdout);
553 if (O_NONBLOCK)
554 fputs (_(" nonblock use non-blocking I/O\n"), stdout);
555 if (O_NOATIME)
556 fputs (_(" noatime do not update access time\n"), stdout);
557 #if HAVE_POSIX_FADVISE
558 if (O_NOCACHE)
559 fputs (_(" nocache discard cached data\n"), stdout);
560 #endif
561 if (O_NOCTTY)
562 fputs (_(" noctty do not assign controlling terminal from file\n"),
563 stdout);
564 if (HAVE_WORKING_O_NOFOLLOW)
565 fputs (_(" nofollow do not follow symlinks\n"), stdout);
566 if (O_NOLINKS)
567 fputs (_(" nolinks fail if multiply-linked\n"), stdout);
568 if (O_BINARY)
569 fputs (_(" binary use binary I/O for data\n"), stdout);
570 if (O_TEXT)
571 fputs (_(" text use text I/O for data\n"), stdout);
574 char const *siginfo_name = (SIGINFO == SIGUSR1 ? "USR1" : "INFO");
575 printf (_("\
577 Sending a %s signal to a running `dd' process makes it\n\
578 print I/O statistics to standard error and then resume copying.\n\
580 $ dd if=/dev/zero of=/dev/null& pid=$!\n\
581 $ kill -%s $pid; sleep 1; kill $pid\n\
582 18335302+0 records in\n\
583 18335302+0 records out\n\
584 9387674624 bytes (9.4 GB) copied, 34.6279 seconds, 271 MB/s\n\
586 Options are:\n\
589 siginfo_name, siginfo_name);
592 fputs (HELP_OPTION_DESCRIPTION, stdout);
593 fputs (VERSION_OPTION_DESCRIPTION, stdout);
594 emit_ancillary_info ();
596 exit (status);
599 static void
600 translate_charset (char const *new_trans)
602 int i;
604 for (i = 0; i < 256; i++)
605 trans_table[i] = new_trans[trans_table[i]];
606 translation_needed = true;
609 /* Return true if I has more than one bit set. I must be nonnegative. */
611 static inline bool
612 multiple_bits_set (int i)
614 return MULTIPLE_BITS_SET (i);
617 /* Print transfer statistics. */
619 static void
620 print_stats (void)
622 xtime_t now = gethrxtime ();
623 char hbuf[LONGEST_HUMAN_READABLE + 1];
624 int human_opts =
625 (human_autoscale | human_round_to_nearest
626 | human_space_before_unit | human_SI | human_B);
627 double delta_s;
628 char const *bytes_per_second;
630 fprintf (stderr,
631 _("%"PRIuMAX"+%"PRIuMAX" records in\n"
632 "%"PRIuMAX"+%"PRIuMAX" records out\n"),
633 r_full, r_partial, w_full, w_partial);
635 if (r_truncate != 0)
636 fprintf (stderr,
637 ngettext ("%"PRIuMAX" truncated record\n",
638 "%"PRIuMAX" truncated records\n",
639 select_plural (r_truncate)),
640 r_truncate);
642 if (status_flags & STATUS_NOXFER)
643 return;
645 /* Use integer arithmetic to compute the transfer rate,
646 since that makes it easy to use SI abbreviations. */
648 fprintf (stderr,
649 ngettext ("%"PRIuMAX" byte (%s) copied",
650 "%"PRIuMAX" bytes (%s) copied",
651 select_plural (w_bytes)),
652 w_bytes,
653 human_readable (w_bytes, hbuf, human_opts, 1, 1));
655 if (start_time < now)
657 double XTIME_PRECISIONe0 = XTIME_PRECISION;
658 uintmax_t delta_xtime = now;
659 delta_xtime -= start_time;
660 delta_s = delta_xtime / XTIME_PRECISIONe0;
661 bytes_per_second = human_readable (w_bytes, hbuf, human_opts,
662 XTIME_PRECISION, delta_xtime);
664 else
666 delta_s = 0;
667 bytes_per_second = _("Infinity B");
670 /* TRANSLATORS: The two instances of "s" in this string are the SI
671 symbol "s" (meaning second), and should not be translated.
673 This format used to be:
675 ngettext (", %g second, %s/s\n", ", %g seconds, %s/s\n", delta_s == 1)
677 but that was incorrect for languages like Polish. To fix this
678 bug we now use SI symbols even though they're a bit more
679 confusing in English. */
680 fprintf (stderr, _(", %g s, %s/s\n"), delta_s, bytes_per_second);
683 /* An ordinary signal was received; arrange for the program to exit. */
685 static void
686 interrupt_handler (int sig)
688 if (! SA_RESETHAND)
689 signal (sig, SIG_DFL);
690 interrupt_signal = sig;
693 /* An info signal was received; arrange for the program to print status. */
695 static void
696 siginfo_handler (int sig)
698 if (! SA_NOCLDSTOP)
699 signal (sig, siginfo_handler);
700 info_signal_count++;
703 /* Install the signal handlers. */
705 static void
706 install_signal_handlers (void)
708 bool catch_siginfo = ! (SIGINFO == SIGUSR1 && getenv ("POSIXLY_CORRECT"));
710 #if SA_NOCLDSTOP
712 struct sigaction act;
713 sigemptyset (&caught_signals);
714 if (catch_siginfo)
716 sigaction (SIGINFO, NULL, &act);
717 if (act.sa_handler != SIG_IGN)
718 sigaddset (&caught_signals, SIGINFO);
720 sigaction (SIGINT, NULL, &act);
721 if (act.sa_handler != SIG_IGN)
722 sigaddset (&caught_signals, SIGINT);
723 act.sa_mask = caught_signals;
725 if (sigismember (&caught_signals, SIGINFO))
727 act.sa_handler = siginfo_handler;
728 act.sa_flags = 0;
729 sigaction (SIGINFO, &act, NULL);
732 if (sigismember (&caught_signals, SIGINT))
734 act.sa_handler = interrupt_handler;
735 act.sa_flags = SA_NODEFER | SA_RESETHAND;
736 sigaction (SIGINT, &act, NULL);
739 #else
741 if (catch_siginfo && signal (SIGINFO, SIG_IGN) != SIG_IGN)
743 signal (SIGINFO, siginfo_handler);
744 siginterrupt (SIGINFO, 1);
746 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
748 signal (SIGINT, interrupt_handler);
749 siginterrupt (SIGINT, 1);
751 #endif
754 static void
755 cleanup (void)
757 if (close (STDIN_FILENO) < 0)
758 error (EXIT_FAILURE, errno,
759 _("closing input file %s"), quote (input_file));
761 /* Don't remove this call to close, even though close_stdout
762 closes standard output. This close is necessary when cleanup
763 is called as part of a signal handler. */
764 if (close (STDOUT_FILENO) < 0)
765 error (EXIT_FAILURE, errno,
766 _("closing output file %s"), quote (output_file));
769 /* Process any pending signals. If signals are caught, this function
770 should be called periodically. Ideally there should never be an
771 unbounded amount of time when signals are not being processed. */
773 static void
774 process_signals (void)
776 while (interrupt_signal || info_signal_count)
778 int interrupt;
779 int infos;
780 sigset_t oldset;
782 sigprocmask (SIG_BLOCK, &caught_signals, &oldset);
784 /* Reload interrupt_signal and info_signal_count, in case a new
785 signal was handled before sigprocmask took effect. */
786 interrupt = interrupt_signal;
787 infos = info_signal_count;
789 if (infos)
790 info_signal_count = infos - 1;
792 sigprocmask (SIG_SETMASK, &oldset, NULL);
794 if (interrupt)
795 cleanup ();
796 print_stats ();
797 if (interrupt)
798 raise (interrupt);
802 static void ATTRIBUTE_NORETURN
803 quit (int code)
805 cleanup ();
806 print_stats ();
807 process_signals ();
808 exit (code);
811 /* Return LEN rounded down to a multiple of PAGE_SIZE
812 while storing the remainder internally per FD.
813 Pass LEN == 0 to get the current remainder. */
815 static off_t
816 cache_round (int fd, off_t len)
818 static off_t i_pending, o_pending;
819 off_t *pending = (fd == STDIN_FILENO ? &i_pending : &o_pending);
821 if (len)
823 off_t c_pending = *pending + len;
824 *pending = c_pending % page_size;
825 if (c_pending > *pending)
826 len = c_pending - *pending;
827 else
828 len = 0;
830 else
831 len = *pending;
833 return len;
836 /* Discard the cache from the current offset of either
837 STDIN_FILENO or STDOUT_FILENO.
838 Return true on success. */
840 static bool
841 invalidate_cache (int fd, off_t len)
843 int adv_ret = -1;
845 /* Minimize syscalls. */
846 off_t clen = cache_round (fd, len);
847 if (len && !clen)
848 return true; /* Don't advise this time. */
849 if (!len && !clen && max_records)
850 return true; /* Nothing pending. */
851 off_t pending = len ? cache_round (fd, 0) : 0;
853 if (fd == STDIN_FILENO)
855 if (input_seekable)
857 /* Note we're being careful here to only invalidate what
858 we've read, so as not to dump any read ahead cache. */
859 #if HAVE_POSIX_FADVISE
860 adv_ret = posix_fadvise (fd, input_offset - clen - pending, clen,
861 POSIX_FADV_DONTNEED);
862 #else
863 errno = ENOTSUP;
864 #endif
866 else
867 errno = ESPIPE;
869 else if (fd == STDOUT_FILENO)
871 static off_t output_offset = -2;
873 if (output_offset != -1)
875 if (0 > output_offset)
877 output_offset = lseek (fd, 0, SEEK_CUR);
878 output_offset -= clen + pending;
880 if (0 <= output_offset)
882 #if HAVE_POSIX_FADVISE
883 adv_ret = posix_fadvise (fd, output_offset, clen,
884 POSIX_FADV_DONTNEED);
885 #else
886 errno = ENOTSUP;
887 #endif
888 output_offset += clen + pending;
893 return adv_ret != -1 ? true : false;
896 /* Read from FD into the buffer BUF of size SIZE, processing any
897 signals that arrive before bytes are read. Return the number of
898 bytes read if successful, -1 (setting errno) on failure. */
900 static ssize_t
901 iread (int fd, char *buf, size_t size)
903 ssize_t nread;
907 process_signals ();
908 nread = read (fd, buf, size);
910 while (nread < 0 && errno == EINTR);
912 if (0 < nread && warn_partial_read)
914 static ssize_t prev_nread;
916 if (0 < prev_nread && prev_nread < size)
918 uintmax_t prev = prev_nread;
919 error (0, 0, ngettext (("warning: partial read (%"PRIuMAX" byte); "
920 "suggest iflag=fullblock"),
921 ("warning: partial read (%"PRIuMAX" bytes); "
922 "suggest iflag=fullblock"),
923 select_plural (prev)),
924 prev);
925 warn_partial_read = false;
928 prev_nread = nread;
931 return nread;
934 /* Wrapper around iread function to accumulate full blocks. */
935 static ssize_t
936 iread_fullblock (int fd, char *buf, size_t size)
938 ssize_t nread = 0;
940 while (0 < size)
942 ssize_t ncurr = iread (fd, buf, size);
943 if (ncurr < 0)
944 return ncurr;
945 if (ncurr == 0)
946 break;
947 nread += ncurr;
948 buf += ncurr;
949 size -= ncurr;
952 return nread;
955 /* Write to FD the buffer BUF of size SIZE, processing any signals
956 that arrive. Return the number of bytes written, setting errno if
957 this is less than SIZE. Keep trying if there are partial
958 writes. */
960 static size_t
961 iwrite (int fd, char const *buf, size_t size)
963 size_t total_written = 0;
965 if ((output_flags & O_DIRECT) && size < output_blocksize)
967 int old_flags = fcntl (STDOUT_FILENO, F_GETFL);
968 if (fcntl (STDOUT_FILENO, F_SETFL, old_flags & ~O_DIRECT) != 0)
969 error (0, errno, _("failed to turn off O_DIRECT: %s"),
970 quote (output_file));
972 /* Since we have just turned off O_DIRECT for the final write,
973 here we try to preserve some of its semantics. First, use
974 posix_fadvise to tell the system not to pollute the buffer
975 cache with this data. Don't bother to diagnose lseek or
976 posix_fadvise failure. */
977 invalidate_cache (STDOUT_FILENO, 0);
979 /* Attempt to ensure that that final block is committed
980 to disk as quickly as possible. */
981 conversions_mask |= C_FSYNC;
984 while (total_written < size)
986 ssize_t nwritten;
987 process_signals ();
988 nwritten = write (fd, buf + total_written, size - total_written);
989 if (nwritten < 0)
991 if (errno != EINTR)
992 break;
994 else if (nwritten == 0)
996 /* Some buggy drivers return 0 when one tries to write beyond
997 a device's end. (Example: Linux kernel 1.2.13 on /dev/fd0.)
998 Set errno to ENOSPC so they get a sensible diagnostic. */
999 errno = ENOSPC;
1000 break;
1002 else
1003 total_written += nwritten;
1006 if (o_nocache && total_written)
1007 invalidate_cache (fd, total_written);
1009 return total_written;
1012 /* Write, then empty, the output buffer `obuf'. */
1014 static void
1015 write_output (void)
1017 size_t nwritten = iwrite (STDOUT_FILENO, obuf, output_blocksize);
1018 w_bytes += nwritten;
1019 if (nwritten != output_blocksize)
1021 error (0, errno, _("writing to %s"), quote (output_file));
1022 if (nwritten != 0)
1023 w_partial++;
1024 quit (EXIT_FAILURE);
1026 else
1027 w_full++;
1028 oc = 0;
1031 /* Return true if STR is of the form "PATTERN" or "PATTERNDELIM...". */
1033 static bool _GL_ATTRIBUTE_PURE
1034 operand_matches (char const *str, char const *pattern, char delim)
1036 while (*pattern)
1037 if (*str++ != *pattern++)
1038 return false;
1039 return !*str || *str == delim;
1042 /* Interpret one "conv=..." or similar operand STR according to the
1043 symbols in TABLE, returning the flags specified. If the operand
1044 cannot be parsed, use ERROR_MSGID to generate a diagnostic. */
1046 static int
1047 parse_symbols (char const *str, struct symbol_value const *table,
1048 char const *error_msgid)
1050 int value = 0;
1052 while (true)
1054 char const *strcomma = strchr (str, ',');
1055 struct symbol_value const *entry;
1057 for (entry = table;
1058 ! (operand_matches (str, entry->symbol, ',') && entry->value);
1059 entry++)
1061 if (! entry->symbol[0])
1063 size_t slen = strcomma ? strcomma - str : strlen (str);
1064 error (0, 0, "%s: %s", _(error_msgid),
1065 quotearg_n_style_mem (0, locale_quoting_style, str, slen));
1066 usage (EXIT_FAILURE);
1070 value |= entry->value;
1071 if (!strcomma)
1072 break;
1073 str = strcomma + 1;
1076 return value;
1079 /* Return the value of STR, interpreted as a non-negative decimal integer,
1080 optionally multiplied by various values.
1081 Set *INVALID if STR does not represent a number in this format. */
1083 static uintmax_t
1084 parse_integer (const char *str, bool *invalid)
1086 uintmax_t n;
1087 char *suffix;
1088 enum strtol_error e = xstrtoumax (str, &suffix, 10, &n, "bcEGkKMPTwYZ0");
1090 if (e == LONGINT_INVALID_SUFFIX_CHAR && *suffix == 'x')
1092 uintmax_t multiplier = parse_integer (suffix + 1, invalid);
1094 if (multiplier != 0 && n * multiplier / multiplier != n)
1096 *invalid = true;
1097 return 0;
1100 n *= multiplier;
1102 else if (e != LONGINT_OK)
1104 *invalid = true;
1105 return 0;
1108 return n;
1111 /* OPERAND is of the form "X=...". Return true if X is NAME. */
1113 static bool _GL_ATTRIBUTE_PURE
1114 operand_is (char const *operand, char const *name)
1116 return operand_matches (operand, name, '=');
1119 static void
1120 scanargs (int argc, char *const *argv)
1122 int i;
1123 size_t blocksize = 0;
1125 for (i = optind; i < argc; i++)
1127 char const *name = argv[i];
1128 char const *val = strchr (name, '=');
1130 if (val == NULL)
1132 error (0, 0, _("unrecognized operand %s"), quote (name));
1133 usage (EXIT_FAILURE);
1135 val++;
1137 if (operand_is (name, "if"))
1138 input_file = val;
1139 else if (operand_is (name, "of"))
1140 output_file = val;
1141 else if (operand_is (name, "conv"))
1142 conversions_mask |= parse_symbols (val, conversions,
1143 N_("invalid conversion"));
1144 else if (operand_is (name, "iflag"))
1145 input_flags |= parse_symbols (val, flags,
1146 N_("invalid input flag"));
1147 else if (operand_is (name, "oflag"))
1148 output_flags |= parse_symbols (val, flags,
1149 N_("invalid output flag"));
1150 else if (operand_is (name, "status"))
1151 status_flags |= parse_symbols (val, statuses,
1152 N_("invalid status flag"));
1153 else
1155 bool invalid = false;
1156 uintmax_t n = parse_integer (val, &invalid);
1158 if (operand_is (name, "ibs"))
1160 invalid |= ! (0 < n && n <= MAX_BLOCKSIZE (INPUT_BLOCK_SLOP));
1161 input_blocksize = n;
1163 else if (operand_is (name, "obs"))
1165 invalid |= ! (0 < n && n <= MAX_BLOCKSIZE (OUTPUT_BLOCK_SLOP));
1166 output_blocksize = n;
1168 else if (operand_is (name, "bs"))
1170 invalid |= ! (0 < n && n <= MAX_BLOCKSIZE (INPUT_BLOCK_SLOP));
1171 blocksize = n;
1173 else if (operand_is (name, "cbs"))
1175 invalid |= ! (0 < n && n <= SIZE_MAX);
1176 conversion_blocksize = n;
1178 else if (operand_is (name, "skip"))
1179 skip_records = n;
1180 else if (operand_is (name, "seek"))
1181 seek_records = n;
1182 else if (operand_is (name, "count"))
1183 max_records = n;
1184 else
1186 error (0, 0, _("unrecognized operand %s"), quote (name));
1187 usage (EXIT_FAILURE);
1190 if (invalid)
1191 error (EXIT_FAILURE, 0, _("invalid number %s"), quote (val));
1195 if (blocksize)
1196 input_blocksize = output_blocksize = blocksize;
1197 else
1199 /* POSIX says dd aggregates partial reads into
1200 output_blocksize if bs= is not specified. */
1201 conversions_mask |= C_TWOBUFS;
1204 if (input_blocksize == 0)
1205 input_blocksize = DEFAULT_BLOCKSIZE;
1206 if (output_blocksize == 0)
1207 output_blocksize = DEFAULT_BLOCKSIZE;
1208 if (conversion_blocksize == 0)
1209 conversions_mask &= ~(C_BLOCK | C_UNBLOCK);
1211 if (input_flags & (O_DSYNC | O_SYNC))
1212 input_flags |= O_RSYNC;
1214 if (output_flags & O_FULLBLOCK)
1216 error (0, 0, "%s: %s", _("invalid output flag"), "'fullblock'");
1217 usage (EXIT_FAILURE);
1220 /* Warn about partial reads if bs=SIZE is given and iflag=fullblock
1221 is not, and if counting or skipping bytes or using direct I/O.
1222 This helps to avoid confusion with miscounts, and to avoid issues
1223 with direct I/O on GNU/Linux. */
1224 warn_partial_read =
1225 (! (conversions_mask & C_TWOBUFS) && ! (input_flags & O_FULLBLOCK)
1226 && (skip_records
1227 || (0 < max_records && max_records < (uintmax_t) -1)
1228 || (input_flags | output_flags) & O_DIRECT));
1230 iread_fnc = ((input_flags & O_FULLBLOCK)
1231 ? iread_fullblock
1232 : iread);
1233 input_flags &= ~O_FULLBLOCK;
1235 if (multiple_bits_set (conversions_mask & (C_ASCII | C_EBCDIC | C_IBM)))
1236 error (EXIT_FAILURE, 0, _("cannot combine any two of {ascii,ebcdic,ibm}"));
1237 if (multiple_bits_set (conversions_mask & (C_BLOCK | C_UNBLOCK)))
1238 error (EXIT_FAILURE, 0, _("cannot combine block and unblock"));
1239 if (multiple_bits_set (conversions_mask & (C_LCASE | C_UCASE)))
1240 error (EXIT_FAILURE, 0, _("cannot combine lcase and ucase"));
1241 if (multiple_bits_set (conversions_mask & (C_EXCL | C_NOCREAT)))
1242 error (EXIT_FAILURE, 0, _("cannot combine excl and nocreat"));
1243 if (multiple_bits_set (input_flags & (O_DIRECT | O_NOCACHE))
1244 || multiple_bits_set (output_flags & (O_DIRECT | O_NOCACHE)))
1245 error (EXIT_FAILURE, 0, _("cannot combine direct and nocache"));
1247 if (input_flags & O_NOCACHE)
1249 i_nocache = true;
1250 input_flags &= ~O_NOCACHE;
1252 if (output_flags & O_NOCACHE)
1254 o_nocache = true;
1255 output_flags &= ~O_NOCACHE;
1259 /* Fix up translation table. */
1261 static void
1262 apply_translations (void)
1264 int i;
1266 if (conversions_mask & C_ASCII)
1267 translate_charset (ebcdic_to_ascii);
1269 if (conversions_mask & C_UCASE)
1271 for (i = 0; i < 256; i++)
1272 trans_table[i] = toupper (trans_table[i]);
1273 translation_needed = true;
1275 else if (conversions_mask & C_LCASE)
1277 for (i = 0; i < 256; i++)
1278 trans_table[i] = tolower (trans_table[i]);
1279 translation_needed = true;
1282 if (conversions_mask & C_EBCDIC)
1284 translate_charset (ascii_to_ebcdic);
1285 newline_character = ascii_to_ebcdic['\n'];
1286 space_character = ascii_to_ebcdic[' '];
1288 else if (conversions_mask & C_IBM)
1290 translate_charset (ascii_to_ibm);
1291 newline_character = ascii_to_ibm['\n'];
1292 space_character = ascii_to_ibm[' '];
1296 /* Apply the character-set translations specified by the user
1297 to the NREAD bytes in BUF. */
1299 static void
1300 translate_buffer (char *buf, size_t nread)
1302 char *cp;
1303 size_t i;
1305 for (i = nread, cp = buf; i; i--, cp++)
1306 *cp = trans_table[to_uchar (*cp)];
1309 /* If true, the last char from the previous call to `swab_buffer'
1310 is saved in `saved_char'. */
1311 static bool char_is_saved = false;
1313 /* Odd char from previous call. */
1314 static char saved_char;
1316 /* Swap NREAD bytes in BUF, plus possibly an initial char from the
1317 previous call. If NREAD is odd, save the last char for the
1318 next call. Return the new start of the BUF buffer. */
1320 static char *
1321 swab_buffer (char *buf, size_t *nread)
1323 char *bufstart = buf;
1324 char *cp;
1325 size_t i;
1327 /* Is a char left from last time? */
1328 if (char_is_saved)
1330 *--bufstart = saved_char;
1331 (*nread)++;
1332 char_is_saved = false;
1335 if (*nread & 1)
1337 /* An odd number of chars are in the buffer. */
1338 saved_char = bufstart[--*nread];
1339 char_is_saved = true;
1342 /* Do the byte-swapping by moving every second character two
1343 positions toward the end, working from the end of the buffer
1344 toward the beginning. This way we only move half of the data. */
1346 cp = bufstart + *nread; /* Start one char past the last. */
1347 for (i = *nread / 2; i; i--, cp -= 2)
1348 *cp = *(cp - 2);
1350 return ++bufstart;
1353 /* Add OFFSET to the input offset, setting the overflow flag if
1354 necessary. */
1356 static void
1357 advance_input_offset (uintmax_t offset)
1359 input_offset += offset;
1360 if (input_offset < offset)
1361 input_offset_overflow = true;
1364 /* This is a wrapper for lseek. It detects and warns about a kernel
1365 bug that makes lseek a no-op for tape devices, even though the kernel
1366 lseek return value suggests that the function succeeded.
1368 The parameters are the same as those of the lseek function, but
1369 with the addition of FILENAME, the name of the file associated with
1370 descriptor FDESC. The file name is used solely in the warning that's
1371 printed when the bug is detected. Return the same value that lseek
1372 would have returned, but when the lseek bug is detected, return -1
1373 to indicate that lseek failed.
1375 The offending behavior has been confirmed with an Exabyte SCSI tape
1376 drive accessed via /dev/nst0 on both Linux 2.2.17 and 2.4.16 kernels. */
1378 #ifdef __linux__
1380 # include <sys/mtio.h>
1382 # define MT_SAME_POSITION(P, Q) \
1383 ((P).mt_resid == (Q).mt_resid \
1384 && (P).mt_fileno == (Q).mt_fileno \
1385 && (P).mt_blkno == (Q).mt_blkno)
1387 static off_t
1388 skip_via_lseek (char const *filename, int fdesc, off_t offset, int whence)
1390 struct mtget s1;
1391 struct mtget s2;
1392 bool got_original_tape_position = (ioctl (fdesc, MTIOCGET, &s1) == 0);
1393 /* known bad device type */
1394 /* && s.mt_type == MT_ISSCSI2 */
1396 off_t new_position = lseek (fdesc, offset, whence);
1397 if (0 <= new_position
1398 && got_original_tape_position
1399 && ioctl (fdesc, MTIOCGET, &s2) == 0
1400 && MT_SAME_POSITION (s1, s2))
1402 error (0, 0, _("warning: working around lseek kernel bug for file (%s)\n\
1403 of mt_type=0x%0lx -- see <sys/mtio.h> for the list of types"),
1404 filename, s2.mt_type);
1405 errno = 0;
1406 new_position = -1;
1409 return new_position;
1411 #else
1412 # define skip_via_lseek(Filename, Fd, Offset, Whence) lseek (Fd, Offset, Whence)
1413 #endif
1415 /* Throw away RECORDS blocks of BLOCKSIZE bytes on file descriptor FDESC,
1416 which is open with read permission for FILE. Store up to BLOCKSIZE
1417 bytes of the data at a time in BUF, if necessary. RECORDS must be
1418 nonzero. If fdesc is STDIN_FILENO, advance the input offset.
1419 Return the number of records remaining, i.e., that were not skipped
1420 because EOF was reached. */
1422 static uintmax_t
1423 skip (int fdesc, char const *file, uintmax_t records, size_t blocksize,
1424 char *buf)
1426 uintmax_t offset = records * blocksize;
1428 /* Try lseek and if an error indicates it was an inappropriate operation --
1429 or if the file offset is not representable as an off_t --
1430 fall back on using read. */
1432 errno = 0;
1433 if (records <= OFF_T_MAX / blocksize
1434 && 0 <= skip_via_lseek (file, fdesc, offset, SEEK_CUR))
1436 if (fdesc == STDIN_FILENO)
1438 struct stat st;
1439 if (fstat (STDIN_FILENO, &st) != 0)
1440 error (EXIT_FAILURE, errno, _("cannot fstat %s"), quote (file));
1441 if (S_ISREG (st.st_mode) && st.st_size < (input_offset + offset))
1443 /* When skipping past EOF, return the number of _full_ blocks
1444 * that are not skipped, and set offset to EOF, so the caller
1445 * can determine the requested skip was not satisfied. */
1446 records = ( offset - st.st_size ) / blocksize;
1447 offset = st.st_size - input_offset;
1449 else
1450 records = 0;
1451 advance_input_offset (offset);
1453 else
1454 records = 0;
1455 return records;
1457 else
1459 int lseek_errno = errno;
1461 /* The seek request may have failed above if it was too big
1462 (> device size, > max file size, etc.)
1463 Or it may not have been done at all (> OFF_T_MAX).
1464 Therefore try to seek to the end of the file,
1465 to avoid redundant reading. */
1466 if ((skip_via_lseek (file, fdesc, 0, SEEK_END)) >= 0)
1468 /* File is seekable, and we're at the end of it, and
1469 size <= OFF_T_MAX. So there's no point using read to advance. */
1471 if (!lseek_errno)
1473 /* The original seek was not attempted as offset > OFF_T_MAX.
1474 We should error for write as can't get to the desired
1475 location, even if OFF_T_MAX < max file size.
1476 For read we're not going to read any data anyway,
1477 so we should error for consistency.
1478 It would be nice to not error for /dev/{zero,null}
1479 for any offset, but that's not a significant issue. */
1480 lseek_errno = EOVERFLOW;
1483 if (fdesc == STDIN_FILENO)
1484 error (0, lseek_errno, _("%s: cannot skip"), quote (file));
1485 else
1486 error (0, lseek_errno, _("%s: cannot seek"), quote (file));
1487 /* If the file has a specific size and we've asked
1488 to skip/seek beyond the max allowable, then quit. */
1489 quit (EXIT_FAILURE);
1491 /* else file_size && offset > OFF_T_MAX or file ! seekable */
1495 ssize_t nread = iread_fnc (fdesc, buf, blocksize);
1496 if (nread < 0)
1498 if (fdesc == STDIN_FILENO)
1500 error (0, errno, _("reading %s"), quote (file));
1501 if (conversions_mask & C_NOERROR)
1503 print_stats ();
1504 continue;
1507 else
1508 error (0, lseek_errno, _("%s: cannot seek"), quote (file));
1509 quit (EXIT_FAILURE);
1512 if (nread == 0)
1513 break;
1514 if (fdesc == STDIN_FILENO)
1515 advance_input_offset (nread);
1517 while (--records != 0);
1519 return records;
1523 /* Advance the input by NBYTES if possible, after a read error.
1524 The input file offset may or may not have advanced after the failed
1525 read; adjust it to point just after the bad record regardless.
1526 Return true if successful, or if the input is already known to not
1527 be seekable. */
1529 static bool
1530 advance_input_after_read_error (size_t nbytes)
1532 if (! input_seekable)
1534 if (input_seek_errno == ESPIPE)
1535 return true;
1536 errno = input_seek_errno;
1538 else
1540 off_t offset;
1541 advance_input_offset (nbytes);
1542 input_offset_overflow |= (OFF_T_MAX < input_offset);
1543 if (input_offset_overflow)
1545 error (0, 0, _("offset overflow while reading file %s"),
1546 quote (input_file));
1547 return false;
1549 offset = lseek (STDIN_FILENO, 0, SEEK_CUR);
1550 if (0 <= offset)
1552 off_t diff;
1553 if (offset == input_offset)
1554 return true;
1555 diff = input_offset - offset;
1556 if (! (0 <= diff && diff <= nbytes))
1557 error (0, 0, _("warning: invalid file offset after failed read"));
1558 if (0 <= skip_via_lseek (input_file, STDIN_FILENO, diff, SEEK_CUR))
1559 return true;
1560 if (errno == 0)
1561 error (0, 0, _("cannot work around kernel bug after all"));
1565 error (0, errno, _("%s: cannot seek"), quote (input_file));
1566 return false;
1569 /* Copy NREAD bytes of BUF, with no conversions. */
1571 static void
1572 copy_simple (char const *buf, size_t nread)
1574 const char *start = buf; /* First uncopied char in BUF. */
1578 size_t nfree = MIN (nread, output_blocksize - oc);
1580 memcpy (obuf + oc, start, nfree);
1582 nread -= nfree; /* Update the number of bytes left to copy. */
1583 start += nfree;
1584 oc += nfree;
1585 if (oc >= output_blocksize)
1586 write_output ();
1588 while (nread != 0);
1591 /* Copy NREAD bytes of BUF, doing conv=block
1592 (pad newline-terminated records to `conversion_blocksize',
1593 replacing the newline with trailing spaces). */
1595 static void
1596 copy_with_block (char const *buf, size_t nread)
1598 size_t i;
1600 for (i = nread; i; i--, buf++)
1602 if (*buf == newline_character)
1604 if (col < conversion_blocksize)
1606 size_t j;
1607 for (j = col; j < conversion_blocksize; j++)
1608 output_char (space_character);
1610 col = 0;
1612 else
1614 if (col == conversion_blocksize)
1615 r_truncate++;
1616 else if (col < conversion_blocksize)
1617 output_char (*buf);
1618 col++;
1623 /* Copy NREAD bytes of BUF, doing conv=unblock
1624 (replace trailing spaces in `conversion_blocksize'-sized records
1625 with a newline). */
1627 static void
1628 copy_with_unblock (char const *buf, size_t nread)
1630 size_t i;
1631 char c;
1632 static size_t pending_spaces = 0;
1634 for (i = 0; i < nread; i++)
1636 c = buf[i];
1638 if (col++ >= conversion_blocksize)
1640 col = pending_spaces = 0; /* Wipe out any pending spaces. */
1641 i--; /* Push the char back; get it later. */
1642 output_char (newline_character);
1644 else if (c == space_character)
1645 pending_spaces++;
1646 else
1648 /* `c' is the character after a run of spaces that were not
1649 at the end of the conversion buffer. Output them. */
1650 while (pending_spaces)
1652 output_char (space_character);
1653 --pending_spaces;
1655 output_char (c);
1660 /* Set the file descriptor flags for FD that correspond to the nonzero bits
1661 in ADD_FLAGS. The file's name is NAME. */
1663 static void
1664 set_fd_flags (int fd, int add_flags, char const *name)
1666 /* Ignore file creation flags that are no-ops on file descriptors. */
1667 add_flags &= ~ (O_NOCTTY | O_NOFOLLOW);
1669 if (add_flags)
1671 int old_flags = fcntl (fd, F_GETFL);
1672 int new_flags = old_flags | add_flags;
1673 bool ok = true;
1674 if (old_flags < 0)
1675 ok = false;
1676 else if (old_flags != new_flags)
1678 if (new_flags & (O_DIRECTORY | O_NOLINKS))
1680 /* NEW_FLAGS contains at least one file creation flag that
1681 requires some checking of the open file descriptor. */
1682 struct stat st;
1683 if (fstat (fd, &st) != 0)
1684 ok = false;
1685 else if ((new_flags & O_DIRECTORY) && ! S_ISDIR (st.st_mode))
1687 errno = ENOTDIR;
1688 ok = false;
1690 else if ((new_flags & O_NOLINKS) && 1 < st.st_nlink)
1692 errno = EMLINK;
1693 ok = false;
1695 new_flags &= ~ (O_DIRECTORY | O_NOLINKS);
1698 if (ok && old_flags != new_flags
1699 && fcntl (fd, F_SETFL, new_flags) == -1)
1700 ok = false;
1703 if (!ok)
1704 error (EXIT_FAILURE, errno, _("setting flags for %s"), quote (name));
1708 static char *
1709 human_size (size_t n)
1711 static char hbuf[LONGEST_HUMAN_READABLE + 1];
1712 int human_opts =
1713 (human_autoscale | human_round_to_nearest | human_base_1024
1714 | human_space_before_unit | human_SI | human_B);
1715 return human_readable (n, hbuf, human_opts, 1, 1);
1718 /* The main loop. */
1720 static int
1721 dd_copy (void)
1723 char *ibuf, *bufstart; /* Input buffer. */
1724 /* These are declared static so that even though we don't free the
1725 buffers, valgrind will recognize that there is no "real" leak. */
1726 static char *real_buf; /* real buffer address before alignment */
1727 static char *real_obuf;
1728 ssize_t nread; /* Bytes read in the current block. */
1730 /* If nonzero, then the previously read block was partial and
1731 PARTREAD was its size. */
1732 size_t partread = 0;
1734 int exit_status = EXIT_SUCCESS;
1735 size_t n_bytes_read;
1737 /* Leave at least one extra byte at the beginning and end of `ibuf'
1738 for conv=swab, but keep the buffer address even. But some peculiar
1739 device drivers work only with word-aligned buffers, so leave an
1740 extra two bytes. */
1742 /* Some devices require alignment on a sector or page boundary
1743 (e.g. character disk devices). Align the input buffer to a
1744 page boundary to cover all bases. Note that due to the swab
1745 algorithm, we must have at least one byte in the page before
1746 the input buffer; thus we allocate 2 pages of slop in the
1747 real buffer. 8k above the blocksize shouldn't bother anyone.
1749 The page alignment is necessary on any Linux kernel that supports
1750 either the SGI raw I/O patch or Steven Tweedies raw I/O patch.
1751 It is necessary when accessing raw (i.e. character special) disk
1752 devices on Unixware or other SVR4-derived system. */
1754 real_buf = malloc (input_blocksize + INPUT_BLOCK_SLOP);
1755 if (!real_buf)
1756 error (EXIT_FAILURE, 0,
1757 _("memory exhausted by input buffer of size %zu bytes (%s)"),
1758 input_blocksize, human_size (input_blocksize));
1760 ibuf = real_buf;
1761 ibuf += SWAB_ALIGN_OFFSET; /* allow space for swab */
1763 ibuf = ptr_align (ibuf, page_size);
1765 if (conversions_mask & C_TWOBUFS)
1767 /* Page-align the output buffer, too. */
1768 real_obuf = malloc (output_blocksize + OUTPUT_BLOCK_SLOP);
1769 if (!real_obuf)
1770 error (EXIT_FAILURE, 0,
1771 _("memory exhausted by output buffer of size %zu bytes (%s)"),
1772 output_blocksize, human_size (output_blocksize));
1773 obuf = ptr_align (real_obuf, page_size);
1775 else
1777 real_obuf = NULL;
1778 obuf = ibuf;
1781 if (skip_records != 0)
1783 uintmax_t us_bytes = input_offset + (skip_records * input_blocksize);
1784 uintmax_t us_blocks = skip (STDIN_FILENO, input_file,
1785 skip_records, input_blocksize, ibuf);
1786 us_bytes -= input_offset;
1788 /* POSIX doesn't say what to do when dd detects it has been
1789 asked to skip past EOF, so I assume it's non-fatal.
1790 There are 3 reasons why there might be unskipped blocks/bytes:
1791 1. file is too small
1792 2. pipe has not enough data
1793 3. partial reads */
1794 if (us_blocks || (!input_offset_overflow && us_bytes))
1796 error (0, 0,
1797 _("%s: cannot skip to specified offset"), quote (input_file));
1801 if (seek_records != 0)
1803 uintmax_t write_records = skip (STDOUT_FILENO, output_file,
1804 seek_records, output_blocksize, obuf);
1806 if (write_records != 0)
1808 memset (obuf, 0, output_blocksize);
1811 if (iwrite (STDOUT_FILENO, obuf, output_blocksize)
1812 != output_blocksize)
1814 error (0, errno, _("writing to %s"), quote (output_file));
1815 quit (EXIT_FAILURE);
1817 while (--write_records != 0);
1821 if (max_records == 0)
1822 return exit_status;
1824 while (1)
1826 if (r_partial + r_full >= max_records)
1827 break;
1829 /* Zero the buffer before reading, so that if we get a read error,
1830 whatever data we are able to read is followed by zeros.
1831 This minimizes data loss. */
1832 if ((conversions_mask & C_SYNC) && (conversions_mask & C_NOERROR))
1833 memset (ibuf,
1834 (conversions_mask & (C_BLOCK | C_UNBLOCK)) ? ' ' : '\0',
1835 input_blocksize);
1837 nread = iread_fnc (STDIN_FILENO, ibuf, input_blocksize);
1839 if (nread >= 0 && i_nocache)
1840 invalidate_cache (STDIN_FILENO, nread);
1842 if (nread == 0)
1843 break; /* EOF. */
1845 if (nread < 0)
1847 error (0, errno, _("reading %s"), quote (input_file));
1848 if (conversions_mask & C_NOERROR)
1850 print_stats ();
1851 size_t bad_portion = input_blocksize - partread;
1853 /* We already know this data is not cached,
1854 but call this so that correct offsets are maintained. */
1855 invalidate_cache (STDIN_FILENO, bad_portion);
1857 /* Seek past the bad block if possible. */
1858 if (!advance_input_after_read_error (bad_portion))
1860 exit_status = EXIT_FAILURE;
1862 /* Suppress duplicate diagnostics. */
1863 input_seekable = false;
1864 input_seek_errno = ESPIPE;
1866 if ((conversions_mask & C_SYNC) && !partread)
1867 /* Replace the missing input with null bytes and
1868 proceed normally. */
1869 nread = 0;
1870 else
1871 continue;
1873 else
1875 /* Write any partial block. */
1876 exit_status = EXIT_FAILURE;
1877 break;
1881 n_bytes_read = nread;
1882 advance_input_offset (nread);
1884 if (n_bytes_read < input_blocksize)
1886 r_partial++;
1887 partread = n_bytes_read;
1888 if (conversions_mask & C_SYNC)
1890 if (!(conversions_mask & C_NOERROR))
1891 /* If C_NOERROR, we zeroed the block before reading. */
1892 memset (ibuf + n_bytes_read,
1893 (conversions_mask & (C_BLOCK | C_UNBLOCK)) ? ' ' : '\0',
1894 input_blocksize - n_bytes_read);
1895 n_bytes_read = input_blocksize;
1898 else
1900 r_full++;
1901 partread = 0;
1904 if (ibuf == obuf) /* If not C_TWOBUFS. */
1906 size_t nwritten = iwrite (STDOUT_FILENO, obuf, n_bytes_read);
1907 w_bytes += nwritten;
1908 if (nwritten != n_bytes_read)
1910 error (0, errno, _("writing %s"), quote (output_file));
1911 return EXIT_FAILURE;
1913 else if (n_bytes_read == input_blocksize)
1914 w_full++;
1915 else
1916 w_partial++;
1917 continue;
1920 /* Do any translations on the whole buffer at once. */
1922 if (translation_needed)
1923 translate_buffer (ibuf, n_bytes_read);
1925 if (conversions_mask & C_SWAB)
1926 bufstart = swab_buffer (ibuf, &n_bytes_read);
1927 else
1928 bufstart = ibuf;
1930 if (conversions_mask & C_BLOCK)
1931 copy_with_block (bufstart, n_bytes_read);
1932 else if (conversions_mask & C_UNBLOCK)
1933 copy_with_unblock (bufstart, n_bytes_read);
1934 else
1935 copy_simple (bufstart, n_bytes_read);
1938 /* If we have a char left as a result of conv=swab, output it. */
1939 if (char_is_saved)
1941 if (conversions_mask & C_BLOCK)
1942 copy_with_block (&saved_char, 1);
1943 else if (conversions_mask & C_UNBLOCK)
1944 copy_with_unblock (&saved_char, 1);
1945 else
1946 output_char (saved_char);
1949 if ((conversions_mask & C_BLOCK) && col > 0)
1951 /* If the final input line didn't end with a '\n', pad
1952 the output block to `conversion_blocksize' chars. */
1953 size_t i;
1954 for (i = col; i < conversion_blocksize; i++)
1955 output_char (space_character);
1958 if (col && (conversions_mask & C_UNBLOCK))
1960 /* If there was any output, add a final '\n'. */
1961 output_char (newline_character);
1964 /* Write out the last block. */
1965 if (oc != 0)
1967 size_t nwritten = iwrite (STDOUT_FILENO, obuf, oc);
1968 w_bytes += nwritten;
1969 if (nwritten != 0)
1970 w_partial++;
1971 if (nwritten != oc)
1973 error (0, errno, _("writing %s"), quote (output_file));
1974 return EXIT_FAILURE;
1978 if ((conversions_mask & C_FDATASYNC) && fdatasync (STDOUT_FILENO) != 0)
1980 if (errno != ENOSYS && errno != EINVAL)
1982 error (0, errno, _("fdatasync failed for %s"), quote (output_file));
1983 exit_status = EXIT_FAILURE;
1985 conversions_mask |= C_FSYNC;
1988 if (conversions_mask & C_FSYNC)
1989 while (fsync (STDOUT_FILENO) != 0)
1990 if (errno != EINTR)
1992 error (0, errno, _("fsync failed for %s"), quote (output_file));
1993 return EXIT_FAILURE;
1996 return exit_status;
2000 main (int argc, char **argv)
2002 int i;
2003 int exit_status;
2004 off_t offset;
2006 install_signal_handlers ();
2008 initialize_main (&argc, &argv);
2009 set_program_name (argv[0]);
2010 setlocale (LC_ALL, "");
2011 bindtextdomain (PACKAGE, LOCALEDIR);
2012 textdomain (PACKAGE);
2014 /* Arrange to close stdout if parse_long_options exits. */
2015 atexit (maybe_close_stdout);
2017 page_size = getpagesize ();
2019 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, Version,
2020 usage, AUTHORS, (char const *) NULL);
2021 close_stdout_required = false;
2023 if (getopt_long (argc, argv, "", NULL, NULL) != -1)
2024 usage (EXIT_FAILURE);
2026 /* Initialize translation table to identity translation. */
2027 for (i = 0; i < 256; i++)
2028 trans_table[i] = i;
2030 /* Decode arguments. */
2031 scanargs (argc, argv);
2033 apply_translations ();
2035 if (input_file == NULL)
2037 input_file = _("standard input");
2038 set_fd_flags (STDIN_FILENO, input_flags, input_file);
2040 else
2042 if (fd_reopen (STDIN_FILENO, input_file, O_RDONLY | input_flags, 0) < 0)
2043 error (EXIT_FAILURE, errno, _("opening %s"), quote (input_file));
2046 offset = lseek (STDIN_FILENO, 0, SEEK_CUR);
2047 input_seekable = (0 <= offset);
2048 input_offset = MAX (0, offset);
2049 input_seek_errno = errno;
2051 if (output_file == NULL)
2053 output_file = _("standard output");
2054 set_fd_flags (STDOUT_FILENO, output_flags, output_file);
2056 else
2058 mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
2059 int opts
2060 = (output_flags
2061 | (conversions_mask & C_NOCREAT ? 0 : O_CREAT)
2062 | (conversions_mask & C_EXCL ? O_EXCL : 0)
2063 | (seek_records || (conversions_mask & C_NOTRUNC) ? 0 : O_TRUNC));
2065 /* Open the output file with *read* access only if we might
2066 need to read to satisfy a `seek=' request. If we can't read
2067 the file, go ahead with write-only access; it might work. */
2068 if ((! seek_records
2069 || fd_reopen (STDOUT_FILENO, output_file, O_RDWR | opts, perms) < 0)
2070 && (fd_reopen (STDOUT_FILENO, output_file, O_WRONLY | opts, perms)
2071 < 0))
2072 error (EXIT_FAILURE, errno, _("opening %s"), quote (output_file));
2074 if (seek_records != 0 && !(conversions_mask & C_NOTRUNC))
2076 uintmax_t size = seek_records * output_blocksize;
2077 unsigned long int obs = output_blocksize;
2079 if (OFF_T_MAX / output_blocksize < seek_records)
2080 error (EXIT_FAILURE, 0,
2081 _("offset too large: "
2082 "cannot truncate to a length of seek=%"PRIuMAX""
2083 " (%lu-byte) blocks"),
2084 seek_records, obs);
2086 if (ftruncate (STDOUT_FILENO, size) != 0)
2088 /* Complain only when ftruncate fails on a regular file, a
2089 directory, or a shared memory object, as POSIX 1003.1-2004
2090 specifies ftruncate's behavior only for these file types.
2091 For example, do not complain when Linux kernel 2.4 ftruncate
2092 fails on /dev/fd0. */
2093 int ftruncate_errno = errno;
2094 struct stat stdout_stat;
2095 if (fstat (STDOUT_FILENO, &stdout_stat) != 0)
2096 error (EXIT_FAILURE, errno, _("cannot fstat %s"),
2097 quote (output_file));
2098 if (S_ISREG (stdout_stat.st_mode)
2099 || S_ISDIR (stdout_stat.st_mode)
2100 || S_TYPEISSHM (&stdout_stat))
2101 error (EXIT_FAILURE, ftruncate_errno,
2102 _("failed to truncate to %"PRIuMAX" bytes"
2103 " in output file %s"),
2104 size, quote (output_file));
2109 start_time = gethrxtime ();
2111 exit_status = dd_copy ();
2113 if (max_records == 0)
2115 /* Special case to invalidate cache to end of file. */
2116 if (i_nocache && !invalidate_cache (STDIN_FILENO, 0))
2118 error (0, errno, _("failed to discard cache for: %s"),
2119 quote (input_file));
2120 exit_status = EXIT_FAILURE;
2122 if (o_nocache && !invalidate_cache (STDOUT_FILENO, 0))
2124 error (0, errno, _("failed to discard cache for: %s"),
2125 quote (output_file));
2126 exit_status = EXIT_FAILURE;
2129 else if (max_records != (uintmax_t) -1)
2131 /* Invalidate any pending region less that page size,
2132 in case the kernel might round up. */
2133 if (i_nocache)
2134 invalidate_cache (STDIN_FILENO, 0);
2135 if (o_nocache)
2136 invalidate_cache (STDOUT_FILENO, 0);
2139 quit (exit_status);