1 /* dd -- convert a file while copying it.
2 Copyright (C) 1985-2015 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. */
21 #define SWAB_ALIGN_OFFSET 2
23 #include <sys/types.h>
28 #include "close-stream.h"
30 #include "fd-reopen.h"
31 #include "gethrxtime.h"
33 #include "long-options.h"
40 /* The official name of this program (e.g., no 'g' prefix). */
41 #define PROGRAM_NAME "dd"
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
51 # define SA_NOCLDSTOP 0
52 # define sigprocmask(How, Set, Oset) /* empty */
54 # if ! HAVE_SIGINTERRUPT
55 # define siginterrupt(sig, flag) /* empty */
59 /* NonStop circa 2011 lacks SA_RESETHAND; see Bug#9076. */
61 # define SA_RESETHAND 0
65 # define SIGINFO SIGUSR1
68 /* This may belong in GNULIB's fcntl module instead.
69 Define O_CIO to 0 if it is not supported by this OS. */
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. */
79 # define fdatasync(fd) (errno = ENOSYS, -1)
82 #define output_char(c) \
86 if (oc >= output_blocksize) \
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
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. */
123 /* Use separate input and output buffers, and combine partial
129 C_FDATASYNC
= 040000,
144 /* The name of the input file, or NULL for the standard input. */
145 static char const *input_file
= NULL
;
147 /* The name of the output file, or NULL for the standard output. */
148 static char const *output_file
= NULL
;
150 /* The page size on this host. */
151 static size_t page_size
;
153 /* The number of bytes in which atomic reads are done. */
154 static size_t input_blocksize
= 0;
156 /* The number of bytes in which atomic writes are done. */
157 static size_t output_blocksize
= 0;
159 /* Conversion buffer size, in bytes. 0 prevents conversions. */
160 static size_t conversion_blocksize
= 0;
162 /* Skip this many records of 'input_blocksize' bytes before input. */
163 static uintmax_t skip_records
= 0;
165 /* Skip this many bytes before input in addition of 'skip_records'
167 static size_t skip_bytes
= 0;
169 /* Skip this many records of 'output_blocksize' bytes before output. */
170 static uintmax_t seek_records
= 0;
172 /* Skip this many bytes in addition to 'seek_records' records before
174 static uintmax_t seek_bytes
= 0;
176 /* Whether the final output was done with a seek (rather than a write). */
177 static bool final_op_was_seek
;
179 /* Copy only this many records. The default is effectively infinity. */
180 static uintmax_t max_records
= (uintmax_t) -1;
182 /* Copy this many bytes in addition to 'max_records' records. */
183 static size_t max_bytes
= 0;
185 /* Bit vector of conversions to apply. */
186 static int conversions_mask
= 0;
188 /* Open flags for the input and output files. */
189 static int input_flags
= 0;
190 static int output_flags
= 0;
192 /* Status flags for what is printed to stderr. */
193 static int status_level
= STATUS_DEFAULT
;
195 /* If nonzero, filter characters through the translation table. */
196 static bool translation_needed
= false;
198 /* Number of partial blocks written. */
199 static uintmax_t w_partial
= 0;
201 /* Number of full blocks written. */
202 static uintmax_t w_full
= 0;
204 /* Number of partial blocks read. */
205 static uintmax_t r_partial
= 0;
207 /* Number of full blocks read. */
208 static uintmax_t r_full
= 0;
210 /* Number of bytes written. */
211 static uintmax_t w_bytes
= 0;
213 /* Time that dd started. */
214 static xtime_t start_time
;
216 /* Previous time for periodic progress. */
217 static xtime_t previous_time
;
219 /* Whether a '\n' is pending after writing progress. */
220 static bool newline_pending
;
222 /* True if input is seekable. */
223 static bool input_seekable
;
225 /* Error number corresponding to initial attempt to lseek input.
226 If ESPIPE, do not issue any more diagnostics about it. */
227 static int input_seek_errno
;
229 /* File offset of the input, in bytes, along with a flag recording
230 whether it overflowed. */
231 static uintmax_t input_offset
;
232 static bool input_offset_overflow
;
234 /* True if a partial read should be diagnosed. */
235 static bool warn_partial_read
;
237 /* Records truncated by conv=block. */
238 static uintmax_t r_truncate
= 0;
240 /* Output representation of newline and space characters.
241 They change if we're converting to EBCDIC. */
242 static char newline_character
= '\n';
243 static char space_character
= ' ';
251 /* Current index into 'obuf'. */
252 static size_t oc
= 0;
254 /* Index into current line, for 'conv=block' and 'conv=unblock'. */
255 static size_t col
= 0;
257 /* The set of signals that are caught. */
258 static sigset_t caught_signals
;
260 /* If nonzero, the value of the pending fatal signal. */
261 static sig_atomic_t volatile interrupt_signal
;
263 /* A count of the number of pending info signals that have been received. */
264 static sig_atomic_t volatile info_signal_count
;
266 /* Whether to discard cache for input or output. */
267 static bool i_nocache
, o_nocache
;
269 /* Function used for read (to handle iflag=fullblock parameter). */
270 static ssize_t (*iread_fnc
) (int fd
, char *buf
, size_t size
);
272 /* A longest symbol in the struct symbol_values tables below. */
273 #define LONGEST_SYMBOL "count_bytes"
275 /* A symbol and the corresponding integer value. */
278 char symbol
[sizeof LONGEST_SYMBOL
];
282 /* Conversion symbols, for conv="...". */
283 static struct symbol_value
const conversions
[] =
285 {"ascii", C_ASCII
| C_UNBLOCK
| C_TWOBUFS
}, /* EBCDIC to ASCII. */
286 {"ebcdic", C_EBCDIC
| C_BLOCK
| C_TWOBUFS
}, /* ASCII to EBCDIC. */
287 {"ibm", C_IBM
| C_BLOCK
| C_TWOBUFS
}, /* Different ASCII to EBCDIC. */
288 {"block", C_BLOCK
| C_TWOBUFS
}, /* Variable to fixed length records. */
289 {"unblock", C_UNBLOCK
| C_TWOBUFS
}, /* Fixed to variable length records. */
290 {"lcase", C_LCASE
| C_TWOBUFS
}, /* Translate upper to lower case. */
291 {"ucase", C_UCASE
| C_TWOBUFS
}, /* Translate lower to upper case. */
292 {"sparse", C_SPARSE
}, /* Try to sparsely write output. */
293 {"swab", C_SWAB
| C_TWOBUFS
}, /* Swap bytes of input. */
294 {"noerror", C_NOERROR
}, /* Ignore i/o errors. */
295 {"nocreat", C_NOCREAT
}, /* Do not create output file. */
296 {"excl", C_EXCL
}, /* Fail if the output file already exists. */
297 {"notrunc", C_NOTRUNC
}, /* Do not truncate output file. */
298 {"sync", C_SYNC
}, /* Pad input records to ibs with NULs. */
299 {"fdatasync", C_FDATASYNC
}, /* Synchronize output data before finishing. */
300 {"fsync", C_FSYNC
}, /* Also synchronize output metadata. */
304 #define FFS_MASK(x) ((x) ^ ((x) & ((x) - 1)))
307 /* Compute a value that's bitwise disjoint from the union
325 /* Use its lowest bits for private flags. */
326 O_FULLBLOCK
= FFS_MASK (v
),
327 v2
= v
^ O_FULLBLOCK
,
329 O_NOCACHE
= FFS_MASK (v2
),
332 O_COUNT_BYTES
= FFS_MASK (v3
),
333 v4
= v3
^ O_COUNT_BYTES
,
335 O_SKIP_BYTES
= FFS_MASK (v4
),
336 v5
= v4
^ O_SKIP_BYTES
,
338 O_SEEK_BYTES
= FFS_MASK (v5
)
341 /* Ensure that we got something. */
342 verify (O_FULLBLOCK
!= 0);
343 verify (O_NOCACHE
!= 0);
344 verify (O_COUNT_BYTES
!= 0);
345 verify (O_SKIP_BYTES
!= 0);
346 verify (O_SEEK_BYTES
!= 0);
348 #define MULTIPLE_BITS_SET(i) (((i) & ((i) - 1)) != 0)
350 /* Ensure that this is a single-bit value. */
351 verify ( ! MULTIPLE_BITS_SET (O_FULLBLOCK
));
352 verify ( ! MULTIPLE_BITS_SET (O_NOCACHE
));
353 verify ( ! MULTIPLE_BITS_SET (O_COUNT_BYTES
));
354 verify ( ! MULTIPLE_BITS_SET (O_SKIP_BYTES
));
355 verify ( ! MULTIPLE_BITS_SET (O_SEEK_BYTES
));
357 /* Flags, for iflag="..." and oflag="...". */
358 static struct symbol_value
const flags
[] =
360 {"append", O_APPEND
},
361 {"binary", O_BINARY
},
363 {"direct", O_DIRECT
},
364 {"directory", O_DIRECTORY
},
366 {"noatime", O_NOATIME
},
367 {"nocache", O_NOCACHE
}, /* Discard cache. */
368 {"noctty", O_NOCTTY
},
369 {"nofollow", HAVE_WORKING_O_NOFOLLOW
? O_NOFOLLOW
: 0},
370 {"nolinks", O_NOLINKS
},
371 {"nonblock", O_NONBLOCK
},
374 {"fullblock", O_FULLBLOCK
}, /* Accumulate full blocks from input. */
375 {"count_bytes", O_COUNT_BYTES
},
376 {"skip_bytes", O_SKIP_BYTES
},
377 {"seek_bytes", O_SEEK_BYTES
},
381 /* Status, for status="...". */
382 static struct symbol_value
const statuses
[] =
384 {"none", STATUS_NONE
},
385 {"noxfer", STATUS_NOXFER
},
386 {"progress", STATUS_PROGRESS
},
390 /* Translation table formed by applying successive transformations. */
391 static unsigned char trans_table
[256];
393 /* Standard translation tables, taken from POSIX 1003.1-2013.
394 Beware of imitations; there are lots of ASCII<->EBCDIC tables
395 floating around the net, perhaps valid for some applications but
398 static char const ascii_to_ebcdic
[] =
400 '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
401 '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
402 '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
403 '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
404 '\100', '\132', '\177', '\173', '\133', '\154', '\120', '\175',
405 '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
406 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
407 '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
408 '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
409 '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
410 '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
411 '\347', '\350', '\351', '\255', '\340', '\275', '\232', '\155',
412 '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
413 '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
414 '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
415 '\247', '\250', '\251', '\300', '\117', '\320', '\137', '\007',
416 '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
417 '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
418 '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
419 '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
420 '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
421 '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
422 '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
423 '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
424 '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
425 '\216', '\217', '\220', '\152', '\233', '\234', '\235', '\236',
426 '\237', '\240', '\252', '\253', '\254', '\112', '\256', '\257',
427 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
428 '\270', '\271', '\272', '\273', '\274', '\241', '\276', '\277',
429 '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
430 '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
431 '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
434 static char const ascii_to_ibm
[] =
436 '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
437 '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
438 '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
439 '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
440 '\100', '\132', '\177', '\173', '\133', '\154', '\120', '\175',
441 '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
442 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
443 '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
444 '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
445 '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
446 '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
447 '\347', '\350', '\351', '\255', '\340', '\275', '\137', '\155',
448 '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
449 '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
450 '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
451 '\247', '\250', '\251', '\300', '\117', '\320', '\241', '\007',
452 '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
453 '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
454 '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
455 '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
456 '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
457 '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
458 '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
459 '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
460 '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
461 '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',
462 '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',
463 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
464 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
465 '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
466 '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
467 '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
470 static char const ebcdic_to_ascii
[] =
472 '\000', '\001', '\002', '\003', '\234', '\011', '\206', '\177',
473 '\227', '\215', '\216', '\013', '\014', '\015', '\016', '\017',
474 '\020', '\021', '\022', '\023', '\235', '\205', '\010', '\207',
475 '\030', '\031', '\222', '\217', '\034', '\035', '\036', '\037',
476 '\200', '\201', '\202', '\203', '\204', '\012', '\027', '\033',
477 '\210', '\211', '\212', '\213', '\214', '\005', '\006', '\007',
478 '\220', '\221', '\026', '\223', '\224', '\225', '\226', '\004',
479 '\230', '\231', '\232', '\233', '\024', '\025', '\236', '\032',
480 '\040', '\240', '\241', '\242', '\243', '\244', '\245', '\246',
481 '\247', '\250', '\325', '\056', '\074', '\050', '\053', '\174',
482 '\046', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
483 '\260', '\261', '\041', '\044', '\052', '\051', '\073', '\176',
484 '\055', '\057', '\262', '\263', '\264', '\265', '\266', '\267',
485 '\270', '\271', '\313', '\054', '\045', '\137', '\076', '\077',
486 '\272', '\273', '\274', '\275', '\276', '\277', '\300', '\301',
487 '\302', '\140', '\072', '\043', '\100', '\047', '\075', '\042',
488 '\303', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
489 '\150', '\151', '\304', '\305', '\306', '\307', '\310', '\311',
490 '\312', '\152', '\153', '\154', '\155', '\156', '\157', '\160',
491 '\161', '\162', '\136', '\314', '\315', '\316', '\317', '\320',
492 '\321', '\345', '\163', '\164', '\165', '\166', '\167', '\170',
493 '\171', '\172', '\322', '\323', '\324', '\133', '\326', '\327',
494 '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
495 '\340', '\341', '\342', '\343', '\344', '\135', '\346', '\347',
496 '\173', '\101', '\102', '\103', '\104', '\105', '\106', '\107',
497 '\110', '\111', '\350', '\351', '\352', '\353', '\354', '\355',
498 '\175', '\112', '\113', '\114', '\115', '\116', '\117', '\120',
499 '\121', '\122', '\356', '\357', '\360', '\361', '\362', '\363',
500 '\134', '\237', '\123', '\124', '\125', '\126', '\127', '\130',
501 '\131', '\132', '\364', '\365', '\366', '\367', '\370', '\371',
502 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
503 '\070', '\071', '\372', '\373', '\374', '\375', '\376', '\377'
506 /* True if we need to close the standard output *stream*. */
507 static bool close_stdout_required
= true;
509 /* The only reason to close the standard output *stream* is if
510 parse_long_options fails (as it does for --help or --version).
511 In any other case, dd uses only the STDOUT_FILENO file descriptor,
512 and the "cleanup" function calls "close (STDOUT_FILENO)".
513 Closing the file descriptor and then letting the usual atexit-run
514 close_stdout function call "fclose (stdout)" would result in a
515 harmless failure of the close syscall (with errno EBADF).
516 This function serves solely to avoid the unnecessary close_stdout
517 call, once parse_long_options has succeeded.
518 Meanwhile, we guarantee that the standard error stream is flushed,
519 by inlining the last half of close_stdout as needed. */
521 maybe_close_stdout (void)
523 if (close_stdout_required
)
525 else if (close_stream (stderr
) != 0)
526 _exit (EXIT_FAILURE
);
529 /* Like error() but handle any pending newline. */
531 static void _GL_ATTRIBUTE_FORMAT ((__printf__
, 3, 4))
532 nl_error (int status
, int errnum
, const char *fmt
, ...)
536 fputc ('\n', stderr
);
537 newline_pending
= false;
542 verror (status
, errnum
, fmt
, ap
);
546 #define error nl_error
551 if (status
!= EXIT_SUCCESS
)
556 Usage: %s [OPERAND]...\n\
559 program_name
, program_name
);
561 Copy a file, converting and formatting according to the operands.\n\
563 bs=BYTES read and write up to BYTES bytes at a time\n\
564 cbs=BYTES convert BYTES bytes at a time\n\
565 conv=CONVS convert the file as per the comma separated symbol list\n\
566 count=N copy only N input blocks\n\
567 ibs=BYTES read up to BYTES bytes at a time (default: 512)\n\
570 if=FILE read from FILE instead of stdin\n\
571 iflag=FLAGS read as per the comma separated symbol list\n\
572 obs=BYTES write BYTES bytes at a time (default: 512)\n\
573 of=FILE write to FILE instead of stdout\n\
574 oflag=FLAGS write as per the comma separated symbol list\n\
575 seek=N skip N obs-sized blocks at start of output\n\
576 skip=N skip N ibs-sized blocks at start of input\n\
577 status=LEVEL The LEVEL of information to print to stderr;\n\
578 'none' suppresses everything but error messages,\n\
579 'noxfer' suppresses the final transfer statistics,\n\
580 'progress' shows periodic transfer statistics\n\
584 N and BYTES may be followed by the following multiplicative suffixes:\n\
585 c =1, w =2, b =512, kB =1000, K =1024, MB =1000*1000, M =1024*1024, xM =M\n\
586 GB =1000*1000*1000, G =1024*1024*1024, and so on for T, P, E, Z, Y.\n\
588 Each CONV symbol may be:\n\
592 ascii from EBCDIC to ASCII\n\
593 ebcdic from ASCII to EBCDIC\n\
594 ibm from ASCII to alternate EBCDIC\n\
595 block pad newline-terminated records with spaces to cbs-size\n\
596 unblock replace trailing spaces in cbs-size records with newline\n\
597 lcase change upper case to lower case\n\
598 ucase change lower case to upper case\n\
599 sparse try to seek rather than write the output for NUL input blocks\n\
600 swab swap every pair of input bytes\n\
601 sync pad every input block with NULs to ibs-size; when used\n\
602 with block or unblock, pad with spaces rather than NULs\n\
605 excl fail if the output file already exists\n\
606 nocreat do not create the output file\n\
607 notrunc do not truncate the output file\n\
608 noerror continue after read errors\n\
609 fdatasync physically write output file data before finishing\n\
610 fsync likewise, but also write metadata\n\
614 Each FLAG symbol may be:\n\
616 append append mode (makes sense only for output; conv=notrunc suggested)\n\
619 fputs (_(" cio use concurrent I/O for data\n"), stdout
);
621 fputs (_(" direct use direct I/O for data\n"), stdout
);
623 fputs (_(" directory fail unless a directory\n"), stdout
);
625 fputs (_(" dsync use synchronized I/O for data\n"), stdout
);
627 fputs (_(" sync likewise, but also for metadata\n"), stdout
);
628 fputs (_(" fullblock accumulate full blocks of input (iflag only)\n"),
631 fputs (_(" nonblock use non-blocking I/O\n"), stdout
);
633 fputs (_(" noatime do not update access time\n"), stdout
);
634 #if HAVE_POSIX_FADVISE
636 fputs (_(" nocache discard cached data\n"), stdout
);
639 fputs (_(" noctty do not assign controlling terminal from file\n"),
641 if (HAVE_WORKING_O_NOFOLLOW
)
642 fputs (_(" nofollow do not follow symlinks\n"), stdout
);
644 fputs (_(" nolinks fail if multiply-linked\n"), stdout
);
646 fputs (_(" binary use binary I/O for data\n"), stdout
);
648 fputs (_(" text use text I/O for data\n"), stdout
);
650 fputs (_(" count_bytes treat 'count=N' as a byte count (iflag only)\n\
653 fputs (_(" skip_bytes treat 'skip=N' as a byte count (iflag only)\n\
656 fputs (_(" seek_bytes treat 'seek=N' as a byte count (oflag only)\n\
662 Sending a %s signal to a running 'dd' process makes it\n\
663 print I/O statistics to standard error and then resume copying.\n\
667 "), SIGINFO
== SIGUSR1
? "USR1" : "INFO");
670 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
671 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
672 emit_ancillary_info (PROGRAM_NAME
);
678 human_size (size_t n
)
680 static char hbuf
[LONGEST_HUMAN_READABLE
+ 1];
682 (human_autoscale
| human_round_to_nearest
| human_base_1024
683 | human_space_before_unit
| human_SI
| human_B
);
684 return human_readable (n
, hbuf
, human_opts
, 1, 1);
687 /* Ensure input buffer IBUF is allocated. */
695 char *real_buf
= malloc (input_blocksize
+ INPUT_BLOCK_SLOP
);
697 error (EXIT_FAILURE
, 0,
698 _("memory exhausted by input buffer of size %"PRIuMAX
" bytes (%s)"),
699 (uintmax_t) input_blocksize
, human_size (input_blocksize
));
701 real_buf
+= SWAB_ALIGN_OFFSET
; /* allow space for swab */
703 ibuf
= ptr_align (real_buf
, page_size
);
706 /* Ensure output buffer OBUF is allocated/initialized. */
714 if (conversions_mask
& C_TWOBUFS
)
716 /* Page-align the output buffer, too. */
717 char *real_obuf
= malloc (output_blocksize
+ OUTPUT_BLOCK_SLOP
);
719 error (EXIT_FAILURE
, 0,
720 _("memory exhausted by output buffer of size %"PRIuMAX
722 (uintmax_t) output_blocksize
, human_size (output_blocksize
));
723 obuf
= ptr_align (real_obuf
, page_size
);
733 translate_charset (char const *new_trans
)
737 for (i
= 0; i
< 256; i
++)
738 trans_table
[i
] = new_trans
[trans_table
[i
]];
739 translation_needed
= true;
742 /* Return true if I has more than one bit set. I must be nonnegative. */
745 multiple_bits_set (int i
)
747 return MULTIPLE_BITS_SET (i
);
750 /* Print transfer statistics. */
753 print_xfer_stats (xtime_t progress_time
) {
754 char hbuf
[LONGEST_HUMAN_READABLE
+ 1];
756 (human_autoscale
| human_round_to_nearest
757 | human_space_before_unit
| human_SI
| human_B
);
759 char const *bytes_per_second
;
762 fputc ('\r', stderr
);
764 /* Use integer arithmetic to compute the transfer rate,
765 since that makes it easy to use SI abbreviations. */
768 ngettext ("%"PRIuMAX
" byte (%s) copied",
769 "%"PRIuMAX
" bytes (%s) copied",
770 select_plural (w_bytes
)),
772 human_readable (w_bytes
, hbuf
, human_opts
, 1, 1));
774 xtime_t now
= progress_time
? progress_time
: gethrxtime ();
776 if (start_time
< now
)
778 double XTIME_PRECISIONe0
= XTIME_PRECISION
;
779 uintmax_t delta_xtime
= now
;
780 delta_xtime
-= start_time
;
781 delta_s
= delta_xtime
/ XTIME_PRECISIONe0
;
782 bytes_per_second
= human_readable (w_bytes
, hbuf
, human_opts
,
783 XTIME_PRECISION
, delta_xtime
);
788 bytes_per_second
= _("Infinity B");
791 /* TRANSLATORS: The two instances of "s" in this string are the SI
792 symbol "s" (meaning second), and should not be translated.
794 This format used to be:
796 ngettext (", %g second, %s/s\n", ", %g seconds, %s/s\n", delta_s == 1)
798 but that was incorrect for languages like Polish. To fix this
799 bug we now use SI symbols even though they're a bit more
800 confusing in English. */
801 char const *time_fmt
= _(", %g s, %s/s\n");
803 time_fmt
= _(", %.6f s, %s/s"); /* OK with '\r' as increasing width. */
804 fprintf (stderr
, time_fmt
, delta_s
, bytes_per_second
);
806 newline_pending
= !!progress_time
;
812 if (status_level
== STATUS_NONE
)
817 fputc ('\n', stderr
);
818 newline_pending
= false;
822 _("%"PRIuMAX
"+%"PRIuMAX
" records in\n"
823 "%"PRIuMAX
"+%"PRIuMAX
" records out\n"),
824 r_full
, r_partial
, w_full
, w_partial
);
828 ngettext ("%"PRIuMAX
" truncated record\n",
829 "%"PRIuMAX
" truncated records\n",
830 select_plural (r_truncate
)),
833 if (status_level
== STATUS_NOXFER
)
836 print_xfer_stats (0);
839 /* An ordinary signal was received; arrange for the program to exit. */
842 interrupt_handler (int sig
)
845 signal (sig
, SIG_DFL
);
846 interrupt_signal
= sig
;
849 /* An info signal was received; arrange for the program to print status. */
852 siginfo_handler (int sig
)
855 signal (sig
, siginfo_handler
);
859 /* Install the signal handlers. */
862 install_signal_handlers (void)
864 bool catch_siginfo
= ! (SIGINFO
== SIGUSR1
&& getenv ("POSIXLY_CORRECT"));
868 struct sigaction act
;
869 sigemptyset (&caught_signals
);
871 sigaddset (&caught_signals
, SIGINFO
);
872 sigaction (SIGINT
, NULL
, &act
);
873 if (act
.sa_handler
!= SIG_IGN
)
874 sigaddset (&caught_signals
, SIGINT
);
875 act
.sa_mask
= caught_signals
;
877 if (sigismember (&caught_signals
, SIGINFO
))
879 act
.sa_handler
= siginfo_handler
;
880 /* Note we don't use SA_RESTART here and instead
881 handle EINTR explicitly in iftruncate() etc.
882 to avoid blocking on noncommitted read()/write() calls. */
884 sigaction (SIGINFO
, &act
, NULL
);
887 if (sigismember (&caught_signals
, SIGINT
))
889 act
.sa_handler
= interrupt_handler
;
890 act
.sa_flags
= SA_NODEFER
| SA_RESETHAND
;
891 sigaction (SIGINT
, &act
, NULL
);
898 signal (SIGINFO
, siginfo_handler
);
899 siginterrupt (SIGINFO
, 1);
901 if (signal (SIGINT
, SIG_IGN
) != SIG_IGN
)
903 signal (SIGINT
, interrupt_handler
);
904 siginterrupt (SIGINT
, 1);
912 if (close (STDIN_FILENO
) < 0)
913 error (EXIT_FAILURE
, errno
,
914 _("closing input file %s"), quote (input_file
));
916 /* Don't remove this call to close, even though close_stdout
917 closes standard output. This close is necessary when cleanup
918 is called as part of a signal handler. */
919 if (close (STDOUT_FILENO
) < 0)
920 error (EXIT_FAILURE
, errno
,
921 _("closing output file %s"), quote (output_file
));
924 /* Process any pending signals. If signals are caught, this function
925 should be called periodically. Ideally there should never be an
926 unbounded amount of time when signals are not being processed. */
929 process_signals (void)
931 while (interrupt_signal
|| info_signal_count
)
937 sigprocmask (SIG_BLOCK
, &caught_signals
, &oldset
);
939 /* Reload interrupt_signal and info_signal_count, in case a new
940 signal was handled before sigprocmask took effect. */
941 interrupt
= interrupt_signal
;
942 infos
= info_signal_count
;
945 info_signal_count
= infos
- 1;
947 sigprocmask (SIG_SETMASK
, &oldset
, NULL
);
965 static void ATTRIBUTE_NORETURN
972 /* Return LEN rounded down to a multiple of PAGE_SIZE
973 while storing the remainder internally per FD.
974 Pass LEN == 0 to get the current remainder. */
977 cache_round (int fd
, off_t len
)
979 static off_t i_pending
, o_pending
;
980 off_t
*pending
= (fd
== STDIN_FILENO
? &i_pending
: &o_pending
);
984 uintmax_t c_pending
= *pending
+ len
;
985 *pending
= c_pending
% page_size
;
986 if (c_pending
> *pending
)
987 len
= c_pending
- *pending
;
997 /* Discard the cache from the current offset of either
998 STDIN_FILENO or STDOUT_FILENO.
999 Return true on success. */
1002 invalidate_cache (int fd
, off_t len
)
1006 /* Minimize syscalls. */
1007 off_t clen
= cache_round (fd
, len
);
1009 return true; /* Don't advise this time. */
1010 if (!len
&& !clen
&& max_records
)
1011 return true; /* Nothing pending. */
1012 off_t pending
= len
? cache_round (fd
, 0) : 0;
1014 if (fd
== STDIN_FILENO
)
1018 /* Note we're being careful here to only invalidate what
1019 we've read, so as not to dump any read ahead cache. */
1020 #if HAVE_POSIX_FADVISE
1021 adv_ret
= posix_fadvise (fd
, input_offset
- clen
- pending
, clen
,
1022 POSIX_FADV_DONTNEED
);
1030 else if (fd
== STDOUT_FILENO
)
1032 static off_t output_offset
= -2;
1034 if (output_offset
!= -1)
1036 if (0 > output_offset
)
1038 output_offset
= lseek (fd
, 0, SEEK_CUR
);
1039 output_offset
-= clen
+ pending
;
1041 if (0 <= output_offset
)
1043 #if HAVE_POSIX_FADVISE
1044 adv_ret
= posix_fadvise (fd
, output_offset
, clen
,
1045 POSIX_FADV_DONTNEED
);
1049 output_offset
+= clen
+ pending
;
1054 return adv_ret
!= -1 ? true : false;
1057 /* Read from FD into the buffer BUF of size SIZE, processing any
1058 signals that arrive before bytes are read. Return the number of
1059 bytes read if successful, -1 (setting errno) on failure. */
1062 iread (int fd
, char *buf
, size_t size
)
1069 nread
= read (fd
, buf
, size
);
1071 while (nread
< 0 && errno
== EINTR
);
1073 /* Short read may be due to received signal. */
1074 if (0 < nread
&& nread
< size
)
1077 if (0 < nread
&& warn_partial_read
)
1079 static ssize_t prev_nread
;
1081 if (0 < prev_nread
&& prev_nread
< size
)
1083 uintmax_t prev
= prev_nread
;
1084 if (status_level
!= STATUS_NONE
)
1085 error (0, 0, ngettext (("warning: partial read (%"PRIuMAX
" byte); "
1086 "suggest iflag=fullblock"),
1087 ("warning: partial read (%"PRIuMAX
" bytes); "
1088 "suggest iflag=fullblock"),
1089 select_plural (prev
)),
1091 warn_partial_read
= false;
1100 /* Wrapper around iread function to accumulate full blocks. */
1102 iread_fullblock (int fd
, char *buf
, size_t size
)
1108 ssize_t ncurr
= iread (fd
, buf
, size
);
1121 /* Write to FD the buffer BUF of size SIZE, processing any signals
1122 that arrive. Return the number of bytes written, setting errno if
1123 this is less than SIZE. Keep trying if there are partial
1127 iwrite (int fd
, char const *buf
, size_t size
)
1129 size_t total_written
= 0;
1131 if ((output_flags
& O_DIRECT
) && size
< output_blocksize
)
1133 int old_flags
= fcntl (STDOUT_FILENO
, F_GETFL
);
1134 if (fcntl (STDOUT_FILENO
, F_SETFL
, old_flags
& ~O_DIRECT
) != 0
1135 && status_level
!= STATUS_NONE
)
1136 error (0, errno
, _("failed to turn off O_DIRECT: %s"),
1137 quote (output_file
));
1139 /* Since we have just turned off O_DIRECT for the final write,
1140 here we try to preserve some of its semantics. First, use
1141 posix_fadvise to tell the system not to pollute the buffer
1142 cache with this data. Don't bother to diagnose lseek or
1143 posix_fadvise failure. */
1144 invalidate_cache (STDOUT_FILENO
, 0);
1146 /* Attempt to ensure that that final block is committed
1147 to disk as quickly as possible. */
1148 conversions_mask
|= C_FSYNC
;
1151 while (total_written
< size
)
1153 ssize_t nwritten
= 0;
1156 /* Perform a seek for a NUL block if sparse output is enabled. */
1157 final_op_was_seek
= false;
1158 if ((conversions_mask
& C_SPARSE
) && is_nul (buf
, size
))
1160 if (lseek (fd
, size
, SEEK_CUR
) < 0)
1162 conversions_mask
&= ~C_SPARSE
;
1163 /* Don't warn about the advisory sparse request. */
1167 final_op_was_seek
= true;
1173 nwritten
= write (fd
, buf
+ total_written
, size
- total_written
);
1180 else if (nwritten
== 0)
1182 /* Some buggy drivers return 0 when one tries to write beyond
1183 a device's end. (Example: Linux kernel 1.2.13 on /dev/fd0.)
1184 Set errno to ENOSPC so they get a sensible diagnostic. */
1189 total_written
+= nwritten
;
1192 if (o_nocache
&& total_written
)
1193 invalidate_cache (fd
, total_written
);
1195 return total_written
;
1198 /* Write, then empty, the output buffer 'obuf'. */
1203 size_t nwritten
= iwrite (STDOUT_FILENO
, obuf
, output_blocksize
);
1204 w_bytes
+= nwritten
;
1205 if (nwritten
!= output_blocksize
)
1207 error (0, errno
, _("writing to %s"), quote (output_file
));
1210 quit (EXIT_FAILURE
);
1217 /* Restart on EINTR from fd_reopen(). */
1220 ifd_reopen (int desired_fd
, char const *file
, int flag
, mode_t mode
)
1227 ret
= fd_reopen (desired_fd
, file
, flag
, mode
);
1229 while (ret
< 0 && errno
== EINTR
);
1234 /* Restart on EINTR from ftruncate(). */
1237 iftruncate (int fd
, off_t length
)
1244 ret
= ftruncate (fd
, length
);
1246 while (ret
< 0 && errno
== EINTR
);
1251 /* Return true if STR is of the form "PATTERN" or "PATTERNDELIM...". */
1253 static bool _GL_ATTRIBUTE_PURE
1254 operand_matches (char const *str
, char const *pattern
, char delim
)
1257 if (*str
++ != *pattern
++)
1259 return !*str
|| *str
== delim
;
1262 /* Interpret one "conv=..." or similar operand STR according to the
1263 symbols in TABLE, returning the flags specified. If the operand
1264 cannot be parsed, use ERROR_MSGID to generate a diagnostic. */
1267 parse_symbols (char const *str
, struct symbol_value
const *table
,
1268 bool exclusive
, char const *error_msgid
)
1274 char const *strcomma
= strchr (str
, ',');
1275 struct symbol_value
const *entry
;
1278 ! (operand_matches (str
, entry
->symbol
, ',') && entry
->value
);
1281 if (! entry
->symbol
[0])
1283 size_t slen
= strcomma
? strcomma
- str
: strlen (str
);
1284 error (0, 0, "%s: %s", _(error_msgid
),
1285 quotearg_n_style_mem (0, locale_quoting_style
, str
, slen
));
1286 usage (EXIT_FAILURE
);
1291 value
= entry
->value
;
1293 value
|= entry
->value
;
1302 /* Return the value of STR, interpreted as a non-negative decimal integer,
1303 optionally multiplied by various values.
1304 Set *INVALID to a nonzero error value if STR does not represent a
1305 number in this format. */
1308 parse_integer (const char *str
, strtol_error
*invalid
)
1312 strtol_error e
= xstrtoumax (str
, &suffix
, 10, &n
, "bcEGkKMPTwYZ0");
1314 if (e
== LONGINT_INVALID_SUFFIX_CHAR
&& *suffix
== 'x')
1316 uintmax_t multiplier
= parse_integer (suffix
+ 1, invalid
);
1318 if (multiplier
!= 0 && n
* multiplier
/ multiplier
!= n
)
1320 *invalid
= LONGINT_OVERFLOW
;
1326 else if (e
!= LONGINT_OK
)
1335 /* OPERAND is of the form "X=...". Return true if X is NAME. */
1337 static bool _GL_ATTRIBUTE_PURE
1338 operand_is (char const *operand
, char const *name
)
1340 return operand_matches (operand
, name
, '=');
1344 scanargs (int argc
, char *const *argv
)
1347 size_t blocksize
= 0;
1348 uintmax_t count
= (uintmax_t) -1;
1352 for (i
= optind
; i
< argc
; i
++)
1354 char const *name
= argv
[i
];
1355 char const *val
= strchr (name
, '=');
1359 error (0, 0, _("unrecognized operand %s"), quote (name
));
1360 usage (EXIT_FAILURE
);
1364 if (operand_is (name
, "if"))
1366 else if (operand_is (name
, "of"))
1368 else if (operand_is (name
, "conv"))
1369 conversions_mask
|= parse_symbols (val
, conversions
, false,
1370 N_("invalid conversion"));
1371 else if (operand_is (name
, "iflag"))
1372 input_flags
|= parse_symbols (val
, flags
, false,
1373 N_("invalid input flag"));
1374 else if (operand_is (name
, "oflag"))
1375 output_flags
|= parse_symbols (val
, flags
, false,
1376 N_("invalid output flag"));
1377 else if (operand_is (name
, "status"))
1378 status_level
= parse_symbols (val
, statuses
, true,
1379 N_("invalid status level"));
1382 strtol_error invalid
= LONGINT_OK
;
1383 uintmax_t n
= parse_integer (val
, &invalid
);
1384 uintmax_t n_min
= 0;
1385 uintmax_t n_max
= UINTMAX_MAX
;
1387 if (operand_is (name
, "ibs"))
1390 n_max
= MAX_BLOCKSIZE (INPUT_BLOCK_SLOP
);
1391 input_blocksize
= n
;
1393 else if (operand_is (name
, "obs"))
1396 n_max
= MAX_BLOCKSIZE (OUTPUT_BLOCK_SLOP
);
1397 output_blocksize
= n
;
1399 else if (operand_is (name
, "bs"))
1402 n_max
= MAX_BLOCKSIZE (INPUT_BLOCK_SLOP
);
1405 else if (operand_is (name
, "cbs"))
1409 conversion_blocksize
= n
;
1411 else if (operand_is (name
, "skip"))
1413 else if (operand_is (name
, "seek"))
1415 else if (operand_is (name
, "count"))
1419 error (0, 0, _("unrecognized operand %s"), quote (name
));
1420 usage (EXIT_FAILURE
);
1424 invalid
= LONGINT_INVALID
;
1426 invalid
= LONGINT_OVERFLOW
;
1428 if (invalid
!= LONGINT_OK
)
1429 error (EXIT_FAILURE
, invalid
== LONGINT_OVERFLOW
? EOVERFLOW
: 0,
1430 "%s: %s", _("invalid number"), quote (val
));
1435 input_blocksize
= output_blocksize
= blocksize
;
1438 /* POSIX says dd aggregates partial reads into
1439 output_blocksize if bs= is not specified. */
1440 conversions_mask
|= C_TWOBUFS
;
1443 if (input_blocksize
== 0)
1444 input_blocksize
= DEFAULT_BLOCKSIZE
;
1445 if (output_blocksize
== 0)
1446 output_blocksize
= DEFAULT_BLOCKSIZE
;
1447 if (conversion_blocksize
== 0)
1448 conversions_mask
&= ~(C_BLOCK
| C_UNBLOCK
);
1450 if (input_flags
& (O_DSYNC
| O_SYNC
))
1451 input_flags
|= O_RSYNC
;
1453 if (output_flags
& O_FULLBLOCK
)
1455 error (0, 0, "%s: %s", _("invalid output flag"), quote ("fullblock"));
1456 usage (EXIT_FAILURE
);
1459 if (input_flags
& O_SEEK_BYTES
)
1461 error (0, 0, "%s: %s", _("invalid input flag"), quote ("seek_bytes"));
1462 usage (EXIT_FAILURE
);
1465 if (output_flags
& (O_COUNT_BYTES
| O_SKIP_BYTES
))
1467 error (0, 0, "%s: %s", _("invalid output flag"),
1468 quote (output_flags
& O_COUNT_BYTES
1469 ? "count_bytes" : "skip_bytes"));
1470 usage (EXIT_FAILURE
);
1473 if (input_flags
& O_SKIP_BYTES
&& skip
!= 0)
1475 skip_records
= skip
/ input_blocksize
;
1476 skip_bytes
= skip
% input_blocksize
;
1479 skip_records
= skip
;
1481 if (input_flags
& O_COUNT_BYTES
&& count
!= (uintmax_t) -1)
1483 max_records
= count
/ input_blocksize
;
1484 max_bytes
= count
% input_blocksize
;
1486 else if (count
!= (uintmax_t) -1)
1487 max_records
= count
;
1489 if (output_flags
& O_SEEK_BYTES
&& seek
!= 0)
1491 seek_records
= seek
/ output_blocksize
;
1492 seek_bytes
= seek
% output_blocksize
;
1495 seek_records
= seek
;
1497 /* Warn about partial reads if bs=SIZE is given and iflag=fullblock
1498 is not, and if counting or skipping bytes or using direct I/O.
1499 This helps to avoid confusion with miscounts, and to avoid issues
1500 with direct I/O on GNU/Linux. */
1502 (! (conversions_mask
& C_TWOBUFS
) && ! (input_flags
& O_FULLBLOCK
)
1504 || (0 < max_records
&& max_records
< (uintmax_t) -1)
1505 || (input_flags
| output_flags
) & O_DIRECT
));
1507 iread_fnc
= ((input_flags
& O_FULLBLOCK
)
1510 input_flags
&= ~O_FULLBLOCK
;
1512 if (multiple_bits_set (conversions_mask
& (C_ASCII
| C_EBCDIC
| C_IBM
)))
1513 error (EXIT_FAILURE
, 0, _("cannot combine any two of {ascii,ebcdic,ibm}"));
1514 if (multiple_bits_set (conversions_mask
& (C_BLOCK
| C_UNBLOCK
)))
1515 error (EXIT_FAILURE
, 0, _("cannot combine block and unblock"));
1516 if (multiple_bits_set (conversions_mask
& (C_LCASE
| C_UCASE
)))
1517 error (EXIT_FAILURE
, 0, _("cannot combine lcase and ucase"));
1518 if (multiple_bits_set (conversions_mask
& (C_EXCL
| C_NOCREAT
)))
1519 error (EXIT_FAILURE
, 0, _("cannot combine excl and nocreat"));
1520 if (multiple_bits_set (input_flags
& (O_DIRECT
| O_NOCACHE
))
1521 || multiple_bits_set (output_flags
& (O_DIRECT
| O_NOCACHE
)))
1522 error (EXIT_FAILURE
, 0, _("cannot combine direct and nocache"));
1524 if (input_flags
& O_NOCACHE
)
1527 input_flags
&= ~O_NOCACHE
;
1529 if (output_flags
& O_NOCACHE
)
1532 output_flags
&= ~O_NOCACHE
;
1536 /* Fix up translation table. */
1539 apply_translations (void)
1543 if (conversions_mask
& C_ASCII
)
1544 translate_charset (ebcdic_to_ascii
);
1546 if (conversions_mask
& C_UCASE
)
1548 for (i
= 0; i
< 256; i
++)
1549 trans_table
[i
] = toupper (trans_table
[i
]);
1550 translation_needed
= true;
1552 else if (conversions_mask
& C_LCASE
)
1554 for (i
= 0; i
< 256; i
++)
1555 trans_table
[i
] = tolower (trans_table
[i
]);
1556 translation_needed
= true;
1559 if (conversions_mask
& C_EBCDIC
)
1561 translate_charset (ascii_to_ebcdic
);
1562 newline_character
= ascii_to_ebcdic
['\n'];
1563 space_character
= ascii_to_ebcdic
[' '];
1565 else if (conversions_mask
& C_IBM
)
1567 translate_charset (ascii_to_ibm
);
1568 newline_character
= ascii_to_ibm
['\n'];
1569 space_character
= ascii_to_ibm
[' '];
1573 /* Apply the character-set translations specified by the user
1574 to the NREAD bytes in BUF. */
1577 translate_buffer (char *buf
, size_t nread
)
1582 for (i
= nread
, cp
= buf
; i
; i
--, cp
++)
1583 *cp
= trans_table
[to_uchar (*cp
)];
1586 /* If true, the last char from the previous call to 'swab_buffer'
1587 is saved in 'saved_char'. */
1588 static bool char_is_saved
= false;
1590 /* Odd char from previous call. */
1591 static char saved_char
;
1593 /* Swap NREAD bytes in BUF, plus possibly an initial char from the
1594 previous call. If NREAD is odd, save the last char for the
1595 next call. Return the new start of the BUF buffer. */
1598 swab_buffer (char *buf
, size_t *nread
)
1600 char *bufstart
= buf
;
1604 /* Is a char left from last time? */
1607 *--bufstart
= saved_char
;
1609 char_is_saved
= false;
1614 /* An odd number of chars are in the buffer. */
1615 saved_char
= bufstart
[--*nread
];
1616 char_is_saved
= true;
1619 /* Do the byte-swapping by moving every second character two
1620 positions toward the end, working from the end of the buffer
1621 toward the beginning. This way we only move half of the data. */
1623 cp
= bufstart
+ *nread
; /* Start one char past the last. */
1624 for (i
= *nread
/ 2; i
; i
--, cp
-= 2)
1630 /* Add OFFSET to the input offset, setting the overflow flag if
1634 advance_input_offset (uintmax_t offset
)
1636 input_offset
+= offset
;
1637 if (input_offset
< offset
)
1638 input_offset_overflow
= true;
1641 /* This is a wrapper for lseek. It detects and warns about a kernel
1642 bug that makes lseek a no-op for tape devices, even though the kernel
1643 lseek return value suggests that the function succeeded.
1645 The parameters are the same as those of the lseek function, but
1646 with the addition of FILENAME, the name of the file associated with
1647 descriptor FDESC. The file name is used solely in the warning that's
1648 printed when the bug is detected. Return the same value that lseek
1649 would have returned, but when the lseek bug is detected, return -1
1650 to indicate that lseek failed.
1652 The offending behavior has been confirmed with an Exabyte SCSI tape
1653 drive accessed via /dev/nst0 on both Linux 2.2.17 and 2.4.16 kernels. */
1657 # include <sys/mtio.h>
1659 # define MT_SAME_POSITION(P, Q) \
1660 ((P).mt_resid == (Q).mt_resid \
1661 && (P).mt_fileno == (Q).mt_fileno \
1662 && (P).mt_blkno == (Q).mt_blkno)
1665 skip_via_lseek (char const *filename
, int fdesc
, off_t offset
, int whence
)
1669 bool got_original_tape_position
= (ioctl (fdesc
, MTIOCGET
, &s1
) == 0);
1670 /* known bad device type */
1671 /* && s.mt_type == MT_ISSCSI2 */
1673 off_t new_position
= lseek (fdesc
, offset
, whence
);
1674 if (0 <= new_position
1675 && got_original_tape_position
1676 && ioctl (fdesc
, MTIOCGET
, &s2
) == 0
1677 && MT_SAME_POSITION (s1
, s2
))
1679 if (status_level
!= STATUS_NONE
)
1680 error (0, 0, _("warning: working around lseek kernel bug for file "
1681 "(%s)\n of mt_type=0x%0lx -- "
1682 "see <sys/mtio.h> for the list of types"),
1683 filename
, s2
.mt_type
+ 0Lu);
1688 return new_position
;
1691 # define skip_via_lseek(Filename, Fd, Offset, Whence) lseek (Fd, Offset, Whence)
1694 /* Throw away RECORDS blocks of BLOCKSIZE bytes plus BYTES bytes on
1695 file descriptor FDESC, which is open with read permission for FILE.
1696 Store up to BLOCKSIZE bytes of the data at a time in IBUF or OBUF, if
1697 necessary. RECORDS or BYTES must be nonzero. If FDESC is
1698 STDIN_FILENO, advance the input offset. Return the number of
1699 records remaining, i.e., that were not skipped because EOF was
1700 reached. If FDESC is STDOUT_FILENO, on return, BYTES is the
1701 remaining bytes in addition to the remaining records. */
1704 skip (int fdesc
, char const *file
, uintmax_t records
, size_t blocksize
,
1707 uintmax_t offset
= records
* blocksize
+ *bytes
;
1709 /* Try lseek and if an error indicates it was an inappropriate operation --
1710 or if the file offset is not representable as an off_t --
1711 fall back on using read. */
1714 if (records
<= OFF_T_MAX
/ blocksize
1715 && 0 <= skip_via_lseek (file
, fdesc
, offset
, SEEK_CUR
))
1717 if (fdesc
== STDIN_FILENO
)
1720 if (fstat (STDIN_FILENO
, &st
) != 0)
1721 error (EXIT_FAILURE
, errno
, _("cannot fstat %s"), quote (file
));
1722 if (usable_st_size (&st
) && st
.st_size
< input_offset
+ offset
)
1724 /* When skipping past EOF, return the number of _full_ blocks
1725 * that are not skipped, and set offset to EOF, so the caller
1726 * can determine the requested skip was not satisfied. */
1727 records
= ( offset
- st
.st_size
) / blocksize
;
1728 offset
= st
.st_size
- input_offset
;
1732 advance_input_offset (offset
);
1743 int lseek_errno
= errno
;
1745 /* The seek request may have failed above if it was too big
1746 (> device size, > max file size, etc.)
1747 Or it may not have been done at all (> OFF_T_MAX).
1748 Therefore try to seek to the end of the file,
1749 to avoid redundant reading. */
1750 if ((skip_via_lseek (file
, fdesc
, 0, SEEK_END
)) >= 0)
1752 /* File is seekable, and we're at the end of it, and
1753 size <= OFF_T_MAX. So there's no point using read to advance. */
1757 /* The original seek was not attempted as offset > OFF_T_MAX.
1758 We should error for write as can't get to the desired
1759 location, even if OFF_T_MAX < max file size.
1760 For read we're not going to read any data anyway,
1761 so we should error for consistency.
1762 It would be nice to not error for /dev/{zero,null}
1763 for any offset, but that's not a significant issue. */
1764 lseek_errno
= EOVERFLOW
;
1767 if (fdesc
== STDIN_FILENO
)
1768 error (0, lseek_errno
, _("%s: cannot skip"), quote (file
));
1770 error (0, lseek_errno
, _("%s: cannot seek"), quote (file
));
1771 /* If the file has a specific size and we've asked
1772 to skip/seek beyond the max allowable, then quit. */
1773 quit (EXIT_FAILURE
);
1775 /* else file_size && offset > OFF_T_MAX or file ! seekable */
1778 if (fdesc
== STDIN_FILENO
)
1791 ssize_t nread
= iread_fnc (fdesc
, buf
, records
? blocksize
: *bytes
);
1794 if (fdesc
== STDIN_FILENO
)
1796 error (0, errno
, _("error reading %s"), quote (file
));
1797 if (conversions_mask
& C_NOERROR
)
1801 error (0, lseek_errno
, _("%s: cannot seek"), quote (file
));
1802 quit (EXIT_FAILURE
);
1804 else if (nread
== 0)
1806 else if (fdesc
== STDIN_FILENO
)
1807 advance_input_offset (nread
);
1814 while (records
|| *bytes
);
1820 /* Advance the input by NBYTES if possible, after a read error.
1821 The input file offset may or may not have advanced after the failed
1822 read; adjust it to point just after the bad record regardless.
1823 Return true if successful, or if the input is already known to not
1827 advance_input_after_read_error (size_t nbytes
)
1829 if (! input_seekable
)
1831 if (input_seek_errno
== ESPIPE
)
1833 errno
= input_seek_errno
;
1838 advance_input_offset (nbytes
);
1839 input_offset_overflow
|= (OFF_T_MAX
< input_offset
);
1840 if (input_offset_overflow
)
1842 error (0, 0, _("offset overflow while reading file %s"),
1843 quote (input_file
));
1846 offset
= lseek (STDIN_FILENO
, 0, SEEK_CUR
);
1850 if (offset
== input_offset
)
1852 diff
= input_offset
- offset
;
1853 if (! (0 <= diff
&& diff
<= nbytes
) && status_level
!= STATUS_NONE
)
1854 error (0, 0, _("warning: invalid file offset after failed read"));
1855 if (0 <= skip_via_lseek (input_file
, STDIN_FILENO
, diff
, SEEK_CUR
))
1858 error (0, 0, _("cannot work around kernel bug after all"));
1862 error (0, errno
, _("%s: cannot seek"), quote (input_file
));
1866 /* Copy NREAD bytes of BUF, with no conversions. */
1869 copy_simple (char const *buf
, size_t nread
)
1871 const char *start
= buf
; /* First uncopied char in BUF. */
1875 size_t nfree
= MIN (nread
, output_blocksize
- oc
);
1877 memcpy (obuf
+ oc
, start
, nfree
);
1879 nread
-= nfree
; /* Update the number of bytes left to copy. */
1882 if (oc
>= output_blocksize
)
1888 /* Copy NREAD bytes of BUF, doing conv=block
1889 (pad newline-terminated records to 'conversion_blocksize',
1890 replacing the newline with trailing spaces). */
1893 copy_with_block (char const *buf
, size_t nread
)
1897 for (i
= nread
; i
; i
--, buf
++)
1899 if (*buf
== newline_character
)
1901 if (col
< conversion_blocksize
)
1904 for (j
= col
; j
< conversion_blocksize
; j
++)
1905 output_char (space_character
);
1911 if (col
== conversion_blocksize
)
1913 else if (col
< conversion_blocksize
)
1920 /* Copy NREAD bytes of BUF, doing conv=unblock
1921 (replace trailing spaces in 'conversion_blocksize'-sized records
1925 copy_with_unblock (char const *buf
, size_t nread
)
1929 static size_t pending_spaces
= 0;
1931 for (i
= 0; i
< nread
; i
++)
1935 if (col
++ >= conversion_blocksize
)
1937 col
= pending_spaces
= 0; /* Wipe out any pending spaces. */
1938 i
--; /* Push the char back; get it later. */
1939 output_char (newline_character
);
1941 else if (c
== space_character
)
1945 /* 'c' is the character after a run of spaces that were not
1946 at the end of the conversion buffer. Output them. */
1947 while (pending_spaces
)
1949 output_char (space_character
);
1957 /* Set the file descriptor flags for FD that correspond to the nonzero bits
1958 in ADD_FLAGS. The file's name is NAME. */
1961 set_fd_flags (int fd
, int add_flags
, char const *name
)
1963 /* Ignore file creation flags that are no-ops on file descriptors. */
1964 add_flags
&= ~ (O_NOCTTY
| O_NOFOLLOW
);
1968 int old_flags
= fcntl (fd
, F_GETFL
);
1969 int new_flags
= old_flags
| add_flags
;
1973 else if (old_flags
!= new_flags
)
1975 if (new_flags
& (O_DIRECTORY
| O_NOLINKS
))
1977 /* NEW_FLAGS contains at least one file creation flag that
1978 requires some checking of the open file descriptor. */
1980 if (fstat (fd
, &st
) != 0)
1982 else if ((new_flags
& O_DIRECTORY
) && ! S_ISDIR (st
.st_mode
))
1987 else if ((new_flags
& O_NOLINKS
) && 1 < st
.st_nlink
)
1992 new_flags
&= ~ (O_DIRECTORY
| O_NOLINKS
);
1995 if (ok
&& old_flags
!= new_flags
1996 && fcntl (fd
, F_SETFL
, new_flags
) == -1)
2001 error (EXIT_FAILURE
, errno
, _("setting flags for %s"), quote (name
));
2005 /* The main loop. */
2010 char *bufstart
; /* Input buffer. */
2011 ssize_t nread
; /* Bytes read in the current block. */
2013 /* If nonzero, then the previously read block was partial and
2014 PARTREAD was its size. */
2015 size_t partread
= 0;
2017 int exit_status
= EXIT_SUCCESS
;
2018 size_t n_bytes_read
;
2020 /* Leave at least one extra byte at the beginning and end of 'ibuf'
2021 for conv=swab, but keep the buffer address even. But some peculiar
2022 device drivers work only with word-aligned buffers, so leave an
2025 /* Some devices require alignment on a sector or page boundary
2026 (e.g. character disk devices). Align the input buffer to a
2027 page boundary to cover all bases. Note that due to the swab
2028 algorithm, we must have at least one byte in the page before
2029 the input buffer; thus we allocate 2 pages of slop in the
2030 real buffer. 8k above the blocksize shouldn't bother anyone.
2032 The page alignment is necessary on any Linux kernel that supports
2033 either the SGI raw I/O patch or Steven Tweedies raw I/O patch.
2034 It is necessary when accessing raw (i.e., character special) disk
2035 devices on Unixware or other SVR4-derived system. */
2037 if (skip_records
!= 0 || skip_bytes
!= 0)
2039 uintmax_t us_bytes
= input_offset
+ (skip_records
* input_blocksize
)
2041 uintmax_t us_blocks
= skip (STDIN_FILENO
, input_file
,
2042 skip_records
, input_blocksize
, &skip_bytes
);
2043 us_bytes
-= input_offset
;
2045 /* POSIX doesn't say what to do when dd detects it has been
2046 asked to skip past EOF, so I assume it's non-fatal.
2047 There are 3 reasons why there might be unskipped blocks/bytes:
2048 1. file is too small
2049 2. pipe has not enough data
2051 if ((us_blocks
|| (!input_offset_overflow
&& us_bytes
))
2052 && status_level
!= STATUS_NONE
)
2055 _("%s: cannot skip to specified offset"), quote (input_file
));
2059 if (seek_records
!= 0 || seek_bytes
!= 0)
2061 size_t bytes
= seek_bytes
;
2062 uintmax_t write_records
= skip (STDOUT_FILENO
, output_file
,
2063 seek_records
, output_blocksize
, &bytes
);
2065 if (write_records
!= 0 || bytes
!= 0)
2067 memset (obuf
, 0, write_records
? output_blocksize
: bytes
);
2071 size_t size
= write_records
? output_blocksize
: bytes
;
2072 if (iwrite (STDOUT_FILENO
, obuf
, size
) != size
)
2074 error (0, errno
, _("writing to %s"), quote (output_file
));
2075 quit (EXIT_FAILURE
);
2078 if (write_records
!= 0)
2083 while (write_records
|| bytes
);
2087 if (max_records
== 0 && max_bytes
== 0)
2095 if (status_level
== STATUS_PROGRESS
)
2097 xtime_t progress_time
= gethrxtime ();
2098 uintmax_t delta_xtime
= progress_time
;
2099 delta_xtime
-= previous_time
;
2100 double XTIME_PRECISIONe0
= XTIME_PRECISION
;
2101 if (delta_xtime
/ XTIME_PRECISIONe0
> 1)
2103 print_xfer_stats (progress_time
);
2104 previous_time
= progress_time
;
2108 if (r_partial
+ r_full
>= max_records
+ !!max_bytes
)
2111 /* Zero the buffer before reading, so that if we get a read error,
2112 whatever data we are able to read is followed by zeros.
2113 This minimizes data loss. */
2114 if ((conversions_mask
& C_SYNC
) && (conversions_mask
& C_NOERROR
))
2116 (conversions_mask
& (C_BLOCK
| C_UNBLOCK
)) ? ' ' : '\0',
2119 if (r_partial
+ r_full
>= max_records
)
2120 nread
= iread_fnc (STDIN_FILENO
, ibuf
, max_bytes
);
2122 nread
= iread_fnc (STDIN_FILENO
, ibuf
, input_blocksize
);
2124 if (nread
>= 0 && i_nocache
)
2125 invalidate_cache (STDIN_FILENO
, nread
);
2132 if (!(conversions_mask
& C_NOERROR
) || status_level
!= STATUS_NONE
)
2133 error (0, errno
, _("error reading %s"), quote (input_file
));
2135 if (conversions_mask
& C_NOERROR
)
2138 size_t bad_portion
= input_blocksize
- partread
;
2140 /* We already know this data is not cached,
2141 but call this so that correct offsets are maintained. */
2142 invalidate_cache (STDIN_FILENO
, bad_portion
);
2144 /* Seek past the bad block if possible. */
2145 if (!advance_input_after_read_error (bad_portion
))
2147 exit_status
= EXIT_FAILURE
;
2149 /* Suppress duplicate diagnostics. */
2150 input_seekable
= false;
2151 input_seek_errno
= ESPIPE
;
2153 if ((conversions_mask
& C_SYNC
) && !partread
)
2154 /* Replace the missing input with null bytes and
2155 proceed normally. */
2162 /* Write any partial block. */
2163 exit_status
= EXIT_FAILURE
;
2168 n_bytes_read
= nread
;
2169 advance_input_offset (nread
);
2171 if (n_bytes_read
< input_blocksize
)
2174 partread
= n_bytes_read
;
2175 if (conversions_mask
& C_SYNC
)
2177 if (!(conversions_mask
& C_NOERROR
))
2178 /* If C_NOERROR, we zeroed the block before reading. */
2179 memset (ibuf
+ n_bytes_read
,
2180 (conversions_mask
& (C_BLOCK
| C_UNBLOCK
)) ? ' ' : '\0',
2181 input_blocksize
- n_bytes_read
);
2182 n_bytes_read
= input_blocksize
;
2191 if (ibuf
== obuf
) /* If not C_TWOBUFS. */
2193 size_t nwritten
= iwrite (STDOUT_FILENO
, obuf
, n_bytes_read
);
2194 w_bytes
+= nwritten
;
2195 if (nwritten
!= n_bytes_read
)
2197 error (0, errno
, _("error writing %s"), quote (output_file
));
2198 return EXIT_FAILURE
;
2200 else if (n_bytes_read
== input_blocksize
)
2207 /* Do any translations on the whole buffer at once. */
2209 if (translation_needed
)
2210 translate_buffer (ibuf
, n_bytes_read
);
2212 if (conversions_mask
& C_SWAB
)
2213 bufstart
= swab_buffer (ibuf
, &n_bytes_read
);
2217 if (conversions_mask
& C_BLOCK
)
2218 copy_with_block (bufstart
, n_bytes_read
);
2219 else if (conversions_mask
& C_UNBLOCK
)
2220 copy_with_unblock (bufstart
, n_bytes_read
);
2222 copy_simple (bufstart
, n_bytes_read
);
2225 /* If we have a char left as a result of conv=swab, output it. */
2228 if (conversions_mask
& C_BLOCK
)
2229 copy_with_block (&saved_char
, 1);
2230 else if (conversions_mask
& C_UNBLOCK
)
2231 copy_with_unblock (&saved_char
, 1);
2233 output_char (saved_char
);
2236 if ((conversions_mask
& C_BLOCK
) && col
> 0)
2238 /* If the final input line didn't end with a '\n', pad
2239 the output block to 'conversion_blocksize' chars. */
2241 for (i
= col
; i
< conversion_blocksize
; i
++)
2242 output_char (space_character
);
2245 if (col
&& (conversions_mask
& C_UNBLOCK
))
2247 /* If there was any output, add a final '\n'. */
2248 output_char (newline_character
);
2251 /* Write out the last block. */
2254 size_t nwritten
= iwrite (STDOUT_FILENO
, obuf
, oc
);
2255 w_bytes
+= nwritten
;
2260 error (0, errno
, _("error writing %s"), quote (output_file
));
2261 return EXIT_FAILURE
;
2265 /* If the last write was converted to a seek, then for a regular file
2266 or shared memory object, ftruncate to extend the size. */
2267 if (final_op_was_seek
)
2269 struct stat stdout_stat
;
2270 if (fstat (STDOUT_FILENO
, &stdout_stat
) != 0)
2272 error (0, errno
, _("cannot fstat %s"), quote (output_file
));
2273 return EXIT_FAILURE
;
2275 if (S_ISREG (stdout_stat
.st_mode
) || S_TYPEISSHM (&stdout_stat
))
2277 off_t output_offset
= lseek (STDOUT_FILENO
, 0, SEEK_CUR
);
2278 if (0 <= output_offset
&& stdout_stat
.st_size
< output_offset
)
2280 if (iftruncate (STDOUT_FILENO
, output_offset
) != 0)
2283 _("failed to truncate to %" PRIdMAX
" bytes"
2284 " in output file %s"),
2285 (intmax_t) output_offset
, quote (output_file
));
2286 return EXIT_FAILURE
;
2292 if ((conversions_mask
& C_FDATASYNC
) && fdatasync (STDOUT_FILENO
) != 0)
2294 if (errno
!= ENOSYS
&& errno
!= EINVAL
)
2296 error (0, errno
, _("fdatasync failed for %s"), quote (output_file
));
2297 exit_status
= EXIT_FAILURE
;
2299 conversions_mask
|= C_FSYNC
;
2302 if (conversions_mask
& C_FSYNC
)
2303 while (fsync (STDOUT_FILENO
) != 0)
2306 error (0, errno
, _("fsync failed for %s"), quote (output_file
));
2307 return EXIT_FAILURE
;
2314 main (int argc
, char **argv
)
2320 install_signal_handlers ();
2322 initialize_main (&argc
, &argv
);
2323 set_program_name (argv
[0]);
2324 setlocale (LC_ALL
, "");
2325 bindtextdomain (PACKAGE
, LOCALEDIR
);
2326 textdomain (PACKAGE
);
2328 /* Arrange to close stdout if parse_long_options exits. */
2329 atexit (maybe_close_stdout
);
2331 page_size
= getpagesize ();
2333 parse_long_options (argc
, argv
, PROGRAM_NAME
, PACKAGE
, Version
,
2334 usage
, AUTHORS
, (char const *) NULL
);
2335 close_stdout_required
= false;
2337 if (getopt_long (argc
, argv
, "", NULL
, NULL
) != -1)
2338 usage (EXIT_FAILURE
);
2340 /* Initialize translation table to identity translation. */
2341 for (i
= 0; i
< 256; i
++)
2344 /* Decode arguments. */
2345 scanargs (argc
, argv
);
2347 apply_translations ();
2349 if (input_file
== NULL
)
2351 input_file
= _("standard input");
2352 set_fd_flags (STDIN_FILENO
, input_flags
, input_file
);
2356 if (ifd_reopen (STDIN_FILENO
, input_file
, O_RDONLY
| input_flags
, 0) < 0)
2357 error (EXIT_FAILURE
, errno
, _("failed to open %s"), quote (input_file
));
2360 offset
= lseek (STDIN_FILENO
, 0, SEEK_CUR
);
2361 input_seekable
= (0 <= offset
);
2362 input_offset
= MAX (0, offset
);
2363 input_seek_errno
= errno
;
2365 if (output_file
== NULL
)
2367 output_file
= _("standard output");
2368 set_fd_flags (STDOUT_FILENO
, output_flags
, output_file
);
2372 mode_t perms
= MODE_RW_UGO
;
2375 | (conversions_mask
& C_NOCREAT
? 0 : O_CREAT
)
2376 | (conversions_mask
& C_EXCL
? O_EXCL
: 0)
2377 | (seek_records
|| (conversions_mask
& C_NOTRUNC
) ? 0 : O_TRUNC
));
2379 /* Open the output file with *read* access only if we might
2380 need to read to satisfy a 'seek=' request. If we can't read
2381 the file, go ahead with write-only access; it might work. */
2383 || ifd_reopen (STDOUT_FILENO
, output_file
, O_RDWR
| opts
, perms
) < 0)
2384 && (ifd_reopen (STDOUT_FILENO
, output_file
, O_WRONLY
| opts
, perms
)
2386 error (EXIT_FAILURE
, errno
, _("failed to open %s"),
2387 quote (output_file
));
2389 if (seek_records
!= 0 && !(conversions_mask
& C_NOTRUNC
))
2391 uintmax_t size
= seek_records
* output_blocksize
+ seek_bytes
;
2392 unsigned long int obs
= output_blocksize
;
2394 if (OFF_T_MAX
/ output_blocksize
< seek_records
)
2395 error (EXIT_FAILURE
, 0,
2396 _("offset too large: "
2397 "cannot truncate to a length of seek=%"PRIuMAX
""
2398 " (%lu-byte) blocks"),
2401 if (iftruncate (STDOUT_FILENO
, size
) != 0)
2403 /* Complain only when ftruncate fails on a regular file, a
2404 directory, or a shared memory object, as POSIX 1003.1-2004
2405 specifies ftruncate's behavior only for these file types.
2406 For example, do not complain when Linux kernel 2.4 ftruncate
2407 fails on /dev/fd0. */
2408 int ftruncate_errno
= errno
;
2409 struct stat stdout_stat
;
2410 if (fstat (STDOUT_FILENO
, &stdout_stat
) != 0)
2411 error (EXIT_FAILURE
, errno
, _("cannot fstat %s"),
2412 quote (output_file
));
2413 if (S_ISREG (stdout_stat
.st_mode
)
2414 || S_ISDIR (stdout_stat
.st_mode
)
2415 || S_TYPEISSHM (&stdout_stat
))
2416 error (EXIT_FAILURE
, ftruncate_errno
,
2417 _("failed to truncate to %"PRIuMAX
" bytes"
2418 " in output file %s"),
2419 size
, quote (output_file
));
2424 start_time
= previous_time
= gethrxtime ();
2426 exit_status
= dd_copy ();
2428 if (max_records
== 0 && max_bytes
== 0)
2430 /* Special case to invalidate cache to end of file. */
2431 if (i_nocache
&& !invalidate_cache (STDIN_FILENO
, 0))
2433 error (0, errno
, _("failed to discard cache for: %s"),
2434 quote (input_file
));
2435 exit_status
= EXIT_FAILURE
;
2437 if (o_nocache
&& !invalidate_cache (STDOUT_FILENO
, 0))
2439 error (0, errno
, _("failed to discard cache for: %s"),
2440 quote (output_file
));
2441 exit_status
= EXIT_FAILURE
;
2444 else if (max_records
!= (uintmax_t) -1)
2446 /* Invalidate any pending region less than page size,
2447 in case the kernel might round up. */
2449 invalidate_cache (STDIN_FILENO
, 0);
2451 invalidate_cache (STDOUT_FILENO
, 0);