ls: omit unnecessary test
[coreutils.git] / src / dd.c
blobc05dfa01aefdb4966022b85ca8d5583137cdeb39
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. */
19 #include <config.h>
21 #include <ctype.h>
22 #include <sys/types.h>
23 #include <signal.h>
25 #include "system.h"
26 #include "alignalloc.h"
27 #include "close-stream.h"
28 #include "fd-reopen.h"
29 #include "gethrxtime.h"
30 #include "human.h"
31 #include "ioblksize.h"
32 #include "long-options.h"
33 #include "quote.h"
34 #include "xstrtol.h"
35 #include "xtime.h"
37 /* The official name of this program (e.g., no 'g' prefix). */
38 #define PROGRAM_NAME "dd"
40 #define AUTHORS \
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
46 present. */
47 #ifndef SA_NOCLDSTOP
48 # define SA_NOCLDSTOP 0
49 # define sigprocmask(How, Set, Oset) /* empty */
50 # define sigset_t int
51 # if ! HAVE_SIGINTERRUPT
52 # define siginterrupt(sig, flag) /* empty */
53 # endif
54 #endif
56 /* NonStop circa 2011 lacks SA_RESETHAND; see Bug#9076. */
57 #ifndef SA_RESETHAND
58 # define SA_RESETHAND 0
59 #endif
61 #ifndef SIGINFO
62 # define SIGINFO SIGUSR1
63 #endif
65 /* This may belong in GNULIB's fcntl module instead.
66 Define O_CIO to 0 if it is not supported by this OS. */
67 #ifndef O_CIO
68 # define O_CIO 0
69 #endif
71 /* 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. */
73 #undef O_NOCACHE
75 #define output_char(c) \
76 do \
77 { \
78 obuf[oc++] = (c); \
79 if (oc >= output_blocksize) \
80 write_output (); \
81 } \
82 while (0)
84 /* Default input and output blocksize. */
85 #define DEFAULT_BLOCKSIZE 512
87 /* Conversions bit masks. */
88 enum
90 C_ASCII = 01,
92 C_EBCDIC = 02,
93 C_IBM = 04,
94 C_BLOCK = 010,
95 C_UNBLOCK = 020,
96 C_LCASE = 040,
97 C_UCASE = 0100,
98 C_SWAB = 0200,
99 C_NOERROR = 0400,
100 C_NOTRUNC = 01000,
101 C_SYNC = 02000,
103 /* Use separate input and output buffers, and combine partial
104 input blocks. */
105 C_TWOBUFS = 04000,
107 C_NOCREAT = 010000,
108 C_EXCL = 020000,
109 C_FDATASYNC = 040000,
110 C_FSYNC = 0100000,
112 C_SPARSE = 0200000
115 /* Status levels. */
116 enum
118 STATUS_NONE = 1,
119 STATUS_NOXFER = 2,
120 STATUS_DEFAULT = 3,
121 STATUS_PROGRESS = 4
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'
146 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
153 output. */
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 = ' ';
226 /* I/O buffers. */
227 static char *ibuf;
228 static char *obuf;
230 /* Current index into 'obuf'. */
231 static idx_t oc = 0;
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. */
258 struct symbol_value
260 char symbol[sizeof LONGEST_SYMBOL];
261 int value;
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. */
283 {"", 0}
286 #define FFS_MASK(x) ((x) ^ ((x) & ((x) - 1)))
287 enum
289 /* Compute a value that's bitwise disjoint from the union
290 of all O_ values. */
291 v = ~(0
292 | O_APPEND
293 | O_BINARY
294 | O_CIO
295 | O_DIRECT
296 | O_DIRECTORY
297 | O_DSYNC
298 | O_NOATIME
299 | O_NOCTTY
300 | O_NOFOLLOW
301 | O_NOLINKS
302 | O_NONBLOCK
303 | O_SYNC
304 | O_TEXT
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),
312 v3 = v2 ^ O_NOCACHE,
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},
344 {"cio", O_CIO},
345 {"direct", O_DIRECT},
346 {"directory", O_DIRECTORY},
347 {"dsync", O_DSYNC},
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},
354 {"sync", O_SYNC},
355 {"text", O_TEXT},
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},
360 {"", 0}
363 /* Status, for status="...". */
364 static struct symbol_value const statuses[] =
366 {"none", STATUS_NONE},
367 {"noxfer", STATUS_NOXFER},
368 {"progress", STATUS_PROGRESS},
369 {"", 0}
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
378 not correct here. */
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. */
502 static void
503 maybe_close_stdout (void)
505 if (close_stdout_required)
506 close_stdout ();
507 else if (close_stream (stderr) != 0)
508 _exit (EXIT_FAILURE);
511 /* Like the 'error' function but handle any pending newline,
512 and do not exit. */
514 ATTRIBUTE_FORMAT ((__printf__, 2, 3))
515 static void
516 diagnose (int errnum, char const *fmt, ...)
518 if (0 < progress_len)
520 fputc ('\n', stderr);
521 progress_len = 0;
524 va_list ap;
525 va_start (ap, fmt);
526 verror (0, errnum, fmt, ap);
527 va_end (ap);
530 void
531 usage (int status)
533 if (status != EXIT_SUCCESS)
534 emit_try_help ();
535 else
537 printf (_("\
538 Usage: %s [OPERAND]...\n\
539 or: %s OPTION\n\
541 program_name, program_name);
542 fputs (_("\
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\
551 "), stdout);
552 fputs (_("\
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\
564 "), stdout);
565 fputs (_("\
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\
575 "), stdout);
576 fputs (_("\
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\
588 "), stdout);
589 fputs (_("\
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\
596 "), stdout);
597 fputs (_("\
599 Each FLAG symbol may be:\n\
601 append append mode (makes sense only for output; conv=notrunc suggested)\n\
602 "), stdout);
603 if (O_CIO)
604 fputs (_(" cio use concurrent I/O for data\n"), stdout);
605 if (O_DIRECT)
606 fputs (_(" direct use direct I/O for data\n"), stdout);
607 if (O_DIRECTORY)
608 fputs (_(" directory fail unless a directory\n"), stdout);
609 if (O_DSYNC)
610 fputs (_(" dsync use synchronized I/O for data\n"), stdout);
611 if (O_SYNC)
612 fputs (_(" sync likewise, but also for metadata\n"), stdout);
613 fputs (_(" fullblock accumulate full blocks of input (iflag only)\n"),
614 stdout);
615 if (O_NONBLOCK)
616 fputs (_(" nonblock use non-blocking I/O\n"), stdout);
617 if (O_NOATIME)
618 fputs (_(" noatime do not update access time\n"), stdout);
619 #if HAVE_POSIX_FADVISE
620 if (O_NOCACHE)
621 fputs (_(" nocache Request to drop cache. See also oflag=sync\n"),
622 stdout);
623 #endif
624 if (O_NOCTTY)
625 fputs (_(" noctty do not assign controlling terminal from file\n"),
626 stdout);
627 if (HAVE_WORKING_O_NOFOLLOW)
628 fputs (_(" nofollow do not follow symlinks\n"), stdout);
629 if (O_NOLINKS)
630 fputs (_(" nolinks fail if multiply-linked\n"), stdout);
631 if (O_BINARY)
632 fputs (_(" binary use binary I/O for data\n"), stdout);
633 if (O_TEXT)
634 fputs (_(" text use text I/O for data\n"), stdout);
637 printf (_("\
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\
642 Options are:\n\
644 "), SIGINFO == SIGUSR1 ? "USR1" : "INFO");
647 fputs (HELP_OPTION_DESCRIPTION, stdout);
648 fputs (VERSION_OPTION_DESCRIPTION, stdout);
649 emit_ancillary_info (PROGRAM_NAME);
651 exit (status);
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. */
661 static void
662 alloc_ibuf (void)
664 if (ibuf)
665 return;
667 bool extra_byte_for_swab = !!(conversions_mask & C_SWAB);
668 ibuf = alignalloc (page_size, input_blocksize + extra_byte_for_swab);
669 if (!ibuf)
671 char hbuf[LONGEST_HUMAN_READABLE + 1];
672 error (EXIT_FAILURE, 0,
673 _("memory exhausted by input buffer of size %td bytes (%s)"),
674 input_blocksize,
675 human_readable (input_blocksize, hbuf,
676 human_opts | human_base_1024, 1, 1));
680 /* Ensure output buffer OBUF is allocated/initialized. */
682 static void
683 alloc_obuf (void)
685 if (obuf)
686 return;
688 if (conversions_mask & C_TWOBUFS)
690 obuf = alignalloc (page_size, output_blocksize);
691 if (!obuf)
693 char hbuf[LONGEST_HUMAN_READABLE + 1];
694 error (EXIT_FAILURE, 0,
695 _("memory exhausted by output buffer of size %td"
696 " bytes (%s)"),
697 output_blocksize,
698 human_readable (output_blocksize, hbuf,
699 human_opts | human_base_1024, 1, 1));
702 else
704 alloc_ibuf ();
705 obuf = ibuf;
709 static void
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. */
719 static inline bool
720 multiple_bits_set (int i)
722 return MULTIPLE_BITS_SET (i);
725 static bool
726 abbreviation_lacks_prefix (char const *message)
728 return message[strlen (message) - 2] == ' ';
731 /* Print transfer statistics. */
733 static void
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];
739 double delta_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);
758 else
760 delta_s = 0;
761 snprintf (bpsbuf, bpsbufsize, "%s B/s", _("Infinity"));
762 bytes_per_second = bpsbuf;
765 if (progress_time)
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);
777 int stats_len
778 = (abbreviation_lacks_prefix (si)
779 ? fprintf (stderr,
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)
785 ? fprintf (stderr,
786 _("%jd bytes (%s) copied, %s, %s"),
787 w_bytes, si, delta_s_buf, bytes_per_second)
788 : fprintf (stderr,
789 _("%jd bytes (%s, %s) copied, %s, %s"),
790 w_bytes, si, iec, delta_s_buf, bytes_per_second));
792 if (progress_time)
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;
804 else
805 fputc ('\n', stderr);
807 reported_w_bytes = w_bytes;
810 static void
811 print_stats (void)
813 if (status_level == STATUS_NONE)
814 return;
816 if (0 < progress_len)
818 fputc ('\n', stderr);
819 progress_len = 0;
822 fprintf (stderr,
823 _("%jd+%jd records in\n"
824 "%jd+%jd records out\n"),
825 r_full, r_partial, w_full, w_partial);
827 if (r_truncate != 0)
828 fprintf (stderr,
829 ngettext ("%jd truncated record\n",
830 "%jd truncated records\n",
831 select_plural (r_truncate)),
832 r_truncate);
834 if (status_level == STATUS_NOXFER)
835 return;
837 print_xfer_stats (0);
840 /* An ordinary signal was received; arrange for the program to exit. */
842 static void
843 interrupt_handler (int sig)
845 if (! SA_RESETHAND)
846 signal (sig, SIG_DFL);
847 interrupt_signal = sig;
850 /* An info signal was received; arrange for the program to print status. */
852 static void
853 siginfo_handler (int sig)
855 if (! SA_NOCLDSTOP)
856 signal (sig, siginfo_handler);
857 info_signal_count++;
860 /* Install the signal handlers. */
862 static void
863 install_signal_handlers (void)
865 bool catch_siginfo = ! (SIGINFO == SIGUSR1 && getenv ("POSIXLY_CORRECT"));
867 #if SA_NOCLDSTOP
869 struct sigaction act;
870 sigemptyset (&caught_signals);
871 if (catch_siginfo)
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. */
884 act.sa_flags = 0;
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);
895 #else
897 if (catch_siginfo)
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);
907 #endif
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. */
915 static int
916 iclose (int fd)
918 if (close (fd) != 0)
920 if (errno != EINTR)
921 return -1;
922 while (close (fd) != 0 && errno != EBADF);
924 return 0;
927 static int synchronize_output (void);
929 static void
930 cleanup (void)
932 if (!interrupt_signal)
934 int sync_status = synchronize_output ();
935 if (sync_status)
936 exit (sync_status);
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. */
955 static void
956 process_signals (void)
958 while (interrupt_signal || info_signal_count)
960 int interrupt;
961 int infos;
962 sigset_t oldset;
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;
971 if (infos)
972 info_signal_count = infos - 1;
974 sigprocmask (SIG_SETMASK, &oldset, nullptr);
976 if (interrupt)
977 cleanup ();
978 print_stats ();
979 if (interrupt)
980 raise (interrupt);
984 static void
985 finish_up (void)
987 /* Process signals first, so that cleanup is called at most once. */
988 process_signals ();
989 cleanup ();
990 print_stats ();
993 static void
994 quit (int code)
996 finish_up ();
997 exit (code);
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. */
1005 static off_t
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);
1011 if (len)
1013 intmax_t c_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;
1019 else
1020 len = 0;
1022 else
1023 len = *pending;
1025 return len;
1028 /* Discard the cache from the current offset of either
1029 STDIN_FILENO or STDOUT_FILENO.
1030 Return true on success. */
1032 static bool
1033 invalidate_cache (int fd, off_t len)
1035 int adv_ret = -1;
1036 off_t offset;
1037 bool nocache_eof = (fd == STDIN_FILENO ? i_nocache_eof : o_nocache_eof);
1039 /* Minimize syscalls. */
1040 off_t clen = cache_round (fd, len);
1041 if (len && !clen)
1042 return true; /* Don't advise this time. */
1043 else if (! len && ! clen && ! nocache_eof)
1044 return true;
1045 off_t pending = len ? cache_round (fd, 0) : 0;
1047 if (fd == STDIN_FILENO)
1049 if (input_seekable)
1050 offset = input_offset;
1051 else
1053 offset = -1;
1054 errno = ESPIPE;
1057 else
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);
1065 else if (len)
1066 output_offset += clen + pending;
1069 offset = output_offset;
1072 if (0 <= offset)
1074 if (! len && clen && nocache_eof)
1076 pending = clen;
1077 clen = 0;
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. */
1087 if (clen == 0)
1088 offset -= offset % page_size;
1089 adv_ret = posix_fadvise (fd, offset, clen, POSIX_FADV_DONTNEED);
1090 #else
1091 errno = ENOTSUP;
1092 #endif
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. */
1102 static ssize_t
1103 iread (int fd, char *buf, idx_t size)
1105 ssize_t nread;
1106 static ssize_t prev_nread;
1110 process_signals ();
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))
1118 errno = 0;
1119 nread = 0;
1122 while (nread < 0 && errno == EINTR);
1124 /* Short read may be due to received signal. */
1125 if (0 < nread && nread < size)
1126 process_signals ();
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)),
1139 prev);
1140 warn_partial_read = false;
1144 prev_nread = nread;
1145 return nread;
1148 /* Wrapper around iread function to accumulate full blocks. */
1149 static ssize_t
1150 iread_fullblock (int fd, char *buf, idx_t size)
1152 ssize_t nread = 0;
1154 while (0 < size)
1156 ssize_t ncurr = iread (fd, buf, size);
1157 if (ncurr < 0)
1158 return ncurr;
1159 if (ncurr == 0)
1160 break;
1161 nread += ncurr;
1162 buf += ncurr;
1163 size -= ncurr;
1166 return nread;
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
1172 writes. */
1174 static idx_t
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;
1206 process_signals ();
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. */
1217 else
1219 final_op_was_seek = true;
1220 nwritten = size;
1224 if (!nwritten)
1225 nwritten = write (fd, buf + total_written, size - total_written);
1227 if (nwritten < 0)
1229 if (errno != EINTR)
1230 break;
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. */
1237 errno = ENOSPC;
1238 break;
1240 else
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'. */
1252 static void
1253 write_output (void)
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));
1260 if (nwritten != 0)
1261 w_partial++;
1262 quit (EXIT_FAILURE);
1264 else
1265 w_full++;
1266 oc = 0;
1269 /* Restart on EINTR from fdatasync. */
1271 static int
1272 ifdatasync (int fd)
1274 int ret;
1278 process_signals ();
1279 ret = fdatasync (fd);
1281 while (ret < 0 && errno == EINTR);
1283 return ret;
1286 /* Restart on EINTR from fd_reopen. */
1288 static int
1289 ifd_reopen (int desired_fd, char const *file, int flag, mode_t mode)
1291 int ret;
1295 process_signals ();
1296 ret = fd_reopen (desired_fd, file, flag, mode);
1298 while (ret < 0 && errno == EINTR);
1300 return ret;
1303 /* Restart on EINTR from fstat. */
1305 static int
1306 ifstat (int fd, struct stat *st)
1308 int ret;
1312 process_signals ();
1313 ret = fstat (fd, st);
1315 while (ret < 0 && errno == EINTR);
1317 return ret;
1320 /* Restart on EINTR from fsync. */
1322 static int
1323 ifsync (int fd)
1325 int ret;
1329 process_signals ();
1330 ret = fsync (fd);
1332 while (ret < 0 && errno == EINTR);
1334 return ret;
1337 /* Restart on EINTR from ftruncate. */
1339 static int
1340 iftruncate (int fd, off_t length)
1342 int ret;
1346 process_signals ();
1347 ret = ftruncate (fd, length);
1349 while (ret < 0 && errno == EINTR);
1351 return ret;
1354 /* Return true if STR is of the form "PATTERN" or "PATTERNDELIM...". */
1356 ATTRIBUTE_PURE
1357 static bool
1358 operand_matches (char const *str, char const *pattern, char delim)
1360 while (*pattern)
1361 if (*str++ != *pattern++)
1362 return false;
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. */
1370 static int
1371 parse_symbols (char const *str, struct symbol_value const *table,
1372 bool exclusive, char const *error_msgid)
1374 int value = 0;
1376 while (true)
1378 char const *strcomma = strchr (str, ',');
1379 struct symbol_value const *entry;
1381 for (entry = table;
1382 ! (operand_matches (str, entry->symbol, ',') && entry->value);
1383 entry++)
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,
1390 str, slen));
1391 usage (EXIT_FAILURE);
1395 if (exclusive)
1396 value = entry->value;
1397 else
1398 value |= entry->value;
1399 if (!strcomma)
1400 break;
1401 str = strcomma + 1;
1404 return 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. */
1412 static intmax_t
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;
1421 char *suffix;
1422 static char const suffixes[] = "bcEGkKMPQRTwYZ0";
1423 strtol_error e = xstrtoumax (str, &suffix, 10, &n, suffixes);
1424 intmax_t result;
1426 if ((e & ~LONGINT_OVERFLOW) == LONGINT_INVALID_SUFFIX_CHAR
1427 && *suffix == 'B' && str < suffix && suffix[-1] != 'B')
1429 suffix++;
1430 if (!*suffix)
1431 e &= ~LONGINT_INVALID_SUFFIX_CHAR;
1434 if ((e & ~LONGINT_OVERFLOW) == LONGINT_INVALID_SUFFIX_CHAR
1435 && *suffix == 'x')
1437 strtol_error f = LONGINT_OK;
1438 intmax_t o = parse_integer (suffix + 1, &f);
1439 if ((f & ~LONGINT_OVERFLOW) != LONGINT_OK)
1441 e = f;
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;
1450 else
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"));
1456 e = LONGINT_OK;
1459 else if (n <= INTMAX_MAX)
1460 result = n;
1461 else
1463 e = LONGINT_OVERFLOW;
1464 result = INTMAX_MAX;
1467 *invalid = e;
1468 return result;
1471 /* OPERAND is of the form "X=...". Return true if X is NAME. */
1473 ATTRIBUTE_PURE
1474 static bool
1475 operand_is (char const *operand, char const *name)
1477 return operand_matches (operand, name, '=');
1480 static void
1481 scanargs (int argc, char *const *argv)
1483 idx_t blocksize = 0;
1484 intmax_t count = INTMAX_MAX;
1485 intmax_t skip = 0;
1486 intmax_t seek = 0;
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, '=');
1494 if (val == nullptr)
1496 diagnose (0, _("unrecognized operand %s"), quoteaf (name));
1497 usage (EXIT_FAILURE);
1499 val++;
1501 if (operand_is (name, "if"))
1502 input_file = val;
1503 else if (operand_is (name, "of"))
1504 output_file = val;
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"));
1517 else
1519 strtol_error invalid = LONGINT_OK;
1520 intmax_t n = parse_integer (val, &invalid);
1521 bool has_B = !!strchr (val, 'B');
1522 intmax_t n_min = 0;
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"))
1535 n_min = 1;
1536 n_max = max_blocksize;
1537 converted_idx = &input_blocksize;
1539 else if (operand_is (name, "obs"))
1541 n_min = 1;
1542 n_max = max_blocksize;
1543 converted_idx = &output_blocksize;
1545 else if (operand_is (name, "bs"))
1547 n_min = 1;
1548 n_max = max_blocksize;
1549 converted_idx = &blocksize;
1551 else if (operand_is (name, "cbs"))
1553 n_min = 1;
1554 n_max = MIN (SIZE_MAX, IDX_MAX);
1555 converted_idx = &conversion_blocksize;
1557 else if (operand_is (name, "skip") || operand_is (name, "iseek"))
1559 skip = n;
1560 skip_B = has_B;
1562 else if (operand_is (name + (*name == 'o'), "seek"))
1564 seek = n;
1565 seek_B = has_B;
1567 else if (operand_is (name, "count"))
1569 count = n;
1570 count_B = has_B;
1572 else
1574 diagnose (0, _("unrecognized operand %s"), quoteaf (name));
1575 usage (EXIT_FAILURE);
1578 if (n < n_min)
1579 invalid = LONGINT_INVALID;
1580 else if (n_max < n)
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)
1587 *converted_idx = n;
1591 if (blocksize)
1592 input_blocksize = output_blocksize = blocksize;
1593 else
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);
1616 if (skip_B)
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;
1623 else if (skip != 0)
1624 skip_records = skip;
1626 if (count_B)
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;
1636 if (seek_B)
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;
1643 else if (seek != 0)
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. */
1650 warn_partial_read =
1651 (! (conversions_mask & C_TWOBUFS) && ! (input_flags & O_FULLBLOCK)
1652 && (skip_records
1653 || (0 < max_records && max_records < INTMAX_MAX)
1654 || (input_flags | output_flags) & O_DIRECT));
1656 iread_fnc = ((input_flags & O_FULLBLOCK)
1657 ? iread_fullblock
1658 : iread);
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)
1675 i_nocache = true;
1676 i_nocache_eof = (max_records == 0 && max_bytes == 0);
1677 input_flags &= ~O_NOCACHE;
1679 if (output_flags & O_NOCACHE)
1681 o_nocache = true;
1682 o_nocache_eof = (max_records == 0 && max_bytes == 0);
1683 output_flags &= ~O_NOCACHE;
1687 /* Fix up translation table. */
1689 static void
1690 apply_translations (void)
1692 int i;
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. */
1727 static void
1728 translate_buffer (char *buf, idx_t nread)
1730 idx_t i;
1731 char *cp;
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. */
1743 static char *
1744 swab_buffer (char *buf, idx_t *nread, int *saved_byte)
1746 if (*nread == 0)
1747 return buf;
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];
1754 *saved_byte = c;
1756 else
1757 *saved_byte = -1;
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];
1765 if (prev_saved < 0)
1766 return buf + 1;
1768 buf[1] = prev_saved;
1769 ++*nread;
1770 return buf;
1773 /* Add OFFSET to the input offset, setting the overflow flag if
1774 necessary. */
1776 static void
1777 advance_input_offset (intmax_t offset)
1779 if (0 <= input_offset && ckd_add (&input_offset, input_offset, offset))
1780 input_offset = -1;
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. */
1792 static intmax_t
1793 skip (int fdesc, char const *file, intmax_t records, idx_t blocksize,
1794 idx_t *bytes)
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. */
1800 errno = 0;
1801 off_t offset;
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)
1808 struct stat st;
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;
1820 else
1821 records = 0;
1822 advance_input_offset (offset);
1824 else
1826 records = 0;
1827 *bytes = 0;
1829 return records;
1831 else
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. */
1845 if (!lseek_errno)
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")),
1861 quotef (file));
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 */
1868 char *buf;
1869 if (fdesc == STDIN_FILENO)
1871 alloc_ibuf ();
1872 buf = ibuf;
1874 else
1876 alloc_obuf ();
1877 buf = obuf;
1882 ssize_t nread = iread_fnc (fdesc, buf, records ? blocksize : *bytes);
1883 if (nread < 0)
1885 if (fdesc == STDIN_FILENO)
1887 diagnose (errno, _("error reading %s"), quoteaf (file));
1888 if (conversions_mask & C_NOERROR)
1889 print_stats ();
1891 else
1892 diagnose (lseek_errno, _("%s: cannot seek"), quotef (file));
1893 quit (EXIT_FAILURE);
1895 else if (nread == 0)
1896 break;
1897 else if (fdesc == STDIN_FILENO)
1898 advance_input_offset (nread);
1900 if (records != 0)
1901 records--;
1902 else
1903 *bytes = 0;
1905 while (records || *bytes);
1907 return records;
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
1915 be seekable. */
1917 static bool
1918 advance_input_after_read_error (idx_t nbytes)
1920 if (! input_seekable)
1922 if (input_seek_errno == ESPIPE)
1923 return true;
1924 errno = input_seek_errno;
1926 else
1928 off_t offset;
1929 advance_input_offset (nbytes);
1930 if (input_offset < 0)
1932 diagnose (0, _("offset overflow while reading file %s"),
1933 quoteaf (input_file));
1934 return false;
1936 offset = lseek (STDIN_FILENO, 0, SEEK_CUR);
1937 if (0 <= offset)
1939 off_t diff;
1940 if (offset == input_offset)
1941 return true;
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))
1946 return true;
1947 if (errno == 0)
1948 diagnose (0, _("cannot work around kernel bug after all"));
1952 diagnose (errno, _("%s: cannot seek"), quotef (input_file));
1953 return false;
1956 /* Copy NREAD bytes of BUF, with no conversions. */
1958 static void
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. */
1970 start += nfree;
1971 oc += nfree;
1972 if (oc >= output_blocksize)
1973 write_output ();
1975 while (nread != 0);
1978 /* Copy NREAD bytes of BUF, doing conv=block
1979 (pad newline-terminated records to 'conversion_blocksize',
1980 replacing the newline with trailing spaces). */
1982 static void
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)
1991 idx_t j;
1992 for (j = col; j < conversion_blocksize; j++)
1993 output_char (space_character);
1995 col = 0;
1997 else
1999 if (col == conversion_blocksize)
2000 r_truncate++;
2001 else if (col < conversion_blocksize)
2002 output_char (*buf);
2003 col++;
2008 /* Copy NREAD bytes of BUF, doing conv=unblock
2009 (replace trailing spaces in 'conversion_blocksize'-sized records
2010 with a newline). */
2012 static void
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++)
2019 char c = buf[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)
2028 pending_spaces++;
2029 else
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);
2036 --pending_spaces;
2038 output_char (c);
2043 /* Set the file descriptor flags for FD that correspond to the nonzero bits
2044 in ADD_FLAGS. The file's name is NAME. */
2046 static void
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);
2052 if (add_flags)
2054 int old_flags = fcntl (fd, F_GETFL);
2055 int new_flags = old_flags | add_flags;
2056 bool ok = true;
2057 if (old_flags < 0)
2058 ok = false;
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. */
2065 struct stat st;
2066 if (ifstat (fd, &st) != 0)
2067 ok = false;
2068 else if ((new_flags & O_DIRECTORY) && ! S_ISDIR (st.st_mode))
2070 errno = ENOTDIR;
2071 ok = false;
2073 else if ((new_flags & O_NOLINKS) && 1 < st.st_nlink)
2075 errno = EMLINK;
2076 ok = false;
2078 new_flags &= ~ (O_DIRECTORY | O_NOLINKS);
2081 if (ok && old_flags != new_flags
2082 && fcntl (fd, F_SETFL, new_flags) == -1)
2083 ok = false;
2086 if (!ok)
2087 error (EXIT_FAILURE, errno, _("setting flags for %s"), quoteaf (name));
2091 /* The main loop. */
2093 static int
2094 dd_copy (void)
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. */
2101 idx_t partread = 0;
2103 int exit_status = EXIT_SUCCESS;
2104 idx_t n_bytes_read;
2106 if (skip_records != 0 || skip_bytes != 0)
2108 intmax_t us_bytes;
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
2121 3. partial reads */
2122 if ((us_blocks
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)
2153 write_records--;
2154 else
2155 bytes = 0;
2157 while (write_records || bytes);
2161 if (max_records == 0 && max_bytes == 0)
2162 return exit_status;
2164 alloc_ibuf ();
2165 alloc_obuf ();
2166 int saved_byte = -1;
2168 while (true)
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)
2181 break;
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))
2187 memset (ibuf,
2188 (conversions_mask & (C_BLOCK | C_UNBLOCK)) ? ' ' : '\0',
2189 input_blocksize);
2191 if (r_partial + r_full >= max_records)
2192 nread = iread_fnc (STDIN_FILENO, ibuf, max_bytes);
2193 else
2194 nread = iread_fnc (STDIN_FILENO, ibuf, input_blocksize);
2196 if (nread > 0)
2198 advance_input_offset (nread);
2199 if (i_nocache)
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);
2206 break; /* EOF. */
2208 else
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)
2215 print_stats ();
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. */
2234 nread = 0;
2235 else
2236 continue;
2238 else
2240 /* Write any partial block. */
2241 exit_status = EXIT_FAILURE;
2242 break;
2246 n_bytes_read = nread;
2248 if (n_bytes_read < input_blocksize)
2250 r_partial++;
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;
2262 else
2264 r_full++;
2265 partread = 0;
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)
2278 w_full++;
2279 else
2280 w_partial++;
2281 continue;
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);
2291 else
2292 bufstart = ibuf;
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);
2298 else
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);
2310 else
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. */
2329 if (oc != 0)
2331 idx_t nwritten = iwrite (STDOUT_FILENO, obuf, oc);
2332 w_bytes += nwritten;
2333 if (nwritten != 0)
2334 w_partial++;
2335 if (nwritten != oc)
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);
2375 return exit_status;
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. */
2384 static int
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;
2398 mask |= C_FSYNC;
2401 if ((mask & C_FSYNC) && ifsync (STDOUT_FILENO) != 0)
2403 diagnose (errno, _("fsync failed for %s"), quoteaf (output_file));
2404 return EXIT_FAILURE;
2407 return exit_status;
2411 main (int argc, char **argv)
2413 int i;
2414 int exit_status;
2415 off_t offset;
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++)
2437 trans_table[i] = 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);
2449 else
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);
2466 else
2468 mode_t perms = MODE_RW_UGO;
2469 int opts
2470 = (output_flags
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));
2475 off_t size;
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. */
2488 if ((! seek_records
2489 || ifd_reopen (STDOUT_FILENO, output_file, O_RDWR | opts, perms) < 0)
2490 && (ifd_reopen (STDOUT_FILENO, output_file, O_WRONLY | opts, perms)
2491 < 0))
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 ();
2532 if (sync_status)
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;
2551 else
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);
2560 finish_up ();
2561 main_exit (exit_status);