1 /* dd -- convert a file while copying it.
2 Copyright (C) 1985-2024 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 <https://www.gnu.org/licenses/>. */
17 /* Written by Paul Rubin, David MacKenzie, and Stuart Kemp. */
22 #include <sys/types.h>
26 #include "alignalloc.h"
27 #include "close-stream.h"
28 #include "fd-reopen.h"
29 #include "gethrxtime.h"
31 #include "ioblksize.h"
32 #include "long-options.h"
37 /* The official name of this program (e.g., no 'g' prefix). */
38 #define PROGRAM_NAME "dd"
41 proper_name ("Paul Rubin"), \
42 proper_name ("David MacKenzie"), \
43 proper_name ("Stuart Kemp")
45 /* Use SA_NOCLDSTOP as a proxy for whether the sigaction machinery is
48 # define SA_NOCLDSTOP 0
49 # define sigprocmask(How, Set, Oset) /* empty */
51 # if ! HAVE_SIGINTERRUPT
52 # define siginterrupt(sig, flag) /* empty */
56 /* NonStop circa 2011 lacks SA_RESETHAND; see Bug#9076. */
58 # define SA_RESETHAND 0
62 # define SIGINFO SIGUSR1
65 /* This may belong in GNULIB's fcntl module instead.
66 Define O_CIO to 0 if it is not supported by this OS. */
71 /* On AIX 5.1 and AIX 5.2, O_NOCACHE is defined via <fcntl.h>
72 and would interfere with our use of that name, below. */
75 #define output_char(c) \
79 if (oc >= output_blocksize) \
84 /* Default input and output blocksize. */
85 #define DEFAULT_BLOCKSIZE 512
87 /* Conversions bit masks. */
103 /* Use separate input and output buffers, and combine partial
109 C_FDATASYNC
= 040000,
124 /* The name of the input file, or nullptr for the standard input. */
125 static char const *input_file
= nullptr;
127 /* The name of the output file, or nullptr for the standard output. */
128 static char const *output_file
= nullptr;
130 /* The page size on this host. */
131 static idx_t page_size
;
133 /* The number of bytes in which atomic reads are done. */
134 static idx_t input_blocksize
= 0;
136 /* The number of bytes in which atomic writes are done. */
137 static idx_t output_blocksize
= 0;
139 /* Conversion buffer size, in bytes. 0 prevents conversions. */
140 static idx_t conversion_blocksize
= 0;
142 /* Skip this many records of 'input_blocksize' bytes before input. */
143 static intmax_t skip_records
= 0;
145 /* Skip this many bytes before input in addition of 'skip_records'
147 static idx_t skip_bytes
= 0;
149 /* Skip this many records of 'output_blocksize' bytes before output. */
150 static intmax_t seek_records
= 0;
152 /* Skip this many bytes in addition to 'seek_records' records before
154 static intmax_t seek_bytes
= 0;
156 /* Whether the final output was done with a seek (rather than a write). */
157 static bool final_op_was_seek
;
159 /* Copy only this many records. The default is effectively infinity. */
160 static intmax_t max_records
= INTMAX_MAX
;
162 /* Copy this many bytes in addition to 'max_records' records. */
163 static idx_t max_bytes
= 0;
165 /* Bit vector of conversions to apply. */
166 static int conversions_mask
= 0;
168 /* Open flags for the input and output files. */
169 static int input_flags
= 0;
170 static int output_flags
= 0;
172 /* Status flags for what is printed to stderr. */
173 static int status_level
= STATUS_DEFAULT
;
175 /* If nonzero, filter characters through the translation table. */
176 static bool translation_needed
= false;
178 /* Number of partial blocks written. */
179 static intmax_t w_partial
= 0;
181 /* Number of full blocks written. */
182 static intmax_t w_full
= 0;
184 /* Number of partial blocks read. */
185 static intmax_t r_partial
= 0;
187 /* Number of full blocks read. */
188 static intmax_t r_full
= 0;
190 /* Number of bytes written. */
191 static intmax_t w_bytes
= 0;
193 /* Last-reported number of bytes written, or negative if never reported. */
194 static intmax_t reported_w_bytes
= -1;
196 /* Time that dd started. */
197 static xtime_t start_time
;
199 /* Next time to report periodic progress. */
200 static xtime_t next_time
;
202 /* If positive, the number of bytes output in the current progress line. */
203 static int progress_len
;
205 /* True if input is seekable. */
206 static bool input_seekable
;
208 /* Error number corresponding to initial attempt to lseek input.
209 If ESPIPE, do not issue any more diagnostics about it. */
210 static int input_seek_errno
;
212 /* File offset of the input, in bytes, or -1 if it overflowed. */
213 static off_t input_offset
;
215 /* True if a partial read should be diagnosed. */
216 static bool warn_partial_read
;
218 /* Records truncated by conv=block. */
219 static intmax_t r_truncate
= 0;
221 /* Output representation of newline and space characters.
222 They change if we're converting to EBCDIC. */
223 static char newline_character
= '\n';
224 static char space_character
= ' ';
230 /* Current index into 'obuf'. */
233 /* Index into current line, for 'conv=block' and 'conv=unblock'. */
234 static idx_t col
= 0;
236 /* The set of signals that are caught. */
237 static sigset_t caught_signals
;
239 /* If nonzero, the value of the pending fatal signal. */
240 static sig_atomic_t volatile interrupt_signal
;
242 /* A count of the number of pending info signals that have been received. */
243 static sig_atomic_t volatile info_signal_count
;
245 /* Whether to discard cache for input or output. */
246 static bool i_nocache
, o_nocache
;
248 /* Whether to instruct the kernel to discard the complete file. */
249 static bool i_nocache_eof
, o_nocache_eof
;
251 /* Function used for read (to handle iflag=fullblock parameter). */
252 static ssize_t (*iread_fnc
) (int fd
, char *buf
, idx_t size
);
254 /* A longest symbol in the struct symbol_values tables below. */
255 #define LONGEST_SYMBOL "count_bytes"
257 /* A symbol and the corresponding integer value. */
260 char symbol
[sizeof LONGEST_SYMBOL
];
264 /* Conversion symbols, for conv="...". */
265 static struct symbol_value
const conversions
[] =
267 {"ascii", C_ASCII
| C_UNBLOCK
| C_TWOBUFS
}, /* EBCDIC to ASCII. */
268 {"ebcdic", C_EBCDIC
| C_BLOCK
| C_TWOBUFS
}, /* ASCII to EBCDIC. */
269 {"ibm", C_IBM
| C_BLOCK
| C_TWOBUFS
}, /* Different ASCII to EBCDIC. */
270 {"block", C_BLOCK
| C_TWOBUFS
}, /* Variable to fixed length records. */
271 {"unblock", C_UNBLOCK
| C_TWOBUFS
}, /* Fixed to variable length records. */
272 {"lcase", C_LCASE
| C_TWOBUFS
}, /* Translate upper to lower case. */
273 {"ucase", C_UCASE
| C_TWOBUFS
}, /* Translate lower to upper case. */
274 {"sparse", C_SPARSE
}, /* Try to sparsely write output. */
275 {"swab", C_SWAB
| C_TWOBUFS
}, /* Swap bytes of input. */
276 {"noerror", C_NOERROR
}, /* Ignore i/o errors. */
277 {"nocreat", C_NOCREAT
}, /* Do not create output file. */
278 {"excl", C_EXCL
}, /* Fail if the output file already exists. */
279 {"notrunc", C_NOTRUNC
}, /* Do not truncate output file. */
280 {"sync", C_SYNC
}, /* Pad input records to ibs with NULs. */
281 {"fdatasync", C_FDATASYNC
}, /* Synchronize output data before finishing. */
282 {"fsync", C_FSYNC
}, /* Also synchronize output metadata. */
286 #define FFS_MASK(x) ((x) ^ ((x) & ((x) - 1)))
289 /* Compute a value that's bitwise disjoint from the union
307 /* Use its lowest bits for private flags. */
308 O_FULLBLOCK
= FFS_MASK (v
),
309 v2
= v
^ O_FULLBLOCK
,
311 O_NOCACHE
= FFS_MASK (v2
),
314 O_COUNT_BYTES
= FFS_MASK (v3
),
315 v4
= v3
^ O_COUNT_BYTES
,
317 O_SKIP_BYTES
= FFS_MASK (v4
),
318 v5
= v4
^ O_SKIP_BYTES
,
320 O_SEEK_BYTES
= FFS_MASK (v5
)
323 /* Ensure that we got something. */
324 static_assert (O_FULLBLOCK
!= 0);
325 static_assert (O_NOCACHE
!= 0);
326 static_assert (O_COUNT_BYTES
!= 0);
327 static_assert (O_SKIP_BYTES
!= 0);
328 static_assert (O_SEEK_BYTES
!= 0);
330 #define MULTIPLE_BITS_SET(i) (((i) & ((i) - 1)) != 0)
332 /* Ensure that this is a single-bit value. */
333 static_assert ( ! MULTIPLE_BITS_SET (O_FULLBLOCK
));
334 static_assert ( ! MULTIPLE_BITS_SET (O_NOCACHE
));
335 static_assert ( ! MULTIPLE_BITS_SET (O_COUNT_BYTES
));
336 static_assert ( ! MULTIPLE_BITS_SET (O_SKIP_BYTES
));
337 static_assert ( ! MULTIPLE_BITS_SET (O_SEEK_BYTES
));
339 /* Flags, for iflag="..." and oflag="...". */
340 static struct symbol_value
const flags
[] =
342 {"append", O_APPEND
},
343 {"binary", O_BINARY
},
345 {"direct", O_DIRECT
},
346 {"directory", O_DIRECTORY
},
348 {"noatime", O_NOATIME
},
349 {"nocache", O_NOCACHE
}, /* Discard cache. */
350 {"noctty", O_NOCTTY
},
351 {"nofollow", HAVE_WORKING_O_NOFOLLOW
? O_NOFOLLOW
: 0},
352 {"nolinks", O_NOLINKS
},
353 {"nonblock", O_NONBLOCK
},
356 {"fullblock", O_FULLBLOCK
}, /* Accumulate full blocks from input. */
357 {"count_bytes", O_COUNT_BYTES
},
358 {"skip_bytes", O_SKIP_BYTES
},
359 {"seek_bytes", O_SEEK_BYTES
},
363 /* Status, for status="...". */
364 static struct symbol_value
const statuses
[] =
366 {"none", STATUS_NONE
},
367 {"noxfer", STATUS_NOXFER
},
368 {"progress", STATUS_PROGRESS
},
372 /* Translation table formed by applying successive transformations. */
373 static unsigned char trans_table
[256];
375 /* Standard translation tables, taken from POSIX 1003.1-2013.
376 Beware of imitations; there are lots of ASCII<->EBCDIC tables
377 floating around the net, perhaps valid for some applications but
380 static char const ascii_to_ebcdic
[] =
382 '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
383 '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
384 '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
385 '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
386 '\100', '\132', '\177', '\173', '\133', '\154', '\120', '\175',
387 '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
388 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
389 '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
390 '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
391 '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
392 '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
393 '\347', '\350', '\351', '\255', '\340', '\275', '\232', '\155',
394 '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
395 '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
396 '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
397 '\247', '\250', '\251', '\300', '\117', '\320', '\137', '\007',
398 '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
399 '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
400 '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
401 '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
402 '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
403 '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
404 '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
405 '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
406 '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
407 '\216', '\217', '\220', '\152', '\233', '\234', '\235', '\236',
408 '\237', '\240', '\252', '\253', '\254', '\112', '\256', '\257',
409 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
410 '\270', '\271', '\272', '\273', '\274', '\241', '\276', '\277',
411 '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
412 '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
413 '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
416 static char const ascii_to_ibm
[] =
418 '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
419 '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
420 '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
421 '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
422 '\100', '\132', '\177', '\173', '\133', '\154', '\120', '\175',
423 '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
424 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
425 '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
426 '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
427 '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
428 '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
429 '\347', '\350', '\351', '\255', '\340', '\275', '\137', '\155',
430 '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
431 '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
432 '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
433 '\247', '\250', '\251', '\300', '\117', '\320', '\241', '\007',
434 '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
435 '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
436 '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
437 '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
438 '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
439 '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
440 '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
441 '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
442 '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
443 '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',
444 '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',
445 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
446 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
447 '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
448 '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
449 '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
452 static char const ebcdic_to_ascii
[] =
454 '\000', '\001', '\002', '\003', '\234', '\011', '\206', '\177',
455 '\227', '\215', '\216', '\013', '\014', '\015', '\016', '\017',
456 '\020', '\021', '\022', '\023', '\235', '\205', '\010', '\207',
457 '\030', '\031', '\222', '\217', '\034', '\035', '\036', '\037',
458 '\200', '\201', '\202', '\203', '\204', '\012', '\027', '\033',
459 '\210', '\211', '\212', '\213', '\214', '\005', '\006', '\007',
460 '\220', '\221', '\026', '\223', '\224', '\225', '\226', '\004',
461 '\230', '\231', '\232', '\233', '\024', '\025', '\236', '\032',
462 '\040', '\240', '\241', '\242', '\243', '\244', '\245', '\246',
463 '\247', '\250', '\325', '\056', '\074', '\050', '\053', '\174',
464 '\046', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
465 '\260', '\261', '\041', '\044', '\052', '\051', '\073', '\176',
466 '\055', '\057', '\262', '\263', '\264', '\265', '\266', '\267',
467 '\270', '\271', '\313', '\054', '\045', '\137', '\076', '\077',
468 '\272', '\273', '\274', '\275', '\276', '\277', '\300', '\301',
469 '\302', '\140', '\072', '\043', '\100', '\047', '\075', '\042',
470 '\303', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
471 '\150', '\151', '\304', '\305', '\306', '\307', '\310', '\311',
472 '\312', '\152', '\153', '\154', '\155', '\156', '\157', '\160',
473 '\161', '\162', '\136', '\314', '\315', '\316', '\317', '\320',
474 '\321', '\345', '\163', '\164', '\165', '\166', '\167', '\170',
475 '\171', '\172', '\322', '\323', '\324', '\133', '\326', '\327',
476 '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
477 '\340', '\341', '\342', '\343', '\344', '\135', '\346', '\347',
478 '\173', '\101', '\102', '\103', '\104', '\105', '\106', '\107',
479 '\110', '\111', '\350', '\351', '\352', '\353', '\354', '\355',
480 '\175', '\112', '\113', '\114', '\115', '\116', '\117', '\120',
481 '\121', '\122', '\356', '\357', '\360', '\361', '\362', '\363',
482 '\134', '\237', '\123', '\124', '\125', '\126', '\127', '\130',
483 '\131', '\132', '\364', '\365', '\366', '\367', '\370', '\371',
484 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
485 '\070', '\071', '\372', '\373', '\374', '\375', '\376', '\377'
488 /* True if we need to close the standard output *stream*. */
489 static bool close_stdout_required
= true;
491 /* The only reason to close the standard output *stream* is if
492 parse_long_options fails (as it does for --help or --version).
493 In any other case, dd uses only the STDOUT_FILENO file descriptor,
494 and the "cleanup" function calls "close (STDOUT_FILENO)".
495 Closing the file descriptor and then letting the usual atexit-run
496 close_stdout function call "fclose (stdout)" would result in a
497 harmless failure of the close syscall (with errno EBADF).
498 This function serves solely to avoid the unnecessary close_stdout
499 call, once parse_long_options has succeeded.
500 Meanwhile, we guarantee that the standard error stream is flushed,
501 by inlining the last half of close_stdout as needed. */
503 maybe_close_stdout (void)
505 if (close_stdout_required
)
507 else if (close_stream (stderr
) != 0)
508 _exit (EXIT_FAILURE
);
511 /* Like the 'error' function but handle any pending newline,
514 ATTRIBUTE_FORMAT ((__printf__
, 2, 3))
516 diagnose (int errnum
, char const *fmt
, ...)
518 if (0 < progress_len
)
520 fputc ('\n', stderr
);
526 verror (0, errnum
, fmt
, ap
);
533 if (status
!= EXIT_SUCCESS
)
538 Usage: %s [OPERAND]...\n\
541 program_name
, program_name
);
543 Copy a file, converting and formatting according to the operands.\n\
545 bs=BYTES read and write up to BYTES bytes at a time (default: 512);\n\
546 overrides ibs and obs\n\
547 cbs=BYTES convert BYTES bytes at a time\n\
548 conv=CONVS convert the file as per the comma separated symbol list\n\
549 count=N copy only N input blocks\n\
550 ibs=BYTES read up to BYTES bytes at a time (default: 512)\n\
553 if=FILE read from FILE instead of stdin\n\
554 iflag=FLAGS read as per the comma separated symbol list\n\
555 obs=BYTES write BYTES bytes at a time (default: 512)\n\
556 of=FILE write to FILE instead of stdout\n\
557 oflag=FLAGS write as per the comma separated symbol list\n\
558 seek=N (or oseek=N) skip N obs-sized output blocks\n\
559 skip=N (or iseek=N) skip N ibs-sized input blocks\n\
560 status=LEVEL The LEVEL of information to print to stderr;\n\
561 'none' suppresses everything but error messages,\n\
562 'noxfer' suppresses the final transfer statistics,\n\
563 'progress' shows periodic transfer statistics\n\
567 N and BYTES may be followed by the following multiplicative suffixes:\n\
568 c=1, w=2, b=512, kB=1000, K=1024, MB=1000*1000, M=1024*1024, xM=M,\n\
569 GB=1000*1000*1000, G=1024*1024*1024, and so on for T, P, E, Z, Y, R, Q.\n\
570 Binary prefixes can be used, too: KiB=K, MiB=M, and so on.\n\
571 If N ends in 'B', it counts bytes not blocks.\n\
573 Each CONV symbol may be:\n\
577 ascii from EBCDIC to ASCII\n\
578 ebcdic from ASCII to EBCDIC\n\
579 ibm from ASCII to alternate EBCDIC\n\
580 block pad newline-terminated records with spaces to cbs-size\n\
581 unblock replace trailing spaces in cbs-size records with newline\n\
582 lcase change upper case to lower case\n\
583 ucase change lower case to upper case\n\
584 sparse try to seek rather than write all-NUL output blocks\n\
585 swab swap every pair of input bytes\n\
586 sync pad every input block with NULs to ibs-size; when used\n\
587 with block or unblock, pad with spaces rather than NULs\n\
590 excl fail if the output file already exists\n\
591 nocreat do not create the output file\n\
592 notrunc do not truncate the output file\n\
593 noerror continue after read errors\n\
594 fdatasync physically write output file data before finishing\n\
595 fsync likewise, but also write metadata\n\
599 Each FLAG symbol may be:\n\
601 append append mode (makes sense only for output; conv=notrunc suggested)\n\
604 fputs (_(" cio use concurrent I/O for data\n"), stdout
);
606 fputs (_(" direct use direct I/O for data\n"), stdout
);
608 fputs (_(" directory fail unless a directory\n"), stdout
);
610 fputs (_(" dsync use synchronized I/O for data\n"), stdout
);
612 fputs (_(" sync likewise, but also for metadata\n"), stdout
);
613 fputs (_(" fullblock accumulate full blocks of input (iflag only)\n"),
616 fputs (_(" nonblock use non-blocking I/O\n"), stdout
);
618 fputs (_(" noatime do not update access time\n"), stdout
);
619 #if HAVE_POSIX_FADVISE
621 fputs (_(" nocache Request to drop cache. See also oflag=sync\n"),
625 fputs (_(" noctty do not assign controlling terminal from file\n"),
627 if (HAVE_WORKING_O_NOFOLLOW
)
628 fputs (_(" nofollow do not follow symlinks\n"), stdout
);
630 fputs (_(" nolinks fail if multiply-linked\n"), stdout
);
632 fputs (_(" binary use binary I/O for data\n"), stdout
);
634 fputs (_(" text use text I/O for data\n"), stdout
);
639 Sending a %s signal to a running 'dd' process makes it\n\
640 print I/O statistics to standard error and then resume copying.\n\
644 "), SIGINFO
== SIGUSR1
? "USR1" : "INFO");
647 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
648 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
649 emit_ancillary_info (PROGRAM_NAME
);
654 /* Common options to use when displaying sizes and rates. */
656 enum { human_opts
= (human_autoscale
| human_round_to_nearest
657 | human_space_before_unit
| human_SI
| human_B
) };
659 /* Ensure input buffer IBUF is allocated. */
667 bool extra_byte_for_swab
= !!(conversions_mask
& C_SWAB
);
668 ibuf
= alignalloc (page_size
, input_blocksize
+ extra_byte_for_swab
);
671 char hbuf
[LONGEST_HUMAN_READABLE
+ 1];
672 error (EXIT_FAILURE
, 0,
673 _("memory exhausted by input buffer of size %td bytes (%s)"),
675 human_readable (input_blocksize
, hbuf
,
676 human_opts
| human_base_1024
, 1, 1));
680 /* Ensure output buffer OBUF is allocated/initialized. */
688 if (conversions_mask
& C_TWOBUFS
)
690 obuf
= alignalloc (page_size
, output_blocksize
);
693 char hbuf
[LONGEST_HUMAN_READABLE
+ 1];
694 error (EXIT_FAILURE
, 0,
695 _("memory exhausted by output buffer of size %td"
698 human_readable (output_blocksize
, hbuf
,
699 human_opts
| human_base_1024
, 1, 1));
710 translate_charset (char const *new_trans
)
712 for (int i
= 0; i
< 256; i
++)
713 trans_table
[i
] = new_trans
[trans_table
[i
]];
714 translation_needed
= true;
717 /* Return true if I has more than one bit set. I must be nonnegative. */
720 multiple_bits_set (int i
)
722 return MULTIPLE_BITS_SET (i
);
726 abbreviation_lacks_prefix (char const *message
)
728 return message
[strlen (message
) - 2] == ' ';
731 /* Print transfer statistics. */
734 print_xfer_stats (xtime_t progress_time
)
736 xtime_t now
= progress_time
? progress_time
: gethrxtime ();
737 static char const slash_s
[] = "/s";
738 char hbuf
[3][LONGEST_HUMAN_READABLE
+ sizeof slash_s
];
740 char const *bytes_per_second
;
741 char const *si
= human_readable (w_bytes
, hbuf
[0], human_opts
, 1, 1);
742 char const *iec
= human_readable (w_bytes
, hbuf
[1],
743 human_opts
| human_base_1024
, 1, 1);
745 /* Use integer arithmetic to compute the transfer rate,
746 since that makes it easy to use SI abbreviations. */
747 char *bpsbuf
= hbuf
[2];
748 int bpsbufsize
= sizeof hbuf
[2];
749 if (start_time
< now
)
751 double XTIME_PRECISIONe0
= XTIME_PRECISION
;
752 xtime_t delta_xtime
= now
- start_time
;
753 delta_s
= delta_xtime
/ XTIME_PRECISIONe0
;
754 bytes_per_second
= human_readable (w_bytes
, bpsbuf
, human_opts
,
755 XTIME_PRECISION
, delta_xtime
);
756 strcat (bytes_per_second
- bpsbuf
+ bpsbuf
, slash_s
);
761 snprintf (bpsbuf
, bpsbufsize
, "%s B/s", _("Infinity"));
762 bytes_per_second
= bpsbuf
;
766 fputc ('\r', stderr
);
768 /* Use full seconds when printing progress, since the progress
769 report is output once per second and there is little point
770 displaying any subsecond jitter. Use default precision with %g
771 otherwise, as this provides more-useful output then. With long
772 transfers %g can generate a number with an exponent; that is OK. */
773 char delta_s_buf
[24];
774 snprintf (delta_s_buf
, sizeof delta_s_buf
,
775 progress_time
? "%.0f s" : "%g s", delta_s
);
778 = (abbreviation_lacks_prefix (si
)
780 ngettext ("%jd byte copied, %s, %s",
781 "%jd bytes copied, %s, %s",
782 select_plural (w_bytes
)),
783 w_bytes
, delta_s_buf
, bytes_per_second
)
784 : abbreviation_lacks_prefix (iec
)
786 _("%jd bytes (%s) copied, %s, %s"),
787 w_bytes
, si
, delta_s_buf
, bytes_per_second
)
789 _("%jd bytes (%s, %s) copied, %s, %s"),
790 w_bytes
, si
, iec
, delta_s_buf
, bytes_per_second
));
794 /* Erase any trailing junk on the output line by outputting
795 spaces. In theory this could glitch the display because the
796 formatted translation of a line describing a larger file
797 could consume fewer screen columns than the strlen difference
798 from the previously formatted translation. In practice this
799 does not seem to be a problem. */
800 if (0 <= stats_len
&& stats_len
< progress_len
)
801 fprintf (stderr
, "%*s", progress_len
- stats_len
, "");
802 progress_len
= stats_len
;
805 fputc ('\n', stderr
);
807 reported_w_bytes
= w_bytes
;
813 if (status_level
== STATUS_NONE
)
816 if (0 < progress_len
)
818 fputc ('\n', stderr
);
823 _("%jd+%jd records in\n"
824 "%jd+%jd records out\n"),
825 r_full
, r_partial
, w_full
, w_partial
);
829 ngettext ("%jd truncated record\n",
830 "%jd truncated records\n",
831 select_plural (r_truncate
)),
834 if (status_level
== STATUS_NOXFER
)
837 print_xfer_stats (0);
840 /* An ordinary signal was received; arrange for the program to exit. */
843 interrupt_handler (int sig
)
846 signal (sig
, SIG_DFL
);
847 interrupt_signal
= sig
;
850 /* An info signal was received; arrange for the program to print status. */
853 siginfo_handler (int sig
)
856 signal (sig
, siginfo_handler
);
860 /* Install the signal handlers. */
863 install_signal_handlers (void)
865 bool catch_siginfo
= ! (SIGINFO
== SIGUSR1
&& getenv ("POSIXLY_CORRECT"));
869 struct sigaction act
;
870 sigemptyset (&caught_signals
);
872 sigaddset (&caught_signals
, SIGINFO
);
873 sigaction (SIGINT
, nullptr, &act
);
874 if (act
.sa_handler
!= SIG_IGN
)
875 sigaddset (&caught_signals
, SIGINT
);
876 act
.sa_mask
= caught_signals
;
878 if (sigismember (&caught_signals
, SIGINFO
))
880 act
.sa_handler
= siginfo_handler
;
881 /* Note we don't use SA_RESTART here and instead
882 handle EINTR explicitly in iftruncate etc.
883 to avoid blocking on uncommitted read/write calls. */
885 sigaction (SIGINFO
, &act
, nullptr);
888 if (sigismember (&caught_signals
, SIGINT
))
890 act
.sa_handler
= interrupt_handler
;
891 act
.sa_flags
= SA_NODEFER
| SA_RESETHAND
;
892 sigaction (SIGINT
, &act
, nullptr);
899 signal (SIGINFO
, siginfo_handler
);
900 siginterrupt (SIGINFO
, 1);
902 if (signal (SIGINT
, SIG_IGN
) != SIG_IGN
)
904 signal (SIGINT
, interrupt_handler
);
905 siginterrupt (SIGINT
, 1);
910 /* Close FD. Return 0 if successful, -1 (setting errno) otherwise.
911 If close fails with errno == EINTR, POSIX says the file descriptor
912 is in an unspecified state, so keep trying to close FD but do not
913 consider EBADF to be an error. Do not process signals. This all
914 differs somewhat from functions like ifdatasync and ifsync. */
922 while (close (fd
) != 0 && errno
!= EBADF
);
927 static int synchronize_output (void);
932 if (!interrupt_signal
)
934 int sync_status
= synchronize_output ();
939 if (iclose (STDIN_FILENO
) != 0)
940 error (EXIT_FAILURE
, errno
, _("closing input file %s"),
941 quoteaf (input_file
));
943 /* Don't remove this call to close, even though close_stdout
944 closes standard output. This close is necessary when cleanup
945 is called as a consequence of signal handling. */
946 if (iclose (STDOUT_FILENO
) != 0)
947 error (EXIT_FAILURE
, errno
,
948 _("closing output file %s"), quoteaf (output_file
));
951 /* Process any pending signals. If signals are caught, this function
952 should be called periodically. Ideally there should never be an
953 unbounded amount of time when signals are not being processed. */
956 process_signals (void)
958 while (interrupt_signal
|| info_signal_count
)
964 sigprocmask (SIG_BLOCK
, &caught_signals
, &oldset
);
966 /* Reload interrupt_signal and info_signal_count, in case a new
967 signal was handled before sigprocmask took effect. */
968 interrupt
= interrupt_signal
;
969 infos
= info_signal_count
;
972 info_signal_count
= infos
- 1;
974 sigprocmask (SIG_SETMASK
, &oldset
, nullptr);
987 /* Process signals first, so that cleanup is called at most once. */
1000 /* Return LEN rounded down to a multiple of IO_BUFSIZE
1001 (to minimize calls to the expensive posix_fadvise (,POSIX_FADV_DONTNEED),
1002 while storing the remainder internally per FD.
1003 Pass LEN == 0 to get the current remainder. */
1006 cache_round (int fd
, off_t len
)
1008 static off_t i_pending
, o_pending
;
1009 off_t
*pending
= (fd
== STDIN_FILENO
? &i_pending
: &o_pending
);
1014 if (ckd_add (&c_pending
, *pending
, len
))
1015 c_pending
= INTMAX_MAX
;
1016 *pending
= c_pending
% IO_BUFSIZE
;
1017 if (c_pending
> *pending
)
1018 len
= c_pending
- *pending
;
1028 /* Discard the cache from the current offset of either
1029 STDIN_FILENO or STDOUT_FILENO.
1030 Return true on success. */
1033 invalidate_cache (int fd
, off_t len
)
1037 bool nocache_eof
= (fd
== STDIN_FILENO
? i_nocache_eof
: o_nocache_eof
);
1039 /* Minimize syscalls. */
1040 off_t clen
= cache_round (fd
, len
);
1042 return true; /* Don't advise this time. */
1043 else if (! len
&& ! clen
&& ! nocache_eof
)
1045 off_t pending
= len
? cache_round (fd
, 0) : 0;
1047 if (fd
== STDIN_FILENO
)
1050 offset
= input_offset
;
1059 static off_t output_offset
= -2;
1061 if (output_offset
!= -1)
1063 if (output_offset
< 0)
1064 output_offset
= lseek (fd
, 0, SEEK_CUR
);
1066 output_offset
+= clen
+ pending
;
1069 offset
= output_offset
;
1074 if (! len
&& clen
&& nocache_eof
)
1080 /* Note we're being careful here to only invalidate what
1081 we've read, so as not to dump any read ahead cache.
1082 Note also the kernel is conservative and only invalidates
1083 full pages in the specified range. */
1084 #if HAVE_POSIX_FADVISE
1085 offset
= offset
- clen
- pending
;
1086 /* ensure full page specified when invalidating to eof. */
1088 offset
-= offset
% page_size
;
1089 adv_ret
= posix_fadvise (fd
, offset
, clen
, POSIX_FADV_DONTNEED
);
1095 return adv_ret
!= -1 ? true : false;
1098 /* Read from FD into the buffer BUF of size SIZE, processing any
1099 signals that arrive before bytes are read. Return the number of
1100 bytes read if successful, -1 (setting errno) on failure. */
1103 iread (int fd
, char *buf
, idx_t size
)
1106 static ssize_t prev_nread
;
1111 nread
= read (fd
, buf
, size
);
1112 /* Ignore final read error with iflag=direct as that
1113 returns EINVAL due to the non aligned file offset. */
1114 if (nread
== -1 && errno
== EINVAL
1115 && 0 < prev_nread
&& prev_nread
< size
1116 && (input_flags
& O_DIRECT
))
1122 while (nread
< 0 && errno
== EINTR
);
1124 /* Short read may be due to received signal. */
1125 if (0 < nread
&& nread
< size
)
1128 if (0 < nread
&& warn_partial_read
)
1130 if (0 < prev_nread
&& prev_nread
< size
)
1132 idx_t prev
= prev_nread
;
1133 if (status_level
!= STATUS_NONE
)
1134 diagnose (0, ngettext (("warning: partial read (%td byte); "
1135 "suggest iflag=fullblock"),
1136 ("warning: partial read (%td bytes); "
1137 "suggest iflag=fullblock"),
1138 select_plural (prev
)),
1140 warn_partial_read
= false;
1148 /* Wrapper around iread function to accumulate full blocks. */
1150 iread_fullblock (int fd
, char *buf
, idx_t size
)
1156 ssize_t ncurr
= iread (fd
, buf
, size
);
1169 /* Write to FD the buffer BUF of size SIZE, processing any signals
1170 that arrive. Return the number of bytes written, setting errno if
1171 this is less than SIZE. Keep trying if there are partial
1175 iwrite (int fd
, char const *buf
, idx_t size
)
1177 idx_t total_written
= 0;
1179 if ((output_flags
& O_DIRECT
) && size
< output_blocksize
)
1181 int old_flags
= fcntl (STDOUT_FILENO
, F_GETFL
);
1182 if (fcntl (STDOUT_FILENO
, F_SETFL
, old_flags
& ~O_DIRECT
) != 0
1183 && status_level
!= STATUS_NONE
)
1184 diagnose (errno
, _("failed to turn off O_DIRECT: %s"),
1185 quotef (output_file
));
1187 /* Since we have just turned off O_DIRECT for the final write,
1188 we try to preserve some of its semantics. */
1190 /* Call invalidate_cache to setup the appropriate offsets
1191 for subsequent calls. */
1192 o_nocache_eof
= true;
1193 invalidate_cache (STDOUT_FILENO
, 0);
1195 /* Attempt to ensure that that final block is committed
1196 to stable storage as quickly as possible. */
1197 conversions_mask
|= C_FSYNC
;
1199 /* After the subsequent fsync we'll call invalidate_cache
1200 to attempt to clear all data from the page cache. */
1203 while (total_written
< size
)
1205 ssize_t nwritten
= 0;
1208 /* Perform a seek for a NUL block if sparse output is enabled. */
1209 final_op_was_seek
= false;
1210 if ((conversions_mask
& C_SPARSE
) && is_nul (buf
, size
))
1212 if (lseek (fd
, size
, SEEK_CUR
) < 0)
1214 conversions_mask
&= ~C_SPARSE
;
1215 /* Don't warn about the advisory sparse request. */
1219 final_op_was_seek
= true;
1225 nwritten
= write (fd
, buf
+ total_written
, size
- total_written
);
1232 else if (nwritten
== 0)
1234 /* Some buggy drivers return 0 when one tries to write beyond
1235 a device's end. (Example: Linux kernel 1.2.13 on /dev/fd0.)
1236 Set errno to ENOSPC so they get a sensible diagnostic. */
1241 total_written
+= nwritten
;
1244 if (o_nocache
&& total_written
)
1245 invalidate_cache (fd
, total_written
);
1247 return total_written
;
1250 /* Write, then empty, the output buffer 'obuf'. */
1255 idx_t nwritten
= iwrite (STDOUT_FILENO
, obuf
, output_blocksize
);
1256 w_bytes
+= nwritten
;
1257 if (nwritten
!= output_blocksize
)
1259 diagnose (errno
, _("writing to %s"), quoteaf (output_file
));
1262 quit (EXIT_FAILURE
);
1269 /* Restart on EINTR from fdatasync. */
1279 ret
= fdatasync (fd
);
1281 while (ret
< 0 && errno
== EINTR
);
1286 /* Restart on EINTR from fd_reopen. */
1289 ifd_reopen (int desired_fd
, char const *file
, int flag
, mode_t mode
)
1296 ret
= fd_reopen (desired_fd
, file
, flag
, mode
);
1298 while (ret
< 0 && errno
== EINTR
);
1303 /* Restart on EINTR from fstat. */
1306 ifstat (int fd
, struct stat
*st
)
1313 ret
= fstat (fd
, st
);
1315 while (ret
< 0 && errno
== EINTR
);
1320 /* Restart on EINTR from fsync. */
1332 while (ret
< 0 && errno
== EINTR
);
1337 /* Restart on EINTR from ftruncate. */
1340 iftruncate (int fd
, off_t length
)
1347 ret
= ftruncate (fd
, length
);
1349 while (ret
< 0 && errno
== EINTR
);
1354 /* Return true if STR is of the form "PATTERN" or "PATTERNDELIM...". */
1358 operand_matches (char const *str
, char const *pattern
, char delim
)
1361 if (*str
++ != *pattern
++)
1363 return !*str
|| *str
== delim
;
1366 /* Interpret one "conv=..." or similar operand STR according to the
1367 symbols in TABLE, returning the flags specified. If the operand
1368 cannot be parsed, use ERROR_MSGID to generate a diagnostic. */
1371 parse_symbols (char const *str
, struct symbol_value
const *table
,
1372 bool exclusive
, char const *error_msgid
)
1378 char const *strcomma
= strchr (str
, ',');
1379 struct symbol_value
const *entry
;
1382 ! (operand_matches (str
, entry
->symbol
, ',') && entry
->value
);
1385 if (! entry
->symbol
[0])
1387 idx_t slen
= strcomma
? strcomma
- str
: strlen (str
);
1388 diagnose (0, "%s: %s", _(error_msgid
),
1389 quotearg_n_style_mem (0, locale_quoting_style
,
1391 usage (EXIT_FAILURE
);
1396 value
= entry
->value
;
1398 value
|= entry
->value
;
1407 /* Return the value of STR, interpreted as a non-negative decimal integer,
1408 optionally multiplied by various values.
1409 Set *INVALID to an appropriate error value and return INTMAX_MAX if
1410 it is an overflow, an indeterminate value if some other error occurred. */
1413 parse_integer (char const *str
, strtol_error
*invalid
)
1415 /* Call xstrtoumax, not xstrtoimax, since we don't want to
1416 allow strings like " -0". Initialize N to an indeterminate value;
1417 calling code should not rely on this function returning 0
1418 when *INVALID represents a non-overflow error. */
1419 int indeterminate
= 0;
1420 uintmax_t n
= indeterminate
;
1422 static char const suffixes
[] = "bcEGkKMPQRTwYZ0";
1423 strtol_error e
= xstrtoumax (str
, &suffix
, 10, &n
, suffixes
);
1426 if ((e
& ~LONGINT_OVERFLOW
) == LONGINT_INVALID_SUFFIX_CHAR
1427 && *suffix
== 'B' && str
< suffix
&& suffix
[-1] != 'B')
1431 e
&= ~LONGINT_INVALID_SUFFIX_CHAR
;
1434 if ((e
& ~LONGINT_OVERFLOW
) == LONGINT_INVALID_SUFFIX_CHAR
1437 strtol_error f
= LONGINT_OK
;
1438 intmax_t o
= parse_integer (suffix
+ 1, &f
);
1439 if ((f
& ~LONGINT_OVERFLOW
) != LONGINT_OK
)
1442 result
= indeterminate
;
1444 else if (ckd_mul (&result
, n
, o
)
1445 || (result
!= 0 && ((e
| f
) & LONGINT_OVERFLOW
)))
1447 e
= LONGINT_OVERFLOW
;
1448 result
= INTMAX_MAX
;
1452 if (result
== 0 && STRPREFIX (str
, "0x"))
1453 diagnose (0, _("warning: %s is a zero multiplier; "
1454 "use %s if that is intended"),
1455 quote_n (0, "0x"), quote_n (1, "00x"));
1459 else if (n
<= INTMAX_MAX
)
1463 e
= LONGINT_OVERFLOW
;
1464 result
= INTMAX_MAX
;
1471 /* OPERAND is of the form "X=...". Return true if X is NAME. */
1475 operand_is (char const *operand
, char const *name
)
1477 return operand_matches (operand
, name
, '=');
1481 scanargs (int argc
, char *const *argv
)
1483 idx_t blocksize
= 0;
1484 intmax_t count
= INTMAX_MAX
;
1487 bool count_B
= false, skip_B
= false, seek_B
= false;
1489 for (int i
= optind
; i
< argc
; i
++)
1491 char const *name
= argv
[i
];
1492 char const *val
= strchr (name
, '=');
1496 diagnose (0, _("unrecognized operand %s"), quoteaf (name
));
1497 usage (EXIT_FAILURE
);
1501 if (operand_is (name
, "if"))
1503 else if (operand_is (name
, "of"))
1505 else if (operand_is (name
, "conv"))
1506 conversions_mask
|= parse_symbols (val
, conversions
, false,
1507 N_("invalid conversion"));
1508 else if (operand_is (name
, "iflag"))
1509 input_flags
|= parse_symbols (val
, flags
, false,
1510 N_("invalid input flag"));
1511 else if (operand_is (name
, "oflag"))
1512 output_flags
|= parse_symbols (val
, flags
, false,
1513 N_("invalid output flag"));
1514 else if (operand_is (name
, "status"))
1515 status_level
= parse_symbols (val
, statuses
, true,
1516 N_("invalid status level"));
1519 strtol_error invalid
= LONGINT_OK
;
1520 intmax_t n
= parse_integer (val
, &invalid
);
1521 bool has_B
= !!strchr (val
, 'B');
1523 intmax_t n_max
= INTMAX_MAX
;
1524 idx_t
*converted_idx
= nullptr;
1526 /* Maximum blocksize. Keep it smaller than IDX_MAX, so that
1527 it fits into blocksize vars even if 1 is added for conv=swab.
1528 Do not exceed SSIZE_MAX, for the benefit of system calls
1529 like "read". And do not exceed OFF_T_MAX, for the
1530 benefit of the large-offset seek code. */
1531 idx_t max_blocksize
= MIN (IDX_MAX
- 1, MIN (SSIZE_MAX
, OFF_T_MAX
));
1533 if (operand_is (name
, "ibs"))
1536 n_max
= max_blocksize
;
1537 converted_idx
= &input_blocksize
;
1539 else if (operand_is (name
, "obs"))
1542 n_max
= max_blocksize
;
1543 converted_idx
= &output_blocksize
;
1545 else if (operand_is (name
, "bs"))
1548 n_max
= max_blocksize
;
1549 converted_idx
= &blocksize
;
1551 else if (operand_is (name
, "cbs"))
1554 n_max
= MIN (SIZE_MAX
, IDX_MAX
);
1555 converted_idx
= &conversion_blocksize
;
1557 else if (operand_is (name
, "skip") || operand_is (name
, "iseek"))
1562 else if (operand_is (name
+ (*name
== 'o'), "seek"))
1567 else if (operand_is (name
, "count"))
1574 diagnose (0, _("unrecognized operand %s"), quoteaf (name
));
1575 usage (EXIT_FAILURE
);
1579 invalid
= LONGINT_INVALID
;
1581 invalid
= LONGINT_OVERFLOW
;
1583 if (invalid
!= LONGINT_OK
)
1584 error (EXIT_FAILURE
, invalid
== LONGINT_OVERFLOW
? EOVERFLOW
: 0,
1585 "%s: %s", _("invalid number"), quoteaf (val
));
1586 else if (converted_idx
)
1592 input_blocksize
= output_blocksize
= blocksize
;
1595 /* POSIX says dd aggregates partial reads into
1596 output_blocksize if bs= is not specified. */
1597 conversions_mask
|= C_TWOBUFS
;
1600 if (input_blocksize
== 0)
1601 input_blocksize
= DEFAULT_BLOCKSIZE
;
1602 if (output_blocksize
== 0)
1603 output_blocksize
= DEFAULT_BLOCKSIZE
;
1604 if (conversion_blocksize
== 0)
1605 conversions_mask
&= ~(C_BLOCK
| C_UNBLOCK
);
1607 if (input_flags
& (O_DSYNC
| O_SYNC
))
1608 input_flags
|= O_RSYNC
;
1610 if (output_flags
& O_FULLBLOCK
)
1612 diagnose (0, "%s: %s", _("invalid output flag"), quote ("fullblock"));
1613 usage (EXIT_FAILURE
);
1617 input_flags
|= O_SKIP_BYTES
;
1618 if (input_flags
& O_SKIP_BYTES
&& skip
!= 0)
1620 skip_records
= skip
/ input_blocksize
;
1621 skip_bytes
= skip
% input_blocksize
;
1624 skip_records
= skip
;
1627 input_flags
|= O_COUNT_BYTES
;
1628 if (input_flags
& O_COUNT_BYTES
&& count
!= INTMAX_MAX
)
1630 max_records
= count
/ input_blocksize
;
1631 max_bytes
= count
% input_blocksize
;
1633 else if (count
!= INTMAX_MAX
)
1634 max_records
= count
;
1637 output_flags
|= O_SEEK_BYTES
;
1638 if (output_flags
& O_SEEK_BYTES
&& seek
!= 0)
1640 seek_records
= seek
/ output_blocksize
;
1641 seek_bytes
= seek
% output_blocksize
;
1644 seek_records
= seek
;
1646 /* Warn about partial reads if bs=SIZE is given and iflag=fullblock
1647 is not, and if counting or skipping bytes or using direct I/O.
1648 This helps to avoid confusion with miscounts, and to avoid issues
1649 with direct I/O on GNU/Linux. */
1651 (! (conversions_mask
& C_TWOBUFS
) && ! (input_flags
& O_FULLBLOCK
)
1653 || (0 < max_records
&& max_records
< INTMAX_MAX
)
1654 || (input_flags
| output_flags
) & O_DIRECT
));
1656 iread_fnc
= ((input_flags
& O_FULLBLOCK
)
1659 input_flags
&= ~O_FULLBLOCK
;
1661 if (multiple_bits_set (conversions_mask
& (C_ASCII
| C_EBCDIC
| C_IBM
)))
1662 error (EXIT_FAILURE
, 0, _("cannot combine any two of {ascii,ebcdic,ibm}"));
1663 if (multiple_bits_set (conversions_mask
& (C_BLOCK
| C_UNBLOCK
)))
1664 error (EXIT_FAILURE
, 0, _("cannot combine block and unblock"));
1665 if (multiple_bits_set (conversions_mask
& (C_LCASE
| C_UCASE
)))
1666 error (EXIT_FAILURE
, 0, _("cannot combine lcase and ucase"));
1667 if (multiple_bits_set (conversions_mask
& (C_EXCL
| C_NOCREAT
)))
1668 error (EXIT_FAILURE
, 0, _("cannot combine excl and nocreat"));
1669 if (multiple_bits_set (input_flags
& (O_DIRECT
| O_NOCACHE
))
1670 || multiple_bits_set (output_flags
& (O_DIRECT
| O_NOCACHE
)))
1671 error (EXIT_FAILURE
, 0, _("cannot combine direct and nocache"));
1673 if (input_flags
& O_NOCACHE
)
1676 i_nocache_eof
= (max_records
== 0 && max_bytes
== 0);
1677 input_flags
&= ~O_NOCACHE
;
1679 if (output_flags
& O_NOCACHE
)
1682 o_nocache_eof
= (max_records
== 0 && max_bytes
== 0);
1683 output_flags
&= ~O_NOCACHE
;
1687 /* Fix up translation table. */
1690 apply_translations (void)
1694 if (conversions_mask
& C_ASCII
)
1695 translate_charset (ebcdic_to_ascii
);
1697 if (conversions_mask
& C_UCASE
)
1699 for (i
= 0; i
< 256; i
++)
1700 trans_table
[i
] = toupper (trans_table
[i
]);
1701 translation_needed
= true;
1703 else if (conversions_mask
& C_LCASE
)
1705 for (i
= 0; i
< 256; i
++)
1706 trans_table
[i
] = tolower (trans_table
[i
]);
1707 translation_needed
= true;
1710 if (conversions_mask
& C_EBCDIC
)
1712 translate_charset (ascii_to_ebcdic
);
1713 newline_character
= ascii_to_ebcdic
['\n'];
1714 space_character
= ascii_to_ebcdic
[' '];
1716 else if (conversions_mask
& C_IBM
)
1718 translate_charset (ascii_to_ibm
);
1719 newline_character
= ascii_to_ibm
['\n'];
1720 space_character
= ascii_to_ibm
[' '];
1724 /* Apply the character-set translations specified by the user
1725 to the NREAD bytes in BUF. */
1728 translate_buffer (char *buf
, idx_t nread
)
1732 for (i
= nread
, cp
= buf
; i
; i
--, cp
++)
1733 *cp
= trans_table
[to_uchar (*cp
)];
1736 /* Swap *NREAD bytes in BUF, which should have room for an extra byte
1737 after the end because the swapping is not in-place. If *SAVED_BYTE
1738 is nonnegative, also swap that initial byte from the previous call.
1739 Save the last byte into into *SAVED_BYTE if needed to make the
1740 resulting *NREAD even, and set *SAVED_BYTE to -1 otherwise.
1741 Return the buffer's adjusted start, either BUF or BUF + 1. */
1744 swab_buffer (char *buf
, idx_t
*nread
, int *saved_byte
)
1749 /* Update *SAVED_BYTE, and set PREV_SAVED to its old value. */
1750 int prev_saved
= *saved_byte
;
1751 if ((prev_saved
< 0) == (*nread
& 1))
1753 unsigned char c
= buf
[--*nread
];
1759 /* Do the byte-swapping by moving every other byte two
1760 positions toward the end, working from the end of the buffer
1761 toward the beginning. This way we move only half the data. */
1762 for (idx_t i
= *nread
; 1 < i
; i
-= 2)
1763 buf
[i
] = buf
[i
- 2];
1768 buf
[1] = prev_saved
;
1773 /* Add OFFSET to the input offset, setting the overflow flag if
1777 advance_input_offset (intmax_t offset
)
1779 if (0 <= input_offset
&& ckd_add (&input_offset
, input_offset
, offset
))
1783 /* Throw away RECORDS blocks of BLOCKSIZE bytes plus BYTES bytes on
1784 file descriptor FDESC, which is open with read permission for FILE.
1785 Store up to BLOCKSIZE bytes of the data at a time in IBUF or OBUF, if
1786 necessary. RECORDS or BYTES must be nonzero. If FDESC is
1787 STDIN_FILENO, advance the input offset. Return the number of
1788 records remaining, i.e., that were not skipped because EOF was
1789 reached. If FDESC is STDOUT_FILENO, on return, BYTES is the
1790 remaining bytes in addition to the remaining records. */
1793 skip (int fdesc
, char const *file
, intmax_t records
, idx_t blocksize
,
1796 /* Try lseek and if an error indicates it was an inappropriate operation --
1797 or if the file offset is not representable as an off_t --
1798 fall back on using read. */
1802 if (! ckd_mul (&offset
, records
, blocksize
)
1803 && ! ckd_add (&offset
, offset
, *bytes
)
1804 && 0 <= lseek (fdesc
, offset
, SEEK_CUR
))
1806 if (fdesc
== STDIN_FILENO
)
1809 if (ifstat (STDIN_FILENO
, &st
) != 0)
1810 error (EXIT_FAILURE
, errno
, _("cannot fstat %s"), quoteaf (file
));
1811 if (usable_st_size (&st
) && 0 < st
.st_size
&& 0 <= input_offset
1812 && st
.st_size
- input_offset
< offset
)
1814 /* When skipping past EOF, return the number of _full_ blocks
1815 * that are not skipped, and set offset to EOF, so the caller
1816 * can determine the requested skip was not satisfied. */
1817 records
= ( offset
- st
.st_size
) / blocksize
;
1818 offset
= st
.st_size
- input_offset
;
1822 advance_input_offset (offset
);
1833 int lseek_errno
= errno
;
1835 /* The seek request may have failed above if it was too big
1836 (> device size, > max file size, etc.)
1837 Or it may not have been done at all (> OFF_T_MAX).
1838 Therefore try to seek to the end of the file,
1839 to avoid redundant reading. */
1840 if (lseek (fdesc
, 0, SEEK_END
) >= 0)
1842 /* File is seekable, and we're at the end of it, and
1843 size <= OFF_T_MAX. So there's no point using read to advance. */
1847 /* The original seek was not attempted as offset > OFF_T_MAX.
1848 We should error for write as can't get to the desired
1849 location, even if OFF_T_MAX < max file size.
1850 For read we're not going to read any data anyway,
1851 so we should error for consistency.
1852 It would be nice to not error for /dev/{zero,null}
1853 for any offset, but that's not a significant issue. */
1854 lseek_errno
= EOVERFLOW
;
1857 diagnose (lseek_errno
,
1858 gettext (fdesc
== STDIN_FILENO
1859 ? N_("%s: cannot skip")
1860 : N_("%s: cannot seek")),
1862 /* If the file has a specific size and we've asked
1863 to skip/seek beyond the max allowable, then quit. */
1864 quit (EXIT_FAILURE
);
1866 /* else file_size && offset > OFF_T_MAX or file ! seekable */
1869 if (fdesc
== STDIN_FILENO
)
1882 ssize_t nread
= iread_fnc (fdesc
, buf
, records
? blocksize
: *bytes
);
1885 if (fdesc
== STDIN_FILENO
)
1887 diagnose (errno
, _("error reading %s"), quoteaf (file
));
1888 if (conversions_mask
& C_NOERROR
)
1892 diagnose (lseek_errno
, _("%s: cannot seek"), quotef (file
));
1893 quit (EXIT_FAILURE
);
1895 else if (nread
== 0)
1897 else if (fdesc
== STDIN_FILENO
)
1898 advance_input_offset (nread
);
1905 while (records
|| *bytes
);
1911 /* Advance the input by NBYTES if possible, after a read error.
1912 The input file offset may or may not have advanced after the failed
1913 read; adjust it to point just after the bad record regardless.
1914 Return true if successful, or if the input is already known to not
1918 advance_input_after_read_error (idx_t nbytes
)
1920 if (! input_seekable
)
1922 if (input_seek_errno
== ESPIPE
)
1924 errno
= input_seek_errno
;
1929 advance_input_offset (nbytes
);
1930 if (input_offset
< 0)
1932 diagnose (0, _("offset overflow while reading file %s"),
1933 quoteaf (input_file
));
1936 offset
= lseek (STDIN_FILENO
, 0, SEEK_CUR
);
1940 if (offset
== input_offset
)
1942 diff
= input_offset
- offset
;
1943 if (! (0 <= diff
&& diff
<= nbytes
) && status_level
!= STATUS_NONE
)
1944 diagnose (0, _("warning: invalid file offset after failed read"));
1945 if (0 <= lseek (STDIN_FILENO
, diff
, SEEK_CUR
))
1948 diagnose (0, _("cannot work around kernel bug after all"));
1952 diagnose (errno
, _("%s: cannot seek"), quotef (input_file
));
1956 /* Copy NREAD bytes of BUF, with no conversions. */
1959 copy_simple (char const *buf
, idx_t nread
)
1961 char const *start
= buf
; /* First uncopied char in BUF. */
1965 idx_t nfree
= MIN (nread
, output_blocksize
- oc
);
1967 memcpy (obuf
+ oc
, start
, nfree
);
1969 nread
-= nfree
; /* Update the number of bytes left to copy. */
1972 if (oc
>= output_blocksize
)
1978 /* Copy NREAD bytes of BUF, doing conv=block
1979 (pad newline-terminated records to 'conversion_blocksize',
1980 replacing the newline with trailing spaces). */
1983 copy_with_block (char const *buf
, idx_t nread
)
1985 for (idx_t i
= nread
; i
; i
--, buf
++)
1987 if (*buf
== newline_character
)
1989 if (col
< conversion_blocksize
)
1992 for (j
= col
; j
< conversion_blocksize
; j
++)
1993 output_char (space_character
);
1999 if (col
== conversion_blocksize
)
2001 else if (col
< conversion_blocksize
)
2008 /* Copy NREAD bytes of BUF, doing conv=unblock
2009 (replace trailing spaces in 'conversion_blocksize'-sized records
2013 copy_with_unblock (char const *buf
, idx_t nread
)
2015 static idx_t pending_spaces
= 0;
2017 for (idx_t i
= 0; i
< nread
; i
++)
2021 if (col
++ >= conversion_blocksize
)
2023 col
= pending_spaces
= 0; /* Wipe out any pending spaces. */
2024 i
--; /* Push the char back; get it later. */
2025 output_char (newline_character
);
2027 else if (c
== space_character
)
2031 /* 'c' is the character after a run of spaces that were not
2032 at the end of the conversion buffer. Output them. */
2033 while (pending_spaces
)
2035 output_char (space_character
);
2043 /* Set the file descriptor flags for FD that correspond to the nonzero bits
2044 in ADD_FLAGS. The file's name is NAME. */
2047 set_fd_flags (int fd
, int add_flags
, char const *name
)
2049 /* Ignore file creation flags that are no-ops on file descriptors. */
2050 add_flags
&= ~ (O_NOCTTY
| O_NOFOLLOW
);
2054 int old_flags
= fcntl (fd
, F_GETFL
);
2055 int new_flags
= old_flags
| add_flags
;
2059 else if (old_flags
!= new_flags
)
2061 if (new_flags
& (O_DIRECTORY
| O_NOLINKS
))
2063 /* NEW_FLAGS contains at least one file creation flag that
2064 requires some checking of the open file descriptor. */
2066 if (ifstat (fd
, &st
) != 0)
2068 else if ((new_flags
& O_DIRECTORY
) && ! S_ISDIR (st
.st_mode
))
2073 else if ((new_flags
& O_NOLINKS
) && 1 < st
.st_nlink
)
2078 new_flags
&= ~ (O_DIRECTORY
| O_NOLINKS
);
2081 if (ok
&& old_flags
!= new_flags
2082 && fcntl (fd
, F_SETFL
, new_flags
) == -1)
2087 error (EXIT_FAILURE
, errno
, _("setting flags for %s"), quoteaf (name
));
2091 /* The main loop. */
2096 char *bufstart
; /* Input buffer. */
2097 ssize_t nread
; /* Bytes read in the current block. */
2099 /* If nonzero, then the previously read block was partial and
2100 PARTREAD was its size. */
2103 int exit_status
= EXIT_SUCCESS
;
2106 if (skip_records
!= 0 || skip_bytes
!= 0)
2109 bool us_bytes_overflow
=
2110 (ckd_mul (&us_bytes
, skip_records
, input_blocksize
)
2111 || ckd_add (&us_bytes
, skip_bytes
, us_bytes
));
2112 off_t input_offset0
= input_offset
;
2113 intmax_t us_blocks
= skip (STDIN_FILENO
, input_file
,
2114 skip_records
, input_blocksize
, &skip_bytes
);
2116 /* POSIX doesn't say what to do when dd detects it has been
2117 asked to skip past EOF, so I assume it's non-fatal.
2118 There are 3 reasons why there might be unskipped blocks/bytes:
2119 1. file is too small
2120 2. pipe has not enough data
2123 || (0 <= input_offset
2124 && (us_bytes_overflow
2125 || us_bytes
!= input_offset
- input_offset0
)))
2126 && status_level
!= STATUS_NONE
)
2128 diagnose (0, _("%s: cannot skip to specified offset"),
2129 quotef (input_file
));
2133 if (seek_records
!= 0 || seek_bytes
!= 0)
2135 idx_t bytes
= seek_bytes
;
2136 intmax_t write_records
= skip (STDOUT_FILENO
, output_file
,
2137 seek_records
, output_blocksize
, &bytes
);
2139 if (write_records
!= 0 || bytes
!= 0)
2141 memset (obuf
, 0, write_records
? output_blocksize
: bytes
);
2145 idx_t size
= write_records
? output_blocksize
: bytes
;
2146 if (iwrite (STDOUT_FILENO
, obuf
, size
) != size
)
2148 diagnose (errno
, _("writing to %s"), quoteaf (output_file
));
2149 quit (EXIT_FAILURE
);
2152 if (write_records
!= 0)
2157 while (write_records
|| bytes
);
2161 if (max_records
== 0 && max_bytes
== 0)
2166 int saved_byte
= -1;
2170 if (status_level
== STATUS_PROGRESS
)
2172 xtime_t progress_time
= gethrxtime ();
2173 if (next_time
<= progress_time
)
2175 print_xfer_stats (progress_time
);
2176 next_time
+= XTIME_PRECISION
;
2180 if (r_partial
+ r_full
>= max_records
+ !!max_bytes
)
2183 /* Zero the buffer before reading, so that if we get a read error,
2184 whatever data we are able to read is followed by zeros.
2185 This minimizes data loss. */
2186 if ((conversions_mask
& C_SYNC
) && (conversions_mask
& C_NOERROR
))
2188 (conversions_mask
& (C_BLOCK
| C_UNBLOCK
)) ? ' ' : '\0',
2191 if (r_partial
+ r_full
>= max_records
)
2192 nread
= iread_fnc (STDIN_FILENO
, ibuf
, max_bytes
);
2194 nread
= iread_fnc (STDIN_FILENO
, ibuf
, input_blocksize
);
2198 advance_input_offset (nread
);
2200 invalidate_cache (STDIN_FILENO
, nread
);
2202 else if (nread
== 0)
2204 i_nocache_eof
|= i_nocache
;
2205 o_nocache_eof
|= o_nocache
&& ! (conversions_mask
& C_NOTRUNC
);
2210 if (!(conversions_mask
& C_NOERROR
) || status_level
!= STATUS_NONE
)
2211 diagnose (errno
, _("error reading %s"), quoteaf (input_file
));
2213 if (conversions_mask
& C_NOERROR
)
2216 idx_t bad_portion
= input_blocksize
- partread
;
2218 /* We already know this data is not cached,
2219 but call this so that correct offsets are maintained. */
2220 invalidate_cache (STDIN_FILENO
, bad_portion
);
2222 /* Seek past the bad block if possible. */
2223 if (!advance_input_after_read_error (bad_portion
))
2225 exit_status
= EXIT_FAILURE
;
2227 /* Suppress duplicate diagnostics. */
2228 input_seekable
= false;
2229 input_seek_errno
= ESPIPE
;
2231 if ((conversions_mask
& C_SYNC
) && !partread
)
2232 /* Replace the missing input with null bytes and
2233 proceed normally. */
2240 /* Write any partial block. */
2241 exit_status
= EXIT_FAILURE
;
2246 n_bytes_read
= nread
;
2248 if (n_bytes_read
< input_blocksize
)
2251 partread
= n_bytes_read
;
2252 if (conversions_mask
& C_SYNC
)
2254 if (!(conversions_mask
& C_NOERROR
))
2255 /* If C_NOERROR, we zeroed the block before reading. */
2256 memset (ibuf
+ n_bytes_read
,
2257 (conversions_mask
& (C_BLOCK
| C_UNBLOCK
)) ? ' ' : '\0',
2258 input_blocksize
- n_bytes_read
);
2259 n_bytes_read
= input_blocksize
;
2268 if (ibuf
== obuf
) /* If not C_TWOBUFS. */
2270 idx_t nwritten
= iwrite (STDOUT_FILENO
, obuf
, n_bytes_read
);
2271 w_bytes
+= nwritten
;
2272 if (nwritten
!= n_bytes_read
)
2274 diagnose (errno
, _("error writing %s"), quoteaf (output_file
));
2275 return EXIT_FAILURE
;
2277 else if (n_bytes_read
== input_blocksize
)
2284 /* Do any translations on the whole buffer at once. */
2286 if (translation_needed
)
2287 translate_buffer (ibuf
, n_bytes_read
);
2289 if (conversions_mask
& C_SWAB
)
2290 bufstart
= swab_buffer (ibuf
, &n_bytes_read
, &saved_byte
);
2294 if (conversions_mask
& C_BLOCK
)
2295 copy_with_block (bufstart
, n_bytes_read
);
2296 else if (conversions_mask
& C_UNBLOCK
)
2297 copy_with_unblock (bufstart
, n_bytes_read
);
2299 copy_simple (bufstart
, n_bytes_read
);
2302 /* If we have a char left as a result of conv=swab, output it. */
2303 if (0 <= saved_byte
)
2305 char saved_char
= saved_byte
;
2306 if (conversions_mask
& C_BLOCK
)
2307 copy_with_block (&saved_char
, 1);
2308 else if (conversions_mask
& C_UNBLOCK
)
2309 copy_with_unblock (&saved_char
, 1);
2311 output_char (saved_char
);
2314 if ((conversions_mask
& C_BLOCK
) && col
> 0)
2316 /* If the final input line didn't end with a '\n', pad
2317 the output block to 'conversion_blocksize' chars. */
2318 for (idx_t i
= col
; i
< conversion_blocksize
; i
++)
2319 output_char (space_character
);
2322 if (col
&& (conversions_mask
& C_UNBLOCK
))
2324 /* If there was any output, add a final '\n'. */
2325 output_char (newline_character
);
2328 /* Write out the last block. */
2331 idx_t nwritten
= iwrite (STDOUT_FILENO
, obuf
, oc
);
2332 w_bytes
+= nwritten
;
2337 diagnose (errno
, _("error writing %s"), quoteaf (output_file
));
2338 return EXIT_FAILURE
;
2342 /* If the last write was converted to a seek, then for a regular file
2343 or shared memory object, ftruncate to extend the size. */
2344 if (final_op_was_seek
)
2346 struct stat stdout_stat
;
2347 if (ifstat (STDOUT_FILENO
, &stdout_stat
) != 0)
2349 diagnose (errno
, _("cannot fstat %s"), quoteaf (output_file
));
2350 return EXIT_FAILURE
;
2352 if (S_ISREG (stdout_stat
.st_mode
) || S_TYPEISSHM (&stdout_stat
))
2354 off_t output_offset
= lseek (STDOUT_FILENO
, 0, SEEK_CUR
);
2355 if (0 <= output_offset
&& stdout_stat
.st_size
< output_offset
)
2357 if (iftruncate (STDOUT_FILENO
, output_offset
) != 0)
2359 diagnose (errno
, _("failed to truncate to %jd bytes"
2360 " in output file %s"),
2361 (intmax_t) output_offset
, quoteaf (output_file
));
2362 return EXIT_FAILURE
;
2368 /* fdatasync/fsync can take a long time, so issue a final progress
2369 indication now if progress has been made since the previous indication. */
2370 if (conversions_mask
& (C_FDATASYNC
| C_FSYNC
)
2371 && status_level
== STATUS_PROGRESS
2372 && 0 <= reported_w_bytes
&& reported_w_bytes
< w_bytes
)
2373 print_xfer_stats (0);
2378 /* Synchronize output according to conversions_mask.
2379 Do this even if w_bytes is zero, as fsync and fdatasync
2380 flush out write requests from other processes too.
2381 Clear bits in conversions_mask so that synchronization is done only once.
2382 Return zero if successful, an exit status otherwise. */
2385 synchronize_output (void)
2387 int exit_status
= 0;
2388 int mask
= conversions_mask
;
2389 conversions_mask
&= ~ (C_FDATASYNC
| C_FSYNC
);
2391 if ((mask
& C_FDATASYNC
) && ifdatasync (STDOUT_FILENO
) != 0)
2393 if (errno
!= ENOSYS
&& errno
!= EINVAL
)
2395 diagnose (errno
, _("fdatasync failed for %s"), quoteaf (output_file
));
2396 exit_status
= EXIT_FAILURE
;
2401 if ((mask
& C_FSYNC
) && ifsync (STDOUT_FILENO
) != 0)
2403 diagnose (errno
, _("fsync failed for %s"), quoteaf (output_file
));
2404 return EXIT_FAILURE
;
2411 main (int argc
, char **argv
)
2417 install_signal_handlers ();
2419 initialize_main (&argc
, &argv
);
2420 set_program_name (argv
[0]);
2421 setlocale (LC_ALL
, "");
2422 bindtextdomain (PACKAGE
, LOCALEDIR
);
2423 textdomain (PACKAGE
);
2425 /* Arrange to close stdout if parse_long_options exits. */
2426 atexit (maybe_close_stdout
);
2428 page_size
= getpagesize ();
2430 parse_gnu_standard_options_only (argc
, argv
, PROGRAM_NAME
, PACKAGE
, Version
,
2431 true, usage
, AUTHORS
,
2432 (char const *) nullptr);
2433 close_stdout_required
= false;
2435 /* Initialize translation table to identity translation. */
2436 for (i
= 0; i
< 256; i
++)
2439 /* Decode arguments. */
2440 scanargs (argc
, argv
);
2442 apply_translations ();
2444 if (input_file
== nullptr)
2446 input_file
= _("standard input");
2447 set_fd_flags (STDIN_FILENO
, input_flags
, input_file
);
2451 if (ifd_reopen (STDIN_FILENO
, input_file
, O_RDONLY
| input_flags
, 0) < 0)
2452 error (EXIT_FAILURE
, errno
, _("failed to open %s"),
2453 quoteaf (input_file
));
2456 offset
= lseek (STDIN_FILENO
, 0, SEEK_CUR
);
2457 input_seekable
= (0 <= offset
);
2458 input_offset
= MAX (0, offset
);
2459 input_seek_errno
= errno
;
2461 if (output_file
== nullptr)
2463 output_file
= _("standard output");
2464 set_fd_flags (STDOUT_FILENO
, output_flags
, output_file
);
2468 mode_t perms
= MODE_RW_UGO
;
2471 | (conversions_mask
& C_NOCREAT
? 0 : O_CREAT
)
2472 | (conversions_mask
& C_EXCL
? O_EXCL
: 0)
2473 | (seek_records
|| (conversions_mask
& C_NOTRUNC
) ? 0 : O_TRUNC
));
2476 if ((ckd_mul (&size
, seek_records
, output_blocksize
)
2477 || ckd_add (&size
, seek_bytes
, size
))
2478 && !(conversions_mask
& C_NOTRUNC
))
2479 error (EXIT_FAILURE
, 0,
2480 _("offset too large: "
2481 "cannot truncate to a length of seek=%jd"
2482 " (%td-byte) blocks"),
2483 seek_records
, output_blocksize
);
2485 /* Open the output file with *read* access only if we might
2486 need to read to satisfy a 'seek=' request. If we can't read
2487 the file, go ahead with write-only access; it might work. */
2489 || ifd_reopen (STDOUT_FILENO
, output_file
, O_RDWR
| opts
, perms
) < 0)
2490 && (ifd_reopen (STDOUT_FILENO
, output_file
, O_WRONLY
| opts
, perms
)
2492 error (EXIT_FAILURE
, errno
, _("failed to open %s"),
2493 quoteaf (output_file
));
2495 if (seek_records
!= 0 && !(conversions_mask
& C_NOTRUNC
))
2497 if (iftruncate (STDOUT_FILENO
, size
) != 0)
2499 /* Complain only when ftruncate fails on a regular file, a
2500 directory, or a shared memory object, as POSIX 1003.1-2004
2501 specifies ftruncate's behavior only for these file types.
2502 For example, do not complain when Linux kernel 2.4 ftruncate
2503 fails on /dev/fd0. */
2504 int ftruncate_errno
= errno
;
2505 struct stat stdout_stat
;
2506 if (ifstat (STDOUT_FILENO
, &stdout_stat
) != 0)
2508 diagnose (errno
, _("cannot fstat %s"), quoteaf (output_file
));
2509 exit_status
= EXIT_FAILURE
;
2511 else if (S_ISREG (stdout_stat
.st_mode
)
2512 || S_ISDIR (stdout_stat
.st_mode
)
2513 || S_TYPEISSHM (&stdout_stat
))
2515 intmax_t isize
= size
;
2516 diagnose (ftruncate_errno
,
2517 _("failed to truncate to %jd bytes"
2518 " in output file %s"),
2519 isize
, quoteaf (output_file
));
2520 exit_status
= EXIT_FAILURE
;
2526 start_time
= gethrxtime ();
2527 next_time
= start_time
+ XTIME_PRECISION
;
2529 exit_status
= dd_copy ();
2531 int sync_status
= synchronize_output ();
2533 exit_status
= sync_status
;
2535 if (max_records
== 0 && max_bytes
== 0)
2537 /* Special case to invalidate cache to end of file. */
2538 if (i_nocache
&& !invalidate_cache (STDIN_FILENO
, 0))
2540 diagnose (errno
, _("failed to discard cache for: %s"),
2541 quotef (input_file
));
2542 exit_status
= EXIT_FAILURE
;
2544 if (o_nocache
&& !invalidate_cache (STDOUT_FILENO
, 0))
2546 diagnose (errno
, _("failed to discard cache for: %s"),
2547 quotef (output_file
));
2548 exit_status
= EXIT_FAILURE
;
2553 /* Invalidate any pending region or to EOF if appropriate. */
2554 if (i_nocache
|| i_nocache_eof
)
2555 invalidate_cache (STDIN_FILENO
, 0);
2556 if (o_nocache
|| o_nocache_eof
)
2557 invalidate_cache (STDOUT_FILENO
, 0);
2561 main_exit (exit_status
);