1 /* dd -- convert a file while copying it.
2 Copyright (C) 1985-2023 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. */
21 #include <sys/types.h>
25 #include "alignalloc.h"
26 #include "close-stream.h"
27 #include "fd-reopen.h"
28 #include "gethrxtime.h"
30 #include "ioblksize.h"
31 #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. */
76 # define fdatasync(fd) (errno = ENOSYS, -1)
79 #define output_char(c) \
83 if (oc >= output_blocksize) \
88 /* Default input and output blocksize. */
89 #define DEFAULT_BLOCKSIZE 512
91 /* Conversions bit masks. */
107 /* Use separate input and output buffers, and combine partial
113 C_FDATASYNC
= 040000,
128 /* The name of the input file, or nullptr for the standard input. */
129 static char const *input_file
= nullptr;
131 /* The name of the output file, or nullptr for the standard output. */
132 static char const *output_file
= nullptr;
134 /* The page size on this host. */
135 static idx_t page_size
;
137 /* The number of bytes in which atomic reads are done. */
138 static idx_t input_blocksize
= 0;
140 /* The number of bytes in which atomic writes are done. */
141 static idx_t output_blocksize
= 0;
143 /* Conversion buffer size, in bytes. 0 prevents conversions. */
144 static idx_t conversion_blocksize
= 0;
146 /* Skip this many records of 'input_blocksize' bytes before input. */
147 static intmax_t skip_records
= 0;
149 /* Skip this many bytes before input in addition of 'skip_records'
151 static idx_t skip_bytes
= 0;
153 /* Skip this many records of 'output_blocksize' bytes before output. */
154 static intmax_t seek_records
= 0;
156 /* Skip this many bytes in addition to 'seek_records' records before
158 static intmax_t seek_bytes
= 0;
160 /* Whether the final output was done with a seek (rather than a write). */
161 static bool final_op_was_seek
;
163 /* Copy only this many records. The default is effectively infinity. */
164 static intmax_t max_records
= INTMAX_MAX
;
166 /* Copy this many bytes in addition to 'max_records' records. */
167 static idx_t max_bytes
= 0;
169 /* Bit vector of conversions to apply. */
170 static int conversions_mask
= 0;
172 /* Open flags for the input and output files. */
173 static int input_flags
= 0;
174 static int output_flags
= 0;
176 /* Status flags for what is printed to stderr. */
177 static int status_level
= STATUS_DEFAULT
;
179 /* If nonzero, filter characters through the translation table. */
180 static bool translation_needed
= false;
182 /* Number of partial blocks written. */
183 static intmax_t w_partial
= 0;
185 /* Number of full blocks written. */
186 static intmax_t w_full
= 0;
188 /* Number of partial blocks read. */
189 static intmax_t r_partial
= 0;
191 /* Number of full blocks read. */
192 static intmax_t r_full
= 0;
194 /* Number of bytes written. */
195 static intmax_t w_bytes
= 0;
197 /* Last-reported number of bytes written, or negative if never reported. */
198 static intmax_t reported_w_bytes
= -1;
200 /* Time that dd started. */
201 static xtime_t start_time
;
203 /* Next time to report periodic progress. */
204 static xtime_t next_time
;
206 /* If positive, the number of bytes output in the current progress line. */
207 static int progress_len
;
209 /* True if input is seekable. */
210 static bool input_seekable
;
212 /* Error number corresponding to initial attempt to lseek input.
213 If ESPIPE, do not issue any more diagnostics about it. */
214 static int input_seek_errno
;
216 /* File offset of the input, in bytes, or -1 if it overflowed. */
217 static off_t input_offset
;
219 /* True if a partial read should be diagnosed. */
220 static bool warn_partial_read
;
222 /* Records truncated by conv=block. */
223 static intmax_t r_truncate
= 0;
225 /* Output representation of newline and space characters.
226 They change if we're converting to EBCDIC. */
227 static char newline_character
= '\n';
228 static char space_character
= ' ';
234 /* Current index into 'obuf'. */
237 /* Index into current line, for 'conv=block' and 'conv=unblock'. */
238 static idx_t col
= 0;
240 /* The set of signals that are caught. */
241 static sigset_t caught_signals
;
243 /* If nonzero, the value of the pending fatal signal. */
244 static sig_atomic_t volatile interrupt_signal
;
246 /* A count of the number of pending info signals that have been received. */
247 static sig_atomic_t volatile info_signal_count
;
249 /* Whether to discard cache for input or output. */
250 static bool i_nocache
, o_nocache
;
252 /* Whether to instruct the kernel to discard the complete file. */
253 static bool i_nocache_eof
, o_nocache_eof
;
255 /* Function used for read (to handle iflag=fullblock parameter). */
256 static ssize_t (*iread_fnc
) (int fd
, char *buf
, idx_t size
);
258 /* A longest symbol in the struct symbol_values tables below. */
259 #define LONGEST_SYMBOL "count_bytes"
261 /* A symbol and the corresponding integer value. */
264 char symbol
[sizeof LONGEST_SYMBOL
];
268 /* Conversion symbols, for conv="...". */
269 static struct symbol_value
const conversions
[] =
271 {"ascii", C_ASCII
| C_UNBLOCK
| C_TWOBUFS
}, /* EBCDIC to ASCII. */
272 {"ebcdic", C_EBCDIC
| C_BLOCK
| C_TWOBUFS
}, /* ASCII to EBCDIC. */
273 {"ibm", C_IBM
| C_BLOCK
| C_TWOBUFS
}, /* Different ASCII to EBCDIC. */
274 {"block", C_BLOCK
| C_TWOBUFS
}, /* Variable to fixed length records. */
275 {"unblock", C_UNBLOCK
| C_TWOBUFS
}, /* Fixed to variable length records. */
276 {"lcase", C_LCASE
| C_TWOBUFS
}, /* Translate upper to lower case. */
277 {"ucase", C_UCASE
| C_TWOBUFS
}, /* Translate lower to upper case. */
278 {"sparse", C_SPARSE
}, /* Try to sparsely write output. */
279 {"swab", C_SWAB
| C_TWOBUFS
}, /* Swap bytes of input. */
280 {"noerror", C_NOERROR
}, /* Ignore i/o errors. */
281 {"nocreat", C_NOCREAT
}, /* Do not create output file. */
282 {"excl", C_EXCL
}, /* Fail if the output file already exists. */
283 {"notrunc", C_NOTRUNC
}, /* Do not truncate output file. */
284 {"sync", C_SYNC
}, /* Pad input records to ibs with NULs. */
285 {"fdatasync", C_FDATASYNC
}, /* Synchronize output data before finishing. */
286 {"fsync", C_FSYNC
}, /* Also synchronize output metadata. */
290 #define FFS_MASK(x) ((x) ^ ((x) & ((x) - 1)))
293 /* Compute a value that's bitwise disjoint from the union
311 /* Use its lowest bits for private flags. */
312 O_FULLBLOCK
= FFS_MASK (v
),
313 v2
= v
^ O_FULLBLOCK
,
315 O_NOCACHE
= FFS_MASK (v2
),
318 O_COUNT_BYTES
= FFS_MASK (v3
),
319 v4
= v3
^ O_COUNT_BYTES
,
321 O_SKIP_BYTES
= FFS_MASK (v4
),
322 v5
= v4
^ O_SKIP_BYTES
,
324 O_SEEK_BYTES
= FFS_MASK (v5
)
327 /* Ensure that we got something. */
328 static_assert (O_FULLBLOCK
!= 0);
329 static_assert (O_NOCACHE
!= 0);
330 static_assert (O_COUNT_BYTES
!= 0);
331 static_assert (O_SKIP_BYTES
!= 0);
332 static_assert (O_SEEK_BYTES
!= 0);
334 #define MULTIPLE_BITS_SET(i) (((i) & ((i) - 1)) != 0)
336 /* Ensure that this is a single-bit value. */
337 static_assert ( ! MULTIPLE_BITS_SET (O_FULLBLOCK
));
338 static_assert ( ! MULTIPLE_BITS_SET (O_NOCACHE
));
339 static_assert ( ! MULTIPLE_BITS_SET (O_COUNT_BYTES
));
340 static_assert ( ! MULTIPLE_BITS_SET (O_SKIP_BYTES
));
341 static_assert ( ! MULTIPLE_BITS_SET (O_SEEK_BYTES
));
343 /* Flags, for iflag="..." and oflag="...". */
344 static struct symbol_value
const flags
[] =
346 {"append", O_APPEND
},
347 {"binary", O_BINARY
},
349 {"direct", O_DIRECT
},
350 {"directory", O_DIRECTORY
},
352 {"noatime", O_NOATIME
},
353 {"nocache", O_NOCACHE
}, /* Discard cache. */
354 {"noctty", O_NOCTTY
},
355 {"nofollow", HAVE_WORKING_O_NOFOLLOW
? O_NOFOLLOW
: 0},
356 {"nolinks", O_NOLINKS
},
357 {"nonblock", O_NONBLOCK
},
360 {"fullblock", O_FULLBLOCK
}, /* Accumulate full blocks from input. */
361 {"count_bytes", O_COUNT_BYTES
},
362 {"skip_bytes", O_SKIP_BYTES
},
363 {"seek_bytes", O_SEEK_BYTES
},
367 /* Status, for status="...". */
368 static struct symbol_value
const statuses
[] =
370 {"none", STATUS_NONE
},
371 {"noxfer", STATUS_NOXFER
},
372 {"progress", STATUS_PROGRESS
},
376 /* Translation table formed by applying successive transformations. */
377 static unsigned char trans_table
[256];
379 /* Standard translation tables, taken from POSIX 1003.1-2013.
380 Beware of imitations; there are lots of ASCII<->EBCDIC tables
381 floating around the net, perhaps valid for some applications but
384 static char const ascii_to_ebcdic
[] =
386 '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
387 '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
388 '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
389 '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
390 '\100', '\132', '\177', '\173', '\133', '\154', '\120', '\175',
391 '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
392 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
393 '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
394 '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
395 '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
396 '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
397 '\347', '\350', '\351', '\255', '\340', '\275', '\232', '\155',
398 '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
399 '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
400 '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
401 '\247', '\250', '\251', '\300', '\117', '\320', '\137', '\007',
402 '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
403 '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
404 '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
405 '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
406 '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
407 '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
408 '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
409 '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
410 '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
411 '\216', '\217', '\220', '\152', '\233', '\234', '\235', '\236',
412 '\237', '\240', '\252', '\253', '\254', '\112', '\256', '\257',
413 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
414 '\270', '\271', '\272', '\273', '\274', '\241', '\276', '\277',
415 '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
416 '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
417 '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
420 static char const ascii_to_ibm
[] =
422 '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
423 '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
424 '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
425 '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
426 '\100', '\132', '\177', '\173', '\133', '\154', '\120', '\175',
427 '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
428 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
429 '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
430 '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
431 '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
432 '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
433 '\347', '\350', '\351', '\255', '\340', '\275', '\137', '\155',
434 '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
435 '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
436 '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
437 '\247', '\250', '\251', '\300', '\117', '\320', '\241', '\007',
438 '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
439 '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
440 '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
441 '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
442 '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
443 '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
444 '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
445 '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
446 '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
447 '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',
448 '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',
449 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
450 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
451 '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
452 '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
453 '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
456 static char const ebcdic_to_ascii
[] =
458 '\000', '\001', '\002', '\003', '\234', '\011', '\206', '\177',
459 '\227', '\215', '\216', '\013', '\014', '\015', '\016', '\017',
460 '\020', '\021', '\022', '\023', '\235', '\205', '\010', '\207',
461 '\030', '\031', '\222', '\217', '\034', '\035', '\036', '\037',
462 '\200', '\201', '\202', '\203', '\204', '\012', '\027', '\033',
463 '\210', '\211', '\212', '\213', '\214', '\005', '\006', '\007',
464 '\220', '\221', '\026', '\223', '\224', '\225', '\226', '\004',
465 '\230', '\231', '\232', '\233', '\024', '\025', '\236', '\032',
466 '\040', '\240', '\241', '\242', '\243', '\244', '\245', '\246',
467 '\247', '\250', '\325', '\056', '\074', '\050', '\053', '\174',
468 '\046', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
469 '\260', '\261', '\041', '\044', '\052', '\051', '\073', '\176',
470 '\055', '\057', '\262', '\263', '\264', '\265', '\266', '\267',
471 '\270', '\271', '\313', '\054', '\045', '\137', '\076', '\077',
472 '\272', '\273', '\274', '\275', '\276', '\277', '\300', '\301',
473 '\302', '\140', '\072', '\043', '\100', '\047', '\075', '\042',
474 '\303', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
475 '\150', '\151', '\304', '\305', '\306', '\307', '\310', '\311',
476 '\312', '\152', '\153', '\154', '\155', '\156', '\157', '\160',
477 '\161', '\162', '\136', '\314', '\315', '\316', '\317', '\320',
478 '\321', '\345', '\163', '\164', '\165', '\166', '\167', '\170',
479 '\171', '\172', '\322', '\323', '\324', '\133', '\326', '\327',
480 '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
481 '\340', '\341', '\342', '\343', '\344', '\135', '\346', '\347',
482 '\173', '\101', '\102', '\103', '\104', '\105', '\106', '\107',
483 '\110', '\111', '\350', '\351', '\352', '\353', '\354', '\355',
484 '\175', '\112', '\113', '\114', '\115', '\116', '\117', '\120',
485 '\121', '\122', '\356', '\357', '\360', '\361', '\362', '\363',
486 '\134', '\237', '\123', '\124', '\125', '\126', '\127', '\130',
487 '\131', '\132', '\364', '\365', '\366', '\367', '\370', '\371',
488 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
489 '\070', '\071', '\372', '\373', '\374', '\375', '\376', '\377'
492 /* True if we need to close the standard output *stream*. */
493 static bool close_stdout_required
= true;
495 /* The only reason to close the standard output *stream* is if
496 parse_long_options fails (as it does for --help or --version).
497 In any other case, dd uses only the STDOUT_FILENO file descriptor,
498 and the "cleanup" function calls "close (STDOUT_FILENO)".
499 Closing the file descriptor and then letting the usual atexit-run
500 close_stdout function call "fclose (stdout)" would result in a
501 harmless failure of the close syscall (with errno EBADF).
502 This function serves solely to avoid the unnecessary close_stdout
503 call, once parse_long_options has succeeded.
504 Meanwhile, we guarantee that the standard error stream is flushed,
505 by inlining the last half of close_stdout as needed. */
507 maybe_close_stdout (void)
509 if (close_stdout_required
)
511 else if (close_stream (stderr
) != 0)
512 _exit (EXIT_FAILURE
);
515 /* Like the 'error' function but handle any pending newline,
518 ATTRIBUTE_FORMAT ((__printf__
, 2, 3))
520 diagnose (int errnum
, char const *fmt
, ...)
522 if (0 < progress_len
)
524 fputc ('\n', stderr
);
530 verror (0, errnum
, fmt
, ap
);
537 if (status
!= EXIT_SUCCESS
)
542 Usage: %s [OPERAND]...\n\
545 program_name
, program_name
);
547 Copy a file, converting and formatting according to the operands.\n\
549 bs=BYTES read and write up to BYTES bytes at a time (default: 512);\n\
550 overrides ibs and obs\n\
551 cbs=BYTES convert BYTES bytes at a time\n\
552 conv=CONVS convert the file as per the comma separated symbol list\n\
553 count=N copy only N input blocks\n\
554 ibs=BYTES read up to BYTES bytes at a time (default: 512)\n\
557 if=FILE read from FILE instead of stdin\n\
558 iflag=FLAGS read as per the comma separated symbol list\n\
559 obs=BYTES write BYTES bytes at a time (default: 512)\n\
560 of=FILE write to FILE instead of stdout\n\
561 oflag=FLAGS write as per the comma separated symbol list\n\
562 seek=N (or oseek=N) skip N obs-sized output blocks\n\
563 skip=N (or iseek=N) skip N ibs-sized input blocks\n\
564 status=LEVEL The LEVEL of information to print to stderr;\n\
565 'none' suppresses everything but error messages,\n\
566 'noxfer' suppresses the final transfer statistics,\n\
567 'progress' shows periodic transfer statistics\n\
571 N and BYTES may be followed by the following multiplicative suffixes:\n\
572 c=1, w=2, b=512, kB=1000, K=1024, MB=1000*1000, M=1024*1024, xM=M,\n\
573 GB=1000*1000*1000, G=1024*1024*1024, and so on for T, P, E, Z, Y, R, Q.\n\
574 Binary prefixes can be used, too: KiB=K, MiB=M, and so on.\n\
575 If N ends in 'B', it counts bytes not blocks.\n\
577 Each CONV symbol may be:\n\
581 ascii from EBCDIC to ASCII\n\
582 ebcdic from ASCII to EBCDIC\n\
583 ibm from ASCII to alternate EBCDIC\n\
584 block pad newline-terminated records with spaces to cbs-size\n\
585 unblock replace trailing spaces in cbs-size records with newline\n\
586 lcase change upper case to lower case\n\
587 ucase change lower case to upper case\n\
588 sparse try to seek rather than write all-NUL output blocks\n\
589 swab swap every pair of input bytes\n\
590 sync pad every input block with NULs to ibs-size; when used\n\
591 with block or unblock, pad with spaces rather than NULs\n\
594 excl fail if the output file already exists\n\
595 nocreat do not create the output file\n\
596 notrunc do not truncate the output file\n\
597 noerror continue after read errors\n\
598 fdatasync physically write output file data before finishing\n\
599 fsync likewise, but also write metadata\n\
603 Each FLAG symbol may be:\n\
605 append append mode (makes sense only for output; conv=notrunc suggested)\n\
608 fputs (_(" cio use concurrent I/O for data\n"), stdout
);
610 fputs (_(" direct use direct I/O for data\n"), stdout
);
612 fputs (_(" directory fail unless a directory\n"), stdout
);
614 fputs (_(" dsync use synchronized I/O for data\n"), stdout
);
616 fputs (_(" sync likewise, but also for metadata\n"), stdout
);
617 fputs (_(" fullblock accumulate full blocks of input (iflag only)\n"),
620 fputs (_(" nonblock use non-blocking I/O\n"), stdout
);
622 fputs (_(" noatime do not update access time\n"), stdout
);
623 #if HAVE_POSIX_FADVISE
625 fputs (_(" nocache Request to drop cache. See also oflag=sync\n"),
629 fputs (_(" noctty do not assign controlling terminal from file\n"),
631 if (HAVE_WORKING_O_NOFOLLOW
)
632 fputs (_(" nofollow do not follow symlinks\n"), stdout
);
634 fputs (_(" nolinks fail if multiply-linked\n"), stdout
);
636 fputs (_(" binary use binary I/O for data\n"), stdout
);
638 fputs (_(" text use text I/O for data\n"), stdout
);
643 Sending a %s signal to a running 'dd' process makes it\n\
644 print I/O statistics to standard error and then resume copying.\n\
648 "), SIGINFO
== SIGUSR1
? "USR1" : "INFO");
651 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
652 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
653 emit_ancillary_info (PROGRAM_NAME
);
658 /* Common options to use when displaying sizes and rates. */
660 enum { human_opts
= (human_autoscale
| human_round_to_nearest
661 | human_space_before_unit
| human_SI
| human_B
) };
663 /* Ensure input buffer IBUF is allocated. */
671 bool extra_byte_for_swab
= !!(conversions_mask
& C_SWAB
);
672 ibuf
= alignalloc (page_size
, input_blocksize
+ extra_byte_for_swab
);
675 char hbuf
[LONGEST_HUMAN_READABLE
+ 1];
676 error (EXIT_FAILURE
, 0,
677 _("memory exhausted by input buffer of size %td bytes (%s)"),
679 human_readable (input_blocksize
, hbuf
,
680 human_opts
| human_base_1024
, 1, 1));
684 /* Ensure output buffer OBUF is allocated/initialized. */
692 if (conversions_mask
& C_TWOBUFS
)
694 obuf
= alignalloc (page_size
, output_blocksize
);
697 char hbuf
[LONGEST_HUMAN_READABLE
+ 1];
698 error (EXIT_FAILURE
, 0,
699 _("memory exhausted by output buffer of size %td"
702 human_readable (output_blocksize
, hbuf
,
703 human_opts
| human_base_1024
, 1, 1));
714 translate_charset (char const *new_trans
)
716 for (int i
= 0; i
< 256; i
++)
717 trans_table
[i
] = new_trans
[trans_table
[i
]];
718 translation_needed
= true;
721 /* Return true if I has more than one bit set. I must be nonnegative. */
724 multiple_bits_set (int i
)
726 return MULTIPLE_BITS_SET (i
);
730 abbreviation_lacks_prefix (char const *message
)
732 return message
[strlen (message
) - 2] == ' ';
735 /* Print transfer statistics. */
738 print_xfer_stats (xtime_t progress_time
)
740 xtime_t now
= progress_time
? progress_time
: gethrxtime ();
741 static char const slash_s
[] = "/s";
742 char hbuf
[3][LONGEST_HUMAN_READABLE
+ sizeof slash_s
];
744 char const *bytes_per_second
;
745 char const *si
= human_readable (w_bytes
, hbuf
[0], human_opts
, 1, 1);
746 char const *iec
= human_readable (w_bytes
, hbuf
[1],
747 human_opts
| human_base_1024
, 1, 1);
749 /* Use integer arithmetic to compute the transfer rate,
750 since that makes it easy to use SI abbreviations. */
751 char *bpsbuf
= hbuf
[2];
752 int bpsbufsize
= sizeof hbuf
[2];
753 if (start_time
< now
)
755 double XTIME_PRECISIONe0
= XTIME_PRECISION
;
756 xtime_t delta_xtime
= now
- start_time
;
757 delta_s
= delta_xtime
/ XTIME_PRECISIONe0
;
758 bytes_per_second
= human_readable (w_bytes
, bpsbuf
, human_opts
,
759 XTIME_PRECISION
, delta_xtime
);
760 strcat (bytes_per_second
- bpsbuf
+ bpsbuf
, slash_s
);
765 snprintf (bpsbuf
, bpsbufsize
, "%s B/s", _("Infinity"));
766 bytes_per_second
= bpsbuf
;
770 fputc ('\r', stderr
);
772 /* Use full seconds when printing progress, since the progress
773 report is output once per second and there is little point
774 displaying any subsecond jitter. Use default precision with %g
775 otherwise, as this provides more-useful output then. With long
776 transfers %g can generate a number with an exponent; that is OK. */
777 char delta_s_buf
[24];
778 snprintf (delta_s_buf
, sizeof delta_s_buf
,
779 progress_time
? "%.0f s" : "%g s", delta_s
);
782 = (abbreviation_lacks_prefix (si
)
784 ngettext ("%"PRIdMAX
" byte copied, %s, %s",
785 "%"PRIdMAX
" bytes copied, %s, %s",
786 select_plural (w_bytes
)),
787 w_bytes
, delta_s_buf
, bytes_per_second
)
788 : abbreviation_lacks_prefix (iec
)
790 _("%"PRIdMAX
" bytes (%s) copied, %s, %s"),
791 w_bytes
, si
, delta_s_buf
, bytes_per_second
)
793 _("%"PRIdMAX
" bytes (%s, %s) copied, %s, %s"),
794 w_bytes
, si
, iec
, delta_s_buf
, bytes_per_second
));
798 /* Erase any trailing junk on the output line by outputting
799 spaces. In theory this could glitch the display because the
800 formatted translation of a line describing a larger file
801 could consume fewer screen columns than the strlen difference
802 from the previously formatted translation. In practice this
803 does not seem to be a problem. */
804 if (0 <= stats_len
&& stats_len
< progress_len
)
805 fprintf (stderr
, "%*s", progress_len
- stats_len
, "");
806 progress_len
= stats_len
;
809 fputc ('\n', stderr
);
811 reported_w_bytes
= w_bytes
;
817 if (status_level
== STATUS_NONE
)
820 if (0 < progress_len
)
822 fputc ('\n', stderr
);
827 _("%"PRIdMAX
"+%"PRIdMAX
" records in\n"
828 "%"PRIdMAX
"+%"PRIdMAX
" records out\n"),
829 r_full
, r_partial
, w_full
, w_partial
);
833 ngettext ("%"PRIdMAX
" truncated record\n",
834 "%"PRIdMAX
" truncated records\n",
835 select_plural (r_truncate
)),
838 if (status_level
== STATUS_NOXFER
)
841 print_xfer_stats (0);
844 /* An ordinary signal was received; arrange for the program to exit. */
847 interrupt_handler (int sig
)
850 signal (sig
, SIG_DFL
);
851 interrupt_signal
= sig
;
854 /* An info signal was received; arrange for the program to print status. */
857 siginfo_handler (int sig
)
860 signal (sig
, siginfo_handler
);
864 /* Install the signal handlers. */
867 install_signal_handlers (void)
869 bool catch_siginfo
= ! (SIGINFO
== SIGUSR1
&& getenv ("POSIXLY_CORRECT"));
873 struct sigaction act
;
874 sigemptyset (&caught_signals
);
876 sigaddset (&caught_signals
, SIGINFO
);
877 sigaction (SIGINT
, nullptr, &act
);
878 if (act
.sa_handler
!= SIG_IGN
)
879 sigaddset (&caught_signals
, SIGINT
);
880 act
.sa_mask
= caught_signals
;
882 if (sigismember (&caught_signals
, SIGINFO
))
884 act
.sa_handler
= siginfo_handler
;
885 /* Note we don't use SA_RESTART here and instead
886 handle EINTR explicitly in iftruncate etc.
887 to avoid blocking on noncommitted read/write calls. */
889 sigaction (SIGINFO
, &act
, nullptr);
892 if (sigismember (&caught_signals
, SIGINT
))
894 act
.sa_handler
= interrupt_handler
;
895 act
.sa_flags
= SA_NODEFER
| SA_RESETHAND
;
896 sigaction (SIGINT
, &act
, nullptr);
903 signal (SIGINFO
, siginfo_handler
);
904 siginterrupt (SIGINFO
, 1);
906 if (signal (SIGINT
, SIG_IGN
) != SIG_IGN
)
908 signal (SIGINT
, interrupt_handler
);
909 siginterrupt (SIGINT
, 1);
914 /* Close FD. Return 0 if successful, -1 (setting errno) otherwise.
915 If close fails with errno == EINTR, POSIX says the file descriptor
916 is in an unspecified state, so keep trying to close FD but do not
917 consider EBADF to be an error. Do not process signals. This all
918 differs somewhat from functions like ifdatasync and ifsync. */
926 while (close (fd
) != 0 && errno
!= EBADF
);
931 static int synchronize_output (void);
936 if (!interrupt_signal
)
938 int sync_status
= synchronize_output ();
943 if (iclose (STDIN_FILENO
) != 0)
944 error (EXIT_FAILURE
, errno
, _("closing input file %s"),
945 quoteaf (input_file
));
947 /* Don't remove this call to close, even though close_stdout
948 closes standard output. This close is necessary when cleanup
949 is called as a consequence of signal handling. */
950 if (iclose (STDOUT_FILENO
) != 0)
951 error (EXIT_FAILURE
, errno
,
952 _("closing output file %s"), quoteaf (output_file
));
955 /* Process any pending signals. If signals are caught, this function
956 should be called periodically. Ideally there should never be an
957 unbounded amount of time when signals are not being processed. */
960 process_signals (void)
962 while (interrupt_signal
|| info_signal_count
)
968 sigprocmask (SIG_BLOCK
, &caught_signals
, &oldset
);
970 /* Reload interrupt_signal and info_signal_count, in case a new
971 signal was handled before sigprocmask took effect. */
972 interrupt
= interrupt_signal
;
973 infos
= info_signal_count
;
976 info_signal_count
= infos
- 1;
978 sigprocmask (SIG_SETMASK
, &oldset
, nullptr);
991 /* Process signals first, so that cleanup is called at most once. */
1004 /* Return LEN rounded down to a multiple of IO_BUFSIZE
1005 (to minimize calls to the expensive posix_fadvise (,POSIX_FADV_DONTNEED),
1006 while storing the remainder internally per FD.
1007 Pass LEN == 0 to get the current remainder. */
1010 cache_round (int fd
, off_t len
)
1012 static off_t i_pending
, o_pending
;
1013 off_t
*pending
= (fd
== STDIN_FILENO
? &i_pending
: &o_pending
);
1018 if (INT_ADD_WRAPV (*pending
, len
, &c_pending
))
1019 c_pending
= INTMAX_MAX
;
1020 *pending
= c_pending
% IO_BUFSIZE
;
1021 if (c_pending
> *pending
)
1022 len
= c_pending
- *pending
;
1032 /* Discard the cache from the current offset of either
1033 STDIN_FILENO or STDOUT_FILENO.
1034 Return true on success. */
1037 invalidate_cache (int fd
, off_t len
)
1041 bool nocache_eof
= (fd
== STDIN_FILENO
? i_nocache_eof
: o_nocache_eof
);
1043 /* Minimize syscalls. */
1044 off_t clen
= cache_round (fd
, len
);
1046 return true; /* Don't advise this time. */
1047 else if (! len
&& ! clen
&& ! nocache_eof
)
1049 off_t pending
= len
? cache_round (fd
, 0) : 0;
1051 if (fd
== STDIN_FILENO
)
1054 offset
= input_offset
;
1063 static off_t output_offset
= -2;
1065 if (output_offset
!= -1)
1067 if (output_offset
< 0)
1068 output_offset
= lseek (fd
, 0, SEEK_CUR
);
1070 output_offset
+= clen
+ pending
;
1073 offset
= output_offset
;
1078 if (! len
&& clen
&& nocache_eof
)
1084 /* Note we're being careful here to only invalidate what
1085 we've read, so as not to dump any read ahead cache.
1086 Note also the kernel is conservative and only invalidates
1087 full pages in the specified range. */
1088 #if HAVE_POSIX_FADVISE
1089 offset
= offset
- clen
- pending
;
1090 /* ensure full page specified when invalidating to eof. */
1092 offset
-= offset
% page_size
;
1093 adv_ret
= posix_fadvise (fd
, offset
, clen
, POSIX_FADV_DONTNEED
);
1099 return adv_ret
!= -1 ? true : false;
1102 /* Read from FD into the buffer BUF of size SIZE, processing any
1103 signals that arrive before bytes are read. Return the number of
1104 bytes read if successful, -1 (setting errno) on failure. */
1107 iread (int fd
, char *buf
, idx_t size
)
1110 static ssize_t prev_nread
;
1115 nread
= read (fd
, buf
, size
);
1116 /* Ignore final read error with iflag=direct as that
1117 returns EINVAL due to the non aligned file offset. */
1118 if (nread
== -1 && errno
== EINVAL
1119 && 0 < prev_nread
&& prev_nread
< size
1120 && (input_flags
& O_DIRECT
))
1126 while (nread
< 0 && errno
== EINTR
);
1128 /* Short read may be due to received signal. */
1129 if (0 < nread
&& nread
< size
)
1132 if (0 < nread
&& warn_partial_read
)
1134 if (0 < prev_nread
&& prev_nread
< size
)
1136 idx_t prev
= prev_nread
;
1137 if (status_level
!= STATUS_NONE
)
1138 diagnose (0, ngettext (("warning: partial read (%td byte); "
1139 "suggest iflag=fullblock"),
1140 ("warning: partial read (%td bytes); "
1141 "suggest iflag=fullblock"),
1142 select_plural (prev
)),
1144 warn_partial_read
= false;
1152 /* Wrapper around iread function to accumulate full blocks. */
1154 iread_fullblock (int fd
, char *buf
, idx_t size
)
1160 ssize_t ncurr
= iread (fd
, buf
, size
);
1173 /* Write to FD the buffer BUF of size SIZE, processing any signals
1174 that arrive. Return the number of bytes written, setting errno if
1175 this is less than SIZE. Keep trying if there are partial
1179 iwrite (int fd
, char const *buf
, idx_t size
)
1181 idx_t total_written
= 0;
1183 if ((output_flags
& O_DIRECT
) && size
< output_blocksize
)
1185 int old_flags
= fcntl (STDOUT_FILENO
, F_GETFL
);
1186 if (fcntl (STDOUT_FILENO
, F_SETFL
, old_flags
& ~O_DIRECT
) != 0
1187 && status_level
!= STATUS_NONE
)
1188 diagnose (errno
, _("failed to turn off O_DIRECT: %s"),
1189 quotef (output_file
));
1191 /* Since we have just turned off O_DIRECT for the final write,
1192 we try to preserve some of its semantics. */
1194 /* Call invalidate_cache to setup the appropriate offsets
1195 for subsequent calls. */
1196 o_nocache_eof
= true;
1197 invalidate_cache (STDOUT_FILENO
, 0);
1199 /* Attempt to ensure that that final block is committed
1200 to stable storage as quickly as possible. */
1201 conversions_mask
|= C_FSYNC
;
1203 /* After the subsequent fsync we'll call invalidate_cache
1204 to attempt to clear all data from the page cache. */
1207 while (total_written
< size
)
1209 ssize_t nwritten
= 0;
1212 /* Perform a seek for a NUL block if sparse output is enabled. */
1213 final_op_was_seek
= false;
1214 if ((conversions_mask
& C_SPARSE
) && is_nul (buf
, size
))
1216 if (lseek (fd
, size
, SEEK_CUR
) < 0)
1218 conversions_mask
&= ~C_SPARSE
;
1219 /* Don't warn about the advisory sparse request. */
1223 final_op_was_seek
= true;
1229 nwritten
= write (fd
, buf
+ total_written
, size
- total_written
);
1236 else if (nwritten
== 0)
1238 /* Some buggy drivers return 0 when one tries to write beyond
1239 a device's end. (Example: Linux kernel 1.2.13 on /dev/fd0.)
1240 Set errno to ENOSPC so they get a sensible diagnostic. */
1245 total_written
+= nwritten
;
1248 if (o_nocache
&& total_written
)
1249 invalidate_cache (fd
, total_written
);
1251 return total_written
;
1254 /* Write, then empty, the output buffer 'obuf'. */
1259 idx_t nwritten
= iwrite (STDOUT_FILENO
, obuf
, output_blocksize
);
1260 w_bytes
+= nwritten
;
1261 if (nwritten
!= output_blocksize
)
1263 diagnose (errno
, _("writing to %s"), quoteaf (output_file
));
1266 quit (EXIT_FAILURE
);
1273 /* Restart on EINTR from fdatasync. */
1283 ret
= fdatasync (fd
);
1285 while (ret
< 0 && errno
== EINTR
);
1290 /* Restart on EINTR from fd_reopen. */
1293 ifd_reopen (int desired_fd
, char const *file
, int flag
, mode_t mode
)
1300 ret
= fd_reopen (desired_fd
, file
, flag
, mode
);
1302 while (ret
< 0 && errno
== EINTR
);
1307 /* Restart on EINTR from fstat. */
1310 ifstat (int fd
, struct stat
*st
)
1317 ret
= fstat (fd
, st
);
1319 while (ret
< 0 && errno
== EINTR
);
1324 /* Restart on EINTR from fsync. */
1336 while (ret
< 0 && errno
== EINTR
);
1341 /* Restart on EINTR from ftruncate. */
1344 iftruncate (int fd
, off_t length
)
1351 ret
= ftruncate (fd
, length
);
1353 while (ret
< 0 && errno
== EINTR
);
1358 /* Return true if STR is of the form "PATTERN" or "PATTERNDELIM...". */
1362 operand_matches (char const *str
, char const *pattern
, char delim
)
1365 if (*str
++ != *pattern
++)
1367 return !*str
|| *str
== delim
;
1370 /* Interpret one "conv=..." or similar operand STR according to the
1371 symbols in TABLE, returning the flags specified. If the operand
1372 cannot be parsed, use ERROR_MSGID to generate a diagnostic. */
1375 parse_symbols (char const *str
, struct symbol_value
const *table
,
1376 bool exclusive
, char const *error_msgid
)
1382 char const *strcomma
= strchr (str
, ',');
1383 struct symbol_value
const *entry
;
1386 ! (operand_matches (str
, entry
->symbol
, ',') && entry
->value
);
1389 if (! entry
->symbol
[0])
1391 idx_t slen
= strcomma
? strcomma
- str
: strlen (str
);
1392 diagnose (0, "%s: %s", _(error_msgid
),
1393 quotearg_n_style_mem (0, locale_quoting_style
,
1395 usage (EXIT_FAILURE
);
1400 value
= entry
->value
;
1402 value
|= entry
->value
;
1411 /* Return the value of STR, interpreted as a non-negative decimal integer,
1412 optionally multiplied by various values.
1413 Set *INVALID to an appropriate error value and return INTMAX_MAX if
1414 it is an overflow, an indeterminate value if some other error occurred. */
1417 parse_integer (char const *str
, strtol_error
*invalid
)
1419 /* Call xstrtoumax, not xstrtoimax, since we don't want to
1420 allow strings like " -0". Initialize N to an interminate value;
1421 calling code should not rely on this function returning 0
1422 when *INVALID represents a non-overflow error. */
1423 int indeterminate
= 0;
1424 uintmax_t n
= indeterminate
;
1426 static char const suffixes
[] = "bcEGkKMPQRTwYZ0";
1427 strtol_error e
= xstrtoumax (str
, &suffix
, 10, &n
, suffixes
);
1430 if ((e
& ~LONGINT_OVERFLOW
) == LONGINT_INVALID_SUFFIX_CHAR
1431 && *suffix
== 'B' && str
< suffix
&& suffix
[-1] != 'B')
1435 e
&= ~LONGINT_INVALID_SUFFIX_CHAR
;
1438 if ((e
& ~LONGINT_OVERFLOW
) == LONGINT_INVALID_SUFFIX_CHAR
1441 strtol_error f
= LONGINT_OK
;
1442 intmax_t o
= parse_integer (suffix
+ 1, &f
);
1443 if ((f
& ~LONGINT_OVERFLOW
) != LONGINT_OK
)
1446 result
= indeterminate
;
1448 else if (INT_MULTIPLY_WRAPV (n
, o
, &result
)
1449 || (result
!= 0 && ((e
| f
) & LONGINT_OVERFLOW
)))
1451 e
= LONGINT_OVERFLOW
;
1452 result
= INTMAX_MAX
;
1456 if (result
== 0 && STRPREFIX (str
, "0x"))
1457 diagnose (0, _("warning: %s is a zero multiplier; "
1458 "use %s if that is intended"),
1459 quote_n (0, "0x"), quote_n (1, "00x"));
1463 else if (n
<= INTMAX_MAX
)
1467 e
= LONGINT_OVERFLOW
;
1468 result
= INTMAX_MAX
;
1475 /* OPERAND is of the form "X=...". Return true if X is NAME. */
1479 operand_is (char const *operand
, char const *name
)
1481 return operand_matches (operand
, name
, '=');
1485 scanargs (int argc
, char *const *argv
)
1487 idx_t blocksize
= 0;
1488 intmax_t count
= INTMAX_MAX
;
1491 bool count_B
= false, skip_B
= false, seek_B
= false;
1493 for (int i
= optind
; i
< argc
; i
++)
1495 char const *name
= argv
[i
];
1496 char const *val
= strchr (name
, '=');
1500 diagnose (0, _("unrecognized operand %s"), quoteaf (name
));
1501 usage (EXIT_FAILURE
);
1505 if (operand_is (name
, "if"))
1507 else if (operand_is (name
, "of"))
1509 else if (operand_is (name
, "conv"))
1510 conversions_mask
|= parse_symbols (val
, conversions
, false,
1511 N_("invalid conversion"));
1512 else if (operand_is (name
, "iflag"))
1513 input_flags
|= parse_symbols (val
, flags
, false,
1514 N_("invalid input flag"));
1515 else if (operand_is (name
, "oflag"))
1516 output_flags
|= parse_symbols (val
, flags
, false,
1517 N_("invalid output flag"));
1518 else if (operand_is (name
, "status"))
1519 status_level
= parse_symbols (val
, statuses
, true,
1520 N_("invalid status level"));
1523 strtol_error invalid
= LONGINT_OK
;
1524 intmax_t n
= parse_integer (val
, &invalid
);
1525 bool has_B
= !!strchr (val
, 'B');
1527 intmax_t n_max
= INTMAX_MAX
;
1528 idx_t
*converted_idx
= nullptr;
1530 /* Maximum blocksize. Keep it smaller than IDX_MAX, so that
1531 it fits into blocksize vars even if 1 is added for conv=swab.
1532 Do not exceed SSIZE_MAX, for the benefit of system calls
1533 like "read". And do not exceed OFF_T_MAX, for the
1534 benefit of the large-offset seek code. */
1535 idx_t max_blocksize
= MIN (IDX_MAX
- 1, MIN (SSIZE_MAX
, OFF_T_MAX
));
1537 if (operand_is (name
, "ibs"))
1540 n_max
= max_blocksize
;
1541 converted_idx
= &input_blocksize
;
1543 else if (operand_is (name
, "obs"))
1546 n_max
= max_blocksize
;
1547 converted_idx
= &output_blocksize
;
1549 else if (operand_is (name
, "bs"))
1552 n_max
= max_blocksize
;
1553 converted_idx
= &blocksize
;
1555 else if (operand_is (name
, "cbs"))
1558 n_max
= MIN (SIZE_MAX
, IDX_MAX
);
1559 converted_idx
= &conversion_blocksize
;
1561 else if (operand_is (name
, "skip") || operand_is (name
, "iseek"))
1566 else if (operand_is (name
+ (*name
== 'o'), "seek"))
1571 else if (operand_is (name
, "count"))
1578 diagnose (0, _("unrecognized operand %s"), quoteaf (name
));
1579 usage (EXIT_FAILURE
);
1583 invalid
= LONGINT_INVALID
;
1585 invalid
= LONGINT_OVERFLOW
;
1587 if (invalid
!= LONGINT_OK
)
1588 error (EXIT_FAILURE
, invalid
== LONGINT_OVERFLOW
? EOVERFLOW
: 0,
1589 "%s: %s", _("invalid number"), quoteaf (val
));
1590 else if (converted_idx
)
1596 input_blocksize
= output_blocksize
= blocksize
;
1599 /* POSIX says dd aggregates partial reads into
1600 output_blocksize if bs= is not specified. */
1601 conversions_mask
|= C_TWOBUFS
;
1604 if (input_blocksize
== 0)
1605 input_blocksize
= DEFAULT_BLOCKSIZE
;
1606 if (output_blocksize
== 0)
1607 output_blocksize
= DEFAULT_BLOCKSIZE
;
1608 if (conversion_blocksize
== 0)
1609 conversions_mask
&= ~(C_BLOCK
| C_UNBLOCK
);
1611 if (input_flags
& (O_DSYNC
| O_SYNC
))
1612 input_flags
|= O_RSYNC
;
1614 if (output_flags
& O_FULLBLOCK
)
1616 diagnose (0, "%s: %s", _("invalid output flag"), quote ("fullblock"));
1617 usage (EXIT_FAILURE
);
1621 input_flags
|= O_SKIP_BYTES
;
1622 if (input_flags
& O_SKIP_BYTES
&& skip
!= 0)
1624 skip_records
= skip
/ input_blocksize
;
1625 skip_bytes
= skip
% input_blocksize
;
1628 skip_records
= skip
;
1631 input_flags
|= O_COUNT_BYTES
;
1632 if (input_flags
& O_COUNT_BYTES
&& count
!= INTMAX_MAX
)
1634 max_records
= count
/ input_blocksize
;
1635 max_bytes
= count
% input_blocksize
;
1637 else if (count
!= INTMAX_MAX
)
1638 max_records
= count
;
1641 output_flags
|= O_SEEK_BYTES
;
1642 if (output_flags
& O_SEEK_BYTES
&& seek
!= 0)
1644 seek_records
= seek
/ output_blocksize
;
1645 seek_bytes
= seek
% output_blocksize
;
1648 seek_records
= seek
;
1650 /* Warn about partial reads if bs=SIZE is given and iflag=fullblock
1651 is not, and if counting or skipping bytes or using direct I/O.
1652 This helps to avoid confusion with miscounts, and to avoid issues
1653 with direct I/O on GNU/Linux. */
1655 (! (conversions_mask
& C_TWOBUFS
) && ! (input_flags
& O_FULLBLOCK
)
1657 || (0 < max_records
&& max_records
< INTMAX_MAX
)
1658 || (input_flags
| output_flags
) & O_DIRECT
));
1660 iread_fnc
= ((input_flags
& O_FULLBLOCK
)
1663 input_flags
&= ~O_FULLBLOCK
;
1665 if (multiple_bits_set (conversions_mask
& (C_ASCII
| C_EBCDIC
| C_IBM
)))
1666 error (EXIT_FAILURE
, 0, _("cannot combine any two of {ascii,ebcdic,ibm}"));
1667 if (multiple_bits_set (conversions_mask
& (C_BLOCK
| C_UNBLOCK
)))
1668 error (EXIT_FAILURE
, 0, _("cannot combine block and unblock"));
1669 if (multiple_bits_set (conversions_mask
& (C_LCASE
| C_UCASE
)))
1670 error (EXIT_FAILURE
, 0, _("cannot combine lcase and ucase"));
1671 if (multiple_bits_set (conversions_mask
& (C_EXCL
| C_NOCREAT
)))
1672 error (EXIT_FAILURE
, 0, _("cannot combine excl and nocreat"));
1673 if (multiple_bits_set (input_flags
& (O_DIRECT
| O_NOCACHE
))
1674 || multiple_bits_set (output_flags
& (O_DIRECT
| O_NOCACHE
)))
1675 error (EXIT_FAILURE
, 0, _("cannot combine direct and nocache"));
1677 if (input_flags
& O_NOCACHE
)
1680 i_nocache_eof
= (max_records
== 0 && max_bytes
== 0);
1681 input_flags
&= ~O_NOCACHE
;
1683 if (output_flags
& O_NOCACHE
)
1686 o_nocache_eof
= (max_records
== 0 && max_bytes
== 0);
1687 output_flags
&= ~O_NOCACHE
;
1691 /* Fix up translation table. */
1694 apply_translations (void)
1698 if (conversions_mask
& C_ASCII
)
1699 translate_charset (ebcdic_to_ascii
);
1701 if (conversions_mask
& C_UCASE
)
1703 for (i
= 0; i
< 256; i
++)
1704 trans_table
[i
] = toupper (trans_table
[i
]);
1705 translation_needed
= true;
1707 else if (conversions_mask
& C_LCASE
)
1709 for (i
= 0; i
< 256; i
++)
1710 trans_table
[i
] = tolower (trans_table
[i
]);
1711 translation_needed
= true;
1714 if (conversions_mask
& C_EBCDIC
)
1716 translate_charset (ascii_to_ebcdic
);
1717 newline_character
= ascii_to_ebcdic
['\n'];
1718 space_character
= ascii_to_ebcdic
[' '];
1720 else if (conversions_mask
& C_IBM
)
1722 translate_charset (ascii_to_ibm
);
1723 newline_character
= ascii_to_ibm
['\n'];
1724 space_character
= ascii_to_ibm
[' '];
1728 /* Apply the character-set translations specified by the user
1729 to the NREAD bytes in BUF. */
1732 translate_buffer (char *buf
, idx_t nread
)
1736 for (i
= nread
, cp
= buf
; i
; i
--, cp
++)
1737 *cp
= trans_table
[to_uchar (*cp
)];
1740 /* Swap *NREAD bytes in BUF, which should have room for an extra byte
1741 after the end because the swapping is not in-place. If *SAVED_BYTE
1742 is nonnegative, also swap that initial byte from the previous call.
1743 Save the last byte into into *SAVED_BYTE if needed to make the
1744 resulting *NREAD even, and set *SAVED_BYTE to -1 otherwise.
1745 Return the buffer's adjusted start, either BUF or BUF + 1. */
1748 swab_buffer (char *buf
, idx_t
*nread
, int *saved_byte
)
1753 /* Update *SAVED_BYTE, and set PREV_SAVED to its old value. */
1754 int prev_saved
= *saved_byte
;
1755 if ((prev_saved
< 0) == (*nread
& 1))
1757 unsigned char c
= buf
[--*nread
];
1763 /* Do the byte-swapping by moving every other byte two
1764 positions toward the end, working from the end of the buffer
1765 toward the beginning. This way we move only half the data. */
1766 for (idx_t i
= *nread
; 1 < i
; i
-= 2)
1767 buf
[i
] = buf
[i
- 2];
1772 buf
[1] = prev_saved
;
1777 /* Add OFFSET to the input offset, setting the overflow flag if
1781 advance_input_offset (intmax_t offset
)
1783 if (0 <= input_offset
&& INT_ADD_WRAPV (input_offset
, offset
, &input_offset
))
1787 /* Throw away RECORDS blocks of BLOCKSIZE bytes plus BYTES bytes on
1788 file descriptor FDESC, which is open with read permission for FILE.
1789 Store up to BLOCKSIZE bytes of the data at a time in IBUF or OBUF, if
1790 necessary. RECORDS or BYTES must be nonzero. If FDESC is
1791 STDIN_FILENO, advance the input offset. Return the number of
1792 records remaining, i.e., that were not skipped because EOF was
1793 reached. If FDESC is STDOUT_FILENO, on return, BYTES is the
1794 remaining bytes in addition to the remaining records. */
1797 skip (int fdesc
, char const *file
, intmax_t records
, idx_t blocksize
,
1800 /* Try lseek and if an error indicates it was an inappropriate operation --
1801 or if the file offset is not representable as an off_t --
1802 fall back on using read. */
1806 if (! INT_MULTIPLY_WRAPV (records
, blocksize
, &offset
)
1807 && ! INT_ADD_WRAPV (offset
, *bytes
, &offset
)
1808 && 0 <= lseek (fdesc
, offset
, SEEK_CUR
))
1810 if (fdesc
== STDIN_FILENO
)
1813 if (ifstat (STDIN_FILENO
, &st
) != 0)
1814 error (EXIT_FAILURE
, errno
, _("cannot fstat %s"), quoteaf (file
));
1815 if (usable_st_size (&st
) && 0 <= input_offset
1816 && st
.st_size
- input_offset
< offset
)
1818 /* When skipping past EOF, return the number of _full_ blocks
1819 * that are not skipped, and set offset to EOF, so the caller
1820 * can determine the requested skip was not satisfied. */
1821 records
= ( offset
- st
.st_size
) / blocksize
;
1822 offset
= st
.st_size
- input_offset
;
1826 advance_input_offset (offset
);
1837 int lseek_errno
= errno
;
1839 /* The seek request may have failed above if it was too big
1840 (> device size, > max file size, etc.)
1841 Or it may not have been done at all (> OFF_T_MAX).
1842 Therefore try to seek to the end of the file,
1843 to avoid redundant reading. */
1844 if (lseek (fdesc
, 0, SEEK_END
) >= 0)
1846 /* File is seekable, and we're at the end of it, and
1847 size <= OFF_T_MAX. So there's no point using read to advance. */
1851 /* The original seek was not attempted as offset > OFF_T_MAX.
1852 We should error for write as can't get to the desired
1853 location, even if OFF_T_MAX < max file size.
1854 For read we're not going to read any data anyway,
1855 so we should error for consistency.
1856 It would be nice to not error for /dev/{zero,null}
1857 for any offset, but that's not a significant issue. */
1858 lseek_errno
= EOVERFLOW
;
1861 diagnose (lseek_errno
,
1862 gettext (fdesc
== STDIN_FILENO
1863 ? N_("%s: cannot skip")
1864 : N_("%s: cannot seek")),
1866 /* If the file has a specific size and we've asked
1867 to skip/seek beyond the max allowable, then quit. */
1868 quit (EXIT_FAILURE
);
1870 /* else file_size && offset > OFF_T_MAX or file ! seekable */
1873 if (fdesc
== STDIN_FILENO
)
1886 ssize_t nread
= iread_fnc (fdesc
, buf
, records
? blocksize
: *bytes
);
1889 if (fdesc
== STDIN_FILENO
)
1891 diagnose (errno
, _("error reading %s"), quoteaf (file
));
1892 if (conversions_mask
& C_NOERROR
)
1896 diagnose (lseek_errno
, _("%s: cannot seek"), quotef (file
));
1897 quit (EXIT_FAILURE
);
1899 else if (nread
== 0)
1901 else if (fdesc
== STDIN_FILENO
)
1902 advance_input_offset (nread
);
1909 while (records
|| *bytes
);
1915 /* Advance the input by NBYTES if possible, after a read error.
1916 The input file offset may or may not have advanced after the failed
1917 read; adjust it to point just after the bad record regardless.
1918 Return true if successful, or if the input is already known to not
1922 advance_input_after_read_error (idx_t nbytes
)
1924 if (! input_seekable
)
1926 if (input_seek_errno
== ESPIPE
)
1928 errno
= input_seek_errno
;
1933 advance_input_offset (nbytes
);
1934 if (input_offset
< 0)
1936 diagnose (0, _("offset overflow while reading file %s"),
1937 quoteaf (input_file
));
1940 offset
= lseek (STDIN_FILENO
, 0, SEEK_CUR
);
1944 if (offset
== input_offset
)
1946 diff
= input_offset
- offset
;
1947 if (! (0 <= diff
&& diff
<= nbytes
) && status_level
!= STATUS_NONE
)
1948 diagnose (0, _("warning: invalid file offset after failed read"));
1949 if (0 <= lseek (STDIN_FILENO
, diff
, SEEK_CUR
))
1952 diagnose (0, _("cannot work around kernel bug after all"));
1956 diagnose (errno
, _("%s: cannot seek"), quotef (input_file
));
1960 /* Copy NREAD bytes of BUF, with no conversions. */
1963 copy_simple (char const *buf
, idx_t nread
)
1965 char const *start
= buf
; /* First uncopied char in BUF. */
1969 idx_t nfree
= MIN (nread
, output_blocksize
- oc
);
1971 memcpy (obuf
+ oc
, start
, nfree
);
1973 nread
-= nfree
; /* Update the number of bytes left to copy. */
1976 if (oc
>= output_blocksize
)
1982 /* Copy NREAD bytes of BUF, doing conv=block
1983 (pad newline-terminated records to 'conversion_blocksize',
1984 replacing the newline with trailing spaces). */
1987 copy_with_block (char const *buf
, idx_t nread
)
1989 for (idx_t i
= nread
; i
; i
--, buf
++)
1991 if (*buf
== newline_character
)
1993 if (col
< conversion_blocksize
)
1996 for (j
= col
; j
< conversion_blocksize
; j
++)
1997 output_char (space_character
);
2003 if (col
== conversion_blocksize
)
2005 else if (col
< conversion_blocksize
)
2012 /* Copy NREAD bytes of BUF, doing conv=unblock
2013 (replace trailing spaces in 'conversion_blocksize'-sized records
2017 copy_with_unblock (char const *buf
, idx_t nread
)
2019 static idx_t pending_spaces
= 0;
2021 for (idx_t i
= 0; i
< nread
; i
++)
2025 if (col
++ >= conversion_blocksize
)
2027 col
= pending_spaces
= 0; /* Wipe out any pending spaces. */
2028 i
--; /* Push the char back; get it later. */
2029 output_char (newline_character
);
2031 else if (c
== space_character
)
2035 /* 'c' is the character after a run of spaces that were not
2036 at the end of the conversion buffer. Output them. */
2037 while (pending_spaces
)
2039 output_char (space_character
);
2047 /* Set the file descriptor flags for FD that correspond to the nonzero bits
2048 in ADD_FLAGS. The file's name is NAME. */
2051 set_fd_flags (int fd
, int add_flags
, char const *name
)
2053 /* Ignore file creation flags that are no-ops on file descriptors. */
2054 add_flags
&= ~ (O_NOCTTY
| O_NOFOLLOW
);
2058 int old_flags
= fcntl (fd
, F_GETFL
);
2059 int new_flags
= old_flags
| add_flags
;
2063 else if (old_flags
!= new_flags
)
2065 if (new_flags
& (O_DIRECTORY
| O_NOLINKS
))
2067 /* NEW_FLAGS contains at least one file creation flag that
2068 requires some checking of the open file descriptor. */
2070 if (ifstat (fd
, &st
) != 0)
2072 else if ((new_flags
& O_DIRECTORY
) && ! S_ISDIR (st
.st_mode
))
2077 else if ((new_flags
& O_NOLINKS
) && 1 < st
.st_nlink
)
2082 new_flags
&= ~ (O_DIRECTORY
| O_NOLINKS
);
2085 if (ok
&& old_flags
!= new_flags
2086 && fcntl (fd
, F_SETFL
, new_flags
) == -1)
2091 error (EXIT_FAILURE
, errno
, _("setting flags for %s"), quoteaf (name
));
2095 /* The main loop. */
2100 char *bufstart
; /* Input buffer. */
2101 ssize_t nread
; /* Bytes read in the current block. */
2103 /* If nonzero, then the previously read block was partial and
2104 PARTREAD was its size. */
2107 int exit_status
= EXIT_SUCCESS
;
2110 if (skip_records
!= 0 || skip_bytes
!= 0)
2113 bool us_bytes_overflow
=
2114 (INT_MULTIPLY_WRAPV (skip_records
, input_blocksize
, &us_bytes
)
2115 || INT_ADD_WRAPV (skip_bytes
, us_bytes
, &us_bytes
));
2116 off_t input_offset0
= input_offset
;
2117 intmax_t us_blocks
= skip (STDIN_FILENO
, input_file
,
2118 skip_records
, input_blocksize
, &skip_bytes
);
2120 /* POSIX doesn't say what to do when dd detects it has been
2121 asked to skip past EOF, so I assume it's non-fatal.
2122 There are 3 reasons why there might be unskipped blocks/bytes:
2123 1. file is too small
2124 2. pipe has not enough data
2127 || (0 <= input_offset
2128 && (us_bytes_overflow
2129 || us_bytes
!= input_offset
- input_offset0
)))
2130 && status_level
!= STATUS_NONE
)
2132 diagnose (0, _("%s: cannot skip to specified offset"),
2133 quotef (input_file
));
2137 if (seek_records
!= 0 || seek_bytes
!= 0)
2139 idx_t bytes
= seek_bytes
;
2140 intmax_t write_records
= skip (STDOUT_FILENO
, output_file
,
2141 seek_records
, output_blocksize
, &bytes
);
2143 if (write_records
!= 0 || bytes
!= 0)
2145 memset (obuf
, 0, write_records
? output_blocksize
: bytes
);
2149 idx_t size
= write_records
? output_blocksize
: bytes
;
2150 if (iwrite (STDOUT_FILENO
, obuf
, size
) != size
)
2152 diagnose (errno
, _("writing to %s"), quoteaf (output_file
));
2153 quit (EXIT_FAILURE
);
2156 if (write_records
!= 0)
2161 while (write_records
|| bytes
);
2165 if (max_records
== 0 && max_bytes
== 0)
2170 int saved_byte
= -1;
2174 if (status_level
== STATUS_PROGRESS
)
2176 xtime_t progress_time
= gethrxtime ();
2177 if (next_time
<= progress_time
)
2179 print_xfer_stats (progress_time
);
2180 next_time
+= XTIME_PRECISION
;
2184 if (r_partial
+ r_full
>= max_records
+ !!max_bytes
)
2187 /* Zero the buffer before reading, so that if we get a read error,
2188 whatever data we are able to read is followed by zeros.
2189 This minimizes data loss. */
2190 if ((conversions_mask
& C_SYNC
) && (conversions_mask
& C_NOERROR
))
2192 (conversions_mask
& (C_BLOCK
| C_UNBLOCK
)) ? ' ' : '\0',
2195 if (r_partial
+ r_full
>= max_records
)
2196 nread
= iread_fnc (STDIN_FILENO
, ibuf
, max_bytes
);
2198 nread
= iread_fnc (STDIN_FILENO
, ibuf
, input_blocksize
);
2202 advance_input_offset (nread
);
2204 invalidate_cache (STDIN_FILENO
, nread
);
2206 else if (nread
== 0)
2208 i_nocache_eof
|= i_nocache
;
2209 o_nocache_eof
|= o_nocache
&& ! (conversions_mask
& C_NOTRUNC
);
2214 if (!(conversions_mask
& C_NOERROR
) || status_level
!= STATUS_NONE
)
2215 diagnose (errno
, _("error reading %s"), quoteaf (input_file
));
2217 if (conversions_mask
& C_NOERROR
)
2220 idx_t bad_portion
= input_blocksize
- partread
;
2222 /* We already know this data is not cached,
2223 but call this so that correct offsets are maintained. */
2224 invalidate_cache (STDIN_FILENO
, bad_portion
);
2226 /* Seek past the bad block if possible. */
2227 if (!advance_input_after_read_error (bad_portion
))
2229 exit_status
= EXIT_FAILURE
;
2231 /* Suppress duplicate diagnostics. */
2232 input_seekable
= false;
2233 input_seek_errno
= ESPIPE
;
2235 if ((conversions_mask
& C_SYNC
) && !partread
)
2236 /* Replace the missing input with null bytes and
2237 proceed normally. */
2244 /* Write any partial block. */
2245 exit_status
= EXIT_FAILURE
;
2250 n_bytes_read
= nread
;
2252 if (n_bytes_read
< input_blocksize
)
2255 partread
= n_bytes_read
;
2256 if (conversions_mask
& C_SYNC
)
2258 if (!(conversions_mask
& C_NOERROR
))
2259 /* If C_NOERROR, we zeroed the block before reading. */
2260 memset (ibuf
+ n_bytes_read
,
2261 (conversions_mask
& (C_BLOCK
| C_UNBLOCK
)) ? ' ' : '\0',
2262 input_blocksize
- n_bytes_read
);
2263 n_bytes_read
= input_blocksize
;
2272 if (ibuf
== obuf
) /* If not C_TWOBUFS. */
2274 idx_t nwritten
= iwrite (STDOUT_FILENO
, obuf
, n_bytes_read
);
2275 w_bytes
+= nwritten
;
2276 if (nwritten
!= n_bytes_read
)
2278 diagnose (errno
, _("error writing %s"), quoteaf (output_file
));
2279 return EXIT_FAILURE
;
2281 else if (n_bytes_read
== input_blocksize
)
2288 /* Do any translations on the whole buffer at once. */
2290 if (translation_needed
)
2291 translate_buffer (ibuf
, n_bytes_read
);
2293 if (conversions_mask
& C_SWAB
)
2294 bufstart
= swab_buffer (ibuf
, &n_bytes_read
, &saved_byte
);
2298 if (conversions_mask
& C_BLOCK
)
2299 copy_with_block (bufstart
, n_bytes_read
);
2300 else if (conversions_mask
& C_UNBLOCK
)
2301 copy_with_unblock (bufstart
, n_bytes_read
);
2303 copy_simple (bufstart
, n_bytes_read
);
2306 /* If we have a char left as a result of conv=swab, output it. */
2307 if (0 <= saved_byte
)
2309 char saved_char
= saved_byte
;
2310 if (conversions_mask
& C_BLOCK
)
2311 copy_with_block (&saved_char
, 1);
2312 else if (conversions_mask
& C_UNBLOCK
)
2313 copy_with_unblock (&saved_char
, 1);
2315 output_char (saved_char
);
2318 if ((conversions_mask
& C_BLOCK
) && col
> 0)
2320 /* If the final input line didn't end with a '\n', pad
2321 the output block to 'conversion_blocksize' chars. */
2322 for (idx_t i
= col
; i
< conversion_blocksize
; i
++)
2323 output_char (space_character
);
2326 if (col
&& (conversions_mask
& C_UNBLOCK
))
2328 /* If there was any output, add a final '\n'. */
2329 output_char (newline_character
);
2332 /* Write out the last block. */
2335 idx_t nwritten
= iwrite (STDOUT_FILENO
, obuf
, oc
);
2336 w_bytes
+= nwritten
;
2341 diagnose (errno
, _("error writing %s"), quoteaf (output_file
));
2342 return EXIT_FAILURE
;
2346 /* If the last write was converted to a seek, then for a regular file
2347 or shared memory object, ftruncate to extend the size. */
2348 if (final_op_was_seek
)
2350 struct stat stdout_stat
;
2351 if (ifstat (STDOUT_FILENO
, &stdout_stat
) != 0)
2353 diagnose (errno
, _("cannot fstat %s"), quoteaf (output_file
));
2354 return EXIT_FAILURE
;
2356 if (S_ISREG (stdout_stat
.st_mode
) || S_TYPEISSHM (&stdout_stat
))
2358 off_t output_offset
= lseek (STDOUT_FILENO
, 0, SEEK_CUR
);
2359 if (0 <= output_offset
&& stdout_stat
.st_size
< output_offset
)
2361 if (iftruncate (STDOUT_FILENO
, output_offset
) != 0)
2363 diagnose (errno
, _("failed to truncate to %" PRIdMAX
" bytes"
2364 " in output file %s"),
2365 (intmax_t) output_offset
, quoteaf (output_file
));
2366 return EXIT_FAILURE
;
2372 /* fdatasync/fsync can take a long time, so issue a final progress
2373 indication now if progress has been made since the previous indication. */
2374 if (conversions_mask
& (C_FDATASYNC
| C_FSYNC
)
2375 && status_level
== STATUS_PROGRESS
2376 && 0 <= reported_w_bytes
&& reported_w_bytes
< w_bytes
)
2377 print_xfer_stats (0);
2382 /* Synchronize output according to conversions_mask.
2383 Do this even if w_bytes is zero, as fsync and fdatasync
2384 flush out write requests from other processes too.
2385 Clear bits in conversions_mask so that synchronization is done only once.
2386 Return zero if successful, an exit status otherwise. */
2389 synchronize_output (void)
2391 int exit_status
= 0;
2392 int mask
= conversions_mask
;
2393 conversions_mask
&= ~ (C_FDATASYNC
| C_FSYNC
);
2395 if ((mask
& C_FDATASYNC
) && ifdatasync (STDOUT_FILENO
) != 0)
2397 if (errno
!= ENOSYS
&& errno
!= EINVAL
)
2399 diagnose (errno
, _("fdatasync failed for %s"), quoteaf (output_file
));
2400 exit_status
= EXIT_FAILURE
;
2405 if ((mask
& C_FSYNC
) && ifsync (STDOUT_FILENO
) != 0)
2407 diagnose (errno
, _("fsync failed for %s"), quoteaf (output_file
));
2408 return EXIT_FAILURE
;
2415 main (int argc
, char **argv
)
2421 install_signal_handlers ();
2423 initialize_main (&argc
, &argv
);
2424 set_program_name (argv
[0]);
2425 setlocale (LC_ALL
, "");
2426 bindtextdomain (PACKAGE
, LOCALEDIR
);
2427 textdomain (PACKAGE
);
2429 /* Arrange to close stdout if parse_long_options exits. */
2430 atexit (maybe_close_stdout
);
2432 page_size
= getpagesize ();
2434 parse_gnu_standard_options_only (argc
, argv
, PROGRAM_NAME
, PACKAGE
, Version
,
2435 true, usage
, AUTHORS
,
2436 (char const *) nullptr);
2437 close_stdout_required
= false;
2439 /* Initialize translation table to identity translation. */
2440 for (i
= 0; i
< 256; i
++)
2443 /* Decode arguments. */
2444 scanargs (argc
, argv
);
2446 apply_translations ();
2448 if (input_file
== nullptr)
2450 input_file
= _("standard input");
2451 set_fd_flags (STDIN_FILENO
, input_flags
, input_file
);
2455 if (ifd_reopen (STDIN_FILENO
, input_file
, O_RDONLY
| input_flags
, 0) < 0)
2456 error (EXIT_FAILURE
, errno
, _("failed to open %s"),
2457 quoteaf (input_file
));
2460 offset
= lseek (STDIN_FILENO
, 0, SEEK_CUR
);
2461 input_seekable
= (0 <= offset
);
2462 input_offset
= MAX (0, offset
);
2463 input_seek_errno
= errno
;
2465 if (output_file
== nullptr)
2467 output_file
= _("standard output");
2468 set_fd_flags (STDOUT_FILENO
, output_flags
, output_file
);
2472 mode_t perms
= MODE_RW_UGO
;
2475 | (conversions_mask
& C_NOCREAT
? 0 : O_CREAT
)
2476 | (conversions_mask
& C_EXCL
? O_EXCL
: 0)
2477 | (seek_records
|| (conversions_mask
& C_NOTRUNC
) ? 0 : O_TRUNC
));
2480 if ((INT_MULTIPLY_WRAPV (seek_records
, output_blocksize
, &size
)
2481 || INT_ADD_WRAPV (seek_bytes
, size
, &size
))
2482 && !(conversions_mask
& C_NOTRUNC
))
2483 error (EXIT_FAILURE
, 0,
2484 _("offset too large: "
2485 "cannot truncate to a length of seek=%"PRIdMAX
""
2486 " (%td-byte) blocks"),
2487 seek_records
, output_blocksize
);
2489 /* Open the output file with *read* access only if we might
2490 need to read to satisfy a 'seek=' request. If we can't read
2491 the file, go ahead with write-only access; it might work. */
2493 || ifd_reopen (STDOUT_FILENO
, output_file
, O_RDWR
| opts
, perms
) < 0)
2494 && (ifd_reopen (STDOUT_FILENO
, output_file
, O_WRONLY
| opts
, perms
)
2496 error (EXIT_FAILURE
, errno
, _("failed to open %s"),
2497 quoteaf (output_file
));
2499 if (seek_records
!= 0 && !(conversions_mask
& C_NOTRUNC
))
2501 if (iftruncate (STDOUT_FILENO
, size
) != 0)
2503 /* Complain only when ftruncate fails on a regular file, a
2504 directory, or a shared memory object, as POSIX 1003.1-2004
2505 specifies ftruncate's behavior only for these file types.
2506 For example, do not complain when Linux kernel 2.4 ftruncate
2507 fails on /dev/fd0. */
2508 int ftruncate_errno
= errno
;
2509 struct stat stdout_stat
;
2510 if (ifstat (STDOUT_FILENO
, &stdout_stat
) != 0)
2512 diagnose (errno
, _("cannot fstat %s"), quoteaf (output_file
));
2513 exit_status
= EXIT_FAILURE
;
2515 else if (S_ISREG (stdout_stat
.st_mode
)
2516 || S_ISDIR (stdout_stat
.st_mode
)
2517 || S_TYPEISSHM (&stdout_stat
))
2519 intmax_t isize
= size
;
2520 diagnose (ftruncate_errno
,
2521 _("failed to truncate to %"PRIdMAX
" bytes"
2522 " in output file %s"),
2523 isize
, quoteaf (output_file
));
2524 exit_status
= EXIT_FAILURE
;
2530 start_time
= gethrxtime ();
2531 next_time
= start_time
+ XTIME_PRECISION
;
2533 exit_status
= dd_copy ();
2535 int sync_status
= synchronize_output ();
2537 exit_status
= sync_status
;
2539 if (max_records
== 0 && max_bytes
== 0)
2541 /* Special case to invalidate cache to end of file. */
2542 if (i_nocache
&& !invalidate_cache (STDIN_FILENO
, 0))
2544 diagnose (errno
, _("failed to discard cache for: %s"),
2545 quotef (input_file
));
2546 exit_status
= EXIT_FAILURE
;
2548 if (o_nocache
&& !invalidate_cache (STDOUT_FILENO
, 0))
2550 diagnose (errno
, _("failed to discard cache for: %s"),
2551 quotef (output_file
));
2552 exit_status
= EXIT_FAILURE
;
2557 /* Invalidate any pending region or to EOF if appropriate. */
2558 if (i_nocache
|| i_nocache_eof
)
2559 invalidate_cache (STDIN_FILENO
, 0);
2560 if (o_nocache
|| o_nocache_eof
)
2561 invalidate_cache (STDOUT_FILENO
, 0);
2565 main_exit (exit_status
);