dd: add a flag to discard cached data
[coreutils/ericb.git] / src / dd.c
blob6069671ff481fb60f42b4c736c8be6e535186a93
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 "fadvise.h"
31 #include "fd-reopen.h"
32 #include "gethrxtime.h"
33 #include "human.h"
34 #include "long-options.h"
35 #include "quote.h"
36 #include "quotearg.h"
37 #include "xstrtol.h"
38 #include "xtime.h"
40 static void process_signals (void);
42 /* The official name of this program (e.g., no `g' prefix). */
43 #define PROGRAM_NAME "dd"
45 #define AUTHORS \
46 proper_name ("Paul Rubin"), \
47 proper_name ("David MacKenzie"), \
48 proper_name ("Stuart Kemp")
50 /* Use SA_NOCLDSTOP as a proxy for whether the sigaction machinery is
51 present. SA_NODEFER and SA_RESETHAND are XSI extensions. */
52 #ifndef SA_NOCLDSTOP
53 # define SA_NOCLDSTOP 0
54 # define sigprocmask(How, Set, Oset) /* empty */
55 # define sigset_t int
56 # if ! HAVE_SIGINTERRUPT
57 # define siginterrupt(sig, flag) /* empty */
58 # endif
59 #endif
61 #ifndef SIGINFO
62 # define SIGINFO SIGUSR1
63 #endif
65 /* This may belong in GNULIB's fcntl module instead.
66 Define O_CIO to 0 if it is not supported by this OS. */
67 #ifndef O_CIO
68 # define O_CIO 0
69 #endif
71 #if ! HAVE_FDATASYNC
72 # define fdatasync(fd) (errno = ENOSYS, -1)
73 #endif
75 #define output_char(c) \
76 do \
77 { \
78 obuf[oc++] = (c); \
79 if (oc >= output_blocksize) \
80 write_output (); \
81 } \
82 while (0)
84 /* Default input and output blocksize. */
85 #define DEFAULT_BLOCKSIZE 512
87 /* How many bytes to add to the input and output block sizes before invoking
88 malloc. See dd_copy for details. INPUT_BLOCK_SLOP must be no less than
89 OUTPUT_BLOCK_SLOP. */
90 #define INPUT_BLOCK_SLOP (2 * SWAB_ALIGN_OFFSET + 2 * page_size - 1)
91 #define OUTPUT_BLOCK_SLOP (page_size - 1)
93 /* Maximum blocksize for the given SLOP.
94 Keep it smaller than SIZE_MAX - SLOP, so that we can
95 allocate buffers that size. Keep it smaller than SSIZE_MAX, for
96 the benefit of system calls like "read". And keep it smaller than
97 OFF_T_MAX, for the benefit of the large-offset seek code. */
98 #define MAX_BLOCKSIZE(slop) MIN (SIZE_MAX - (slop), MIN (SSIZE_MAX, OFF_T_MAX))
100 /* Conversions bit masks. */
101 enum
103 C_ASCII = 01,
105 C_EBCDIC = 02,
106 C_IBM = 04,
107 C_BLOCK = 010,
108 C_UNBLOCK = 020,
109 C_LCASE = 040,
110 C_UCASE = 0100,
111 C_SWAB = 0200,
112 C_NOERROR = 0400,
113 C_NOTRUNC = 01000,
114 C_SYNC = 02000,
116 /* Use separate input and output buffers, and combine partial
117 input blocks. */
118 C_TWOBUFS = 04000,
120 C_NOCREAT = 010000,
121 C_EXCL = 020000,
122 C_FDATASYNC = 040000,
123 C_FSYNC = 0100000
126 /* Status bit masks. */
127 enum
129 STATUS_NOXFER = 01
132 /* The name of the input file, or NULL for the standard input. */
133 static char const *input_file = NULL;
135 /* The name of the output file, or NULL for the standard output. */
136 static char const *output_file = NULL;
138 /* The page size on this host. */
139 static size_t page_size;
141 /* The number of bytes in which atomic reads are done. */
142 static size_t input_blocksize = 0;
144 /* The number of bytes in which atomic writes are done. */
145 static size_t output_blocksize = 0;
147 /* Conversion buffer size, in bytes. 0 prevents conversions. */
148 static size_t conversion_blocksize = 0;
150 /* Skip this many records of `input_blocksize' bytes before input. */
151 static uintmax_t skip_records = 0;
153 /* Skip this many records of `output_blocksize' bytes before output. */
154 static uintmax_t seek_records = 0;
156 /* Copy only this many records. The default is effectively infinity. */
157 static uintmax_t max_records = (uintmax_t) -1;
159 /* Bit vector of conversions to apply. */
160 static int conversions_mask = 0;
162 /* Open flags for the input and output files. */
163 static int input_flags = 0;
164 static int output_flags = 0;
166 /* Status flags for what is printed to stderr. */
167 static int status_flags = 0;
169 /* If nonzero, filter characters through the translation table. */
170 static bool translation_needed = false;
172 /* Number of partial blocks written. */
173 static uintmax_t w_partial = 0;
175 /* Number of full blocks written. */
176 static uintmax_t w_full = 0;
178 /* Number of partial blocks read. */
179 static uintmax_t r_partial = 0;
181 /* Number of full blocks read. */
182 static uintmax_t r_full = 0;
184 /* Number of bytes written. */
185 static uintmax_t w_bytes = 0;
187 /* Time that dd started. */
188 static xtime_t start_time;
190 /* True if input is seekable. */
191 static bool input_seekable;
193 /* Error number corresponding to initial attempt to lseek input.
194 If ESPIPE, do not issue any more diagnostics about it. */
195 static int input_seek_errno;
197 /* File offset of the input, in bytes, along with a flag recording
198 whether it overflowed. */
199 static uintmax_t input_offset;
200 static bool input_offset_overflow;
202 /* Records truncated by conv=block. */
203 static uintmax_t r_truncate = 0;
205 /* Output representation of newline and space characters.
206 They change if we're converting to EBCDIC. */
207 static char newline_character = '\n';
208 static char space_character = ' ';
210 /* Output buffer. */
211 static char *obuf;
213 /* Current index into `obuf'. */
214 static size_t oc = 0;
216 /* Index into current line, for `conv=block' and `conv=unblock'. */
217 static size_t col = 0;
219 /* The set of signals that are caught. */
220 static sigset_t caught_signals;
222 /* If nonzero, the value of the pending fatal signal. */
223 static sig_atomic_t volatile interrupt_signal;
225 /* A count of the number of pending info signals that have been received. */
226 static sig_atomic_t volatile info_signal_count;
228 /* Whether to discard cache for input or output. */
229 static bool i_nocache, o_nocache;
231 /* Function used for read (to handle iflag=fullblock parameter). */
232 static ssize_t (*iread_fnc) (int fd, char *buf, size_t size);
234 /* A longest symbol in the struct symbol_values tables below. */
235 #define LONGEST_SYMBOL "fdatasync"
237 /* A symbol and the corresponding integer value. */
238 struct symbol_value
240 char symbol[sizeof LONGEST_SYMBOL];
241 int value;
244 /* Conversion symbols, for conv="...". */
245 static struct symbol_value const conversions[] =
247 {"ascii", C_ASCII | C_TWOBUFS}, /* EBCDIC to ASCII. */
248 {"ebcdic", C_EBCDIC | C_TWOBUFS}, /* ASCII to EBCDIC. */
249 {"ibm", C_IBM | C_TWOBUFS}, /* Slightly different ASCII to EBCDIC. */
250 {"block", C_BLOCK | C_TWOBUFS}, /* Variable to fixed length records. */
251 {"unblock", C_UNBLOCK | C_TWOBUFS}, /* Fixed to variable length records. */
252 {"lcase", C_LCASE | C_TWOBUFS}, /* Translate upper to lower case. */
253 {"ucase", C_UCASE | C_TWOBUFS}, /* Translate lower to upper case. */
254 {"swab", C_SWAB | C_TWOBUFS}, /* Swap bytes of input. */
255 {"noerror", C_NOERROR}, /* Ignore i/o errors. */
256 {"nocreat", C_NOCREAT}, /* Do not create output file. */
257 {"excl", C_EXCL}, /* Fail if the output file already exists. */
258 {"notrunc", C_NOTRUNC}, /* Do not truncate output file. */
259 {"sync", C_SYNC}, /* Pad input records to ibs with NULs. */
260 {"fdatasync", C_FDATASYNC}, /* Synchronize output data before finishing. */
261 {"fsync", C_FSYNC}, /* Also synchronize output metadata. */
262 {"", 0}
265 #define FFS_MASK(x) ((x) ^ ((x) & ((x) - 1)))
266 enum
268 /* Compute a value that's bitwise disjoint from the union
269 of all O_ values. */
270 v = ~(0
271 | O_APPEND
272 | O_BINARY
273 | O_CIO
274 | O_DIRECT
275 | O_DIRECTORY
276 | O_DSYNC
277 | O_NOATIME
278 | O_NOCTTY
279 | O_NOFOLLOW
280 | O_NOLINKS
281 | O_NONBLOCK
282 | O_SYNC
283 | O_TEXT
286 /* Use its lowest bits for private flags. */
287 O_FULLBLOCK = FFS_MASK (v),
288 v2 = v ^ O_FULLBLOCK,
290 O_NOCACHE = FFS_MASK (v2)
293 /* Ensure that we got something. */
294 verify (O_FULLBLOCK != 0);
295 verify (O_NOCACHE != 0);
297 #define MULTIPLE_BITS_SET(i) (((i) & ((i) - 1)) != 0)
299 /* Ensure that this is a single-bit value. */
300 verify ( ! MULTIPLE_BITS_SET (O_FULLBLOCK));
301 verify ( ! MULTIPLE_BITS_SET (O_NOCACHE));
303 /* Flags, for iflag="..." and oflag="...". */
304 static struct symbol_value const flags[] =
306 {"append", O_APPEND},
307 {"binary", O_BINARY},
308 {"cio", O_CIO},
309 {"direct", O_DIRECT},
310 {"directory", O_DIRECTORY},
311 {"dsync", O_DSYNC},
312 {"noatime", O_NOATIME},
313 {"nocache", O_NOCACHE}, /* Discard cache. */
314 {"noctty", O_NOCTTY},
315 {"nofollow", HAVE_WORKING_O_NOFOLLOW ? O_NOFOLLOW : 0},
316 {"nolinks", O_NOLINKS},
317 {"nonblock", O_NONBLOCK},
318 {"sync", O_SYNC},
319 {"text", O_TEXT},
320 {"fullblock", O_FULLBLOCK}, /* Accumulate full blocks from input. */
321 {"", 0}
324 /* Status, for status="...". */
325 static struct symbol_value const statuses[] =
327 {"noxfer", STATUS_NOXFER},
328 {"", 0}
331 /* Translation table formed by applying successive transformations. */
332 static unsigned char trans_table[256];
334 static char const ascii_to_ebcdic[] =
336 '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
337 '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
338 '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
339 '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
340 '\100', '\117', '\177', '\173', '\133', '\154', '\120', '\175',
341 '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
342 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
343 '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
344 '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
345 '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
346 '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
347 '\347', '\350', '\351', '\112', '\340', '\132', '\137', '\155',
348 '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
349 '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
350 '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
351 '\247', '\250', '\251', '\300', '\152', '\320', '\241', '\007',
352 '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
353 '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
354 '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
355 '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
356 '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
357 '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
358 '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
359 '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
360 '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
361 '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',
362 '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',
363 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
364 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
365 '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
366 '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
367 '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
370 static char const ascii_to_ibm[] =
372 '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
373 '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
374 '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
375 '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
376 '\100', '\132', '\177', '\173', '\133', '\154', '\120', '\175',
377 '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
378 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
379 '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
380 '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
381 '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
382 '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
383 '\347', '\350', '\351', '\255', '\340', '\275', '\137', '\155',
384 '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
385 '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
386 '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
387 '\247', '\250', '\251', '\300', '\117', '\320', '\241', '\007',
388 '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
389 '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
390 '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
391 '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
392 '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
393 '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
394 '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
395 '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
396 '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
397 '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',
398 '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',
399 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
400 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
401 '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
402 '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
403 '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
406 static char const ebcdic_to_ascii[] =
408 '\000', '\001', '\002', '\003', '\234', '\011', '\206', '\177',
409 '\227', '\215', '\216', '\013', '\014', '\015', '\016', '\017',
410 '\020', '\021', '\022', '\023', '\235', '\205', '\010', '\207',
411 '\030', '\031', '\222', '\217', '\034', '\035', '\036', '\037',
412 '\200', '\201', '\202', '\203', '\204', '\012', '\027', '\033',
413 '\210', '\211', '\212', '\213', '\214', '\005', '\006', '\007',
414 '\220', '\221', '\026', '\223', '\224', '\225', '\226', '\004',
415 '\230', '\231', '\232', '\233', '\024', '\025', '\236', '\032',
416 '\040', '\240', '\241', '\242', '\243', '\244', '\245', '\246',
417 '\247', '\250', '\133', '\056', '\074', '\050', '\053', '\041',
418 '\046', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
419 '\260', '\261', '\135', '\044', '\052', '\051', '\073', '\136',
420 '\055', '\057', '\262', '\263', '\264', '\265', '\266', '\267',
421 '\270', '\271', '\174', '\054', '\045', '\137', '\076', '\077',
422 '\272', '\273', '\274', '\275', '\276', '\277', '\300', '\301',
423 '\302', '\140', '\072', '\043', '\100', '\047', '\075', '\042',
424 '\303', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
425 '\150', '\151', '\304', '\305', '\306', '\307', '\310', '\311',
426 '\312', '\152', '\153', '\154', '\155', '\156', '\157', '\160',
427 '\161', '\162', '\313', '\314', '\315', '\316', '\317', '\320',
428 '\321', '\176', '\163', '\164', '\165', '\166', '\167', '\170',
429 '\171', '\172', '\322', '\323', '\324', '\325', '\326', '\327',
430 '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
431 '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
432 '\173', '\101', '\102', '\103', '\104', '\105', '\106', '\107',
433 '\110', '\111', '\350', '\351', '\352', '\353', '\354', '\355',
434 '\175', '\112', '\113', '\114', '\115', '\116', '\117', '\120',
435 '\121', '\122', '\356', '\357', '\360', '\361', '\362', '\363',
436 '\134', '\237', '\123', '\124', '\125', '\126', '\127', '\130',
437 '\131', '\132', '\364', '\365', '\366', '\367', '\370', '\371',
438 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
439 '\070', '\071', '\372', '\373', '\374', '\375', '\376', '\377'
442 /* True if we need to close the standard output *stream*. */
443 static bool close_stdout_required = true;
445 /* The only reason to close the standard output *stream* is if
446 parse_long_options fails (as it does for --help or --version).
447 In any other case, dd uses only the STDOUT_FILENO file descriptor,
448 and the "cleanup" function calls "close (STDOUT_FILENO)".
449 Closing the file descriptor and then letting the usual atexit-run
450 close_stdout function call "fclose (stdout)" would result in a
451 harmless failure of the close syscall (with errno EBADF).
452 This function serves solely to avoid the unnecessary close_stdout
453 call, once parse_long_options has succeeded.
454 Meanwhile, we guarantee that the standard error stream is flushed,
455 by inlining the last half of close_stdout as needed. */
456 static void
457 maybe_close_stdout (void)
459 if (close_stdout_required)
460 close_stdout ();
461 else if (close_stream (stderr) != 0)
462 _exit (EXIT_FAILURE);
465 void
466 usage (int status)
468 if (status != EXIT_SUCCESS)
469 fprintf (stderr, _("Try `%s --help' for more information.\n"),
470 program_name);
471 else
473 printf (_("\
474 Usage: %s [OPERAND]...\n\
475 or: %s OPTION\n\
477 program_name, program_name);
478 fputs (_("\
479 Copy a file, converting and formatting according to the operands.\n\
481 bs=BYTES read and write up to BYTES bytes at a time\n\
482 cbs=BYTES convert BYTES bytes at a time\n\
483 conv=CONVS convert the file as per the comma separated symbol list\n\
484 count=BLOCKS copy only BLOCKS input blocks\n\
485 ibs=BYTES read up to BYTES bytes at a time (default: 512)\n\
486 "), stdout);
487 fputs (_("\
488 if=FILE read from FILE instead of stdin\n\
489 iflag=FLAGS read as per the comma separated symbol list\n\
490 obs=BYTES write BYTES bytes at a time (default: 512)\n\
491 of=FILE write to FILE instead of stdout\n\
492 oflag=FLAGS write as per the comma separated symbol list\n\
493 seek=BLOCKS skip BLOCKS obs-sized blocks at start of output\n\
494 skip=BLOCKS skip BLOCKS ibs-sized blocks at start of input\n\
495 status=noxfer suppress transfer statistics\n\
496 "), stdout);
497 fputs (_("\
499 BLOCKS and BYTES may be followed by the following multiplicative suffixes:\n\
500 c =1, w =2, b =512, kB =1000, K =1024, MB =1000*1000, M =1024*1024, xM =M\n\
501 GB =1000*1000*1000, G =1024*1024*1024, and so on for T, P, E, Z, Y.\n\
503 Each CONV symbol may be:\n\
505 "), stdout);
506 fputs (_("\
507 ascii from EBCDIC to ASCII\n\
508 ebcdic from ASCII to EBCDIC\n\
509 ibm from ASCII to alternate EBCDIC\n\
510 block pad newline-terminated records with spaces to cbs-size\n\
511 unblock replace trailing spaces in cbs-size records with newline\n\
512 lcase change upper case to lower case\n\
513 ucase change lower case to upper case\n\
514 swab swap every pair of input bytes\n\
515 sync pad every input block with NULs to ibs-size; when used\n\
516 with block or unblock, pad with spaces rather than NULs\n\
517 "), stdout);
518 fputs (_("\
519 excl fail if the output file already exists\n\
520 nocreat do not create the output file\n\
521 notrunc do not truncate the output file\n\
522 noerror continue after read errors\n\
523 fdatasync physically write output file data before finishing\n\
524 fsync likewise, but also write metadata\n\
525 "), stdout);
526 fputs (_("\
528 Each FLAG symbol may be:\n\
530 append append mode (makes sense only for output; conv=notrunc suggested)\n\
531 "), stdout);
532 if (O_CIO)
533 fputs (_(" cio use concurrent I/O for data\n"), stdout);
534 if (O_DIRECT)
535 fputs (_(" direct use direct I/O for data\n"), stdout);
536 if (O_DIRECTORY)
537 fputs (_(" directory fail unless a directory\n"), stdout);
538 if (O_DSYNC)
539 fputs (_(" dsync use synchronized I/O for data\n"), stdout);
540 if (O_SYNC)
541 fputs (_(" sync likewise, but also for metadata\n"), stdout);
542 fputs (_(" fullblock accumulate full blocks of input (iflag only)\n"),
543 stdout);
544 if (O_NONBLOCK)
545 fputs (_(" nonblock use non-blocking I/O\n"), stdout);
546 if (O_NOATIME)
547 fputs (_(" noatime do not update access time\n"), stdout);
548 #if HAVE_POSIX_FADVISE
549 if (O_NOCACHE)
550 fputs (_(" nocache discard cached data\n"), stdout);
551 #endif
552 if (O_NOCTTY)
553 fputs (_(" noctty do not assign controlling terminal from file\n"),
554 stdout);
555 if (HAVE_WORKING_O_NOFOLLOW)
556 fputs (_(" nofollow do not follow symlinks\n"), stdout);
557 if (O_NOLINKS)
558 fputs (_(" nolinks fail if multiply-linked\n"), stdout);
559 if (O_BINARY)
560 fputs (_(" binary use binary I/O for data\n"), stdout);
561 if (O_TEXT)
562 fputs (_(" text use text I/O for data\n"), stdout);
565 char const *siginfo_name = (SIGINFO == SIGUSR1 ? "USR1" : "INFO");
566 printf (_("\
568 Sending a %s signal to a running `dd' process makes it\n\
569 print I/O statistics to standard error and then resume copying.\n\
571 $ dd if=/dev/zero of=/dev/null& pid=$!\n\
572 $ kill -%s $pid; sleep 1; kill $pid\n\
573 18335302+0 records in\n\
574 18335302+0 records out\n\
575 9387674624 bytes (9.4 GB) copied, 34.6279 seconds, 271 MB/s\n\
577 Options are:\n\
580 siginfo_name, siginfo_name);
583 fputs (HELP_OPTION_DESCRIPTION, stdout);
584 fputs (VERSION_OPTION_DESCRIPTION, stdout);
585 emit_ancillary_info ();
587 exit (status);
590 static void
591 translate_charset (char const *new_trans)
593 int i;
595 for (i = 0; i < 256; i++)
596 trans_table[i] = new_trans[trans_table[i]];
597 translation_needed = true;
600 /* Return true if I has more than one bit set. I must be nonnegative. */
602 static inline bool
603 multiple_bits_set (int i)
605 return MULTIPLE_BITS_SET (i);
608 /* Print transfer statistics. */
610 static void
611 print_stats (void)
613 xtime_t now = gethrxtime ();
614 char hbuf[LONGEST_HUMAN_READABLE + 1];
615 int human_opts =
616 (human_autoscale | human_round_to_nearest
617 | human_space_before_unit | human_SI | human_B);
618 double delta_s;
619 char const *bytes_per_second;
621 fprintf (stderr,
622 _("%"PRIuMAX"+%"PRIuMAX" records in\n"
623 "%"PRIuMAX"+%"PRIuMAX" records out\n"),
624 r_full, r_partial, w_full, w_partial);
626 if (r_truncate != 0)
627 fprintf (stderr,
628 ngettext ("%"PRIuMAX" truncated record\n",
629 "%"PRIuMAX" truncated records\n",
630 select_plural (r_truncate)),
631 r_truncate);
633 if (status_flags & STATUS_NOXFER)
634 return;
636 /* Use integer arithmetic to compute the transfer rate,
637 since that makes it easy to use SI abbreviations. */
639 fprintf (stderr,
640 ngettext ("%"PRIuMAX" byte (%s) copied",
641 "%"PRIuMAX" bytes (%s) copied",
642 select_plural (w_bytes)),
643 w_bytes,
644 human_readable (w_bytes, hbuf, human_opts, 1, 1));
646 if (start_time < now)
648 double XTIME_PRECISIONe0 = XTIME_PRECISION;
649 uintmax_t delta_xtime = now;
650 delta_xtime -= start_time;
651 delta_s = delta_xtime / XTIME_PRECISIONe0;
652 bytes_per_second = human_readable (w_bytes, hbuf, human_opts,
653 XTIME_PRECISION, delta_xtime);
655 else
657 delta_s = 0;
658 bytes_per_second = _("Infinity B");
661 /* TRANSLATORS: The two instances of "s" in this string are the SI
662 symbol "s" (meaning second), and should not be translated.
664 This format used to be:
666 ngettext (", %g second, %s/s\n", ", %g seconds, %s/s\n", delta_s == 1)
668 but that was incorrect for languages like Polish. To fix this
669 bug we now use SI symbols even though they're a bit more
670 confusing in English. */
671 fprintf (stderr, _(", %g s, %s/s\n"), delta_s, bytes_per_second);
674 static void
675 cleanup (void)
677 if (close (STDIN_FILENO) < 0)
678 error (EXIT_FAILURE, errno,
679 _("closing input file %s"), quote (input_file));
681 /* Don't remove this call to close, even though close_stdout
682 closes standard output. This close is necessary when cleanup
683 is called as part of a signal handler. */
684 if (close (STDOUT_FILENO) < 0)
685 error (EXIT_FAILURE, errno,
686 _("closing output file %s"), quote (output_file));
689 static void ATTRIBUTE_NORETURN
690 quit (int code)
692 cleanup ();
693 print_stats ();
694 process_signals ();
695 exit (code);
698 /* An ordinary signal was received; arrange for the program to exit. */
700 static void
701 interrupt_handler (int sig)
703 if (! SA_RESETHAND)
704 signal (sig, SIG_DFL);
705 interrupt_signal = sig;
708 /* An info signal was received; arrange for the program to print status. */
710 static void
711 siginfo_handler (int sig)
713 if (! SA_NOCLDSTOP)
714 signal (sig, siginfo_handler);
715 info_signal_count++;
718 /* Install the signal handlers. */
720 static void
721 install_signal_handlers (void)
723 bool catch_siginfo = ! (SIGINFO == SIGUSR1 && getenv ("POSIXLY_CORRECT"));
725 #if SA_NOCLDSTOP
727 struct sigaction act;
728 sigemptyset (&caught_signals);
729 if (catch_siginfo)
731 sigaction (SIGINFO, NULL, &act);
732 if (act.sa_handler != SIG_IGN)
733 sigaddset (&caught_signals, SIGINFO);
735 sigaction (SIGINT, NULL, &act);
736 if (act.sa_handler != SIG_IGN)
737 sigaddset (&caught_signals, SIGINT);
738 act.sa_mask = caught_signals;
740 if (sigismember (&caught_signals, SIGINFO))
742 act.sa_handler = siginfo_handler;
743 act.sa_flags = 0;
744 sigaction (SIGINFO, &act, NULL);
747 if (sigismember (&caught_signals, SIGINT))
749 /* POSIX 1003.1-2001 says SA_RESETHAND implies SA_NODEFER,
750 but this is not true on Solaris 8 at least. It doesn't
751 hurt to use SA_NODEFER here, so leave it in. */
752 act.sa_handler = interrupt_handler;
753 act.sa_flags = SA_NODEFER | SA_RESETHAND;
754 sigaction (SIGINT, &act, NULL);
757 #else
759 if (catch_siginfo && signal (SIGINFO, SIG_IGN) != SIG_IGN)
761 signal (SIGINFO, siginfo_handler);
762 siginterrupt (SIGINFO, 1);
764 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
766 signal (SIGINT, interrupt_handler);
767 siginterrupt (SIGINT, 1);
769 #endif
772 /* Process any pending signals. If signals are caught, this function
773 should be called periodically. Ideally there should never be an
774 unbounded amount of time when signals are not being processed. */
776 static void
777 process_signals (void)
779 while (interrupt_signal || info_signal_count)
781 int interrupt;
782 int infos;
783 sigset_t oldset;
785 sigprocmask (SIG_BLOCK, &caught_signals, &oldset);
787 /* Reload interrupt_signal and info_signal_count, in case a new
788 signal was handled before sigprocmask took effect. */
789 interrupt = interrupt_signal;
790 infos = info_signal_count;
792 if (infos)
793 info_signal_count = infos - 1;
795 sigprocmask (SIG_SETMASK, &oldset, NULL);
797 if (interrupt)
798 cleanup ();
799 print_stats ();
800 if (interrupt)
801 raise (interrupt);
805 /* Return LEN rounded down to a multiple of PAGE_SIZE
806 while storing the remainder internally per FD.
807 Pass LEN == 0 to get the current remainder. */
809 static off_t
810 cache_round (int fd, off_t len)
812 static off_t i_pending, o_pending;
813 off_t *pending = (fd == STDIN_FILENO ? &i_pending : &o_pending);
815 if (len)
817 off_t c_pending = *pending + len;
818 *pending = c_pending % page_size;
819 if (c_pending > *pending)
820 len = c_pending - *pending;
821 else
822 len = 0;
824 else
825 len = *pending;
827 return len;
830 /* Discard the cache from the current offset of either
831 STDIN_FILENO or STDOUT_FILENO.
832 Return true on success. */
834 static bool
835 invalidate_cache (int fd, off_t len)
837 int adv_ret = -1;
839 /* Minimize syscalls. */
840 off_t clen = cache_round (fd, len);
841 if (len && !clen)
842 return true; /* Don't advise this time. */
843 if (!len && !clen && max_records)
844 return true; /* Nothing pending. */
845 off_t pending = len ? cache_round (fd, 0) : 0;
847 if (fd == STDIN_FILENO)
849 if (input_seekable)
851 /* Note we're being careful here to only invalidate what
852 we've read, so as not to dump any read ahead cache. */
853 #if HAVE_POSIX_FADVISE
854 adv_ret = posix_fadvise (fd, input_offset - clen - pending, clen,
855 POSIX_FADV_DONTNEED);
856 #else
857 errno = ENOTSUP;
858 #endif
860 else
861 errno = ESPIPE;
863 else if (fd == STDOUT_FILENO)
865 static off_t output_offset = -2;
867 if (output_offset != -1)
869 if (0 > output_offset)
871 output_offset = lseek (fd, 0, SEEK_CUR);
872 output_offset -= clen + pending;
874 if (0 <= output_offset)
876 #if HAVE_POSIX_FADVISE
877 adv_ret = posix_fadvise (fd, output_offset, clen,
878 POSIX_FADV_DONTNEED);
879 #else
880 errno = ENOTSUP;
881 #endif
882 output_offset += clen + pending;
887 return adv_ret != -1 ? true : false;
890 /* Read from FD into the buffer BUF of size SIZE, processing any
891 signals that arrive before bytes are read. Return the number of
892 bytes read if successful, -1 (setting errno) on failure. */
894 static ssize_t
895 iread (int fd, char *buf, size_t size)
897 while (true)
899 ssize_t nread;
900 process_signals ();
901 nread = read (fd, buf, size);
902 if (! (nread < 0 && errno == EINTR))
903 return nread;
907 /* Wrapper around iread function to accumulate full blocks. */
908 static ssize_t
909 iread_fullblock (int fd, char *buf, size_t size)
911 ssize_t nread = 0;
913 while (0 < size)
915 ssize_t ncurr = iread (fd, buf, size);
916 if (ncurr < 0)
917 return ncurr;
918 if (ncurr == 0)
919 break;
920 nread += ncurr;
921 buf += ncurr;
922 size -= ncurr;
925 return nread;
928 /* Write to FD the buffer BUF of size SIZE, processing any signals
929 that arrive. Return the number of bytes written, setting errno if
930 this is less than SIZE. Keep trying if there are partial
931 writes. */
933 static size_t
934 iwrite (int fd, char const *buf, size_t size)
936 size_t total_written = 0;
938 if ((output_flags & O_DIRECT) && w_partial == 1)
940 error (0, 0, _("dd: warning: partial read; oflag=direct disabled; "
941 "suggest iflag=fullblock"));
944 if ((output_flags & O_DIRECT) && size < output_blocksize)
946 int old_flags = fcntl (STDOUT_FILENO, F_GETFL);
947 if (fcntl (STDOUT_FILENO, F_SETFL, old_flags & ~O_DIRECT) != 0)
948 error (0, errno, _("failed to turn off O_DIRECT: %s"),
949 quote (output_file));
951 /* Since we have just turned off O_DIRECT for the final write,
952 here we try to preserve some of its semantics. First, use
953 posix_fadvise to tell the system not to pollute the buffer
954 cache with this data. Don't bother to diagnose lseek or
955 posix_fadvise failure. */
956 invalidate_cache (STDOUT_FILENO, 0);
958 /* Attempt to ensure that that final block is committed
959 to disk as quickly as possible. */
960 conversions_mask |= C_FSYNC;
963 while (total_written < size)
965 ssize_t nwritten;
966 process_signals ();
967 nwritten = write (fd, buf + total_written, size - total_written);
968 if (nwritten < 0)
970 if (errno != EINTR)
971 break;
973 else if (nwritten == 0)
975 /* Some buggy drivers return 0 when one tries to write beyond
976 a device's end. (Example: Linux kernel 1.2.13 on /dev/fd0.)
977 Set errno to ENOSPC so they get a sensible diagnostic. */
978 errno = ENOSPC;
979 break;
981 else
982 total_written += nwritten;
985 if (o_nocache && total_written)
986 invalidate_cache (fd, total_written);
988 return total_written;
991 /* Write, then empty, the output buffer `obuf'. */
993 static void
994 write_output (void)
996 size_t nwritten = iwrite (STDOUT_FILENO, obuf, output_blocksize);
997 w_bytes += nwritten;
998 if (nwritten != output_blocksize)
1000 error (0, errno, _("writing to %s"), quote (output_file));
1001 if (nwritten != 0)
1002 w_partial++;
1003 quit (EXIT_FAILURE);
1005 else
1006 w_full++;
1007 oc = 0;
1010 /* Return true if STR is of the form "PATTERN" or "PATTERNDELIM...". */
1012 static bool
1013 operand_matches (char const *str, char const *pattern, char delim)
1015 while (*pattern)
1016 if (*str++ != *pattern++)
1017 return false;
1018 return !*str || *str == delim;
1021 /* Interpret one "conv=..." or similar operand STR according to the
1022 symbols in TABLE, returning the flags specified. If the operand
1023 cannot be parsed, use ERROR_MSGID to generate a diagnostic. */
1025 static int
1026 parse_symbols (char const *str, struct symbol_value const *table,
1027 char const *error_msgid)
1029 int value = 0;
1031 while (true)
1033 char const *strcomma = strchr (str, ',');
1034 struct symbol_value const *entry;
1036 for (entry = table;
1037 ! (operand_matches (str, entry->symbol, ',') && entry->value);
1038 entry++)
1040 if (! entry->symbol[0])
1042 size_t slen = strcomma ? strcomma - str : strlen (str);
1043 error (0, 0, "%s: %s", _(error_msgid),
1044 quotearg_n_style_mem (0, locale_quoting_style, str, slen));
1045 usage (EXIT_FAILURE);
1049 value |= entry->value;
1050 if (!strcomma)
1051 break;
1052 str = strcomma + 1;
1055 return value;
1058 /* Return the value of STR, interpreted as a non-negative decimal integer,
1059 optionally multiplied by various values.
1060 Set *INVALID if STR does not represent a number in this format. */
1062 static uintmax_t
1063 parse_integer (const char *str, bool *invalid)
1065 uintmax_t n;
1066 char *suffix;
1067 enum strtol_error e = xstrtoumax (str, &suffix, 10, &n, "bcEGkKMPTwYZ0");
1069 if (e == LONGINT_INVALID_SUFFIX_CHAR && *suffix == 'x')
1071 uintmax_t multiplier = parse_integer (suffix + 1, invalid);
1073 if (multiplier != 0 && n * multiplier / multiplier != n)
1075 *invalid = true;
1076 return 0;
1079 n *= multiplier;
1081 else if (e != LONGINT_OK)
1083 *invalid = true;
1084 return 0;
1087 return n;
1090 /* OPERAND is of the form "X=...". Return true if X is NAME. */
1092 static bool
1093 operand_is (char const *operand, char const *name)
1095 return operand_matches (operand, name, '=');
1098 static void
1099 scanargs (int argc, char *const *argv)
1101 int i;
1102 size_t blocksize = 0;
1104 for (i = optind; i < argc; i++)
1106 char const *name = argv[i];
1107 char const *val = strchr (name, '=');
1109 if (val == NULL)
1111 error (0, 0, _("unrecognized operand %s"), quote (name));
1112 usage (EXIT_FAILURE);
1114 val++;
1116 if (operand_is (name, "if"))
1117 input_file = val;
1118 else if (operand_is (name, "of"))
1119 output_file = val;
1120 else if (operand_is (name, "conv"))
1121 conversions_mask |= parse_symbols (val, conversions,
1122 N_("invalid conversion"));
1123 else if (operand_is (name, "iflag"))
1124 input_flags |= parse_symbols (val, flags,
1125 N_("invalid input flag"));
1126 else if (operand_is (name, "oflag"))
1127 output_flags |= parse_symbols (val, flags,
1128 N_("invalid output flag"));
1129 else if (operand_is (name, "status"))
1130 status_flags |= parse_symbols (val, statuses,
1131 N_("invalid status flag"));
1132 else
1134 bool invalid = false;
1135 uintmax_t n = parse_integer (val, &invalid);
1137 if (operand_is (name, "ibs"))
1139 invalid |= ! (0 < n && n <= MAX_BLOCKSIZE (INPUT_BLOCK_SLOP));
1140 input_blocksize = n;
1142 else if (operand_is (name, "obs"))
1144 invalid |= ! (0 < n && n <= MAX_BLOCKSIZE (OUTPUT_BLOCK_SLOP));
1145 output_blocksize = n;
1147 else if (operand_is (name, "bs"))
1149 invalid |= ! (0 < n && n <= MAX_BLOCKSIZE (INPUT_BLOCK_SLOP));
1150 blocksize = n;
1152 else if (operand_is (name, "cbs"))
1154 invalid |= ! (0 < n && n <= SIZE_MAX);
1155 conversion_blocksize = n;
1157 else if (operand_is (name, "skip"))
1158 skip_records = n;
1159 else if (operand_is (name, "seek"))
1160 seek_records = n;
1161 else if (operand_is (name, "count"))
1162 max_records = n;
1163 else
1165 error (0, 0, _("unrecognized operand %s"), quote (name));
1166 usage (EXIT_FAILURE);
1169 if (invalid)
1170 error (EXIT_FAILURE, 0, _("invalid number %s"), quote (val));
1174 if (blocksize)
1175 input_blocksize = output_blocksize = blocksize;
1176 else
1178 /* POSIX says dd aggregates short reads into
1179 output_blocksize if bs= is not specified. */
1180 conversions_mask |= C_TWOBUFS;
1183 if (input_blocksize == 0)
1184 input_blocksize = DEFAULT_BLOCKSIZE;
1185 if (output_blocksize == 0)
1186 output_blocksize = DEFAULT_BLOCKSIZE;
1187 if (conversion_blocksize == 0)
1188 conversions_mask &= ~(C_BLOCK | C_UNBLOCK);
1190 if (input_flags & (O_DSYNC | O_SYNC))
1191 input_flags |= O_RSYNC;
1193 if (output_flags & O_FULLBLOCK)
1195 error (0, 0, "%s: %s", _("invalid output flag"), "'fullblock'");
1196 usage (EXIT_FAILURE);
1198 iread_fnc = ((input_flags & O_FULLBLOCK)
1199 ? iread_fullblock
1200 : iread);
1201 input_flags &= ~O_FULLBLOCK;
1203 if (multiple_bits_set (conversions_mask & (C_ASCII | C_EBCDIC | C_IBM)))
1204 error (EXIT_FAILURE, 0, _("cannot combine any two of {ascii,ebcdic,ibm}"));
1205 if (multiple_bits_set (conversions_mask & (C_BLOCK | C_UNBLOCK)))
1206 error (EXIT_FAILURE, 0, _("cannot combine block and unblock"));
1207 if (multiple_bits_set (conversions_mask & (C_LCASE | C_UCASE)))
1208 error (EXIT_FAILURE, 0, _("cannot combine lcase and ucase"));
1209 if (multiple_bits_set (conversions_mask & (C_EXCL | C_NOCREAT)))
1210 error (EXIT_FAILURE, 0, _("cannot combine excl and nocreat"));
1211 if (multiple_bits_set (input_flags & (O_DIRECT | O_NOCACHE))
1212 || multiple_bits_set (output_flags & (O_DIRECT | O_NOCACHE)))
1213 error (EXIT_FAILURE, 0, _("cannot combine direct and nocache"));
1215 if (input_flags & O_NOCACHE)
1217 i_nocache = true;
1218 input_flags &= ~O_NOCACHE;
1220 if (output_flags & O_NOCACHE)
1222 o_nocache = true;
1223 output_flags &= ~O_NOCACHE;
1227 /* Fix up translation table. */
1229 static void
1230 apply_translations (void)
1232 int i;
1234 if (conversions_mask & C_ASCII)
1235 translate_charset (ebcdic_to_ascii);
1237 if (conversions_mask & C_UCASE)
1239 for (i = 0; i < 256; i++)
1240 trans_table[i] = toupper (trans_table[i]);
1241 translation_needed = true;
1243 else if (conversions_mask & C_LCASE)
1245 for (i = 0; i < 256; i++)
1246 trans_table[i] = tolower (trans_table[i]);
1247 translation_needed = true;
1250 if (conversions_mask & C_EBCDIC)
1252 translate_charset (ascii_to_ebcdic);
1253 newline_character = ascii_to_ebcdic['\n'];
1254 space_character = ascii_to_ebcdic[' '];
1256 else if (conversions_mask & C_IBM)
1258 translate_charset (ascii_to_ibm);
1259 newline_character = ascii_to_ibm['\n'];
1260 space_character = ascii_to_ibm[' '];
1264 /* Apply the character-set translations specified by the user
1265 to the NREAD bytes in BUF. */
1267 static void
1268 translate_buffer (char *buf, size_t nread)
1270 char *cp;
1271 size_t i;
1273 for (i = nread, cp = buf; i; i--, cp++)
1274 *cp = trans_table[to_uchar (*cp)];
1277 /* If true, the last char from the previous call to `swab_buffer'
1278 is saved in `saved_char'. */
1279 static bool char_is_saved = false;
1281 /* Odd char from previous call. */
1282 static char saved_char;
1284 /* Swap NREAD bytes in BUF, plus possibly an initial char from the
1285 previous call. If NREAD is odd, save the last char for the
1286 next call. Return the new start of the BUF buffer. */
1288 static char *
1289 swab_buffer (char *buf, size_t *nread)
1291 char *bufstart = buf;
1292 char *cp;
1293 size_t i;
1295 /* Is a char left from last time? */
1296 if (char_is_saved)
1298 *--bufstart = saved_char;
1299 (*nread)++;
1300 char_is_saved = false;
1303 if (*nread & 1)
1305 /* An odd number of chars are in the buffer. */
1306 saved_char = bufstart[--*nread];
1307 char_is_saved = true;
1310 /* Do the byte-swapping by moving every second character two
1311 positions toward the end, working from the end of the buffer
1312 toward the beginning. This way we only move half of the data. */
1314 cp = bufstart + *nread; /* Start one char past the last. */
1315 for (i = *nread / 2; i; i--, cp -= 2)
1316 *cp = *(cp - 2);
1318 return ++bufstart;
1321 /* Add OFFSET to the input offset, setting the overflow flag if
1322 necessary. */
1324 static void
1325 advance_input_offset (uintmax_t offset)
1327 input_offset += offset;
1328 if (input_offset < offset)
1329 input_offset_overflow = true;
1332 /* This is a wrapper for lseek. It detects and warns about a kernel
1333 bug that makes lseek a no-op for tape devices, even though the kernel
1334 lseek return value suggests that the function succeeded.
1336 The parameters are the same as those of the lseek function, but
1337 with the addition of FILENAME, the name of the file associated with
1338 descriptor FDESC. The file name is used solely in the warning that's
1339 printed when the bug is detected. Return the same value that lseek
1340 would have returned, but when the lseek bug is detected, return -1
1341 to indicate that lseek failed.
1343 The offending behavior has been confirmed with an Exabyte SCSI tape
1344 drive accessed via /dev/nst0 on both Linux 2.2.17 and 2.4.16 kernels. */
1346 #ifdef __linux__
1348 # include <sys/mtio.h>
1350 # define MT_SAME_POSITION(P, Q) \
1351 ((P).mt_resid == (Q).mt_resid \
1352 && (P).mt_fileno == (Q).mt_fileno \
1353 && (P).mt_blkno == (Q).mt_blkno)
1355 static off_t
1356 skip_via_lseek (char const *filename, int fdesc, off_t offset, int whence)
1358 struct mtget s1;
1359 struct mtget s2;
1360 bool got_original_tape_position = (ioctl (fdesc, MTIOCGET, &s1) == 0);
1361 /* known bad device type */
1362 /* && s.mt_type == MT_ISSCSI2 */
1364 off_t new_position = lseek (fdesc, offset, whence);
1365 if (0 <= new_position
1366 && got_original_tape_position
1367 && ioctl (fdesc, MTIOCGET, &s2) == 0
1368 && MT_SAME_POSITION (s1, s2))
1370 error (0, 0, _("warning: working around lseek kernel bug for file (%s)\n\
1371 of mt_type=0x%0lx -- see <sys/mtio.h> for the list of types"),
1372 filename, s2.mt_type);
1373 errno = 0;
1374 new_position = -1;
1377 return new_position;
1379 #else
1380 # define skip_via_lseek(Filename, Fd, Offset, Whence) lseek (Fd, Offset, Whence)
1381 #endif
1383 /* Throw away RECORDS blocks of BLOCKSIZE bytes on file descriptor FDESC,
1384 which is open with read permission for FILE. Store up to BLOCKSIZE
1385 bytes of the data at a time in BUF, if necessary. RECORDS must be
1386 nonzero. If fdesc is STDIN_FILENO, advance the input offset.
1387 Return the number of records remaining, i.e., that were not skipped
1388 because EOF was reached. */
1390 static uintmax_t
1391 skip (int fdesc, char const *file, uintmax_t records, size_t blocksize,
1392 char *buf)
1394 uintmax_t offset = records * blocksize;
1396 /* Try lseek and if an error indicates it was an inappropriate operation --
1397 or if the file offset is not representable as an off_t --
1398 fall back on using read. */
1400 errno = 0;
1401 if (records <= OFF_T_MAX / blocksize
1402 && 0 <= skip_via_lseek (file, fdesc, offset, SEEK_CUR))
1404 if (fdesc == STDIN_FILENO)
1406 struct stat st;
1407 if (fstat (STDIN_FILENO, &st) != 0)
1408 error (EXIT_FAILURE, errno, _("cannot fstat %s"), quote (file));
1409 if (S_ISREG (st.st_mode) && st.st_size < (input_offset + offset))
1411 /* When skipping past EOF, return the number of _full_ blocks
1412 * that are not skipped, and set offset to EOF, so the caller
1413 * can determine the requested skip was not satisfied. */
1414 records = ( offset - st.st_size ) / blocksize;
1415 offset = st.st_size - input_offset;
1417 else
1418 records = 0;
1419 advance_input_offset (offset);
1421 else
1422 records = 0;
1423 return records;
1425 else
1427 int lseek_errno = errno;
1429 /* The seek request may have failed above if it was too big
1430 (> device size, > max file size, etc.)
1431 Or it may not have been done at all (> OFF_T_MAX).
1432 Therefore try to seek to the end of the file,
1433 to avoid redundant reading. */
1434 if ((skip_via_lseek (file, fdesc, 0, SEEK_END)) >= 0)
1436 /* File is seekable, and we're at the end of it, and
1437 size <= OFF_T_MAX. So there's no point using read to advance. */
1439 if (!lseek_errno)
1441 /* The original seek was not attempted as offset > OFF_T_MAX.
1442 We should error for write as can't get to the desired
1443 location, even if OFF_T_MAX < max file size.
1444 For read we're not going to read any data anyway,
1445 so we should error for consistency.
1446 It would be nice to not error for /dev/{zero,null}
1447 for any offset, but that's not a significant issue. */
1448 lseek_errno = EOVERFLOW;
1451 if (fdesc == STDIN_FILENO)
1452 error (0, lseek_errno, _("%s: cannot skip"), quote (file));
1453 else
1454 error (0, lseek_errno, _("%s: cannot seek"), quote (file));
1455 /* If the file has a specific size and we've asked
1456 to skip/seek beyond the max allowable, then quit. */
1457 quit (EXIT_FAILURE);
1459 /* else file_size && offset > OFF_T_MAX or file ! seekable */
1463 ssize_t nread = iread_fnc (fdesc, buf, blocksize);
1464 if (nread < 0)
1466 if (fdesc == STDIN_FILENO)
1468 error (0, errno, _("reading %s"), quote (file));
1469 if (conversions_mask & C_NOERROR)
1471 print_stats ();
1472 continue;
1475 else
1476 error (0, lseek_errno, _("%s: cannot seek"), quote (file));
1477 quit (EXIT_FAILURE);
1480 if (nread == 0)
1481 break;
1482 if (fdesc == STDIN_FILENO)
1483 advance_input_offset (nread);
1485 while (--records != 0);
1487 return records;
1491 /* Advance the input by NBYTES if possible, after a read error.
1492 The input file offset may or may not have advanced after the failed
1493 read; adjust it to point just after the bad record regardless.
1494 Return true if successful, or if the input is already known to not
1495 be seekable. */
1497 static bool
1498 advance_input_after_read_error (size_t nbytes)
1500 if (! input_seekable)
1502 if (input_seek_errno == ESPIPE)
1503 return true;
1504 errno = input_seek_errno;
1506 else
1508 off_t offset;
1509 advance_input_offset (nbytes);
1510 input_offset_overflow |= (OFF_T_MAX < input_offset);
1511 if (input_offset_overflow)
1513 error (0, 0, _("offset overflow while reading file %s"),
1514 quote (input_file));
1515 return false;
1517 offset = lseek (STDIN_FILENO, 0, SEEK_CUR);
1518 if (0 <= offset)
1520 off_t diff;
1521 if (offset == input_offset)
1522 return true;
1523 diff = input_offset - offset;
1524 if (! (0 <= diff && diff <= nbytes))
1525 error (0, 0, _("warning: invalid file offset after failed read"));
1526 if (0 <= skip_via_lseek (input_file, STDIN_FILENO, diff, SEEK_CUR))
1527 return true;
1528 if (errno == 0)
1529 error (0, 0, _("cannot work around kernel bug after all"));
1533 error (0, errno, _("%s: cannot seek"), quote (input_file));
1534 return false;
1537 /* Copy NREAD bytes of BUF, with no conversions. */
1539 static void
1540 copy_simple (char const *buf, size_t nread)
1542 const char *start = buf; /* First uncopied char in BUF. */
1546 size_t nfree = MIN (nread, output_blocksize - oc);
1548 memcpy (obuf + oc, start, nfree);
1550 nread -= nfree; /* Update the number of bytes left to copy. */
1551 start += nfree;
1552 oc += nfree;
1553 if (oc >= output_blocksize)
1554 write_output ();
1556 while (nread != 0);
1559 /* Copy NREAD bytes of BUF, doing conv=block
1560 (pad newline-terminated records to `conversion_blocksize',
1561 replacing the newline with trailing spaces). */
1563 static void
1564 copy_with_block (char const *buf, size_t nread)
1566 size_t i;
1568 for (i = nread; i; i--, buf++)
1570 if (*buf == newline_character)
1572 if (col < conversion_blocksize)
1574 size_t j;
1575 for (j = col; j < conversion_blocksize; j++)
1576 output_char (space_character);
1578 col = 0;
1580 else
1582 if (col == conversion_blocksize)
1583 r_truncate++;
1584 else if (col < conversion_blocksize)
1585 output_char (*buf);
1586 col++;
1591 /* Copy NREAD bytes of BUF, doing conv=unblock
1592 (replace trailing spaces in `conversion_blocksize'-sized records
1593 with a newline). */
1595 static void
1596 copy_with_unblock (char const *buf, size_t nread)
1598 size_t i;
1599 char c;
1600 static size_t pending_spaces = 0;
1602 for (i = 0; i < nread; i++)
1604 c = buf[i];
1606 if (col++ >= conversion_blocksize)
1608 col = pending_spaces = 0; /* Wipe out any pending spaces. */
1609 i--; /* Push the char back; get it later. */
1610 output_char (newline_character);
1612 else if (c == space_character)
1613 pending_spaces++;
1614 else
1616 /* `c' is the character after a run of spaces that were not
1617 at the end of the conversion buffer. Output them. */
1618 while (pending_spaces)
1620 output_char (space_character);
1621 --pending_spaces;
1623 output_char (c);
1628 /* Set the file descriptor flags for FD that correspond to the nonzero bits
1629 in ADD_FLAGS. The file's name is NAME. */
1631 static void
1632 set_fd_flags (int fd, int add_flags, char const *name)
1634 /* Ignore file creation flags that are no-ops on file descriptors. */
1635 add_flags &= ~ (O_NOCTTY | O_NOFOLLOW);
1637 if (add_flags)
1639 int old_flags = fcntl (fd, F_GETFL);
1640 int new_flags = old_flags | add_flags;
1641 bool ok = true;
1642 if (old_flags < 0)
1643 ok = false;
1644 else if (old_flags != new_flags)
1646 if (new_flags & (O_DIRECTORY | O_NOLINKS))
1648 /* NEW_FLAGS contains at least one file creation flag that
1649 requires some checking of the open file descriptor. */
1650 struct stat st;
1651 if (fstat (fd, &st) != 0)
1652 ok = false;
1653 else if ((new_flags & O_DIRECTORY) && ! S_ISDIR (st.st_mode))
1655 errno = ENOTDIR;
1656 ok = false;
1658 else if ((new_flags & O_NOLINKS) && 1 < st.st_nlink)
1660 errno = EMLINK;
1661 ok = false;
1663 new_flags &= ~ (O_DIRECTORY | O_NOLINKS);
1666 if (ok && old_flags != new_flags
1667 && fcntl (fd, F_SETFL, new_flags) == -1)
1668 ok = false;
1671 if (!ok)
1672 error (EXIT_FAILURE, errno, _("setting flags for %s"), quote (name));
1676 static char *
1677 human_size (size_t n)
1679 static char hbuf[LONGEST_HUMAN_READABLE + 1];
1680 int human_opts =
1681 (human_autoscale | human_round_to_nearest | human_base_1024
1682 | human_space_before_unit | human_SI | human_B);
1683 return human_readable (n, hbuf, human_opts, 1, 1);
1686 /* The main loop. */
1688 static int
1689 dd_copy (void)
1691 char *ibuf, *bufstart; /* Input buffer. */
1692 /* These are declared static so that even though we don't free the
1693 buffers, valgrind will recognize that there is no "real" leak. */
1694 static char *real_buf; /* real buffer address before alignment */
1695 static char *real_obuf;
1696 ssize_t nread; /* Bytes read in the current block. */
1698 /* If nonzero, then the previously read block was partial and
1699 PARTREAD was its size. */
1700 size_t partread = 0;
1702 int exit_status = EXIT_SUCCESS;
1703 size_t n_bytes_read;
1705 /* Leave at least one extra byte at the beginning and end of `ibuf'
1706 for conv=swab, but keep the buffer address even. But some peculiar
1707 device drivers work only with word-aligned buffers, so leave an
1708 extra two bytes. */
1710 /* Some devices require alignment on a sector or page boundary
1711 (e.g. character disk devices). Align the input buffer to a
1712 page boundary to cover all bases. Note that due to the swab
1713 algorithm, we must have at least one byte in the page before
1714 the input buffer; thus we allocate 2 pages of slop in the
1715 real buffer. 8k above the blocksize shouldn't bother anyone.
1717 The page alignment is necessary on any Linux kernel that supports
1718 either the SGI raw I/O patch or Steven Tweedies raw I/O patch.
1719 It is necessary when accessing raw (i.e. character special) disk
1720 devices on Unixware or other SVR4-derived system. */
1722 real_buf = malloc (input_blocksize + INPUT_BLOCK_SLOP);
1723 if (!real_buf)
1724 error (EXIT_FAILURE, 0,
1725 _("memory exhausted by input buffer of size %zu bytes (%s)"),
1726 input_blocksize, human_size (input_blocksize));
1728 ibuf = real_buf;
1729 ibuf += SWAB_ALIGN_OFFSET; /* allow space for swab */
1731 ibuf = ptr_align (ibuf, page_size);
1733 if (conversions_mask & C_TWOBUFS)
1735 /* Page-align the output buffer, too. */
1736 real_obuf = malloc (output_blocksize + OUTPUT_BLOCK_SLOP);
1737 if (!real_obuf)
1738 error (EXIT_FAILURE, 0,
1739 _("memory exhausted by output buffer of size %zu bytes (%s)"),
1740 output_blocksize, human_size (output_blocksize));
1741 obuf = ptr_align (real_obuf, page_size);
1743 else
1745 real_obuf = NULL;
1746 obuf = ibuf;
1749 if (skip_records != 0)
1751 uintmax_t us_bytes = input_offset + (skip_records * input_blocksize);
1752 uintmax_t us_blocks = skip (STDIN_FILENO, input_file,
1753 skip_records, input_blocksize, ibuf);
1754 us_bytes -= input_offset;
1756 /* POSIX doesn't say what to do when dd detects it has been
1757 asked to skip past EOF, so I assume it's non-fatal.
1758 There are 3 reasons why there might be unskipped blocks/bytes:
1759 1. file is too small
1760 2. pipe has not enough data
1761 3. short reads */
1762 if (us_blocks || (!input_offset_overflow && us_bytes))
1764 error (0, 0,
1765 _("%s: cannot skip to specified offset"), quote (input_file));
1769 if (seek_records != 0)
1771 uintmax_t write_records = skip (STDOUT_FILENO, output_file,
1772 seek_records, output_blocksize, obuf);
1774 if (write_records != 0)
1776 memset (obuf, 0, output_blocksize);
1779 if (iwrite (STDOUT_FILENO, obuf, output_blocksize)
1780 != output_blocksize)
1782 error (0, errno, _("writing to %s"), quote (output_file));
1783 quit (EXIT_FAILURE);
1785 while (--write_records != 0);
1789 if (max_records == 0)
1790 return exit_status;
1792 while (1)
1794 if (r_partial + r_full >= max_records)
1795 break;
1797 /* Zero the buffer before reading, so that if we get a read error,
1798 whatever data we are able to read is followed by zeros.
1799 This minimizes data loss. */
1800 if ((conversions_mask & C_SYNC) && (conversions_mask & C_NOERROR))
1801 memset (ibuf,
1802 (conversions_mask & (C_BLOCK | C_UNBLOCK)) ? ' ' : '\0',
1803 input_blocksize);
1805 nread = iread_fnc (STDIN_FILENO, ibuf, input_blocksize);
1807 if (nread >= 0 && i_nocache)
1808 invalidate_cache (STDIN_FILENO, nread);
1810 if (nread == 0)
1811 break; /* EOF. */
1813 if (nread < 0)
1815 error (0, errno, _("reading %s"), quote (input_file));
1816 if (conversions_mask & C_NOERROR)
1818 print_stats ();
1819 size_t bad_portion = input_blocksize - partread;
1821 /* We already know this data is not cached,
1822 but call this so that correct offsets are maintained. */
1823 invalidate_cache (STDIN_FILENO, bad_portion);
1825 /* Seek past the bad block if possible. */
1826 if (!advance_input_after_read_error (bad_portion))
1828 exit_status = EXIT_FAILURE;
1830 /* Suppress duplicate diagnostics. */
1831 input_seekable = false;
1832 input_seek_errno = ESPIPE;
1834 if ((conversions_mask & C_SYNC) && !partread)
1835 /* Replace the missing input with null bytes and
1836 proceed normally. */
1837 nread = 0;
1838 else
1839 continue;
1841 else
1843 /* Write any partial block. */
1844 exit_status = EXIT_FAILURE;
1845 break;
1849 n_bytes_read = nread;
1850 advance_input_offset (nread);
1852 if (n_bytes_read < input_blocksize)
1854 r_partial++;
1855 partread = n_bytes_read;
1856 if (conversions_mask & C_SYNC)
1858 if (!(conversions_mask & C_NOERROR))
1859 /* If C_NOERROR, we zeroed the block before reading. */
1860 memset (ibuf + n_bytes_read,
1861 (conversions_mask & (C_BLOCK | C_UNBLOCK)) ? ' ' : '\0',
1862 input_blocksize - n_bytes_read);
1863 n_bytes_read = input_blocksize;
1866 else
1868 r_full++;
1869 partread = 0;
1872 if (ibuf == obuf) /* If not C_TWOBUFS. */
1874 size_t nwritten = iwrite (STDOUT_FILENO, obuf, n_bytes_read);
1875 w_bytes += nwritten;
1876 if (nwritten != n_bytes_read)
1878 error (0, errno, _("writing %s"), quote (output_file));
1879 return EXIT_FAILURE;
1881 else if (n_bytes_read == input_blocksize)
1882 w_full++;
1883 else
1884 w_partial++;
1885 continue;
1888 /* Do any translations on the whole buffer at once. */
1890 if (translation_needed)
1891 translate_buffer (ibuf, n_bytes_read);
1893 if (conversions_mask & C_SWAB)
1894 bufstart = swab_buffer (ibuf, &n_bytes_read);
1895 else
1896 bufstart = ibuf;
1898 if (conversions_mask & C_BLOCK)
1899 copy_with_block (bufstart, n_bytes_read);
1900 else if (conversions_mask & C_UNBLOCK)
1901 copy_with_unblock (bufstart, n_bytes_read);
1902 else
1903 copy_simple (bufstart, n_bytes_read);
1906 /* If we have a char left as a result of conv=swab, output it. */
1907 if (char_is_saved)
1909 if (conversions_mask & C_BLOCK)
1910 copy_with_block (&saved_char, 1);
1911 else if (conversions_mask & C_UNBLOCK)
1912 copy_with_unblock (&saved_char, 1);
1913 else
1914 output_char (saved_char);
1917 if ((conversions_mask & C_BLOCK) && col > 0)
1919 /* If the final input line didn't end with a '\n', pad
1920 the output block to `conversion_blocksize' chars. */
1921 size_t i;
1922 for (i = col; i < conversion_blocksize; i++)
1923 output_char (space_character);
1926 if (col && (conversions_mask & C_UNBLOCK))
1928 /* If there was any output, add a final '\n'. */
1929 output_char (newline_character);
1932 /* Write out the last block. */
1933 if (oc != 0)
1935 size_t nwritten = iwrite (STDOUT_FILENO, obuf, oc);
1936 w_bytes += nwritten;
1937 if (nwritten != 0)
1938 w_partial++;
1939 if (nwritten != oc)
1941 error (0, errno, _("writing %s"), quote (output_file));
1942 return EXIT_FAILURE;
1946 if ((conversions_mask & C_FDATASYNC) && fdatasync (STDOUT_FILENO) != 0)
1948 if (errno != ENOSYS && errno != EINVAL)
1950 error (0, errno, _("fdatasync failed for %s"), quote (output_file));
1951 exit_status = EXIT_FAILURE;
1953 conversions_mask |= C_FSYNC;
1956 if (conversions_mask & C_FSYNC)
1957 while (fsync (STDOUT_FILENO) != 0)
1958 if (errno != EINTR)
1960 error (0, errno, _("fsync failed for %s"), quote (output_file));
1961 return EXIT_FAILURE;
1964 return exit_status;
1968 main (int argc, char **argv)
1970 int i;
1971 int exit_status;
1972 off_t offset;
1974 install_signal_handlers ();
1976 initialize_main (&argc, &argv);
1977 set_program_name (argv[0]);
1978 setlocale (LC_ALL, "");
1979 bindtextdomain (PACKAGE, LOCALEDIR);
1980 textdomain (PACKAGE);
1982 /* Arrange to close stdout if parse_long_options exits. */
1983 atexit (maybe_close_stdout);
1985 page_size = getpagesize ();
1987 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, Version,
1988 usage, AUTHORS, (char const *) NULL);
1989 close_stdout_required = false;
1991 if (getopt_long (argc, argv, "", NULL, NULL) != -1)
1992 usage (EXIT_FAILURE);
1994 /* Initialize translation table to identity translation. */
1995 for (i = 0; i < 256; i++)
1996 trans_table[i] = i;
1998 /* Decode arguments. */
1999 scanargs (argc, argv);
2001 apply_translations ();
2003 if (input_file == NULL)
2005 input_file = _("standard input");
2006 set_fd_flags (STDIN_FILENO, input_flags, input_file);
2008 else
2010 if (fd_reopen (STDIN_FILENO, input_file, O_RDONLY | input_flags, 0) < 0)
2011 error (EXIT_FAILURE, errno, _("opening %s"), quote (input_file));
2014 offset = lseek (STDIN_FILENO, 0, SEEK_CUR);
2015 input_seekable = (0 <= offset);
2016 input_offset = MAX (0, offset);
2017 input_seek_errno = errno;
2019 if (output_file == NULL)
2021 output_file = _("standard output");
2022 set_fd_flags (STDOUT_FILENO, output_flags, output_file);
2024 else
2026 mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
2027 int opts
2028 = (output_flags
2029 | (conversions_mask & C_NOCREAT ? 0 : O_CREAT)
2030 | (conversions_mask & C_EXCL ? O_EXCL : 0)
2031 | (seek_records || (conversions_mask & C_NOTRUNC) ? 0 : O_TRUNC));
2033 /* Open the output file with *read* access only if we might
2034 need to read to satisfy a `seek=' request. If we can't read
2035 the file, go ahead with write-only access; it might work. */
2036 if ((! seek_records
2037 || fd_reopen (STDOUT_FILENO, output_file, O_RDWR | opts, perms) < 0)
2038 && (fd_reopen (STDOUT_FILENO, output_file, O_WRONLY | opts, perms)
2039 < 0))
2040 error (EXIT_FAILURE, errno, _("opening %s"), quote (output_file));
2042 if (seek_records != 0 && !(conversions_mask & C_NOTRUNC))
2044 uintmax_t size = seek_records * output_blocksize;
2045 unsigned long int obs = output_blocksize;
2047 if (OFF_T_MAX / output_blocksize < seek_records)
2048 error (EXIT_FAILURE, 0,
2049 _("offset too large: "
2050 "cannot truncate to a length of seek=%"PRIuMAX""
2051 " (%lu-byte) blocks"),
2052 seek_records, obs);
2054 if (ftruncate (STDOUT_FILENO, size) != 0)
2056 /* Complain only when ftruncate fails on a regular file, a
2057 directory, or a shared memory object, as POSIX 1003.1-2004
2058 specifies ftruncate's behavior only for these file types.
2059 For example, do not complain when Linux kernel 2.4 ftruncate
2060 fails on /dev/fd0. */
2061 int ftruncate_errno = errno;
2062 struct stat stdout_stat;
2063 if (fstat (STDOUT_FILENO, &stdout_stat) != 0)
2064 error (EXIT_FAILURE, errno, _("cannot fstat %s"),
2065 quote (output_file));
2066 if (S_ISREG (stdout_stat.st_mode)
2067 || S_ISDIR (stdout_stat.st_mode)
2068 || S_TYPEISSHM (&stdout_stat))
2069 error (EXIT_FAILURE, ftruncate_errno,
2070 _("failed to truncate to %"PRIuMAX" bytes"
2071 " in output file %s"),
2072 size, quote (output_file));
2077 start_time = gethrxtime ();
2079 exit_status = dd_copy ();
2081 if (max_records == 0)
2083 /* Special case to invalidate cache to end of file. */
2084 if (i_nocache && !invalidate_cache (STDIN_FILENO, 0))
2086 error (0, errno, _("failed to discard cache for: %s"),
2087 quote (input_file));
2088 exit_status = EXIT_FAILURE;
2090 if (o_nocache && !invalidate_cache (STDOUT_FILENO, 0))
2092 error (0, errno, _("failed to discard cache for: %s"),
2093 quote (output_file));
2094 exit_status = EXIT_FAILURE;
2097 else if (max_records != (uintmax_t) -1)
2099 /* Invalidate any pending region less that page size,
2100 in case the kernel might round up. */
2101 if (i_nocache)
2102 invalidate_cache (STDIN_FILENO, 0);
2103 if (o_nocache)
2104 invalidate_cache (STDOUT_FILENO, 0);
2107 quit (exit_status);