ls: fix possible incorrect exit status when recursing directories
[coreutils.git] / src / dd.c
blob59d6cd1dcb94a70e945fbe6cc3d6b804c5910abc
1 /* dd -- convert a file while copying it.
2 Copyright (C) 1985-2013 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 <assert.h>
24 #include <sys/types.h>
25 #include <signal.h>
26 #include <getopt.h>
28 #include "system.h"
29 #include "close-stream.h"
30 #include "error.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 /* The official name of this program (e.g., no 'g' prefix). */
41 #define PROGRAM_NAME "dd"
43 #define AUTHORS \
44 proper_name ("Paul Rubin"), \
45 proper_name ("David MacKenzie"), \
46 proper_name ("Stuart Kemp")
48 /* Use SA_NOCLDSTOP as a proxy for whether the sigaction machinery is
49 present. */
50 #ifndef SA_NOCLDSTOP
51 # define SA_NOCLDSTOP 0
52 # define sigprocmask(How, Set, Oset) /* empty */
53 # define sigset_t int
54 # if ! HAVE_SIGINTERRUPT
55 # define siginterrupt(sig, flag) /* empty */
56 # endif
57 #endif
59 /* NonStop circa 2011 lacks SA_RESETHAND; see Bug#9076. */
60 #ifndef SA_RESETHAND
61 # define SA_RESETHAND 0
62 #endif
64 #ifndef SIGINFO
65 # define SIGINFO SIGUSR1
66 #endif
68 /* This may belong in GNULIB's fcntl module instead.
69 Define O_CIO to 0 if it is not supported by this OS. */
70 #ifndef O_CIO
71 # define O_CIO 0
72 #endif
74 /* On AIX 5.1 and AIX 5.2, O_NOCACHE is defined via <fcntl.h>
75 and would interfere with our use of that name, below. */
76 #undef O_NOCACHE
78 #if ! HAVE_FDATASYNC
79 # define fdatasync(fd) (errno = ENOSYS, -1)
80 #endif
82 #define output_char(c) \
83 do \
84 { \
85 obuf[oc++] = (c); \
86 if (oc >= output_blocksize) \
87 write_output (); \
88 } \
89 while (0)
91 /* Default input and output blocksize. */
92 #define DEFAULT_BLOCKSIZE 512
94 /* How many bytes to add to the input and output block sizes before invoking
95 malloc. See dd_copy for details. INPUT_BLOCK_SLOP must be no less than
96 OUTPUT_BLOCK_SLOP. */
97 #define INPUT_BLOCK_SLOP (2 * SWAB_ALIGN_OFFSET + 2 * page_size - 1)
98 #define OUTPUT_BLOCK_SLOP (page_size - 1)
100 /* Maximum blocksize for the given SLOP.
101 Keep it smaller than SIZE_MAX - SLOP, so that we can
102 allocate buffers that size. Keep it smaller than SSIZE_MAX, for
103 the benefit of system calls like "read". And keep it smaller than
104 OFF_T_MAX, for the benefit of the large-offset seek code. */
105 #define MAX_BLOCKSIZE(slop) MIN (SIZE_MAX - (slop), MIN (SSIZE_MAX, OFF_T_MAX))
107 /* Conversions bit masks. */
108 enum
110 C_ASCII = 01,
112 C_EBCDIC = 02,
113 C_IBM = 04,
114 C_BLOCK = 010,
115 C_UNBLOCK = 020,
116 C_LCASE = 040,
117 C_UCASE = 0100,
118 C_SWAB = 0200,
119 C_NOERROR = 0400,
120 C_NOTRUNC = 01000,
121 C_SYNC = 02000,
123 /* Use separate input and output buffers, and combine partial
124 input blocks. */
125 C_TWOBUFS = 04000,
127 C_NOCREAT = 010000,
128 C_EXCL = 020000,
129 C_FDATASYNC = 040000,
130 C_FSYNC = 0100000,
132 C_SPARSE = 0200000
135 /* Status bit masks. */
136 enum
138 STATUS_NOXFER = 01,
139 STATUS_NONE = 02
142 /* The name of the input file, or NULL for the standard input. */
143 static char const *input_file = NULL;
145 /* The name of the output file, or NULL for the standard output. */
146 static char const *output_file = NULL;
148 /* The page size on this host. */
149 static size_t page_size;
151 /* The number of bytes in which atomic reads are done. */
152 static size_t input_blocksize = 0;
154 /* The number of bytes in which atomic writes are done. */
155 static size_t output_blocksize = 0;
157 /* Conversion buffer size, in bytes. 0 prevents conversions. */
158 static size_t conversion_blocksize = 0;
160 /* Skip this many records of 'input_blocksize' bytes before input. */
161 static uintmax_t skip_records = 0;
163 /* Skip this many bytes before input in addition of 'skip_records'
164 records. */
165 static size_t skip_bytes = 0;
167 /* Skip this many records of 'output_blocksize' bytes before output. */
168 static uintmax_t seek_records = 0;
170 /* Skip this many bytes in addition to 'seek_records' records before
171 output. */
172 static uintmax_t seek_bytes = 0;
174 /* Whether the final output was done with a seek (rather than a write). */
175 static bool final_op_was_seek;
177 /* Copy only this many records. The default is effectively infinity. */
178 static uintmax_t max_records = (uintmax_t) -1;
180 /* Copy this many bytes in addition to 'max_records' records. */
181 static size_t max_bytes = 0;
183 /* Bit vector of conversions to apply. */
184 static int conversions_mask = 0;
186 /* Open flags for the input and output files. */
187 static int input_flags = 0;
188 static int output_flags = 0;
190 /* Status flags for what is printed to stderr. */
191 static int status_flags = 0;
193 /* If nonzero, filter characters through the translation table. */
194 static bool translation_needed = false;
196 /* Number of partial blocks written. */
197 static uintmax_t w_partial = 0;
199 /* Number of full blocks written. */
200 static uintmax_t w_full = 0;
202 /* Number of partial blocks read. */
203 static uintmax_t r_partial = 0;
205 /* Number of full blocks read. */
206 static uintmax_t r_full = 0;
208 /* Number of bytes written. */
209 static uintmax_t w_bytes = 0;
211 /* Time that dd started. */
212 static xtime_t start_time;
214 /* True if input is seekable. */
215 static bool input_seekable;
217 /* Error number corresponding to initial attempt to lseek input.
218 If ESPIPE, do not issue any more diagnostics about it. */
219 static int input_seek_errno;
221 /* File offset of the input, in bytes, along with a flag recording
222 whether it overflowed. */
223 static uintmax_t input_offset;
224 static bool input_offset_overflow;
226 /* True if a partial read should be diagnosed. */
227 static bool warn_partial_read;
229 /* Records truncated by conv=block. */
230 static uintmax_t r_truncate = 0;
232 /* Output representation of newline and space characters.
233 They change if we're converting to EBCDIC. */
234 static char newline_character = '\n';
235 static char space_character = ' ';
237 /* Input buffer. */
238 static char *ibuf;
240 /* Output buffer. */
241 static char *obuf;
243 /* Current index into 'obuf'. */
244 static size_t oc = 0;
246 /* Index into current line, for 'conv=block' and 'conv=unblock'. */
247 static size_t col = 0;
249 /* The set of signals that are caught. */
250 static sigset_t caught_signals;
252 /* If nonzero, the value of the pending fatal signal. */
253 static sig_atomic_t volatile interrupt_signal;
255 /* A count of the number of pending info signals that have been received. */
256 static sig_atomic_t volatile info_signal_count;
258 /* Whether to discard cache for input or output. */
259 static bool i_nocache, o_nocache;
261 /* Function used for read (to handle iflag=fullblock parameter). */
262 static ssize_t (*iread_fnc) (int fd, char *buf, size_t size);
264 /* A longest symbol in the struct symbol_values tables below. */
265 #define LONGEST_SYMBOL "count_bytes"
267 /* A symbol and the corresponding integer value. */
268 struct symbol_value
270 char symbol[sizeof LONGEST_SYMBOL];
271 int value;
274 /* Conversion symbols, for conv="...". */
275 static struct symbol_value const conversions[] =
277 {"ascii", C_ASCII | C_TWOBUFS}, /* EBCDIC to ASCII. */
278 {"ebcdic", C_EBCDIC | C_TWOBUFS}, /* ASCII to EBCDIC. */
279 {"ibm", C_IBM | C_TWOBUFS}, /* Slightly different ASCII to EBCDIC. */
280 {"block", C_BLOCK | C_TWOBUFS}, /* Variable to fixed length records. */
281 {"unblock", C_UNBLOCK | C_TWOBUFS}, /* Fixed to variable length records. */
282 {"lcase", C_LCASE | C_TWOBUFS}, /* Translate upper to lower case. */
283 {"ucase", C_UCASE | C_TWOBUFS}, /* Translate lower to upper case. */
284 {"sparse", C_SPARSE}, /* Try to sparsely write output. */
285 {"swab", C_SWAB | C_TWOBUFS}, /* Swap bytes of input. */
286 {"noerror", C_NOERROR}, /* Ignore i/o errors. */
287 {"nocreat", C_NOCREAT}, /* Do not create output file. */
288 {"excl", C_EXCL}, /* Fail if the output file already exists. */
289 {"notrunc", C_NOTRUNC}, /* Do not truncate output file. */
290 {"sync", C_SYNC}, /* Pad input records to ibs with NULs. */
291 {"fdatasync", C_FDATASYNC}, /* Synchronize output data before finishing. */
292 {"fsync", C_FSYNC}, /* Also synchronize output metadata. */
293 {"", 0}
296 #define FFS_MASK(x) ((x) ^ ((x) & ((x) - 1)))
297 enum
299 /* Compute a value that's bitwise disjoint from the union
300 of all O_ values. */
301 v = ~(0
302 | O_APPEND
303 | O_BINARY
304 | O_CIO
305 | O_DIRECT
306 | O_DIRECTORY
307 | O_DSYNC
308 | O_NOATIME
309 | O_NOCTTY
310 | O_NOFOLLOW
311 | O_NOLINKS
312 | O_NONBLOCK
313 | O_SYNC
314 | O_TEXT
317 /* Use its lowest bits for private flags. */
318 O_FULLBLOCK = FFS_MASK (v),
319 v2 = v ^ O_FULLBLOCK,
321 O_NOCACHE = FFS_MASK (v2),
322 v3 = v2 ^ O_NOCACHE,
324 O_COUNT_BYTES = FFS_MASK (v3),
325 v4 = v3 ^ O_COUNT_BYTES,
327 O_SKIP_BYTES = FFS_MASK (v4),
328 v5 = v4 ^ O_SKIP_BYTES,
330 O_SEEK_BYTES = FFS_MASK (v5)
333 /* Ensure that we got something. */
334 verify (O_FULLBLOCK != 0);
335 verify (O_NOCACHE != 0);
336 verify (O_COUNT_BYTES != 0);
337 verify (O_SKIP_BYTES != 0);
338 verify (O_SEEK_BYTES != 0);
340 #define MULTIPLE_BITS_SET(i) (((i) & ((i) - 1)) != 0)
342 /* Ensure that this is a single-bit value. */
343 verify ( ! MULTIPLE_BITS_SET (O_FULLBLOCK));
344 verify ( ! MULTIPLE_BITS_SET (O_NOCACHE));
345 verify ( ! MULTIPLE_BITS_SET (O_COUNT_BYTES));
346 verify ( ! MULTIPLE_BITS_SET (O_SKIP_BYTES));
347 verify ( ! MULTIPLE_BITS_SET (O_SEEK_BYTES));
349 /* Flags, for iflag="..." and oflag="...". */
350 static struct symbol_value const flags[] =
352 {"append", O_APPEND},
353 {"binary", O_BINARY},
354 {"cio", O_CIO},
355 {"direct", O_DIRECT},
356 {"directory", O_DIRECTORY},
357 {"dsync", O_DSYNC},
358 {"noatime", O_NOATIME},
359 {"nocache", O_NOCACHE}, /* Discard cache. */
360 {"noctty", O_NOCTTY},
361 {"nofollow", HAVE_WORKING_O_NOFOLLOW ? O_NOFOLLOW : 0},
362 {"nolinks", O_NOLINKS},
363 {"nonblock", O_NONBLOCK},
364 {"sync", O_SYNC},
365 {"text", O_TEXT},
366 {"fullblock", O_FULLBLOCK}, /* Accumulate full blocks from input. */
367 {"count_bytes", O_COUNT_BYTES},
368 {"skip_bytes", O_SKIP_BYTES},
369 {"seek_bytes", O_SEEK_BYTES},
370 {"", 0}
373 /* Status, for status="...". */
374 static struct symbol_value const statuses[] =
376 {"noxfer", STATUS_NOXFER},
377 {"none", STATUS_NONE},
378 {"", 0}
381 /* Translation table formed by applying successive transformations. */
382 static unsigned char trans_table[256];
384 static char const ascii_to_ebcdic[] =
386 '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
387 '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
388 '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
389 '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
390 '\100', '\117', '\177', '\173', '\133', '\154', '\120', '\175',
391 '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
392 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
393 '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
394 '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
395 '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
396 '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
397 '\347', '\350', '\351', '\112', '\340', '\132', '\137', '\155',
398 '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
399 '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
400 '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
401 '\247', '\250', '\251', '\300', '\152', '\320', '\241', '\007',
402 '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
403 '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
404 '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
405 '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
406 '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
407 '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
408 '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
409 '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
410 '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
411 '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',
412 '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',
413 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
414 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
415 '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
416 '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
417 '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
420 static char const ascii_to_ibm[] =
422 '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
423 '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
424 '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
425 '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
426 '\100', '\132', '\177', '\173', '\133', '\154', '\120', '\175',
427 '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
428 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
429 '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
430 '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
431 '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
432 '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
433 '\347', '\350', '\351', '\255', '\340', '\275', '\137', '\155',
434 '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
435 '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
436 '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
437 '\247', '\250', '\251', '\300', '\117', '\320', '\241', '\007',
438 '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
439 '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
440 '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
441 '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
442 '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
443 '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
444 '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
445 '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
446 '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
447 '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',
448 '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',
449 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
450 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
451 '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
452 '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
453 '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
456 static char const ebcdic_to_ascii[] =
458 '\000', '\001', '\002', '\003', '\234', '\011', '\206', '\177',
459 '\227', '\215', '\216', '\013', '\014', '\015', '\016', '\017',
460 '\020', '\021', '\022', '\023', '\235', '\205', '\010', '\207',
461 '\030', '\031', '\222', '\217', '\034', '\035', '\036', '\037',
462 '\200', '\201', '\202', '\203', '\204', '\012', '\027', '\033',
463 '\210', '\211', '\212', '\213', '\214', '\005', '\006', '\007',
464 '\220', '\221', '\026', '\223', '\224', '\225', '\226', '\004',
465 '\230', '\231', '\232', '\233', '\024', '\025', '\236', '\032',
466 '\040', '\240', '\241', '\242', '\243', '\244', '\245', '\246',
467 '\247', '\250', '\133', '\056', '\074', '\050', '\053', '\041',
468 '\046', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
469 '\260', '\261', '\135', '\044', '\052', '\051', '\073', '\136',
470 '\055', '\057', '\262', '\263', '\264', '\265', '\266', '\267',
471 '\270', '\271', '\174', '\054', '\045', '\137', '\076', '\077',
472 '\272', '\273', '\274', '\275', '\276', '\277', '\300', '\301',
473 '\302', '\140', '\072', '\043', '\100', '\047', '\075', '\042',
474 '\303', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
475 '\150', '\151', '\304', '\305', '\306', '\307', '\310', '\311',
476 '\312', '\152', '\153', '\154', '\155', '\156', '\157', '\160',
477 '\161', '\162', '\313', '\314', '\315', '\316', '\317', '\320',
478 '\321', '\176', '\163', '\164', '\165', '\166', '\167', '\170',
479 '\171', '\172', '\322', '\323', '\324', '\325', '\326', '\327',
480 '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
481 '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
482 '\173', '\101', '\102', '\103', '\104', '\105', '\106', '\107',
483 '\110', '\111', '\350', '\351', '\352', '\353', '\354', '\355',
484 '\175', '\112', '\113', '\114', '\115', '\116', '\117', '\120',
485 '\121', '\122', '\356', '\357', '\360', '\361', '\362', '\363',
486 '\134', '\237', '\123', '\124', '\125', '\126', '\127', '\130',
487 '\131', '\132', '\364', '\365', '\366', '\367', '\370', '\371',
488 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
489 '\070', '\071', '\372', '\373', '\374', '\375', '\376', '\377'
492 /* True if we need to close the standard output *stream*. */
493 static bool close_stdout_required = true;
495 /* The only reason to close the standard output *stream* is if
496 parse_long_options fails (as it does for --help or --version).
497 In any other case, dd uses only the STDOUT_FILENO file descriptor,
498 and the "cleanup" function calls "close (STDOUT_FILENO)".
499 Closing the file descriptor and then letting the usual atexit-run
500 close_stdout function call "fclose (stdout)" would result in a
501 harmless failure of the close syscall (with errno EBADF).
502 This function serves solely to avoid the unnecessary close_stdout
503 call, once parse_long_options has succeeded.
504 Meanwhile, we guarantee that the standard error stream is flushed,
505 by inlining the last half of close_stdout as needed. */
506 static void
507 maybe_close_stdout (void)
509 if (close_stdout_required)
510 close_stdout ();
511 else if (close_stream (stderr) != 0)
512 _exit (EXIT_FAILURE);
515 void
516 usage (int status)
518 if (status != EXIT_SUCCESS)
519 emit_try_help ();
520 else
522 printf (_("\
523 Usage: %s [OPERAND]...\n\
524 or: %s OPTION\n\
526 program_name, program_name);
527 fputs (_("\
528 Copy a file, converting and formatting according to the operands.\n\
530 bs=BYTES read and write up to BYTES bytes at a time\n\
531 cbs=BYTES convert BYTES bytes at a time\n\
532 conv=CONVS convert the file as per the comma separated symbol list\n\
533 count=N copy only N input blocks\n\
534 ibs=BYTES read up to BYTES bytes at a time (default: 512)\n\
535 "), stdout);
536 fputs (_("\
537 if=FILE read from FILE instead of stdin\n\
538 iflag=FLAGS read as per the comma separated symbol list\n\
539 obs=BYTES write BYTES bytes at a time (default: 512)\n\
540 of=FILE write to FILE instead of stdout\n\
541 oflag=FLAGS write as per the comma separated symbol list\n\
542 seek=N skip N obs-sized blocks at start of output\n\
543 skip=N skip N ibs-sized blocks at start of input\n\
544 status=WHICH WHICH info to suppress outputting to stderr;\n\
545 'noxfer' suppresses transfer stats, 'none' suppresses all\n\
546 "), stdout);
547 fputs (_("\
549 N and BYTES may be followed by the following multiplicative suffixes:\n\
550 c =1, w =2, b =512, kB =1000, K =1024, MB =1000*1000, M =1024*1024, xM =M\n\
551 GB =1000*1000*1000, G =1024*1024*1024, and so on for T, P, E, Z, Y.\n\
553 Each CONV symbol may be:\n\
555 "), stdout);
556 fputs (_("\
557 ascii from EBCDIC to ASCII\n\
558 ebcdic from ASCII to EBCDIC\n\
559 ibm from ASCII to alternate EBCDIC\n\
560 block pad newline-terminated records with spaces to cbs-size\n\
561 unblock replace trailing spaces in cbs-size records with newline\n\
562 lcase change upper case to lower case\n\
563 ucase change lower case to upper case\n\
564 sparse try to seek rather than write the output for NUL input blocks\n\
565 swab swap every pair of input bytes\n\
566 sync pad every input block with NULs to ibs-size; when used\n\
567 with block or unblock, pad with spaces rather than NULs\n\
568 "), stdout);
569 fputs (_("\
570 excl fail if the output file already exists\n\
571 nocreat do not create the output file\n\
572 notrunc do not truncate the output file\n\
573 noerror continue after read errors\n\
574 fdatasync physically write output file data before finishing\n\
575 fsync likewise, but also write metadata\n\
576 "), stdout);
577 fputs (_("\
579 Each FLAG symbol may be:\n\
581 append append mode (makes sense only for output; conv=notrunc suggested)\n\
582 "), stdout);
583 if (O_CIO)
584 fputs (_(" cio use concurrent I/O for data\n"), stdout);
585 if (O_DIRECT)
586 fputs (_(" direct use direct I/O for data\n"), stdout);
587 if (O_DIRECTORY)
588 fputs (_(" directory fail unless a directory\n"), stdout);
589 if (O_DSYNC)
590 fputs (_(" dsync use synchronized I/O for data\n"), stdout);
591 if (O_SYNC)
592 fputs (_(" sync likewise, but also for metadata\n"), stdout);
593 fputs (_(" fullblock accumulate full blocks of input (iflag only)\n"),
594 stdout);
595 if (O_NONBLOCK)
596 fputs (_(" nonblock use non-blocking I/O\n"), stdout);
597 if (O_NOATIME)
598 fputs (_(" noatime do not update access time\n"), stdout);
599 #if HAVE_POSIX_FADVISE
600 if (O_NOCACHE)
601 fputs (_(" nocache discard cached data\n"), stdout);
602 #endif
603 if (O_NOCTTY)
604 fputs (_(" noctty do not assign controlling terminal from file\n"),
605 stdout);
606 if (HAVE_WORKING_O_NOFOLLOW)
607 fputs (_(" nofollow do not follow symlinks\n"), stdout);
608 if (O_NOLINKS)
609 fputs (_(" nolinks fail if multiply-linked\n"), stdout);
610 if (O_BINARY)
611 fputs (_(" binary use binary I/O for data\n"), stdout);
612 if (O_TEXT)
613 fputs (_(" text use text I/O for data\n"), stdout);
614 if (O_COUNT_BYTES)
615 fputs (_(" count_bytes treat 'count=N' as a byte count (iflag only)\n\
616 "), stdout);
617 if (O_SKIP_BYTES)
618 fputs (_(" skip_bytes treat 'skip=N' as a byte count (iflag only)\n\
619 "), stdout);
620 if (O_SEEK_BYTES)
621 fputs (_(" seek_bytes treat 'seek=N' as a byte count (oflag only)\n\
622 "), stdout);
625 char const *siginfo_name = (SIGINFO == SIGUSR1 ? "USR1" : "INFO");
626 printf (_("\
628 Sending a %s signal to a running 'dd' process makes it\n\
629 print I/O statistics to standard error and then resume copying.\n\
631 $ dd if=/dev/zero of=/dev/null& pid=$!\n\
632 $ kill -%s $pid; sleep 1; kill $pid\n\
633 18335302+0 records in\n\
634 18335302+0 records out\n\
635 9387674624 bytes (9.4 GB) copied, 34.6279 seconds, 271 MB/s\n\
637 Options are:\n\
640 siginfo_name, siginfo_name);
643 fputs (HELP_OPTION_DESCRIPTION, stdout);
644 fputs (VERSION_OPTION_DESCRIPTION, stdout);
645 emit_ancillary_info ();
647 exit (status);
650 static char *
651 human_size (size_t n)
653 static char hbuf[LONGEST_HUMAN_READABLE + 1];
654 int human_opts =
655 (human_autoscale | human_round_to_nearest | human_base_1024
656 | human_space_before_unit | human_SI | human_B);
657 return human_readable (n, hbuf, human_opts, 1, 1);
660 /* Ensure input buffer IBUF is allocated. */
662 static void
663 alloc_ibuf (void)
665 if (ibuf)
666 return;
668 char *real_buf = malloc (input_blocksize + INPUT_BLOCK_SLOP);
669 if (!real_buf)
670 error (EXIT_FAILURE, 0,
671 _("memory exhausted by input buffer of size %zu bytes (%s)"),
672 input_blocksize, human_size (input_blocksize));
674 real_buf += SWAB_ALIGN_OFFSET; /* allow space for swab */
676 ibuf = ptr_align (real_buf, page_size);
679 /* Ensure output buffer OBUF is allocated/initialized. */
681 static void
682 alloc_obuf (void)
684 if (obuf)
685 return;
687 if (conversions_mask & C_TWOBUFS)
689 /* Page-align the output buffer, too. */
690 char *real_obuf = malloc (output_blocksize + OUTPUT_BLOCK_SLOP);
691 if (!real_obuf)
692 error (EXIT_FAILURE, 0,
693 _("memory exhausted by output buffer of size %zu bytes (%s)"),
694 output_blocksize, human_size (output_blocksize));
695 obuf = ptr_align (real_obuf, page_size);
697 else
699 alloc_ibuf ();
700 obuf = ibuf;
703 /* Write a sentinel to the slop after the buffer,
704 to allow efficient checking for NUL blocks. */
705 assert (sizeof (uintptr_t) <= OUTPUT_BLOCK_SLOP);
706 memset (obuf + output_blocksize, 1, sizeof (uintptr_t));
709 static void
710 translate_charset (char const *new_trans)
712 int i;
714 for (i = 0; i < 256; i++)
715 trans_table[i] = new_trans[trans_table[i]];
716 translation_needed = true;
719 /* Return true if I has more than one bit set. I must be nonnegative. */
721 static inline bool
722 multiple_bits_set (int i)
724 return MULTIPLE_BITS_SET (i);
727 /* Print transfer statistics. */
729 static void
730 print_stats (void)
732 char hbuf[LONGEST_HUMAN_READABLE + 1];
733 int human_opts =
734 (human_autoscale | human_round_to_nearest
735 | human_space_before_unit | human_SI | human_B);
736 double delta_s;
737 char const *bytes_per_second;
739 if (status_flags & STATUS_NONE)
740 return;
742 fprintf (stderr,
743 _("%"PRIuMAX"+%"PRIuMAX" records in\n"
744 "%"PRIuMAX"+%"PRIuMAX" records out\n"),
745 r_full, r_partial, w_full, w_partial);
747 if (r_truncate != 0)
748 fprintf (stderr,
749 ngettext ("%"PRIuMAX" truncated record\n",
750 "%"PRIuMAX" truncated records\n",
751 select_plural (r_truncate)),
752 r_truncate);
754 if (status_flags & STATUS_NOXFER)
755 return;
757 /* Use integer arithmetic to compute the transfer rate,
758 since that makes it easy to use SI abbreviations. */
760 fprintf (stderr,
761 ngettext ("%"PRIuMAX" byte (%s) copied",
762 "%"PRIuMAX" bytes (%s) copied",
763 select_plural (w_bytes)),
764 w_bytes,
765 human_readable (w_bytes, hbuf, human_opts, 1, 1));
767 xtime_t now = gethrxtime ();
768 if (start_time < now)
770 double XTIME_PRECISIONe0 = XTIME_PRECISION;
771 uintmax_t delta_xtime = now;
772 delta_xtime -= start_time;
773 delta_s = delta_xtime / XTIME_PRECISIONe0;
774 bytes_per_second = human_readable (w_bytes, hbuf, human_opts,
775 XTIME_PRECISION, delta_xtime);
777 else
779 delta_s = 0;
780 bytes_per_second = _("Infinity B");
783 /* TRANSLATORS: The two instances of "s" in this string are the SI
784 symbol "s" (meaning second), and should not be translated.
786 This format used to be:
788 ngettext (", %g second, %s/s\n", ", %g seconds, %s/s\n", delta_s == 1)
790 but that was incorrect for languages like Polish. To fix this
791 bug we now use SI symbols even though they're a bit more
792 confusing in English. */
793 fprintf (stderr, _(", %g s, %s/s\n"), delta_s, bytes_per_second);
796 /* An ordinary signal was received; arrange for the program to exit. */
798 static void
799 interrupt_handler (int sig)
801 if (! SA_RESETHAND)
802 signal (sig, SIG_DFL);
803 interrupt_signal = sig;
806 /* An info signal was received; arrange for the program to print status. */
808 static void
809 siginfo_handler (int sig)
811 if (! SA_NOCLDSTOP)
812 signal (sig, siginfo_handler);
813 info_signal_count++;
816 /* Install the signal handlers. */
818 static void
819 install_signal_handlers (void)
821 bool catch_siginfo = ! (SIGINFO == SIGUSR1 && getenv ("POSIXLY_CORRECT"));
823 #if SA_NOCLDSTOP
825 struct sigaction act;
826 sigemptyset (&caught_signals);
827 if (catch_siginfo)
829 sigaction (SIGINFO, NULL, &act);
830 if (act.sa_handler != SIG_IGN)
831 sigaddset (&caught_signals, SIGINFO);
833 sigaction (SIGINT, NULL, &act);
834 if (act.sa_handler != SIG_IGN)
835 sigaddset (&caught_signals, SIGINT);
836 act.sa_mask = caught_signals;
838 if (sigismember (&caught_signals, SIGINFO))
840 act.sa_handler = siginfo_handler;
841 act.sa_flags = 0;
842 sigaction (SIGINFO, &act, NULL);
845 if (sigismember (&caught_signals, SIGINT))
847 act.sa_handler = interrupt_handler;
848 act.sa_flags = SA_NODEFER | SA_RESETHAND;
849 sigaction (SIGINT, &act, NULL);
852 #else
854 if (catch_siginfo && signal (SIGINFO, SIG_IGN) != SIG_IGN)
856 signal (SIGINFO, siginfo_handler);
857 siginterrupt (SIGINFO, 1);
859 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
861 signal (SIGINT, interrupt_handler);
862 siginterrupt (SIGINT, 1);
864 #endif
867 static void
868 cleanup (void)
870 if (close (STDIN_FILENO) < 0)
871 error (EXIT_FAILURE, errno,
872 _("closing input file %s"), quote (input_file));
874 /* Don't remove this call to close, even though close_stdout
875 closes standard output. This close is necessary when cleanup
876 is called as part of a signal handler. */
877 if (close (STDOUT_FILENO) < 0)
878 error (EXIT_FAILURE, errno,
879 _("closing output file %s"), quote (output_file));
882 /* Process any pending signals. If signals are caught, this function
883 should be called periodically. Ideally there should never be an
884 unbounded amount of time when signals are not being processed. */
886 static void
887 process_signals (void)
889 while (interrupt_signal || info_signal_count)
891 int interrupt;
892 int infos;
893 sigset_t oldset;
895 sigprocmask (SIG_BLOCK, &caught_signals, &oldset);
897 /* Reload interrupt_signal and info_signal_count, in case a new
898 signal was handled before sigprocmask took effect. */
899 interrupt = interrupt_signal;
900 infos = info_signal_count;
902 if (infos)
903 info_signal_count = infos - 1;
905 sigprocmask (SIG_SETMASK, &oldset, NULL);
907 if (interrupt)
908 cleanup ();
909 print_stats ();
910 if (interrupt)
911 raise (interrupt);
915 static void ATTRIBUTE_NORETURN
916 quit (int code)
918 cleanup ();
919 print_stats ();
920 process_signals ();
921 exit (code);
924 /* Return LEN rounded down to a multiple of PAGE_SIZE
925 while storing the remainder internally per FD.
926 Pass LEN == 0 to get the current remainder. */
928 static off_t
929 cache_round (int fd, off_t len)
931 static off_t i_pending, o_pending;
932 off_t *pending = (fd == STDIN_FILENO ? &i_pending : &o_pending);
934 if (len)
936 off_t c_pending = *pending + len;
937 *pending = c_pending % page_size;
938 if (c_pending > *pending)
939 len = c_pending - *pending;
940 else
941 len = 0;
943 else
944 len = *pending;
946 return len;
949 /* Discard the cache from the current offset of either
950 STDIN_FILENO or STDOUT_FILENO.
951 Return true on success. */
953 static bool
954 invalidate_cache (int fd, off_t len)
956 int adv_ret = -1;
958 /* Minimize syscalls. */
959 off_t clen = cache_round (fd, len);
960 if (len && !clen)
961 return true; /* Don't advise this time. */
962 if (!len && !clen && max_records)
963 return true; /* Nothing pending. */
964 off_t pending = len ? cache_round (fd, 0) : 0;
966 if (fd == STDIN_FILENO)
968 if (input_seekable)
970 /* Note we're being careful here to only invalidate what
971 we've read, so as not to dump any read ahead cache. */
972 #if HAVE_POSIX_FADVISE
973 adv_ret = posix_fadvise (fd, input_offset - clen - pending, clen,
974 POSIX_FADV_DONTNEED);
975 #else
976 errno = ENOTSUP;
977 #endif
979 else
980 errno = ESPIPE;
982 else if (fd == STDOUT_FILENO)
984 static off_t output_offset = -2;
986 if (output_offset != -1)
988 if (0 > output_offset)
990 output_offset = lseek (fd, 0, SEEK_CUR);
991 output_offset -= clen + pending;
993 if (0 <= output_offset)
995 #if HAVE_POSIX_FADVISE
996 adv_ret = posix_fadvise (fd, output_offset, clen,
997 POSIX_FADV_DONTNEED);
998 #else
999 errno = ENOTSUP;
1000 #endif
1001 output_offset += clen + pending;
1006 return adv_ret != -1 ? true : false;
1009 /* Read from FD into the buffer BUF of size SIZE, processing any
1010 signals that arrive before bytes are read. Return the number of
1011 bytes read if successful, -1 (setting errno) on failure. */
1013 static ssize_t
1014 iread (int fd, char *buf, size_t size)
1016 ssize_t nread;
1020 process_signals ();
1021 nread = read (fd, buf, size);
1023 while (nread < 0 && errno == EINTR);
1025 if (0 < nread && warn_partial_read)
1027 static ssize_t prev_nread;
1029 if (0 < prev_nread && prev_nread < size)
1031 uintmax_t prev = prev_nread;
1032 if (!(status_flags & STATUS_NONE))
1033 error (0, 0, ngettext (("warning: partial read (%"PRIuMAX" byte); "
1034 "suggest iflag=fullblock"),
1035 ("warning: partial read (%"PRIuMAX" bytes); "
1036 "suggest iflag=fullblock"),
1037 select_plural (prev)),
1038 prev);
1039 warn_partial_read = false;
1042 prev_nread = nread;
1045 return nread;
1048 /* Wrapper around iread function to accumulate full blocks. */
1049 static ssize_t
1050 iread_fullblock (int fd, char *buf, size_t size)
1052 ssize_t nread = 0;
1054 while (0 < size)
1056 ssize_t ncurr = iread (fd, buf, size);
1057 if (ncurr < 0)
1058 return ncurr;
1059 if (ncurr == 0)
1060 break;
1061 nread += ncurr;
1062 buf += ncurr;
1063 size -= ncurr;
1066 return nread;
1069 /* Write to FD the buffer BUF of size SIZE, processing any signals
1070 that arrive. Return the number of bytes written, setting errno if
1071 this is less than SIZE. Keep trying if there are partial
1072 writes. */
1074 static size_t
1075 iwrite (int fd, char const *buf, size_t size)
1077 size_t total_written = 0;
1079 if ((output_flags & O_DIRECT) && size < output_blocksize)
1081 int old_flags = fcntl (STDOUT_FILENO, F_GETFL);
1082 if (fcntl (STDOUT_FILENO, F_SETFL, old_flags & ~O_DIRECT) != 0
1083 && !(status_flags & STATUS_NONE))
1084 error (0, errno, _("failed to turn off O_DIRECT: %s"),
1085 quote (output_file));
1087 /* Since we have just turned off O_DIRECT for the final write,
1088 here we try to preserve some of its semantics. First, use
1089 posix_fadvise to tell the system not to pollute the buffer
1090 cache with this data. Don't bother to diagnose lseek or
1091 posix_fadvise failure. */
1092 invalidate_cache (STDOUT_FILENO, 0);
1094 /* Attempt to ensure that that final block is committed
1095 to disk as quickly as possible. */
1096 conversions_mask |= C_FSYNC;
1099 while (total_written < size)
1101 ssize_t nwritten = 0;
1102 process_signals ();
1104 /* Perform a seek for a NUL block if sparse output is enabled. */
1105 final_op_was_seek = false;
1106 if ((conversions_mask & C_SPARSE) && is_nul (buf, size))
1108 if (lseek (fd, size, SEEK_CUR) < 0)
1110 conversions_mask &= ~C_SPARSE;
1111 /* Don't warn about the advisory sparse request. */
1113 else
1115 final_op_was_seek = true;
1116 nwritten = size;
1120 if (!nwritten)
1121 nwritten = write (fd, buf + total_written, size - total_written);
1123 if (nwritten < 0)
1125 if (errno != EINTR)
1126 break;
1128 else if (nwritten == 0)
1130 /* Some buggy drivers return 0 when one tries to write beyond
1131 a device's end. (Example: Linux kernel 1.2.13 on /dev/fd0.)
1132 Set errno to ENOSPC so they get a sensible diagnostic. */
1133 errno = ENOSPC;
1134 break;
1136 else
1137 total_written += nwritten;
1140 if (o_nocache && total_written)
1141 invalidate_cache (fd, total_written);
1143 return total_written;
1146 /* Write, then empty, the output buffer 'obuf'. */
1148 static void
1149 write_output (void)
1151 size_t nwritten = iwrite (STDOUT_FILENO, obuf, output_blocksize);
1152 w_bytes += nwritten;
1153 if (nwritten != output_blocksize)
1155 error (0, errno, _("writing to %s"), quote (output_file));
1156 if (nwritten != 0)
1157 w_partial++;
1158 quit (EXIT_FAILURE);
1160 else
1161 w_full++;
1162 oc = 0;
1165 /* Return true if STR is of the form "PATTERN" or "PATTERNDELIM...". */
1167 static bool _GL_ATTRIBUTE_PURE
1168 operand_matches (char const *str, char const *pattern, char delim)
1170 while (*pattern)
1171 if (*str++ != *pattern++)
1172 return false;
1173 return !*str || *str == delim;
1176 /* Interpret one "conv=..." or similar operand STR according to the
1177 symbols in TABLE, returning the flags specified. If the operand
1178 cannot be parsed, use ERROR_MSGID to generate a diagnostic. */
1180 static int
1181 parse_symbols (char const *str, struct symbol_value const *table,
1182 char const *error_msgid)
1184 int value = 0;
1186 while (true)
1188 char const *strcomma = strchr (str, ',');
1189 struct symbol_value const *entry;
1191 for (entry = table;
1192 ! (operand_matches (str, entry->symbol, ',') && entry->value);
1193 entry++)
1195 if (! entry->symbol[0])
1197 size_t slen = strcomma ? strcomma - str : strlen (str);
1198 error (0, 0, "%s: %s", _(error_msgid),
1199 quotearg_n_style_mem (0, locale_quoting_style, str, slen));
1200 usage (EXIT_FAILURE);
1204 value |= entry->value;
1205 if (!strcomma)
1206 break;
1207 str = strcomma + 1;
1210 return value;
1213 /* Return the value of STR, interpreted as a non-negative decimal integer,
1214 optionally multiplied by various values.
1215 Set *INVALID if STR does not represent a number in this format. */
1217 static uintmax_t
1218 parse_integer (const char *str, bool *invalid)
1220 uintmax_t n;
1221 char *suffix;
1222 enum strtol_error e = xstrtoumax (str, &suffix, 10, &n, "bcEGkKMPTwYZ0");
1224 if (e == LONGINT_INVALID_SUFFIX_CHAR && *suffix == 'x')
1226 uintmax_t multiplier = parse_integer (suffix + 1, invalid);
1228 if (multiplier != 0 && n * multiplier / multiplier != n)
1230 *invalid = true;
1231 return 0;
1234 n *= multiplier;
1236 else if (e != LONGINT_OK)
1238 *invalid = true;
1239 return 0;
1242 return n;
1245 /* OPERAND is of the form "X=...". Return true if X is NAME. */
1247 static bool _GL_ATTRIBUTE_PURE
1248 operand_is (char const *operand, char const *name)
1250 return operand_matches (operand, name, '=');
1253 static void
1254 scanargs (int argc, char *const *argv)
1256 int i;
1257 size_t blocksize = 0;
1258 uintmax_t count = (uintmax_t) -1;
1259 uintmax_t skip = 0;
1260 uintmax_t seek = 0;
1262 for (i = optind; i < argc; i++)
1264 char const *name = argv[i];
1265 char const *val = strchr (name, '=');
1267 if (val == NULL)
1269 error (0, 0, _("unrecognized operand %s"), quote (name));
1270 usage (EXIT_FAILURE);
1272 val++;
1274 if (operand_is (name, "if"))
1275 input_file = val;
1276 else if (operand_is (name, "of"))
1277 output_file = val;
1278 else if (operand_is (name, "conv"))
1279 conversions_mask |= parse_symbols (val, conversions,
1280 N_("invalid conversion"));
1281 else if (operand_is (name, "iflag"))
1282 input_flags |= parse_symbols (val, flags,
1283 N_("invalid input flag"));
1284 else if (operand_is (name, "oflag"))
1285 output_flags |= parse_symbols (val, flags,
1286 N_("invalid output flag"));
1287 else if (operand_is (name, "status"))
1288 status_flags |= parse_symbols (val, statuses,
1289 N_("invalid status flag"));
1290 else
1292 bool invalid = false;
1293 uintmax_t n = parse_integer (val, &invalid);
1295 if (operand_is (name, "ibs"))
1297 invalid |= ! (0 < n && n <= MAX_BLOCKSIZE (INPUT_BLOCK_SLOP));
1298 input_blocksize = n;
1300 else if (operand_is (name, "obs"))
1302 invalid |= ! (0 < n && n <= MAX_BLOCKSIZE (OUTPUT_BLOCK_SLOP));
1303 output_blocksize = n;
1305 else if (operand_is (name, "bs"))
1307 invalid |= ! (0 < n && n <= MAX_BLOCKSIZE (INPUT_BLOCK_SLOP));
1308 blocksize = n;
1310 else if (operand_is (name, "cbs"))
1312 invalid |= ! (0 < n && n <= SIZE_MAX);
1313 conversion_blocksize = n;
1315 else if (operand_is (name, "skip"))
1316 skip = n;
1317 else if (operand_is (name, "seek"))
1318 seek = n;
1319 else if (operand_is (name, "count"))
1320 count = n;
1321 else
1323 error (0, 0, _("unrecognized operand %s"), quote (name));
1324 usage (EXIT_FAILURE);
1327 if (invalid)
1328 error (EXIT_FAILURE, 0, _("invalid number %s"), quote (val));
1332 if (blocksize)
1333 input_blocksize = output_blocksize = blocksize;
1334 else
1336 /* POSIX says dd aggregates partial reads into
1337 output_blocksize if bs= is not specified. */
1338 conversions_mask |= C_TWOBUFS;
1341 if (input_blocksize == 0)
1342 input_blocksize = DEFAULT_BLOCKSIZE;
1343 if (output_blocksize == 0)
1344 output_blocksize = DEFAULT_BLOCKSIZE;
1345 if (conversion_blocksize == 0)
1346 conversions_mask &= ~(C_BLOCK | C_UNBLOCK);
1348 if (input_flags & (O_DSYNC | O_SYNC))
1349 input_flags |= O_RSYNC;
1351 if (output_flags & O_FULLBLOCK)
1353 error (0, 0, "%s: %s", _("invalid output flag"), "'fullblock'");
1354 usage (EXIT_FAILURE);
1357 if (input_flags & O_SEEK_BYTES)
1359 error (0, 0, "%s: %s", _("invalid input flag"), "'seek_bytes'");
1360 usage (EXIT_FAILURE);
1363 if (output_flags & (O_COUNT_BYTES | O_SKIP_BYTES))
1365 error (0, 0, "%s: %s", _("invalid output flag"),
1366 output_flags & O_COUNT_BYTES ? "'count_bytes'" : "'skip_bytes'");
1367 usage (EXIT_FAILURE);
1370 if (input_flags & O_SKIP_BYTES && skip != 0)
1372 skip_records = skip / input_blocksize;
1373 skip_bytes = skip % input_blocksize;
1375 else if (skip != 0)
1376 skip_records = skip;
1378 if (input_flags & O_COUNT_BYTES && count != (uintmax_t) -1)
1380 max_records = count / input_blocksize;
1381 max_bytes = count % input_blocksize;
1383 else if (count != (uintmax_t) -1)
1384 max_records = count;
1386 if (output_flags & O_SEEK_BYTES && seek != 0)
1388 seek_records = seek / output_blocksize;
1389 seek_bytes = seek % output_blocksize;
1391 else if (seek != 0)
1392 seek_records = seek;
1394 /* Warn about partial reads if bs=SIZE is given and iflag=fullblock
1395 is not, and if counting or skipping bytes or using direct I/O.
1396 This helps to avoid confusion with miscounts, and to avoid issues
1397 with direct I/O on GNU/Linux. */
1398 warn_partial_read =
1399 (! (conversions_mask & C_TWOBUFS) && ! (input_flags & O_FULLBLOCK)
1400 && (skip_records
1401 || (0 < max_records && max_records < (uintmax_t) -1)
1402 || (input_flags | output_flags) & O_DIRECT));
1404 iread_fnc = ((input_flags & O_FULLBLOCK)
1405 ? iread_fullblock
1406 : iread);
1407 input_flags &= ~O_FULLBLOCK;
1409 if (multiple_bits_set (conversions_mask & (C_ASCII | C_EBCDIC | C_IBM)))
1410 error (EXIT_FAILURE, 0, _("cannot combine any two of {ascii,ebcdic,ibm}"));
1411 if (multiple_bits_set (conversions_mask & (C_BLOCK | C_UNBLOCK)))
1412 error (EXIT_FAILURE, 0, _("cannot combine block and unblock"));
1413 if (multiple_bits_set (conversions_mask & (C_LCASE | C_UCASE)))
1414 error (EXIT_FAILURE, 0, _("cannot combine lcase and ucase"));
1415 if (multiple_bits_set (conversions_mask & (C_EXCL | C_NOCREAT)))
1416 error (EXIT_FAILURE, 0, _("cannot combine excl and nocreat"));
1417 if (multiple_bits_set (input_flags & (O_DIRECT | O_NOCACHE))
1418 || multiple_bits_set (output_flags & (O_DIRECT | O_NOCACHE)))
1419 error (EXIT_FAILURE, 0, _("cannot combine direct and nocache"));
1421 if (input_flags & O_NOCACHE)
1423 i_nocache = true;
1424 input_flags &= ~O_NOCACHE;
1426 if (output_flags & O_NOCACHE)
1428 o_nocache = true;
1429 output_flags &= ~O_NOCACHE;
1433 /* Fix up translation table. */
1435 static void
1436 apply_translations (void)
1438 int i;
1440 if (conversions_mask & C_ASCII)
1441 translate_charset (ebcdic_to_ascii);
1443 if (conversions_mask & C_UCASE)
1445 for (i = 0; i < 256; i++)
1446 trans_table[i] = toupper (trans_table[i]);
1447 translation_needed = true;
1449 else if (conversions_mask & C_LCASE)
1451 for (i = 0; i < 256; i++)
1452 trans_table[i] = tolower (trans_table[i]);
1453 translation_needed = true;
1456 if (conversions_mask & C_EBCDIC)
1458 translate_charset (ascii_to_ebcdic);
1459 newline_character = ascii_to_ebcdic['\n'];
1460 space_character = ascii_to_ebcdic[' '];
1462 else if (conversions_mask & C_IBM)
1464 translate_charset (ascii_to_ibm);
1465 newline_character = ascii_to_ibm['\n'];
1466 space_character = ascii_to_ibm[' '];
1470 /* Apply the character-set translations specified by the user
1471 to the NREAD bytes in BUF. */
1473 static void
1474 translate_buffer (char *buf, size_t nread)
1476 char *cp;
1477 size_t i;
1479 for (i = nread, cp = buf; i; i--, cp++)
1480 *cp = trans_table[to_uchar (*cp)];
1483 /* If true, the last char from the previous call to 'swab_buffer'
1484 is saved in 'saved_char'. */
1485 static bool char_is_saved = false;
1487 /* Odd char from previous call. */
1488 static char saved_char;
1490 /* Swap NREAD bytes in BUF, plus possibly an initial char from the
1491 previous call. If NREAD is odd, save the last char for the
1492 next call. Return the new start of the BUF buffer. */
1494 static char *
1495 swab_buffer (char *buf, size_t *nread)
1497 char *bufstart = buf;
1498 char *cp;
1499 size_t i;
1501 /* Is a char left from last time? */
1502 if (char_is_saved)
1504 *--bufstart = saved_char;
1505 (*nread)++;
1506 char_is_saved = false;
1509 if (*nread & 1)
1511 /* An odd number of chars are in the buffer. */
1512 saved_char = bufstart[--*nread];
1513 char_is_saved = true;
1516 /* Do the byte-swapping by moving every second character two
1517 positions toward the end, working from the end of the buffer
1518 toward the beginning. This way we only move half of the data. */
1520 cp = bufstart + *nread; /* Start one char past the last. */
1521 for (i = *nread / 2; i; i--, cp -= 2)
1522 *cp = *(cp - 2);
1524 return ++bufstart;
1527 /* Add OFFSET to the input offset, setting the overflow flag if
1528 necessary. */
1530 static void
1531 advance_input_offset (uintmax_t offset)
1533 input_offset += offset;
1534 if (input_offset < offset)
1535 input_offset_overflow = true;
1538 /* This is a wrapper for lseek. It detects and warns about a kernel
1539 bug that makes lseek a no-op for tape devices, even though the kernel
1540 lseek return value suggests that the function succeeded.
1542 The parameters are the same as those of the lseek function, but
1543 with the addition of FILENAME, the name of the file associated with
1544 descriptor FDESC. The file name is used solely in the warning that's
1545 printed when the bug is detected. Return the same value that lseek
1546 would have returned, but when the lseek bug is detected, return -1
1547 to indicate that lseek failed.
1549 The offending behavior has been confirmed with an Exabyte SCSI tape
1550 drive accessed via /dev/nst0 on both Linux 2.2.17 and 2.4.16 kernels. */
1552 #ifdef __linux__
1554 # include <sys/mtio.h>
1556 # define MT_SAME_POSITION(P, Q) \
1557 ((P).mt_resid == (Q).mt_resid \
1558 && (P).mt_fileno == (Q).mt_fileno \
1559 && (P).mt_blkno == (Q).mt_blkno)
1561 static off_t
1562 skip_via_lseek (char const *filename, int fdesc, off_t offset, int whence)
1564 struct mtget s1;
1565 struct mtget s2;
1566 bool got_original_tape_position = (ioctl (fdesc, MTIOCGET, &s1) == 0);
1567 /* known bad device type */
1568 /* && s.mt_type == MT_ISSCSI2 */
1570 off_t new_position = lseek (fdesc, offset, whence);
1571 if (0 <= new_position
1572 && got_original_tape_position
1573 && ioctl (fdesc, MTIOCGET, &s2) == 0
1574 && MT_SAME_POSITION (s1, s2))
1576 if (!(status_flags & STATUS_NONE))
1577 error (0, 0, _("warning: working around lseek kernel bug for file "
1578 "(%s)\n of mt_type=0x%0lx -- "
1579 "see <sys/mtio.h> for the list of types"),
1580 filename, s2.mt_type);
1581 errno = 0;
1582 new_position = -1;
1585 return new_position;
1587 #else
1588 # define skip_via_lseek(Filename, Fd, Offset, Whence) lseek (Fd, Offset, Whence)
1589 #endif
1591 /* Throw away RECORDS blocks of BLOCKSIZE bytes plus BYTES bytes on
1592 file descriptor FDESC, which is open with read permission for FILE.
1593 Store up to BLOCKSIZE bytes of the data at a time in IBUF or OBUF, if
1594 necessary. RECORDS or BYTES must be nonzero. If FDESC is
1595 STDIN_FILENO, advance the input offset. Return the number of
1596 records remaining, i.e., that were not skipped because EOF was
1597 reached. If FDESC is STDOUT_FILENO, on return, BYTES is the
1598 remaining bytes in addition to the remaining records. */
1600 static uintmax_t
1601 skip (int fdesc, char const *file, uintmax_t records, size_t blocksize,
1602 size_t *bytes)
1604 uintmax_t offset = records * blocksize + *bytes;
1606 /* Try lseek and if an error indicates it was an inappropriate operation --
1607 or if the file offset is not representable as an off_t --
1608 fall back on using read. */
1610 errno = 0;
1611 if (records <= OFF_T_MAX / blocksize
1612 && 0 <= skip_via_lseek (file, fdesc, offset, SEEK_CUR))
1614 if (fdesc == STDIN_FILENO)
1616 struct stat st;
1617 if (fstat (STDIN_FILENO, &st) != 0)
1618 error (EXIT_FAILURE, errno, _("cannot fstat %s"), quote (file));
1619 if (usable_st_size (&st) && st.st_size < input_offset + offset)
1621 /* When skipping past EOF, return the number of _full_ blocks
1622 * that are not skipped, and set offset to EOF, so the caller
1623 * can determine the requested skip was not satisfied. */
1624 records = ( offset - st.st_size ) / blocksize;
1625 offset = st.st_size - input_offset;
1627 else
1628 records = 0;
1629 advance_input_offset (offset);
1631 else
1633 records = 0;
1634 *bytes = 0;
1636 return records;
1638 else
1640 int lseek_errno = errno;
1642 /* The seek request may have failed above if it was too big
1643 (> device size, > max file size, etc.)
1644 Or it may not have been done at all (> OFF_T_MAX).
1645 Therefore try to seek to the end of the file,
1646 to avoid redundant reading. */
1647 if ((skip_via_lseek (file, fdesc, 0, SEEK_END)) >= 0)
1649 /* File is seekable, and we're at the end of it, and
1650 size <= OFF_T_MAX. So there's no point using read to advance. */
1652 if (!lseek_errno)
1654 /* The original seek was not attempted as offset > OFF_T_MAX.
1655 We should error for write as can't get to the desired
1656 location, even if OFF_T_MAX < max file size.
1657 For read we're not going to read any data anyway,
1658 so we should error for consistency.
1659 It would be nice to not error for /dev/{zero,null}
1660 for any offset, but that's not a significant issue. */
1661 lseek_errno = EOVERFLOW;
1664 if (fdesc == STDIN_FILENO)
1665 error (0, lseek_errno, _("%s: cannot skip"), quote (file));
1666 else
1667 error (0, lseek_errno, _("%s: cannot seek"), quote (file));
1668 /* If the file has a specific size and we've asked
1669 to skip/seek beyond the max allowable, then quit. */
1670 quit (EXIT_FAILURE);
1672 /* else file_size && offset > OFF_T_MAX or file ! seekable */
1674 char *buf;
1675 if (fdesc == STDIN_FILENO)
1677 alloc_ibuf ();
1678 buf = ibuf;
1680 else
1682 alloc_obuf ();
1683 buf = obuf;
1688 ssize_t nread = iread_fnc (fdesc, buf, records ? blocksize : *bytes);
1689 if (nread < 0)
1691 if (fdesc == STDIN_FILENO)
1693 error (0, errno, _("error reading %s"), quote (file));
1694 if (conversions_mask & C_NOERROR)
1695 print_stats ();
1697 else
1698 error (0, lseek_errno, _("%s: cannot seek"), quote (file));
1699 quit (EXIT_FAILURE);
1701 else if (nread == 0)
1702 break;
1703 else if (fdesc == STDIN_FILENO)
1704 advance_input_offset (nread);
1706 if (records != 0)
1707 records--;
1708 else
1709 *bytes = 0;
1711 while (records || *bytes);
1713 return records;
1717 /* Advance the input by NBYTES if possible, after a read error.
1718 The input file offset may or may not have advanced after the failed
1719 read; adjust it to point just after the bad record regardless.
1720 Return true if successful, or if the input is already known to not
1721 be seekable. */
1723 static bool
1724 advance_input_after_read_error (size_t nbytes)
1726 if (! input_seekable)
1728 if (input_seek_errno == ESPIPE)
1729 return true;
1730 errno = input_seek_errno;
1732 else
1734 off_t offset;
1735 advance_input_offset (nbytes);
1736 input_offset_overflow |= (OFF_T_MAX < input_offset);
1737 if (input_offset_overflow)
1739 error (0, 0, _("offset overflow while reading file %s"),
1740 quote (input_file));
1741 return false;
1743 offset = lseek (STDIN_FILENO, 0, SEEK_CUR);
1744 if (0 <= offset)
1746 off_t diff;
1747 if (offset == input_offset)
1748 return true;
1749 diff = input_offset - offset;
1750 if (! (0 <= diff && diff <= nbytes) && !(status_flags & STATUS_NONE))
1751 error (0, 0, _("warning: invalid file offset after failed read"));
1752 if (0 <= skip_via_lseek (input_file, STDIN_FILENO, diff, SEEK_CUR))
1753 return true;
1754 if (errno == 0)
1755 error (0, 0, _("cannot work around kernel bug after all"));
1759 error (0, errno, _("%s: cannot seek"), quote (input_file));
1760 return false;
1763 /* Copy NREAD bytes of BUF, with no conversions. */
1765 static void
1766 copy_simple (char const *buf, size_t nread)
1768 const char *start = buf; /* First uncopied char in BUF. */
1772 size_t nfree = MIN (nread, output_blocksize - oc);
1774 memcpy (obuf + oc, start, nfree);
1776 nread -= nfree; /* Update the number of bytes left to copy. */
1777 start += nfree;
1778 oc += nfree;
1779 if (oc >= output_blocksize)
1780 write_output ();
1782 while (nread != 0);
1785 /* Copy NREAD bytes of BUF, doing conv=block
1786 (pad newline-terminated records to 'conversion_blocksize',
1787 replacing the newline with trailing spaces). */
1789 static void
1790 copy_with_block (char const *buf, size_t nread)
1792 size_t i;
1794 for (i = nread; i; i--, buf++)
1796 if (*buf == newline_character)
1798 if (col < conversion_blocksize)
1800 size_t j;
1801 for (j = col; j < conversion_blocksize; j++)
1802 output_char (space_character);
1804 col = 0;
1806 else
1808 if (col == conversion_blocksize)
1809 r_truncate++;
1810 else if (col < conversion_blocksize)
1811 output_char (*buf);
1812 col++;
1817 /* Copy NREAD bytes of BUF, doing conv=unblock
1818 (replace trailing spaces in 'conversion_blocksize'-sized records
1819 with a newline). */
1821 static void
1822 copy_with_unblock (char const *buf, size_t nread)
1824 size_t i;
1825 char c;
1826 static size_t pending_spaces = 0;
1828 for (i = 0; i < nread; i++)
1830 c = buf[i];
1832 if (col++ >= conversion_blocksize)
1834 col = pending_spaces = 0; /* Wipe out any pending spaces. */
1835 i--; /* Push the char back; get it later. */
1836 output_char (newline_character);
1838 else if (c == space_character)
1839 pending_spaces++;
1840 else
1842 /* 'c' is the character after a run of spaces that were not
1843 at the end of the conversion buffer. Output them. */
1844 while (pending_spaces)
1846 output_char (space_character);
1847 --pending_spaces;
1849 output_char (c);
1854 /* Set the file descriptor flags for FD that correspond to the nonzero bits
1855 in ADD_FLAGS. The file's name is NAME. */
1857 static void
1858 set_fd_flags (int fd, int add_flags, char const *name)
1860 /* Ignore file creation flags that are no-ops on file descriptors. */
1861 add_flags &= ~ (O_NOCTTY | O_NOFOLLOW);
1863 if (add_flags)
1865 int old_flags = fcntl (fd, F_GETFL);
1866 int new_flags = old_flags | add_flags;
1867 bool ok = true;
1868 if (old_flags < 0)
1869 ok = false;
1870 else if (old_flags != new_flags)
1872 if (new_flags & (O_DIRECTORY | O_NOLINKS))
1874 /* NEW_FLAGS contains at least one file creation flag that
1875 requires some checking of the open file descriptor. */
1876 struct stat st;
1877 if (fstat (fd, &st) != 0)
1878 ok = false;
1879 else if ((new_flags & O_DIRECTORY) && ! S_ISDIR (st.st_mode))
1881 errno = ENOTDIR;
1882 ok = false;
1884 else if ((new_flags & O_NOLINKS) && 1 < st.st_nlink)
1886 errno = EMLINK;
1887 ok = false;
1889 new_flags &= ~ (O_DIRECTORY | O_NOLINKS);
1892 if (ok && old_flags != new_flags
1893 && fcntl (fd, F_SETFL, new_flags) == -1)
1894 ok = false;
1897 if (!ok)
1898 error (EXIT_FAILURE, errno, _("setting flags for %s"), quote (name));
1902 /* The main loop. */
1904 static int
1905 dd_copy (void)
1907 char *bufstart; /* Input buffer. */
1908 ssize_t nread; /* Bytes read in the current block. */
1910 /* If nonzero, then the previously read block was partial and
1911 PARTREAD was its size. */
1912 size_t partread = 0;
1914 int exit_status = EXIT_SUCCESS;
1915 size_t n_bytes_read;
1917 /* Leave at least one extra byte at the beginning and end of 'ibuf'
1918 for conv=swab, but keep the buffer address even. But some peculiar
1919 device drivers work only with word-aligned buffers, so leave an
1920 extra two bytes. */
1922 /* Some devices require alignment on a sector or page boundary
1923 (e.g. character disk devices). Align the input buffer to a
1924 page boundary to cover all bases. Note that due to the swab
1925 algorithm, we must have at least one byte in the page before
1926 the input buffer; thus we allocate 2 pages of slop in the
1927 real buffer. 8k above the blocksize shouldn't bother anyone.
1929 The page alignment is necessary on any Linux kernel that supports
1930 either the SGI raw I/O patch or Steven Tweedies raw I/O patch.
1931 It is necessary when accessing raw (i.e. character special) disk
1932 devices on Unixware or other SVR4-derived system. */
1934 if (skip_records != 0 || skip_bytes != 0)
1936 uintmax_t us_bytes = input_offset + (skip_records * input_blocksize)
1937 + skip_bytes;
1938 uintmax_t us_blocks = skip (STDIN_FILENO, input_file,
1939 skip_records, input_blocksize, &skip_bytes);
1940 us_bytes -= input_offset;
1942 /* POSIX doesn't say what to do when dd detects it has been
1943 asked to skip past EOF, so I assume it's non-fatal.
1944 There are 3 reasons why there might be unskipped blocks/bytes:
1945 1. file is too small
1946 2. pipe has not enough data
1947 3. partial reads */
1948 if ((us_blocks || (!input_offset_overflow && us_bytes))
1949 && !(status_flags & STATUS_NONE))
1951 error (0, 0,
1952 _("%s: cannot skip to specified offset"), quote (input_file));
1956 if (seek_records != 0 || seek_bytes != 0)
1958 size_t bytes = seek_bytes;
1959 uintmax_t write_records = skip (STDOUT_FILENO, output_file,
1960 seek_records, output_blocksize, &bytes);
1962 if (write_records != 0 || bytes != 0)
1964 memset (obuf, 0, write_records ? output_blocksize : bytes);
1968 size_t size = write_records ? output_blocksize : bytes;
1969 if (iwrite (STDOUT_FILENO, obuf, size) != size)
1971 error (0, errno, _("writing to %s"), quote (output_file));
1972 quit (EXIT_FAILURE);
1975 if (write_records != 0)
1976 write_records--;
1977 else
1978 bytes = 0;
1980 while (write_records || bytes);
1984 if (max_records == 0 && max_bytes == 0)
1985 return exit_status;
1987 alloc_ibuf ();
1988 alloc_obuf ();
1990 while (1)
1992 if (r_partial + r_full >= max_records + !!max_bytes)
1993 break;
1995 /* Zero the buffer before reading, so that if we get a read error,
1996 whatever data we are able to read is followed by zeros.
1997 This minimizes data loss. */
1998 if ((conversions_mask & C_SYNC) && (conversions_mask & C_NOERROR))
1999 memset (ibuf,
2000 (conversions_mask & (C_BLOCK | C_UNBLOCK)) ? ' ' : '\0',
2001 input_blocksize);
2003 if (r_partial + r_full >= max_records)
2004 nread = iread_fnc (STDIN_FILENO, ibuf, max_bytes);
2005 else
2006 nread = iread_fnc (STDIN_FILENO, ibuf, input_blocksize);
2008 if (nread >= 0 && i_nocache)
2009 invalidate_cache (STDIN_FILENO, nread);
2011 if (nread == 0)
2012 break; /* EOF. */
2014 if (nread < 0)
2016 if (!(conversions_mask & C_NOERROR) || !(status_flags & STATUS_NONE))
2017 error (0, errno, _("error reading %s"), quote (input_file));
2019 if (conversions_mask & C_NOERROR)
2021 print_stats ();
2022 size_t bad_portion = input_blocksize - partread;
2024 /* We already know this data is not cached,
2025 but call this so that correct offsets are maintained. */
2026 invalidate_cache (STDIN_FILENO, bad_portion);
2028 /* Seek past the bad block if possible. */
2029 if (!advance_input_after_read_error (bad_portion))
2031 exit_status = EXIT_FAILURE;
2033 /* Suppress duplicate diagnostics. */
2034 input_seekable = false;
2035 input_seek_errno = ESPIPE;
2037 if ((conversions_mask & C_SYNC) && !partread)
2038 /* Replace the missing input with null bytes and
2039 proceed normally. */
2040 nread = 0;
2041 else
2042 continue;
2044 else
2046 /* Write any partial block. */
2047 exit_status = EXIT_FAILURE;
2048 break;
2052 n_bytes_read = nread;
2053 advance_input_offset (nread);
2055 if (n_bytes_read < input_blocksize)
2057 r_partial++;
2058 partread = n_bytes_read;
2059 if (conversions_mask & C_SYNC)
2061 if (!(conversions_mask & C_NOERROR))
2062 /* If C_NOERROR, we zeroed the block before reading. */
2063 memset (ibuf + n_bytes_read,
2064 (conversions_mask & (C_BLOCK | C_UNBLOCK)) ? ' ' : '\0',
2065 input_blocksize - n_bytes_read);
2066 n_bytes_read = input_blocksize;
2069 else
2071 r_full++;
2072 partread = 0;
2075 if (ibuf == obuf) /* If not C_TWOBUFS. */
2077 size_t nwritten = iwrite (STDOUT_FILENO, obuf, n_bytes_read);
2078 w_bytes += nwritten;
2079 if (nwritten != n_bytes_read)
2081 error (0, errno, _("error writing %s"), quote (output_file));
2082 return EXIT_FAILURE;
2084 else if (n_bytes_read == input_blocksize)
2085 w_full++;
2086 else
2087 w_partial++;
2088 continue;
2091 /* Do any translations on the whole buffer at once. */
2093 if (translation_needed)
2094 translate_buffer (ibuf, n_bytes_read);
2096 if (conversions_mask & C_SWAB)
2097 bufstart = swab_buffer (ibuf, &n_bytes_read);
2098 else
2099 bufstart = ibuf;
2101 if (conversions_mask & C_BLOCK)
2102 copy_with_block (bufstart, n_bytes_read);
2103 else if (conversions_mask & C_UNBLOCK)
2104 copy_with_unblock (bufstart, n_bytes_read);
2105 else
2106 copy_simple (bufstart, n_bytes_read);
2109 /* If we have a char left as a result of conv=swab, output it. */
2110 if (char_is_saved)
2112 if (conversions_mask & C_BLOCK)
2113 copy_with_block (&saved_char, 1);
2114 else if (conversions_mask & C_UNBLOCK)
2115 copy_with_unblock (&saved_char, 1);
2116 else
2117 output_char (saved_char);
2120 if ((conversions_mask & C_BLOCK) && col > 0)
2122 /* If the final input line didn't end with a '\n', pad
2123 the output block to 'conversion_blocksize' chars. */
2124 size_t i;
2125 for (i = col; i < conversion_blocksize; i++)
2126 output_char (space_character);
2129 if (col && (conversions_mask & C_UNBLOCK))
2131 /* If there was any output, add a final '\n'. */
2132 output_char (newline_character);
2135 /* Write out the last block. */
2136 if (oc != 0)
2138 size_t nwritten = iwrite (STDOUT_FILENO, obuf, oc);
2139 w_bytes += nwritten;
2140 if (nwritten != 0)
2141 w_partial++;
2142 if (nwritten != oc)
2144 error (0, errno, _("error writing %s"), quote (output_file));
2145 return EXIT_FAILURE;
2149 /* If the last write was converted to a seek, then for a regular file
2150 or shared memory object, ftruncate to extend the size. */
2151 if (final_op_was_seek)
2153 struct stat stdout_stat;
2154 if (fstat (STDOUT_FILENO, &stdout_stat) != 0)
2156 error (0, errno, _("cannot fstat %s"), quote (output_file));
2157 return EXIT_FAILURE;
2159 if (S_ISREG (stdout_stat.st_mode) || S_TYPEISSHM (&stdout_stat))
2161 off_t output_offset = lseek (STDOUT_FILENO, 0, SEEK_CUR);
2162 if (output_offset > stdout_stat.st_size)
2164 if (ftruncate (STDOUT_FILENO, output_offset) != 0)
2166 error (0, errno,
2167 _("failed to truncate to %" PRIdMAX " bytes"
2168 " in output file %s"),
2169 (intmax_t) output_offset, quote (output_file));
2170 return EXIT_FAILURE;
2176 if ((conversions_mask & C_FDATASYNC) && fdatasync (STDOUT_FILENO) != 0)
2178 if (errno != ENOSYS && errno != EINVAL)
2180 error (0, errno, _("fdatasync failed for %s"), quote (output_file));
2181 exit_status = EXIT_FAILURE;
2183 conversions_mask |= C_FSYNC;
2186 if (conversions_mask & C_FSYNC)
2187 while (fsync (STDOUT_FILENO) != 0)
2188 if (errno != EINTR)
2190 error (0, errno, _("fsync failed for %s"), quote (output_file));
2191 return EXIT_FAILURE;
2194 return exit_status;
2198 main (int argc, char **argv)
2200 int i;
2201 int exit_status;
2202 off_t offset;
2204 install_signal_handlers ();
2206 initialize_main (&argc, &argv);
2207 set_program_name (argv[0]);
2208 setlocale (LC_ALL, "");
2209 bindtextdomain (PACKAGE, LOCALEDIR);
2210 textdomain (PACKAGE);
2212 /* Arrange to close stdout if parse_long_options exits. */
2213 atexit (maybe_close_stdout);
2215 page_size = getpagesize ();
2217 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, Version,
2218 usage, AUTHORS, (char const *) NULL);
2219 close_stdout_required = false;
2221 if (getopt_long (argc, argv, "", NULL, NULL) != -1)
2222 usage (EXIT_FAILURE);
2224 /* Initialize translation table to identity translation. */
2225 for (i = 0; i < 256; i++)
2226 trans_table[i] = i;
2228 /* Decode arguments. */
2229 scanargs (argc, argv);
2231 apply_translations ();
2233 if (input_file == NULL)
2235 input_file = _("standard input");
2236 set_fd_flags (STDIN_FILENO, input_flags, input_file);
2238 else
2240 if (fd_reopen (STDIN_FILENO, input_file, O_RDONLY | input_flags, 0) < 0)
2241 error (EXIT_FAILURE, errno, _("failed to open %s"), quote (input_file));
2244 offset = lseek (STDIN_FILENO, 0, SEEK_CUR);
2245 input_seekable = (0 <= offset);
2246 input_offset = MAX (0, offset);
2247 input_seek_errno = errno;
2249 if (output_file == NULL)
2251 output_file = _("standard output");
2252 set_fd_flags (STDOUT_FILENO, output_flags, output_file);
2254 else
2256 mode_t perms = MODE_RW_UGO;
2257 int opts
2258 = (output_flags
2259 | (conversions_mask & C_NOCREAT ? 0 : O_CREAT)
2260 | (conversions_mask & C_EXCL ? O_EXCL : 0)
2261 | (seek_records || (conversions_mask & C_NOTRUNC) ? 0 : O_TRUNC));
2263 /* Open the output file with *read* access only if we might
2264 need to read to satisfy a 'seek=' request. If we can't read
2265 the file, go ahead with write-only access; it might work. */
2266 if ((! seek_records
2267 || fd_reopen (STDOUT_FILENO, output_file, O_RDWR | opts, perms) < 0)
2268 && (fd_reopen (STDOUT_FILENO, output_file, O_WRONLY | opts, perms)
2269 < 0))
2270 error (EXIT_FAILURE, errno, _("failed to open %s"),
2271 quote (output_file));
2273 if (seek_records != 0 && !(conversions_mask & C_NOTRUNC))
2275 uintmax_t size = seek_records * output_blocksize + seek_bytes;
2276 unsigned long int obs = output_blocksize;
2278 if (OFF_T_MAX / output_blocksize < seek_records)
2279 error (EXIT_FAILURE, 0,
2280 _("offset too large: "
2281 "cannot truncate to a length of seek=%"PRIuMAX""
2282 " (%lu-byte) blocks"),
2283 seek_records, obs);
2285 if (ftruncate (STDOUT_FILENO, size) != 0)
2287 /* Complain only when ftruncate fails on a regular file, a
2288 directory, or a shared memory object, as POSIX 1003.1-2004
2289 specifies ftruncate's behavior only for these file types.
2290 For example, do not complain when Linux kernel 2.4 ftruncate
2291 fails on /dev/fd0. */
2292 int ftruncate_errno = errno;
2293 struct stat stdout_stat;
2294 if (fstat (STDOUT_FILENO, &stdout_stat) != 0)
2295 error (EXIT_FAILURE, errno, _("cannot fstat %s"),
2296 quote (output_file));
2297 if (S_ISREG (stdout_stat.st_mode)
2298 || S_ISDIR (stdout_stat.st_mode)
2299 || S_TYPEISSHM (&stdout_stat))
2300 error (EXIT_FAILURE, ftruncate_errno,
2301 _("failed to truncate to %"PRIuMAX" bytes"
2302 " in output file %s"),
2303 size, quote (output_file));
2308 start_time = gethrxtime ();
2310 exit_status = dd_copy ();
2312 if (max_records == 0 && max_bytes == 0)
2314 /* Special case to invalidate cache to end of file. */
2315 if (i_nocache && !invalidate_cache (STDIN_FILENO, 0))
2317 error (0, errno, _("failed to discard cache for: %s"),
2318 quote (input_file));
2319 exit_status = EXIT_FAILURE;
2321 if (o_nocache && !invalidate_cache (STDOUT_FILENO, 0))
2323 error (0, errno, _("failed to discard cache for: %s"),
2324 quote (output_file));
2325 exit_status = EXIT_FAILURE;
2328 else if (max_records != (uintmax_t) -1)
2330 /* Invalidate any pending region less than page size,
2331 in case the kernel might round up. */
2332 if (i_nocache)
2333 invalidate_cache (STDIN_FILENO, 0);
2334 if (o_nocache)
2335 invalidate_cache (STDOUT_FILENO, 0);
2338 quit (exit_status);