split: port ‘split -n N /dev/null’ better to macOS
[coreutils.git] / src / dd.c
blob2808a79ab307e2db31cffc822b65d9c288ab96c7
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. */
19 #include <config.h>
21 #include <sys/types.h>
22 #include <signal.h>
24 #include "system.h"
25 #include "alignalloc.h"
26 #include "close-stream.h"
27 #include "die.h"
28 #include "error.h"
29 #include "fd-reopen.h"
30 #include "gethrxtime.h"
31 #include "human.h"
32 #include "ioblksize.h"
33 #include "long-options.h"
34 #include "quote.h"
35 #include "verror.h"
36 #include "xstrtol.h"
37 #include "xtime.h"
39 /* The official name of this program (e.g., no 'g' prefix). */
40 #define PROGRAM_NAME "dd"
42 #define AUTHORS \
43 proper_name ("Paul Rubin"), \
44 proper_name ("David MacKenzie"), \
45 proper_name ("Stuart Kemp")
47 /* Use SA_NOCLDSTOP as a proxy for whether the sigaction machinery is
48 present. */
49 #ifndef SA_NOCLDSTOP
50 # define SA_NOCLDSTOP 0
51 # define sigprocmask(How, Set, Oset) /* empty */
52 # define sigset_t int
53 # if ! HAVE_SIGINTERRUPT
54 # define siginterrupt(sig, flag) /* empty */
55 # endif
56 #endif
58 /* NonStop circa 2011 lacks SA_RESETHAND; see Bug#9076. */
59 #ifndef SA_RESETHAND
60 # define SA_RESETHAND 0
61 #endif
63 #ifndef SIGINFO
64 # define SIGINFO SIGUSR1
65 #endif
67 /* This may belong in GNULIB's fcntl module instead.
68 Define O_CIO to 0 if it is not supported by this OS. */
69 #ifndef O_CIO
70 # define O_CIO 0
71 #endif
73 /* On AIX 5.1 and AIX 5.2, O_NOCACHE is defined via <fcntl.h>
74 and would interfere with our use of that name, below. */
75 #undef O_NOCACHE
77 #if ! HAVE_FDATASYNC
78 # define fdatasync(fd) (errno = ENOSYS, -1)
79 #endif
81 #define output_char(c) \
82 do \
83 { \
84 obuf[oc++] = (c); \
85 if (oc >= output_blocksize) \
86 write_output (); \
87 } \
88 while (0)
90 /* Default input and output blocksize. */
91 #define DEFAULT_BLOCKSIZE 512
93 /* Conversions bit masks. */
94 enum
96 C_ASCII = 01,
98 C_EBCDIC = 02,
99 C_IBM = 04,
100 C_BLOCK = 010,
101 C_UNBLOCK = 020,
102 C_LCASE = 040,
103 C_UCASE = 0100,
104 C_SWAB = 0200,
105 C_NOERROR = 0400,
106 C_NOTRUNC = 01000,
107 C_SYNC = 02000,
109 /* Use separate input and output buffers, and combine partial
110 input blocks. */
111 C_TWOBUFS = 04000,
113 C_NOCREAT = 010000,
114 C_EXCL = 020000,
115 C_FDATASYNC = 040000,
116 C_FSYNC = 0100000,
118 C_SPARSE = 0200000
121 /* Status levels. */
122 enum
124 STATUS_NONE = 1,
125 STATUS_NOXFER = 2,
126 STATUS_DEFAULT = 3,
127 STATUS_PROGRESS = 4
130 /* The name of the input file, or NULL for the standard input. */
131 static char const *input_file = NULL;
133 /* The name of the output file, or NULL for the standard output. */
134 static char const *output_file = NULL;
136 /* The page size on this host. */
137 static idx_t page_size;
139 /* The number of bytes in which atomic reads are done. */
140 static idx_t input_blocksize = 0;
142 /* The number of bytes in which atomic writes are done. */
143 static idx_t output_blocksize = 0;
145 /* Conversion buffer size, in bytes. 0 prevents conversions. */
146 static idx_t conversion_blocksize = 0;
148 /* Skip this many records of 'input_blocksize' bytes before input. */
149 static intmax_t skip_records = 0;
151 /* Skip this many bytes before input in addition of 'skip_records'
152 records. */
153 static idx_t skip_bytes = 0;
155 /* Skip this many records of 'output_blocksize' bytes before output. */
156 static intmax_t seek_records = 0;
158 /* Skip this many bytes in addition to 'seek_records' records before
159 output. */
160 static intmax_t seek_bytes = 0;
162 /* Whether the final output was done with a seek (rather than a write). */
163 static bool final_op_was_seek;
165 /* Copy only this many records. The default is effectively infinity. */
166 static intmax_t max_records = INTMAX_MAX;
168 /* Copy this many bytes in addition to 'max_records' records. */
169 static idx_t max_bytes = 0;
171 /* Bit vector of conversions to apply. */
172 static int conversions_mask = 0;
174 /* Open flags for the input and output files. */
175 static int input_flags = 0;
176 static int output_flags = 0;
178 /* Status flags for what is printed to stderr. */
179 static int status_level = STATUS_DEFAULT;
181 /* If nonzero, filter characters through the translation table. */
182 static bool translation_needed = false;
184 /* Number of partial blocks written. */
185 static intmax_t w_partial = 0;
187 /* Number of full blocks written. */
188 static intmax_t w_full = 0;
190 /* Number of partial blocks read. */
191 static intmax_t r_partial = 0;
193 /* Number of full blocks read. */
194 static intmax_t r_full = 0;
196 /* Number of bytes written. */
197 static intmax_t w_bytes = 0;
199 /* Last-reported number of bytes written, or negative if never reported. */
200 static intmax_t reported_w_bytes = -1;
202 /* Time that dd started. */
203 static xtime_t start_time;
205 /* Next time to report periodic progress. */
206 static xtime_t next_time;
208 /* If positive, the number of bytes output in the current progress line. */
209 static int progress_len;
211 /* True if input is seekable. */
212 static bool input_seekable;
214 /* Error number corresponding to initial attempt to lseek input.
215 If ESPIPE, do not issue any more diagnostics about it. */
216 static int input_seek_errno;
218 /* File offset of the input, in bytes, or -1 if it overflowed. */
219 static off_t input_offset;
221 /* True if a partial read should be diagnosed. */
222 static bool warn_partial_read;
224 /* Records truncated by conv=block. */
225 static intmax_t r_truncate = 0;
227 /* Output representation of newline and space characters.
228 They change if we're converting to EBCDIC. */
229 static char newline_character = '\n';
230 static char space_character = ' ';
232 /* I/O buffers. */
233 static char *ibuf;
234 static char *obuf;
236 /* Current index into 'obuf'. */
237 static idx_t oc = 0;
239 /* Index into current line, for 'conv=block' and 'conv=unblock'. */
240 static idx_t col = 0;
242 /* The set of signals that are caught. */
243 static sigset_t caught_signals;
245 /* If nonzero, the value of the pending fatal signal. */
246 static sig_atomic_t volatile interrupt_signal;
248 /* A count of the number of pending info signals that have been received. */
249 static sig_atomic_t volatile info_signal_count;
251 /* Whether to discard cache for input or output. */
252 static bool i_nocache, o_nocache;
254 /* Whether to instruct the kernel to discard the complete file. */
255 static bool i_nocache_eof, o_nocache_eof;
257 /* Function used for read (to handle iflag=fullblock parameter). */
258 static ssize_t (*iread_fnc) (int fd, char *buf, idx_t size);
260 /* A longest symbol in the struct symbol_values tables below. */
261 #define LONGEST_SYMBOL "count_bytes"
263 /* A symbol and the corresponding integer value. */
264 struct symbol_value
266 char symbol[sizeof LONGEST_SYMBOL];
267 int value;
270 /* Conversion symbols, for conv="...". */
271 static struct symbol_value const conversions[] =
273 {"ascii", C_ASCII | C_UNBLOCK | C_TWOBUFS}, /* EBCDIC to ASCII. */
274 {"ebcdic", C_EBCDIC | C_BLOCK | C_TWOBUFS}, /* ASCII to EBCDIC. */
275 {"ibm", C_IBM | C_BLOCK | C_TWOBUFS}, /* Different ASCII to EBCDIC. */
276 {"block", C_BLOCK | C_TWOBUFS}, /* Variable to fixed length records. */
277 {"unblock", C_UNBLOCK | C_TWOBUFS}, /* Fixed to variable length records. */
278 {"lcase", C_LCASE | C_TWOBUFS}, /* Translate upper to lower case. */
279 {"ucase", C_UCASE | C_TWOBUFS}, /* Translate lower to upper case. */
280 {"sparse", C_SPARSE}, /* Try to sparsely write output. */
281 {"swab", C_SWAB | C_TWOBUFS}, /* Swap bytes of input. */
282 {"noerror", C_NOERROR}, /* Ignore i/o errors. */
283 {"nocreat", C_NOCREAT}, /* Do not create output file. */
284 {"excl", C_EXCL}, /* Fail if the output file already exists. */
285 {"notrunc", C_NOTRUNC}, /* Do not truncate output file. */
286 {"sync", C_SYNC}, /* Pad input records to ibs with NULs. */
287 {"fdatasync", C_FDATASYNC}, /* Synchronize output data before finishing. */
288 {"fsync", C_FSYNC}, /* Also synchronize output metadata. */
289 {"", 0}
292 #define FFS_MASK(x) ((x) ^ ((x) & ((x) - 1)))
293 enum
295 /* Compute a value that's bitwise disjoint from the union
296 of all O_ values. */
297 v = ~(0
298 | O_APPEND
299 | O_BINARY
300 | O_CIO
301 | O_DIRECT
302 | O_DIRECTORY
303 | O_DSYNC
304 | O_NOATIME
305 | O_NOCTTY
306 | O_NOFOLLOW
307 | O_NOLINKS
308 | O_NONBLOCK
309 | O_SYNC
310 | O_TEXT
313 /* Use its lowest bits for private flags. */
314 O_FULLBLOCK = FFS_MASK (v),
315 v2 = v ^ O_FULLBLOCK,
317 O_NOCACHE = FFS_MASK (v2),
318 v3 = v2 ^ O_NOCACHE,
320 O_COUNT_BYTES = FFS_MASK (v3),
321 v4 = v3 ^ O_COUNT_BYTES,
323 O_SKIP_BYTES = FFS_MASK (v4),
324 v5 = v4 ^ O_SKIP_BYTES,
326 O_SEEK_BYTES = FFS_MASK (v5)
329 /* Ensure that we got something. */
330 static_assert (O_FULLBLOCK != 0);
331 static_assert (O_NOCACHE != 0);
332 static_assert (O_COUNT_BYTES != 0);
333 static_assert (O_SKIP_BYTES != 0);
334 static_assert (O_SEEK_BYTES != 0);
336 #define MULTIPLE_BITS_SET(i) (((i) & ((i) - 1)) != 0)
338 /* Ensure that this is a single-bit value. */
339 static_assert ( ! MULTIPLE_BITS_SET (O_FULLBLOCK));
340 static_assert ( ! MULTIPLE_BITS_SET (O_NOCACHE));
341 static_assert ( ! MULTIPLE_BITS_SET (O_COUNT_BYTES));
342 static_assert ( ! MULTIPLE_BITS_SET (O_SKIP_BYTES));
343 static_assert ( ! MULTIPLE_BITS_SET (O_SEEK_BYTES));
345 /* Flags, for iflag="..." and oflag="...". */
346 static struct symbol_value const flags[] =
348 {"append", O_APPEND},
349 {"binary", O_BINARY},
350 {"cio", O_CIO},
351 {"direct", O_DIRECT},
352 {"directory", O_DIRECTORY},
353 {"dsync", O_DSYNC},
354 {"noatime", O_NOATIME},
355 {"nocache", O_NOCACHE}, /* Discard cache. */
356 {"noctty", O_NOCTTY},
357 {"nofollow", HAVE_WORKING_O_NOFOLLOW ? O_NOFOLLOW : 0},
358 {"nolinks", O_NOLINKS},
359 {"nonblock", O_NONBLOCK},
360 {"sync", O_SYNC},
361 {"text", O_TEXT},
362 {"fullblock", O_FULLBLOCK}, /* Accumulate full blocks from input. */
363 {"count_bytes", O_COUNT_BYTES},
364 {"skip_bytes", O_SKIP_BYTES},
365 {"seek_bytes", O_SEEK_BYTES},
366 {"", 0}
369 /* Status, for status="...". */
370 static struct symbol_value const statuses[] =
372 {"none", STATUS_NONE},
373 {"noxfer", STATUS_NOXFER},
374 {"progress", STATUS_PROGRESS},
375 {"", 0}
378 /* Translation table formed by applying successive transformations. */
379 static unsigned char trans_table[256];
381 /* Standard translation tables, taken from POSIX 1003.1-2013.
382 Beware of imitations; there are lots of ASCII<->EBCDIC tables
383 floating around the net, perhaps valid for some applications but
384 not correct here. */
386 static char const ascii_to_ebcdic[] =
388 '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
389 '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
390 '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
391 '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
392 '\100', '\132', '\177', '\173', '\133', '\154', '\120', '\175',
393 '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
394 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
395 '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
396 '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
397 '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
398 '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
399 '\347', '\350', '\351', '\255', '\340', '\275', '\232', '\155',
400 '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
401 '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
402 '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
403 '\247', '\250', '\251', '\300', '\117', '\320', '\137', '\007',
404 '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
405 '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
406 '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
407 '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
408 '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
409 '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
410 '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
411 '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
412 '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
413 '\216', '\217', '\220', '\152', '\233', '\234', '\235', '\236',
414 '\237', '\240', '\252', '\253', '\254', '\112', '\256', '\257',
415 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
416 '\270', '\271', '\272', '\273', '\274', '\241', '\276', '\277',
417 '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
418 '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
419 '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
422 static char const ascii_to_ibm[] =
424 '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
425 '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
426 '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
427 '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
428 '\100', '\132', '\177', '\173', '\133', '\154', '\120', '\175',
429 '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
430 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
431 '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
432 '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
433 '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
434 '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
435 '\347', '\350', '\351', '\255', '\340', '\275', '\137', '\155',
436 '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
437 '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
438 '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
439 '\247', '\250', '\251', '\300', '\117', '\320', '\241', '\007',
440 '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
441 '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
442 '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
443 '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
444 '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
445 '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
446 '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
447 '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
448 '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
449 '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',
450 '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',
451 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
452 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
453 '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
454 '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
455 '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
458 static char const ebcdic_to_ascii[] =
460 '\000', '\001', '\002', '\003', '\234', '\011', '\206', '\177',
461 '\227', '\215', '\216', '\013', '\014', '\015', '\016', '\017',
462 '\020', '\021', '\022', '\023', '\235', '\205', '\010', '\207',
463 '\030', '\031', '\222', '\217', '\034', '\035', '\036', '\037',
464 '\200', '\201', '\202', '\203', '\204', '\012', '\027', '\033',
465 '\210', '\211', '\212', '\213', '\214', '\005', '\006', '\007',
466 '\220', '\221', '\026', '\223', '\224', '\225', '\226', '\004',
467 '\230', '\231', '\232', '\233', '\024', '\025', '\236', '\032',
468 '\040', '\240', '\241', '\242', '\243', '\244', '\245', '\246',
469 '\247', '\250', '\325', '\056', '\074', '\050', '\053', '\174',
470 '\046', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
471 '\260', '\261', '\041', '\044', '\052', '\051', '\073', '\176',
472 '\055', '\057', '\262', '\263', '\264', '\265', '\266', '\267',
473 '\270', '\271', '\313', '\054', '\045', '\137', '\076', '\077',
474 '\272', '\273', '\274', '\275', '\276', '\277', '\300', '\301',
475 '\302', '\140', '\072', '\043', '\100', '\047', '\075', '\042',
476 '\303', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
477 '\150', '\151', '\304', '\305', '\306', '\307', '\310', '\311',
478 '\312', '\152', '\153', '\154', '\155', '\156', '\157', '\160',
479 '\161', '\162', '\136', '\314', '\315', '\316', '\317', '\320',
480 '\321', '\345', '\163', '\164', '\165', '\166', '\167', '\170',
481 '\171', '\172', '\322', '\323', '\324', '\133', '\326', '\327',
482 '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
483 '\340', '\341', '\342', '\343', '\344', '\135', '\346', '\347',
484 '\173', '\101', '\102', '\103', '\104', '\105', '\106', '\107',
485 '\110', '\111', '\350', '\351', '\352', '\353', '\354', '\355',
486 '\175', '\112', '\113', '\114', '\115', '\116', '\117', '\120',
487 '\121', '\122', '\356', '\357', '\360', '\361', '\362', '\363',
488 '\134', '\237', '\123', '\124', '\125', '\126', '\127', '\130',
489 '\131', '\132', '\364', '\365', '\366', '\367', '\370', '\371',
490 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
491 '\070', '\071', '\372', '\373', '\374', '\375', '\376', '\377'
494 /* True if we need to close the standard output *stream*. */
495 static bool close_stdout_required = true;
497 /* The only reason to close the standard output *stream* is if
498 parse_long_options fails (as it does for --help or --version).
499 In any other case, dd uses only the STDOUT_FILENO file descriptor,
500 and the "cleanup" function calls "close (STDOUT_FILENO)".
501 Closing the file descriptor and then letting the usual atexit-run
502 close_stdout function call "fclose (stdout)" would result in a
503 harmless failure of the close syscall (with errno EBADF).
504 This function serves solely to avoid the unnecessary close_stdout
505 call, once parse_long_options has succeeded.
506 Meanwhile, we guarantee that the standard error stream is flushed,
507 by inlining the last half of close_stdout as needed. */
508 static void
509 maybe_close_stdout (void)
511 if (close_stdout_required)
512 close_stdout ();
513 else if (close_stream (stderr) != 0)
514 _exit (EXIT_FAILURE);
517 /* Like the 'error' function but handle any pending newline. */
519 ATTRIBUTE_FORMAT ((__printf__, 3, 4))
520 static void
521 nl_error (int status, int errnum, char const *fmt, ...)
523 if (0 < progress_len)
525 fputc ('\n', stderr);
526 progress_len = 0;
529 va_list ap;
530 va_start (ap, fmt);
531 verror (status, errnum, fmt, ap);
532 va_end (ap);
535 #define error nl_error
537 void
538 usage (int status)
540 if (status != EXIT_SUCCESS)
541 emit_try_help ();
542 else
544 printf (_("\
545 Usage: %s [OPERAND]...\n\
546 or: %s OPTION\n\
548 program_name, program_name);
549 fputs (_("\
550 Copy a file, converting and formatting according to the operands.\n\
552 bs=BYTES read and write up to BYTES bytes at a time (default: 512);\n\
553 overrides ibs and obs\n\
554 cbs=BYTES convert BYTES bytes at a time\n\
555 conv=CONVS convert the file as per the comma separated symbol list\n\
556 count=N copy only N input blocks\n\
557 ibs=BYTES read up to BYTES bytes at a time (default: 512)\n\
558 "), stdout);
559 fputs (_("\
560 if=FILE read from FILE instead of stdin\n\
561 iflag=FLAGS read as per the comma separated symbol list\n\
562 obs=BYTES write BYTES bytes at a time (default: 512)\n\
563 of=FILE write to FILE instead of stdout\n\
564 oflag=FLAGS write as per the comma separated symbol list\n\
565 seek=N (or oseek=N) skip N obs-sized output blocks\n\
566 skip=N (or iseek=N) skip N ibs-sized input blocks\n\
567 status=LEVEL The LEVEL of information to print to stderr;\n\
568 'none' suppresses everything but error messages,\n\
569 'noxfer' suppresses the final transfer statistics,\n\
570 'progress' shows periodic transfer statistics\n\
571 "), stdout);
572 fputs (_("\
574 N and BYTES may be followed by the following multiplicative suffixes:\n\
575 c=1, w=2, b=512, kB=1000, K=1024, MB=1000*1000, M=1024*1024, xM=M,\n\
576 GB=1000*1000*1000, G=1024*1024*1024, and so on for T, P, E, Z, Y, R, Q.\n\
577 Binary prefixes can be used, too: KiB=K, MiB=M, and so on.\n\
578 If N ends in 'B', it counts bytes not blocks.\n\
580 Each CONV symbol may be:\n\
582 "), stdout);
583 fputs (_("\
584 ascii from EBCDIC to ASCII\n\
585 ebcdic from ASCII to EBCDIC\n\
586 ibm from ASCII to alternate EBCDIC\n\
587 block pad newline-terminated records with spaces to cbs-size\n\
588 unblock replace trailing spaces in cbs-size records with newline\n\
589 lcase change upper case to lower case\n\
590 ucase change lower case to upper case\n\
591 sparse try to seek rather than write all-NUL output blocks\n\
592 swab swap every pair of input bytes\n\
593 sync pad every input block with NULs to ibs-size; when used\n\
594 with block or unblock, pad with spaces rather than NULs\n\
595 "), stdout);
596 fputs (_("\
597 excl fail if the output file already exists\n\
598 nocreat do not create the output file\n\
599 notrunc do not truncate the output file\n\
600 noerror continue after read errors\n\
601 fdatasync physically write output file data before finishing\n\
602 fsync likewise, but also write metadata\n\
603 "), stdout);
604 fputs (_("\
606 Each FLAG symbol may be:\n\
608 append append mode (makes sense only for output; conv=notrunc suggested)\n\
609 "), stdout);
610 if (O_CIO)
611 fputs (_(" cio use concurrent I/O for data\n"), stdout);
612 if (O_DIRECT)
613 fputs (_(" direct use direct I/O for data\n"), stdout);
614 if (O_DIRECTORY)
615 fputs (_(" directory fail unless a directory\n"), stdout);
616 if (O_DSYNC)
617 fputs (_(" dsync use synchronized I/O for data\n"), stdout);
618 if (O_SYNC)
619 fputs (_(" sync likewise, but also for metadata\n"), stdout);
620 fputs (_(" fullblock accumulate full blocks of input (iflag only)\n"),
621 stdout);
622 if (O_NONBLOCK)
623 fputs (_(" nonblock use non-blocking I/O\n"), stdout);
624 if (O_NOATIME)
625 fputs (_(" noatime do not update access time\n"), stdout);
626 #if HAVE_POSIX_FADVISE
627 if (O_NOCACHE)
628 fputs (_(" nocache Request to drop cache. See also oflag=sync\n"),
629 stdout);
630 #endif
631 if (O_NOCTTY)
632 fputs (_(" noctty do not assign controlling terminal from file\n"),
633 stdout);
634 if (HAVE_WORKING_O_NOFOLLOW)
635 fputs (_(" nofollow do not follow symlinks\n"), stdout);
636 if (O_NOLINKS)
637 fputs (_(" nolinks fail if multiply-linked\n"), stdout);
638 if (O_BINARY)
639 fputs (_(" binary use binary I/O for data\n"), stdout);
640 if (O_TEXT)
641 fputs (_(" text use text I/O for data\n"), stdout);
644 printf (_("\
646 Sending a %s signal to a running 'dd' process makes it\n\
647 print I/O statistics to standard error and then resume copying.\n\
649 Options are:\n\
651 "), SIGINFO == SIGUSR1 ? "USR1" : "INFO");
654 fputs (HELP_OPTION_DESCRIPTION, stdout);
655 fputs (VERSION_OPTION_DESCRIPTION, stdout);
656 emit_ancillary_info (PROGRAM_NAME);
658 exit (status);
661 /* Common options to use when displaying sizes and rates. */
663 enum { human_opts = (human_autoscale | human_round_to_nearest
664 | human_space_before_unit | human_SI | human_B) };
666 /* Ensure input buffer IBUF is allocated. */
668 static void
669 alloc_ibuf (void)
671 if (ibuf)
672 return;
674 bool extra_byte_for_swab = !!(conversions_mask & C_SWAB);
675 ibuf = alignalloc (page_size, input_blocksize + extra_byte_for_swab);
676 if (!ibuf)
678 char hbuf[LONGEST_HUMAN_READABLE + 1];
679 die (EXIT_FAILURE, 0,
680 _("memory exhausted by input buffer of size %td bytes (%s)"),
681 input_blocksize,
682 human_readable (input_blocksize, hbuf,
683 human_opts | human_base_1024, 1, 1));
687 /* Ensure output buffer OBUF is allocated/initialized. */
689 static void
690 alloc_obuf (void)
692 if (obuf)
693 return;
695 if (conversions_mask & C_TWOBUFS)
697 obuf = alignalloc (page_size, output_blocksize);
698 if (!obuf)
700 char hbuf[LONGEST_HUMAN_READABLE + 1];
701 die (EXIT_FAILURE, 0,
702 _("memory exhausted by output buffer of size %td"
703 " bytes (%s)"),
704 output_blocksize,
705 human_readable (output_blocksize, hbuf,
706 human_opts | human_base_1024, 1, 1));
709 else
711 alloc_ibuf ();
712 obuf = ibuf;
716 static void
717 translate_charset (char const *new_trans)
719 for (int i = 0; i < 256; i++)
720 trans_table[i] = new_trans[trans_table[i]];
721 translation_needed = true;
724 /* Return true if I has more than one bit set. I must be nonnegative. */
726 static inline bool
727 multiple_bits_set (int i)
729 return MULTIPLE_BITS_SET (i);
732 static bool
733 abbreviation_lacks_prefix (char const *message)
735 return message[strlen (message) - 2] == ' ';
738 /* Print transfer statistics. */
740 static void
741 print_xfer_stats (xtime_t progress_time)
743 xtime_t now = progress_time ? progress_time : gethrxtime ();
744 static char const slash_s[] = "/s";
745 char hbuf[3][LONGEST_HUMAN_READABLE + sizeof slash_s];
746 double delta_s;
747 char const *bytes_per_second;
748 char const *si = human_readable (w_bytes, hbuf[0], human_opts, 1, 1);
749 char const *iec = human_readable (w_bytes, hbuf[1],
750 human_opts | human_base_1024, 1, 1);
752 /* Use integer arithmetic to compute the transfer rate,
753 since that makes it easy to use SI abbreviations. */
754 char *bpsbuf = hbuf[2];
755 int bpsbufsize = sizeof hbuf[2];
756 if (start_time < now)
758 double XTIME_PRECISIONe0 = XTIME_PRECISION;
759 xtime_t delta_xtime = now - start_time;
760 delta_s = delta_xtime / XTIME_PRECISIONe0;
761 bytes_per_second = human_readable (w_bytes, bpsbuf, human_opts,
762 XTIME_PRECISION, delta_xtime);
763 strcat (bytes_per_second - bpsbuf + bpsbuf, slash_s);
765 else
767 delta_s = 0;
768 snprintf (bpsbuf, bpsbufsize, "%s B/s", _("Infinity"));
769 bytes_per_second = bpsbuf;
772 if (progress_time)
773 fputc ('\r', stderr);
775 /* Use full seconds when printing progress, since the progress
776 report is output once per second and there is little point
777 displaying any subsecond jitter. Use default precision with %g
778 otherwise, as this provides more-useful output then. With long
779 transfers %g can generate a number with an exponent; that is OK. */
780 char delta_s_buf[24];
781 snprintf (delta_s_buf, sizeof delta_s_buf,
782 progress_time ? "%.0f s" : "%g s", delta_s);
784 int stats_len
785 = (abbreviation_lacks_prefix (si)
786 ? fprintf (stderr,
787 ngettext ("%"PRIdMAX" byte copied, %s, %s",
788 "%"PRIdMAX" bytes copied, %s, %s",
789 select_plural (w_bytes)),
790 w_bytes, delta_s_buf, bytes_per_second)
791 : abbreviation_lacks_prefix (iec)
792 ? fprintf (stderr,
793 _("%"PRIdMAX" bytes (%s) copied, %s, %s"),
794 w_bytes, si, delta_s_buf, bytes_per_second)
795 : fprintf (stderr,
796 _("%"PRIdMAX" bytes (%s, %s) copied, %s, %s"),
797 w_bytes, si, iec, delta_s_buf, bytes_per_second));
799 if (progress_time)
801 /* Erase any trailing junk on the output line by outputting
802 spaces. In theory this could glitch the display because the
803 formatted translation of a line describing a larger file
804 could consume fewer screen columns than the strlen difference
805 from the previously formatted translation. In practice this
806 does not seem to be a problem. */
807 if (0 <= stats_len && stats_len < progress_len)
808 fprintf (stderr, "%*s", progress_len - stats_len, "");
809 progress_len = stats_len;
811 else
812 fputc ('\n', stderr);
814 reported_w_bytes = w_bytes;
817 static void
818 print_stats (void)
820 if (status_level == STATUS_NONE)
821 return;
823 if (0 < progress_len)
825 fputc ('\n', stderr);
826 progress_len = 0;
829 fprintf (stderr,
830 _("%"PRIdMAX"+%"PRIdMAX" records in\n"
831 "%"PRIdMAX"+%"PRIdMAX" records out\n"),
832 r_full, r_partial, w_full, w_partial);
834 if (r_truncate != 0)
835 fprintf (stderr,
836 ngettext ("%"PRIdMAX" truncated record\n",
837 "%"PRIdMAX" truncated records\n",
838 select_plural (r_truncate)),
839 r_truncate);
841 if (status_level == STATUS_NOXFER)
842 return;
844 print_xfer_stats (0);
847 /* An ordinary signal was received; arrange for the program to exit. */
849 static void
850 interrupt_handler (int sig)
852 if (! SA_RESETHAND)
853 signal (sig, SIG_DFL);
854 interrupt_signal = sig;
857 /* An info signal was received; arrange for the program to print status. */
859 static void
860 siginfo_handler (int sig)
862 if (! SA_NOCLDSTOP)
863 signal (sig, siginfo_handler);
864 info_signal_count++;
867 /* Install the signal handlers. */
869 static void
870 install_signal_handlers (void)
872 bool catch_siginfo = ! (SIGINFO == SIGUSR1 && getenv ("POSIXLY_CORRECT"));
874 #if SA_NOCLDSTOP
876 struct sigaction act;
877 sigemptyset (&caught_signals);
878 if (catch_siginfo)
879 sigaddset (&caught_signals, SIGINFO);
880 sigaction (SIGINT, NULL, &act);
881 if (act.sa_handler != SIG_IGN)
882 sigaddset (&caught_signals, SIGINT);
883 act.sa_mask = caught_signals;
885 if (sigismember (&caught_signals, SIGINFO))
887 act.sa_handler = siginfo_handler;
888 /* Note we don't use SA_RESTART here and instead
889 handle EINTR explicitly in iftruncate etc.
890 to avoid blocking on noncommitted read/write calls. */
891 act.sa_flags = 0;
892 sigaction (SIGINFO, &act, NULL);
895 if (sigismember (&caught_signals, SIGINT))
897 act.sa_handler = interrupt_handler;
898 act.sa_flags = SA_NODEFER | SA_RESETHAND;
899 sigaction (SIGINT, &act, NULL);
902 #else
904 if (catch_siginfo)
906 signal (SIGINFO, siginfo_handler);
907 siginterrupt (SIGINFO, 1);
909 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
911 signal (SIGINT, interrupt_handler);
912 siginterrupt (SIGINT, 1);
914 #endif
917 /* Close FD. Return 0 if successful, -1 (setting errno) otherwise.
918 If close fails with errno == EINTR, POSIX says the file descriptor
919 is in an unspecified state, so keep trying to close FD but do not
920 consider EBADF to be an error. Do not process signals. This all
921 differs somewhat from functions like ifdatasync and ifsync. */
922 static int
923 iclose (int fd)
925 if (close (fd) != 0)
927 if (errno != EINTR)
928 return -1;
929 while (close (fd) != 0 && errno != EBADF);
931 return 0;
934 static int synchronize_output (void);
936 static void
937 cleanup (void)
939 if (!interrupt_signal)
941 int sync_status = synchronize_output ();
942 if (sync_status)
943 exit (sync_status);
946 if (iclose (STDIN_FILENO) != 0)
947 die (EXIT_FAILURE, errno, _("closing input file %s"), quoteaf (input_file));
949 /* Don't remove this call to close, even though close_stdout
950 closes standard output. This close is necessary when cleanup
951 is called as a consequence of signal handling. */
952 if (iclose (STDOUT_FILENO) != 0)
953 die (EXIT_FAILURE, errno,
954 _("closing output file %s"), quoteaf (output_file));
957 /* Process any pending signals. If signals are caught, this function
958 should be called periodically. Ideally there should never be an
959 unbounded amount of time when signals are not being processed. */
961 static void
962 process_signals (void)
964 while (interrupt_signal || info_signal_count)
966 int interrupt;
967 int infos;
968 sigset_t oldset;
970 sigprocmask (SIG_BLOCK, &caught_signals, &oldset);
972 /* Reload interrupt_signal and info_signal_count, in case a new
973 signal was handled before sigprocmask took effect. */
974 interrupt = interrupt_signal;
975 infos = info_signal_count;
977 if (infos)
978 info_signal_count = infos - 1;
980 sigprocmask (SIG_SETMASK, &oldset, NULL);
982 if (interrupt)
983 cleanup ();
984 print_stats ();
985 if (interrupt)
986 raise (interrupt);
990 static void
991 finish_up (void)
993 /* Process signals first, so that cleanup is called at most once. */
994 process_signals ();
995 cleanup ();
996 print_stats ();
999 static void
1000 quit (int code)
1002 finish_up ();
1003 exit (code);
1006 /* Return LEN rounded down to a multiple of IO_BUFSIZE
1007 (to minimize calls to the expensive posix_fadvise (,POSIX_FADV_DONTNEED),
1008 while storing the remainder internally per FD.
1009 Pass LEN == 0 to get the current remainder. */
1011 static off_t
1012 cache_round (int fd, off_t len)
1014 static off_t i_pending, o_pending;
1015 off_t *pending = (fd == STDIN_FILENO ? &i_pending : &o_pending);
1017 if (len)
1019 intmax_t c_pending;
1020 if (INT_ADD_WRAPV (*pending, len, &c_pending))
1021 c_pending = INTMAX_MAX;
1022 *pending = c_pending % IO_BUFSIZE;
1023 if (c_pending > *pending)
1024 len = c_pending - *pending;
1025 else
1026 len = 0;
1028 else
1029 len = *pending;
1031 return len;
1034 /* Discard the cache from the current offset of either
1035 STDIN_FILENO or STDOUT_FILENO.
1036 Return true on success. */
1038 static bool
1039 invalidate_cache (int fd, off_t len)
1041 int adv_ret = -1;
1042 off_t offset;
1043 bool nocache_eof = (fd == STDIN_FILENO ? i_nocache_eof : o_nocache_eof);
1045 /* Minimize syscalls. */
1046 off_t clen = cache_round (fd, len);
1047 if (len && !clen)
1048 return true; /* Don't advise this time. */
1049 else if (! len && ! clen && ! nocache_eof)
1050 return true;
1051 off_t pending = len ? cache_round (fd, 0) : 0;
1053 if (fd == STDIN_FILENO)
1055 if (input_seekable)
1056 offset = input_offset;
1057 else
1059 offset = -1;
1060 errno = ESPIPE;
1063 else
1065 static off_t output_offset = -2;
1067 if (output_offset != -1)
1069 if (output_offset < 0)
1070 output_offset = lseek (fd, 0, SEEK_CUR);
1071 else if (len)
1072 output_offset += clen + pending;
1075 offset = output_offset;
1078 if (0 <= offset)
1080 if (! len && clen && nocache_eof)
1082 pending = clen;
1083 clen = 0;
1086 /* Note we're being careful here to only invalidate what
1087 we've read, so as not to dump any read ahead cache.
1088 Note also the kernel is conservative and only invalidates
1089 full pages in the specified range. */
1090 #if HAVE_POSIX_FADVISE
1091 offset = offset - clen - pending;
1092 /* ensure full page specified when invalidating to eof. */
1093 if (clen == 0)
1094 offset -= offset % page_size;
1095 adv_ret = posix_fadvise (fd, offset, clen, POSIX_FADV_DONTNEED);
1096 #else
1097 errno = ENOTSUP;
1098 #endif
1101 return adv_ret != -1 ? true : false;
1104 /* Read from FD into the buffer BUF of size SIZE, processing any
1105 signals that arrive before bytes are read. Return the number of
1106 bytes read if successful, -1 (setting errno) on failure. */
1108 static ssize_t
1109 iread (int fd, char *buf, idx_t size)
1111 ssize_t nread;
1112 static ssize_t prev_nread;
1116 process_signals ();
1117 nread = read (fd, buf, size);
1118 /* Ignore final read error with iflag=direct as that
1119 returns EINVAL due to the non aligned file offset. */
1120 if (nread == -1 && errno == EINVAL
1121 && 0 < prev_nread && prev_nread < size
1122 && (input_flags & O_DIRECT))
1124 errno = 0;
1125 nread = 0;
1128 while (nread < 0 && errno == EINTR);
1130 /* Short read may be due to received signal. */
1131 if (0 < nread && nread < size)
1132 process_signals ();
1134 if (0 < nread && warn_partial_read)
1136 if (0 < prev_nread && prev_nread < size)
1138 idx_t prev = prev_nread;
1139 if (status_level != STATUS_NONE)
1140 error (0, 0, ngettext (("warning: partial read (%td byte); "
1141 "suggest iflag=fullblock"),
1142 ("warning: partial read (%td bytes); "
1143 "suggest iflag=fullblock"),
1144 select_plural (prev)),
1145 prev);
1146 warn_partial_read = false;
1150 prev_nread = nread;
1151 return nread;
1154 /* Wrapper around iread function to accumulate full blocks. */
1155 static ssize_t
1156 iread_fullblock (int fd, char *buf, idx_t size)
1158 ssize_t nread = 0;
1160 while (0 < size)
1162 ssize_t ncurr = iread (fd, buf, size);
1163 if (ncurr < 0)
1164 return ncurr;
1165 if (ncurr == 0)
1166 break;
1167 nread += ncurr;
1168 buf += ncurr;
1169 size -= ncurr;
1172 return nread;
1175 /* Write to FD the buffer BUF of size SIZE, processing any signals
1176 that arrive. Return the number of bytes written, setting errno if
1177 this is less than SIZE. Keep trying if there are partial
1178 writes. */
1180 static idx_t
1181 iwrite (int fd, char const *buf, idx_t size)
1183 idx_t total_written = 0;
1185 if ((output_flags & O_DIRECT) && size < output_blocksize)
1187 int old_flags = fcntl (STDOUT_FILENO, F_GETFL);
1188 if (fcntl (STDOUT_FILENO, F_SETFL, old_flags & ~O_DIRECT) != 0
1189 && status_level != STATUS_NONE)
1190 error (0, errno, _("failed to turn off O_DIRECT: %s"),
1191 quotef (output_file));
1193 /* Since we have just turned off O_DIRECT for the final write,
1194 we try to preserve some of its semantics. */
1196 /* Call invalidate_cache to setup the appropriate offsets
1197 for subsequent calls. */
1198 o_nocache_eof = true;
1199 invalidate_cache (STDOUT_FILENO, 0);
1201 /* Attempt to ensure that that final block is committed
1202 to stable storage as quickly as possible. */
1203 conversions_mask |= C_FSYNC;
1205 /* After the subsequent fsync we'll call invalidate_cache
1206 to attempt to clear all data from the page cache. */
1209 while (total_written < size)
1211 ssize_t nwritten = 0;
1212 process_signals ();
1214 /* Perform a seek for a NUL block if sparse output is enabled. */
1215 final_op_was_seek = false;
1216 if ((conversions_mask & C_SPARSE) && is_nul (buf, size))
1218 if (lseek (fd, size, SEEK_CUR) < 0)
1220 conversions_mask &= ~C_SPARSE;
1221 /* Don't warn about the advisory sparse request. */
1223 else
1225 final_op_was_seek = true;
1226 nwritten = size;
1230 if (!nwritten)
1231 nwritten = write (fd, buf + total_written, size - total_written);
1233 if (nwritten < 0)
1235 if (errno != EINTR)
1236 break;
1238 else if (nwritten == 0)
1240 /* Some buggy drivers return 0 when one tries to write beyond
1241 a device's end. (Example: Linux kernel 1.2.13 on /dev/fd0.)
1242 Set errno to ENOSPC so they get a sensible diagnostic. */
1243 errno = ENOSPC;
1244 break;
1246 else
1247 total_written += nwritten;
1250 if (o_nocache && total_written)
1251 invalidate_cache (fd, total_written);
1253 return total_written;
1256 /* Write, then empty, the output buffer 'obuf'. */
1258 static void
1259 write_output (void)
1261 idx_t nwritten = iwrite (STDOUT_FILENO, obuf, output_blocksize);
1262 w_bytes += nwritten;
1263 if (nwritten != output_blocksize)
1265 error (0, errno, _("writing to %s"), quoteaf (output_file));
1266 if (nwritten != 0)
1267 w_partial++;
1268 quit (EXIT_FAILURE);
1270 else
1271 w_full++;
1272 oc = 0;
1275 /* Restart on EINTR from fdatasync. */
1277 static int
1278 ifdatasync (int fd)
1280 int ret;
1284 process_signals ();
1285 ret = fdatasync (fd);
1287 while (ret < 0 && errno == EINTR);
1289 return ret;
1292 /* Restart on EINTR from fd_reopen. */
1294 static int
1295 ifd_reopen (int desired_fd, char const *file, int flag, mode_t mode)
1297 int ret;
1301 process_signals ();
1302 ret = fd_reopen (desired_fd, file, flag, mode);
1304 while (ret < 0 && errno == EINTR);
1306 return ret;
1309 /* Restart on EINTR from fstat. */
1311 static int
1312 ifstat (int fd, struct stat *st)
1314 int ret;
1318 process_signals ();
1319 ret = fstat (fd, st);
1321 while (ret < 0 && errno == EINTR);
1323 return ret;
1326 /* Restart on EINTR from fsync. */
1328 static int
1329 ifsync (int fd)
1331 int ret;
1335 process_signals ();
1336 ret = fsync (fd);
1338 while (ret < 0 && errno == EINTR);
1340 return ret;
1343 /* Restart on EINTR from ftruncate. */
1345 static int
1346 iftruncate (int fd, off_t length)
1348 int ret;
1352 process_signals ();
1353 ret = ftruncate (fd, length);
1355 while (ret < 0 && errno == EINTR);
1357 return ret;
1360 /* Return true if STR is of the form "PATTERN" or "PATTERNDELIM...". */
1362 ATTRIBUTE_PURE
1363 static bool
1364 operand_matches (char const *str, char const *pattern, char delim)
1366 while (*pattern)
1367 if (*str++ != *pattern++)
1368 return false;
1369 return !*str || *str == delim;
1372 /* Interpret one "conv=..." or similar operand STR according to the
1373 symbols in TABLE, returning the flags specified. If the operand
1374 cannot be parsed, use ERROR_MSGID to generate a diagnostic. */
1376 static int
1377 parse_symbols (char const *str, struct symbol_value const *table,
1378 bool exclusive, char const *error_msgid)
1380 int value = 0;
1382 while (true)
1384 char const *strcomma = strchr (str, ',');
1385 struct symbol_value const *entry;
1387 for (entry = table;
1388 ! (operand_matches (str, entry->symbol, ',') && entry->value);
1389 entry++)
1391 if (! entry->symbol[0])
1393 idx_t slen = strcomma ? strcomma - str : strlen (str);
1394 error (0, 0, "%s: %s", _(error_msgid),
1395 quotearg_n_style_mem (0, locale_quoting_style, str, slen));
1396 usage (EXIT_FAILURE);
1400 if (exclusive)
1401 value = entry->value;
1402 else
1403 value |= entry->value;
1404 if (!strcomma)
1405 break;
1406 str = strcomma + 1;
1409 return value;
1412 /* Return the value of STR, interpreted as a non-negative decimal integer,
1413 optionally multiplied by various values.
1414 Set *INVALID to an appropriate error value and return INTMAX_MAX if
1415 it is an overflow, an indeterminate value if some other error occurred. */
1417 static intmax_t
1418 parse_integer (char const *str, strtol_error *invalid)
1420 /* Call xstrtoumax, not xstrtoimax, since we don't want to
1421 allow strings like " -0". Initialize N to an interminate value;
1422 calling code should not rely on this function returning 0
1423 when *INVALID represents a non-overflow error. */
1424 int indeterminate = 0;
1425 uintmax_t n = indeterminate;
1426 char *suffix;
1427 static char const suffixes[] = "bcEGkKMPQRTwYZ0";
1428 strtol_error e = xstrtoumax (str, &suffix, 10, &n, suffixes);
1429 intmax_t result;
1431 if ((e & ~LONGINT_OVERFLOW) == LONGINT_INVALID_SUFFIX_CHAR
1432 && suffix[-1] != 'B' && *suffix == 'B')
1434 suffix++;
1435 if (!*suffix)
1436 e &= ~LONGINT_INVALID_SUFFIX_CHAR;
1439 if ((e & ~LONGINT_OVERFLOW) == LONGINT_INVALID_SUFFIX_CHAR
1440 && *suffix == 'x' && ! (suffix[-1] == 'B' && strchr (suffix + 1, 'B')))
1442 uintmax_t o;
1443 strtol_error f = xstrtoumax (suffix + 1, &suffix, 10, &o, suffixes);
1444 if ((f & ~LONGINT_OVERFLOW) != LONGINT_OK)
1446 e = f;
1447 result = indeterminate;
1449 else if (INT_MULTIPLY_WRAPV (n, o, &result)
1450 || (result != 0 && ((e | f) & LONGINT_OVERFLOW)))
1452 e = LONGINT_OVERFLOW;
1453 result = INTMAX_MAX;
1455 else
1457 if (result == 0 && STRPREFIX (str, "0x"))
1458 error (0, 0,
1459 _("warning: %s is a zero multiplier; "
1460 "use %s if that is intended"),
1461 quote_n (0, "0x"), quote_n (1, "00x"));
1462 e = LONGINT_OK;
1465 else if (n <= INTMAX_MAX)
1466 result = n;
1467 else
1469 e = LONGINT_OVERFLOW;
1470 result = INTMAX_MAX;
1473 *invalid = e;
1474 return result;
1477 /* OPERAND is of the form "X=...". Return true if X is NAME. */
1479 ATTRIBUTE_PURE
1480 static bool
1481 operand_is (char const *operand, char const *name)
1483 return operand_matches (operand, name, '=');
1486 static void
1487 scanargs (int argc, char *const *argv)
1489 idx_t blocksize = 0;
1490 intmax_t count = INTMAX_MAX;
1491 intmax_t skip = 0;
1492 intmax_t seek = 0;
1493 bool count_B = false, skip_B = false, seek_B = false;
1495 for (int i = optind; i < argc; i++)
1497 char const *name = argv[i];
1498 char const *val = strchr (name, '=');
1500 if (val == NULL)
1502 error (0, 0, _("unrecognized operand %s"),
1503 quote (name));
1504 usage (EXIT_FAILURE);
1506 val++;
1508 if (operand_is (name, "if"))
1509 input_file = val;
1510 else if (operand_is (name, "of"))
1511 output_file = val;
1512 else if (operand_is (name, "conv"))
1513 conversions_mask |= parse_symbols (val, conversions, false,
1514 N_("invalid conversion"));
1515 else if (operand_is (name, "iflag"))
1516 input_flags |= parse_symbols (val, flags, false,
1517 N_("invalid input flag"));
1518 else if (operand_is (name, "oflag"))
1519 output_flags |= parse_symbols (val, flags, false,
1520 N_("invalid output flag"));
1521 else if (operand_is (name, "status"))
1522 status_level = parse_symbols (val, statuses, true,
1523 N_("invalid status level"));
1524 else
1526 strtol_error invalid = LONGINT_OK;
1527 intmax_t n = parse_integer (val, &invalid);
1528 bool has_B = !!strchr (val, 'B');
1529 intmax_t n_min = 0;
1530 intmax_t n_max = INTMAX_MAX;
1531 idx_t *converted_idx = NULL;
1533 /* Maximum blocksize. Keep it smaller than IDX_MAX, so that
1534 it fits into blocksize vars even if 1 is added for conv=swab.
1535 Do not exceed SSIZE_MAX, for the benefit of system calls
1536 like "read". And do not exceed OFF_T_MAX, for the
1537 benefit of the large-offset seek code. */
1538 idx_t max_blocksize = MIN (IDX_MAX - 1, MIN (SSIZE_MAX, OFF_T_MAX));
1540 if (operand_is (name, "ibs"))
1542 n_min = 1;
1543 n_max = max_blocksize;
1544 converted_idx = &input_blocksize;
1546 else if (operand_is (name, "obs"))
1548 n_min = 1;
1549 n_max = max_blocksize;
1550 converted_idx = &output_blocksize;
1552 else if (operand_is (name, "bs"))
1554 n_min = 1;
1555 n_max = max_blocksize;
1556 converted_idx = &blocksize;
1558 else if (operand_is (name, "cbs"))
1560 n_min = 1;
1561 n_max = MIN (SIZE_MAX, IDX_MAX);
1562 converted_idx = &conversion_blocksize;
1564 else if (operand_is (name, "skip") || operand_is (name, "iseek"))
1566 skip = n;
1567 skip_B = has_B;
1569 else if (operand_is (name + (*name == 'o'), "seek"))
1571 seek = n;
1572 seek_B = has_B;
1574 else if (operand_is (name, "count"))
1576 count = n;
1577 count_B = has_B;
1579 else
1581 error (0, 0, _("unrecognized operand %s"),
1582 quote (name));
1583 usage (EXIT_FAILURE);
1586 if (n < n_min)
1587 invalid = LONGINT_INVALID;
1588 else if (n_max < n)
1589 invalid = LONGINT_OVERFLOW;
1591 if (invalid != LONGINT_OK)
1592 die (EXIT_FAILURE, invalid == LONGINT_OVERFLOW ? EOVERFLOW : 0,
1593 "%s: %s", _("invalid number"), quote (val));
1594 else if (converted_idx)
1595 *converted_idx = n;
1599 if (blocksize)
1600 input_blocksize = output_blocksize = blocksize;
1601 else
1603 /* POSIX says dd aggregates partial reads into
1604 output_blocksize if bs= is not specified. */
1605 conversions_mask |= C_TWOBUFS;
1608 if (input_blocksize == 0)
1609 input_blocksize = DEFAULT_BLOCKSIZE;
1610 if (output_blocksize == 0)
1611 output_blocksize = DEFAULT_BLOCKSIZE;
1612 if (conversion_blocksize == 0)
1613 conversions_mask &= ~(C_BLOCK | C_UNBLOCK);
1615 if (input_flags & (O_DSYNC | O_SYNC))
1616 input_flags |= O_RSYNC;
1618 if (output_flags & O_FULLBLOCK)
1620 error (0, 0, "%s: %s", _("invalid output flag"), quote ("fullblock"));
1621 usage (EXIT_FAILURE);
1624 if (skip_B)
1625 input_flags |= O_SKIP_BYTES;
1626 if (input_flags & O_SKIP_BYTES && skip != 0)
1628 skip_records = skip / input_blocksize;
1629 skip_bytes = skip % input_blocksize;
1631 else if (skip != 0)
1632 skip_records = skip;
1634 if (count_B)
1635 input_flags |= O_COUNT_BYTES;
1636 if (input_flags & O_COUNT_BYTES && count != INTMAX_MAX)
1638 max_records = count / input_blocksize;
1639 max_bytes = count % input_blocksize;
1641 else if (count != INTMAX_MAX)
1642 max_records = count;
1644 if (seek_B)
1645 output_flags |= O_SEEK_BYTES;
1646 if (output_flags & O_SEEK_BYTES && seek != 0)
1648 seek_records = seek / output_blocksize;
1649 seek_bytes = seek % output_blocksize;
1651 else if (seek != 0)
1652 seek_records = seek;
1654 /* Warn about partial reads if bs=SIZE is given and iflag=fullblock
1655 is not, and if counting or skipping bytes or using direct I/O.
1656 This helps to avoid confusion with miscounts, and to avoid issues
1657 with direct I/O on GNU/Linux. */
1658 warn_partial_read =
1659 (! (conversions_mask & C_TWOBUFS) && ! (input_flags & O_FULLBLOCK)
1660 && (skip_records
1661 || (0 < max_records && max_records < INTMAX_MAX)
1662 || (input_flags | output_flags) & O_DIRECT));
1664 iread_fnc = ((input_flags & O_FULLBLOCK)
1665 ? iread_fullblock
1666 : iread);
1667 input_flags &= ~O_FULLBLOCK;
1669 if (multiple_bits_set (conversions_mask & (C_ASCII | C_EBCDIC | C_IBM)))
1670 die (EXIT_FAILURE, 0, _("cannot combine any two of {ascii,ebcdic,ibm}"));
1671 if (multiple_bits_set (conversions_mask & (C_BLOCK | C_UNBLOCK)))
1672 die (EXIT_FAILURE, 0, _("cannot combine block and unblock"));
1673 if (multiple_bits_set (conversions_mask & (C_LCASE | C_UCASE)))
1674 die (EXIT_FAILURE, 0, _("cannot combine lcase and ucase"));
1675 if (multiple_bits_set (conversions_mask & (C_EXCL | C_NOCREAT)))
1676 die (EXIT_FAILURE, 0, _("cannot combine excl and nocreat"));
1677 if (multiple_bits_set (input_flags & (O_DIRECT | O_NOCACHE))
1678 || multiple_bits_set (output_flags & (O_DIRECT | O_NOCACHE)))
1679 die (EXIT_FAILURE, 0, _("cannot combine direct and nocache"));
1681 if (input_flags & O_NOCACHE)
1683 i_nocache = true;
1684 i_nocache_eof = (max_records == 0 && max_bytes == 0);
1685 input_flags &= ~O_NOCACHE;
1687 if (output_flags & O_NOCACHE)
1689 o_nocache = true;
1690 o_nocache_eof = (max_records == 0 && max_bytes == 0);
1691 output_flags &= ~O_NOCACHE;
1695 /* Fix up translation table. */
1697 static void
1698 apply_translations (void)
1700 int i;
1702 if (conversions_mask & C_ASCII)
1703 translate_charset (ebcdic_to_ascii);
1705 if (conversions_mask & C_UCASE)
1707 for (i = 0; i < 256; i++)
1708 trans_table[i] = toupper (trans_table[i]);
1709 translation_needed = true;
1711 else if (conversions_mask & C_LCASE)
1713 for (i = 0; i < 256; i++)
1714 trans_table[i] = tolower (trans_table[i]);
1715 translation_needed = true;
1718 if (conversions_mask & C_EBCDIC)
1720 translate_charset (ascii_to_ebcdic);
1721 newline_character = ascii_to_ebcdic['\n'];
1722 space_character = ascii_to_ebcdic[' '];
1724 else if (conversions_mask & C_IBM)
1726 translate_charset (ascii_to_ibm);
1727 newline_character = ascii_to_ibm['\n'];
1728 space_character = ascii_to_ibm[' '];
1732 /* Apply the character-set translations specified by the user
1733 to the NREAD bytes in BUF. */
1735 static void
1736 translate_buffer (char *buf, idx_t nread)
1738 idx_t i;
1739 char *cp;
1740 for (i = nread, cp = buf; i; i--, cp++)
1741 *cp = trans_table[to_uchar (*cp)];
1744 /* Swap *NREAD bytes in BUF, which should have room for an extra byte
1745 after the end because the swapping is not in-place. If *SAVED_BYTE
1746 is nonnegative, also swap that initial byte from the previous call.
1747 Save the last byte into into *SAVED_BYTE if needed to make the
1748 resulting *NREAD even, and set *SAVED_BYTE to -1 otherwise.
1749 Return the buffer's adjusted start, either BUF or BUF + 1. */
1751 static char *
1752 swab_buffer (char *buf, idx_t *nread, int *saved_byte)
1754 if (*nread == 0)
1755 return buf;
1757 /* Update *SAVED_BYTE, and set PREV_SAVED to its old value. */
1758 int prev_saved = *saved_byte;
1759 if ((prev_saved < 0) == (*nread & 1))
1761 unsigned char c = buf[--*nread];
1762 *saved_byte = c;
1764 else
1765 *saved_byte = -1;
1767 /* Do the byte-swapping by moving every other byte two
1768 positions toward the end, working from the end of the buffer
1769 toward the beginning. This way we move only half the data. */
1770 for (idx_t i = *nread; 1 < i; i -= 2)
1771 buf[i] = buf[i - 2];
1773 if (prev_saved < 0)
1774 return buf + 1;
1776 buf[1] = prev_saved;
1777 ++*nread;
1778 return buf;
1781 /* Add OFFSET to the input offset, setting the overflow flag if
1782 necessary. */
1784 static void
1785 advance_input_offset (intmax_t offset)
1787 if (0 <= input_offset && INT_ADD_WRAPV (input_offset, offset, &input_offset))
1788 input_offset = -1;
1791 /* Throw away RECORDS blocks of BLOCKSIZE bytes plus BYTES bytes on
1792 file descriptor FDESC, which is open with read permission for FILE.
1793 Store up to BLOCKSIZE bytes of the data at a time in IBUF or OBUF, if
1794 necessary. RECORDS or BYTES must be nonzero. If FDESC is
1795 STDIN_FILENO, advance the input offset. Return the number of
1796 records remaining, i.e., that were not skipped because EOF was
1797 reached. If FDESC is STDOUT_FILENO, on return, BYTES is the
1798 remaining bytes in addition to the remaining records. */
1800 static intmax_t
1801 skip (int fdesc, char const *file, intmax_t records, idx_t blocksize,
1802 idx_t *bytes)
1804 /* Try lseek and if an error indicates it was an inappropriate operation --
1805 or if the file offset is not representable as an off_t --
1806 fall back on using read. */
1808 errno = 0;
1809 off_t offset;
1810 if (! INT_MULTIPLY_WRAPV (records, blocksize, &offset)
1811 && ! INT_ADD_WRAPV (offset, *bytes, &offset)
1812 && 0 <= lseek (fdesc, offset, SEEK_CUR))
1814 if (fdesc == STDIN_FILENO)
1816 struct stat st;
1817 if (ifstat (STDIN_FILENO, &st) != 0)
1818 die (EXIT_FAILURE, errno, _("cannot fstat %s"), quoteaf (file));
1819 if (usable_st_size (&st) && 0 <= input_offset
1820 && st.st_size - input_offset < offset)
1822 /* When skipping past EOF, return the number of _full_ blocks
1823 * that are not skipped, and set offset to EOF, so the caller
1824 * can determine the requested skip was not satisfied. */
1825 records = ( offset - st.st_size ) / blocksize;
1826 offset = st.st_size - input_offset;
1828 else
1829 records = 0;
1830 advance_input_offset (offset);
1832 else
1834 records = 0;
1835 *bytes = 0;
1837 return records;
1839 else
1841 int lseek_errno = errno;
1843 /* The seek request may have failed above if it was too big
1844 (> device size, > max file size, etc.)
1845 Or it may not have been done at all (> OFF_T_MAX).
1846 Therefore try to seek to the end of the file,
1847 to avoid redundant reading. */
1848 if (lseek (fdesc, 0, SEEK_END) >= 0)
1850 /* File is seekable, and we're at the end of it, and
1851 size <= OFF_T_MAX. So there's no point using read to advance. */
1853 if (!lseek_errno)
1855 /* The original seek was not attempted as offset > OFF_T_MAX.
1856 We should error for write as can't get to the desired
1857 location, even if OFF_T_MAX < max file size.
1858 For read we're not going to read any data anyway,
1859 so we should error for consistency.
1860 It would be nice to not error for /dev/{zero,null}
1861 for any offset, but that's not a significant issue. */
1862 lseek_errno = EOVERFLOW;
1865 if (fdesc == STDIN_FILENO)
1866 error (0, lseek_errno, _("%s: cannot skip"), quotef (file));
1867 else
1868 error (0, lseek_errno, _("%s: cannot seek"), quotef (file));
1869 /* If the file has a specific size and we've asked
1870 to skip/seek beyond the max allowable, then quit. */
1871 quit (EXIT_FAILURE);
1873 /* else file_size && offset > OFF_T_MAX or file ! seekable */
1875 char *buf;
1876 if (fdesc == STDIN_FILENO)
1878 alloc_ibuf ();
1879 buf = ibuf;
1881 else
1883 alloc_obuf ();
1884 buf = obuf;
1889 ssize_t nread = iread_fnc (fdesc, buf, records ? blocksize : *bytes);
1890 if (nread < 0)
1892 if (fdesc == STDIN_FILENO)
1894 error (0, errno, _("error reading %s"), quoteaf (file));
1895 if (conversions_mask & C_NOERROR)
1896 print_stats ();
1898 else
1899 error (0, lseek_errno, _("%s: cannot seek"), quotef (file));
1900 quit (EXIT_FAILURE);
1902 else if (nread == 0)
1903 break;
1904 else if (fdesc == STDIN_FILENO)
1905 advance_input_offset (nread);
1907 if (records != 0)
1908 records--;
1909 else
1910 *bytes = 0;
1912 while (records || *bytes);
1914 return records;
1918 /* Advance the input by NBYTES if possible, after a read error.
1919 The input file offset may or may not have advanced after the failed
1920 read; adjust it to point just after the bad record regardless.
1921 Return true if successful, or if the input is already known to not
1922 be seekable. */
1924 static bool
1925 advance_input_after_read_error (idx_t nbytes)
1927 if (! input_seekable)
1929 if (input_seek_errno == ESPIPE)
1930 return true;
1931 errno = input_seek_errno;
1933 else
1935 off_t offset;
1936 advance_input_offset (nbytes);
1937 if (input_offset < 0)
1939 error (0, 0, _("offset overflow while reading file %s"),
1940 quoteaf (input_file));
1941 return false;
1943 offset = lseek (STDIN_FILENO, 0, SEEK_CUR);
1944 if (0 <= offset)
1946 off_t diff;
1947 if (offset == input_offset)
1948 return true;
1949 diff = input_offset - offset;
1950 if (! (0 <= diff && diff <= nbytes) && status_level != STATUS_NONE)
1951 error (0, 0, _("warning: invalid file offset after failed read"));
1952 if (0 <= lseek (STDIN_FILENO, diff, SEEK_CUR))
1953 return true;
1954 if (errno == 0)
1955 error (0, 0, _("cannot work around kernel bug after all"));
1959 error (0, errno, _("%s: cannot seek"), quotef (input_file));
1960 return false;
1963 /* Copy NREAD bytes of BUF, with no conversions. */
1965 static void
1966 copy_simple (char const *buf, idx_t nread)
1968 char const *start = buf; /* First uncopied char in BUF. */
1972 idx_t nfree = MIN (nread, output_blocksize - oc);
1974 memcpy (obuf + oc, start, nfree);
1976 nread -= nfree; /* Update the number of bytes left to copy. */
1977 start += nfree;
1978 oc += nfree;
1979 if (oc >= output_blocksize)
1980 write_output ();
1982 while (nread != 0);
1985 /* Copy NREAD bytes of BUF, doing conv=block
1986 (pad newline-terminated records to 'conversion_blocksize',
1987 replacing the newline with trailing spaces). */
1989 static void
1990 copy_with_block (char const *buf, idx_t nread)
1992 for (idx_t i = nread; i; i--, buf++)
1994 if (*buf == newline_character)
1996 if (col < conversion_blocksize)
1998 idx_t j;
1999 for (j = col; j < conversion_blocksize; j++)
2000 output_char (space_character);
2002 col = 0;
2004 else
2006 if (col == conversion_blocksize)
2007 r_truncate++;
2008 else if (col < conversion_blocksize)
2009 output_char (*buf);
2010 col++;
2015 /* Copy NREAD bytes of BUF, doing conv=unblock
2016 (replace trailing spaces in 'conversion_blocksize'-sized records
2017 with a newline). */
2019 static void
2020 copy_with_unblock (char const *buf, idx_t nread)
2022 static idx_t pending_spaces = 0;
2024 for (idx_t i = 0; i < nread; i++)
2026 char c = buf[i];
2028 if (col++ >= conversion_blocksize)
2030 col = pending_spaces = 0; /* Wipe out any pending spaces. */
2031 i--; /* Push the char back; get it later. */
2032 output_char (newline_character);
2034 else if (c == space_character)
2035 pending_spaces++;
2036 else
2038 /* 'c' is the character after a run of spaces that were not
2039 at the end of the conversion buffer. Output them. */
2040 while (pending_spaces)
2042 output_char (space_character);
2043 --pending_spaces;
2045 output_char (c);
2050 /* Set the file descriptor flags for FD that correspond to the nonzero bits
2051 in ADD_FLAGS. The file's name is NAME. */
2053 static void
2054 set_fd_flags (int fd, int add_flags, char const *name)
2056 /* Ignore file creation flags that are no-ops on file descriptors. */
2057 add_flags &= ~ (O_NOCTTY | O_NOFOLLOW);
2059 if (add_flags)
2061 int old_flags = fcntl (fd, F_GETFL);
2062 int new_flags = old_flags | add_flags;
2063 bool ok = true;
2064 if (old_flags < 0)
2065 ok = false;
2066 else if (old_flags != new_flags)
2068 if (new_flags & (O_DIRECTORY | O_NOLINKS))
2070 /* NEW_FLAGS contains at least one file creation flag that
2071 requires some checking of the open file descriptor. */
2072 struct stat st;
2073 if (ifstat (fd, &st) != 0)
2074 ok = false;
2075 else if ((new_flags & O_DIRECTORY) && ! S_ISDIR (st.st_mode))
2077 errno = ENOTDIR;
2078 ok = false;
2080 else if ((new_flags & O_NOLINKS) && 1 < st.st_nlink)
2082 errno = EMLINK;
2083 ok = false;
2085 new_flags &= ~ (O_DIRECTORY | O_NOLINKS);
2088 if (ok && old_flags != new_flags
2089 && fcntl (fd, F_SETFL, new_flags) == -1)
2090 ok = false;
2093 if (!ok)
2094 die (EXIT_FAILURE, errno, _("setting flags for %s"), quoteaf (name));
2098 /* The main loop. */
2100 static int
2101 dd_copy (void)
2103 char *bufstart; /* Input buffer. */
2104 ssize_t nread; /* Bytes read in the current block. */
2106 /* If nonzero, then the previously read block was partial and
2107 PARTREAD was its size. */
2108 idx_t partread = 0;
2110 int exit_status = EXIT_SUCCESS;
2111 idx_t n_bytes_read;
2113 if (skip_records != 0 || skip_bytes != 0)
2115 intmax_t us_bytes;
2116 bool us_bytes_overflow =
2117 (INT_MULTIPLY_WRAPV (skip_records, input_blocksize, &us_bytes)
2118 || INT_ADD_WRAPV (skip_bytes, us_bytes, &us_bytes));
2119 off_t input_offset0 = input_offset;
2120 intmax_t us_blocks = skip (STDIN_FILENO, input_file,
2121 skip_records, input_blocksize, &skip_bytes);
2123 /* POSIX doesn't say what to do when dd detects it has been
2124 asked to skip past EOF, so I assume it's non-fatal.
2125 There are 3 reasons why there might be unskipped blocks/bytes:
2126 1. file is too small
2127 2. pipe has not enough data
2128 3. partial reads */
2129 if ((us_blocks
2130 || (0 <= input_offset
2131 && (us_bytes_overflow
2132 || us_bytes != input_offset - input_offset0)))
2133 && status_level != STATUS_NONE)
2135 error (0, 0,
2136 _("%s: cannot skip to specified offset"), quotef (input_file));
2140 if (seek_records != 0 || seek_bytes != 0)
2142 idx_t bytes = seek_bytes;
2143 intmax_t write_records = skip (STDOUT_FILENO, output_file,
2144 seek_records, output_blocksize, &bytes);
2146 if (write_records != 0 || bytes != 0)
2148 memset (obuf, 0, write_records ? output_blocksize : bytes);
2152 idx_t size = write_records ? output_blocksize : bytes;
2153 if (iwrite (STDOUT_FILENO, obuf, size) != size)
2155 error (0, errno, _("writing to %s"), quoteaf (output_file));
2156 quit (EXIT_FAILURE);
2159 if (write_records != 0)
2160 write_records--;
2161 else
2162 bytes = 0;
2164 while (write_records || bytes);
2168 if (max_records == 0 && max_bytes == 0)
2169 return exit_status;
2171 alloc_ibuf ();
2172 alloc_obuf ();
2173 int saved_byte = -1;
2175 while (true)
2177 if (status_level == STATUS_PROGRESS)
2179 xtime_t progress_time = gethrxtime ();
2180 if (next_time <= progress_time)
2182 print_xfer_stats (progress_time);
2183 next_time += XTIME_PRECISION;
2187 if (r_partial + r_full >= max_records + !!max_bytes)
2188 break;
2190 /* Zero the buffer before reading, so that if we get a read error,
2191 whatever data we are able to read is followed by zeros.
2192 This minimizes data loss. */
2193 if ((conversions_mask & C_SYNC) && (conversions_mask & C_NOERROR))
2194 memset (ibuf,
2195 (conversions_mask & (C_BLOCK | C_UNBLOCK)) ? ' ' : '\0',
2196 input_blocksize);
2198 if (r_partial + r_full >= max_records)
2199 nread = iread_fnc (STDIN_FILENO, ibuf, max_bytes);
2200 else
2201 nread = iread_fnc (STDIN_FILENO, ibuf, input_blocksize);
2203 if (nread > 0)
2205 advance_input_offset (nread);
2206 if (i_nocache)
2207 invalidate_cache (STDIN_FILENO, nread);
2209 else if (nread == 0)
2211 i_nocache_eof |= i_nocache;
2212 o_nocache_eof |= o_nocache && ! (conversions_mask & C_NOTRUNC);
2213 break; /* EOF. */
2215 else
2217 if (!(conversions_mask & C_NOERROR) || status_level != STATUS_NONE)
2218 error (0, errno, _("error reading %s"), quoteaf (input_file));
2220 if (conversions_mask & C_NOERROR)
2222 print_stats ();
2223 idx_t bad_portion = input_blocksize - partread;
2225 /* We already know this data is not cached,
2226 but call this so that correct offsets are maintained. */
2227 invalidate_cache (STDIN_FILENO, bad_portion);
2229 /* Seek past the bad block if possible. */
2230 if (!advance_input_after_read_error (bad_portion))
2232 exit_status = EXIT_FAILURE;
2234 /* Suppress duplicate diagnostics. */
2235 input_seekable = false;
2236 input_seek_errno = ESPIPE;
2238 if ((conversions_mask & C_SYNC) && !partread)
2239 /* Replace the missing input with null bytes and
2240 proceed normally. */
2241 nread = 0;
2242 else
2243 continue;
2245 else
2247 /* Write any partial block. */
2248 exit_status = EXIT_FAILURE;
2249 break;
2253 n_bytes_read = nread;
2255 if (n_bytes_read < input_blocksize)
2257 r_partial++;
2258 partread = n_bytes_read;
2259 if (conversions_mask & C_SYNC)
2261 if (!(conversions_mask & C_NOERROR))
2262 /* If C_NOERROR, we zeroed the block before reading. */
2263 memset (ibuf + n_bytes_read,
2264 (conversions_mask & (C_BLOCK | C_UNBLOCK)) ? ' ' : '\0',
2265 input_blocksize - n_bytes_read);
2266 n_bytes_read = input_blocksize;
2269 else
2271 r_full++;
2272 partread = 0;
2275 if (ibuf == obuf) /* If not C_TWOBUFS. */
2277 idx_t nwritten = iwrite (STDOUT_FILENO, obuf, n_bytes_read);
2278 w_bytes += nwritten;
2279 if (nwritten != n_bytes_read)
2281 error (0, errno, _("error writing %s"), quoteaf (output_file));
2282 return EXIT_FAILURE;
2284 else if (n_bytes_read == input_blocksize)
2285 w_full++;
2286 else
2287 w_partial++;
2288 continue;
2291 /* Do any translations on the whole buffer at once. */
2293 if (translation_needed)
2294 translate_buffer (ibuf, n_bytes_read);
2296 if (conversions_mask & C_SWAB)
2297 bufstart = swab_buffer (ibuf, &n_bytes_read, &saved_byte);
2298 else
2299 bufstart = ibuf;
2301 if (conversions_mask & C_BLOCK)
2302 copy_with_block (bufstart, n_bytes_read);
2303 else if (conversions_mask & C_UNBLOCK)
2304 copy_with_unblock (bufstart, n_bytes_read);
2305 else
2306 copy_simple (bufstart, n_bytes_read);
2309 /* If we have a char left as a result of conv=swab, output it. */
2310 if (0 <= saved_byte)
2312 char saved_char = saved_byte;
2313 if (conversions_mask & C_BLOCK)
2314 copy_with_block (&saved_char, 1);
2315 else if (conversions_mask & C_UNBLOCK)
2316 copy_with_unblock (&saved_char, 1);
2317 else
2318 output_char (saved_char);
2321 if ((conversions_mask & C_BLOCK) && col > 0)
2323 /* If the final input line didn't end with a '\n', pad
2324 the output block to 'conversion_blocksize' chars. */
2325 for (idx_t i = col; i < conversion_blocksize; i++)
2326 output_char (space_character);
2329 if (col && (conversions_mask & C_UNBLOCK))
2331 /* If there was any output, add a final '\n'. */
2332 output_char (newline_character);
2335 /* Write out the last block. */
2336 if (oc != 0)
2338 idx_t nwritten = iwrite (STDOUT_FILENO, obuf, oc);
2339 w_bytes += nwritten;
2340 if (nwritten != 0)
2341 w_partial++;
2342 if (nwritten != oc)
2344 error (0, errno, _("error writing %s"), quoteaf (output_file));
2345 return EXIT_FAILURE;
2349 /* If the last write was converted to a seek, then for a regular file
2350 or shared memory object, ftruncate to extend the size. */
2351 if (final_op_was_seek)
2353 struct stat stdout_stat;
2354 if (ifstat (STDOUT_FILENO, &stdout_stat) != 0)
2356 error (0, errno, _("cannot fstat %s"), quoteaf (output_file));
2357 return EXIT_FAILURE;
2359 if (S_ISREG (stdout_stat.st_mode) || S_TYPEISSHM (&stdout_stat))
2361 off_t output_offset = lseek (STDOUT_FILENO, 0, SEEK_CUR);
2362 if (0 <= output_offset && stdout_stat.st_size < output_offset)
2364 if (iftruncate (STDOUT_FILENO, output_offset) != 0)
2366 error (0, errno,
2367 _("failed to truncate to %" PRIdMAX " bytes"
2368 " in output file %s"),
2369 (intmax_t) output_offset, quoteaf (output_file));
2370 return EXIT_FAILURE;
2376 /* fdatasync/fsync can take a long time, so issue a final progress
2377 indication now if progress has been made since the previous indication. */
2378 if (conversions_mask & (C_FDATASYNC | C_FSYNC)
2379 && status_level == STATUS_PROGRESS
2380 && 0 <= reported_w_bytes && reported_w_bytes < w_bytes)
2381 print_xfer_stats (0);
2383 return exit_status;
2386 /* Synchronize output according to conversions_mask.
2387 Do this even if w_bytes is zero, as fsync and fdatasync
2388 flush out write requests from other processes too.
2389 Clear bits in conversions_mask so that synchronization is done only once.
2390 Return zero if successful, an exit status otherwise. */
2392 static int
2393 synchronize_output (void)
2395 int exit_status = 0;
2396 int mask = conversions_mask;
2397 conversions_mask &= ~ (C_FDATASYNC | C_FSYNC);
2399 if ((mask & C_FDATASYNC) && ifdatasync (STDOUT_FILENO) != 0)
2401 if (errno != ENOSYS && errno != EINVAL)
2403 error (0, errno, _("fdatasync failed for %s"), quoteaf (output_file));
2404 exit_status = EXIT_FAILURE;
2406 mask |= C_FSYNC;
2409 if ((mask & C_FSYNC) && ifsync (STDOUT_FILENO) != 0)
2411 error (0, errno, _("fsync failed for %s"), quoteaf (output_file));
2412 return EXIT_FAILURE;
2415 return exit_status;
2419 main (int argc, char **argv)
2421 int i;
2422 int exit_status;
2423 off_t offset;
2425 install_signal_handlers ();
2427 initialize_main (&argc, &argv);
2428 set_program_name (argv[0]);
2429 setlocale (LC_ALL, "");
2430 bindtextdomain (PACKAGE, LOCALEDIR);
2431 textdomain (PACKAGE);
2433 /* Arrange to close stdout if parse_long_options exits. */
2434 atexit (maybe_close_stdout);
2436 page_size = getpagesize ();
2438 parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE, Version,
2439 true, usage, AUTHORS, (char const *) NULL);
2440 close_stdout_required = false;
2442 /* Initialize translation table to identity translation. */
2443 for (i = 0; i < 256; i++)
2444 trans_table[i] = i;
2446 /* Decode arguments. */
2447 scanargs (argc, argv);
2449 apply_translations ();
2451 if (input_file == NULL)
2453 input_file = _("standard input");
2454 set_fd_flags (STDIN_FILENO, input_flags, input_file);
2456 else
2458 if (ifd_reopen (STDIN_FILENO, input_file, O_RDONLY | input_flags, 0) < 0)
2459 die (EXIT_FAILURE, errno, _("failed to open %s"),
2460 quoteaf (input_file));
2463 offset = lseek (STDIN_FILENO, 0, SEEK_CUR);
2464 input_seekable = (0 <= offset);
2465 input_offset = MAX (0, offset);
2466 input_seek_errno = errno;
2468 if (output_file == NULL)
2470 output_file = _("standard output");
2471 set_fd_flags (STDOUT_FILENO, output_flags, output_file);
2473 else
2475 mode_t perms = MODE_RW_UGO;
2476 int opts
2477 = (output_flags
2478 | (conversions_mask & C_NOCREAT ? 0 : O_CREAT)
2479 | (conversions_mask & C_EXCL ? O_EXCL : 0)
2480 | (seek_records || (conversions_mask & C_NOTRUNC) ? 0 : O_TRUNC));
2482 off_t size;
2483 if ((INT_MULTIPLY_WRAPV (seek_records, output_blocksize, &size)
2484 || INT_ADD_WRAPV (seek_bytes, size, &size))
2485 && !(conversions_mask & C_NOTRUNC))
2486 die (EXIT_FAILURE, 0,
2487 _("offset too large: "
2488 "cannot truncate to a length of seek=%"PRIdMAX""
2489 " (%td-byte) blocks"),
2490 seek_records, output_blocksize);
2492 /* Open the output file with *read* access only if we might
2493 need to read to satisfy a 'seek=' request. If we can't read
2494 the file, go ahead with write-only access; it might work. */
2495 if ((! seek_records
2496 || ifd_reopen (STDOUT_FILENO, output_file, O_RDWR | opts, perms) < 0)
2497 && (ifd_reopen (STDOUT_FILENO, output_file, O_WRONLY | opts, perms)
2498 < 0))
2499 die (EXIT_FAILURE, errno, _("failed to open %s"),
2500 quoteaf (output_file));
2502 if (seek_records != 0 && !(conversions_mask & C_NOTRUNC))
2504 if (iftruncate (STDOUT_FILENO, size) != 0)
2506 /* Complain only when ftruncate fails on a regular file, a
2507 directory, or a shared memory object, as POSIX 1003.1-2004
2508 specifies ftruncate's behavior only for these file types.
2509 For example, do not complain when Linux kernel 2.4 ftruncate
2510 fails on /dev/fd0. */
2511 int ftruncate_errno = errno;
2512 struct stat stdout_stat;
2513 if (ifstat (STDOUT_FILENO, &stdout_stat) != 0)
2515 error (0, errno, _("cannot fstat %s"),
2516 quoteaf (output_file));
2517 exit_status = EXIT_FAILURE;
2519 else if (S_ISREG (stdout_stat.st_mode)
2520 || S_ISDIR (stdout_stat.st_mode)
2521 || S_TYPEISSHM (&stdout_stat))
2523 intmax_t isize = size;
2524 error (0, ftruncate_errno,
2525 _("failed to truncate to %"PRIdMAX" bytes"
2526 " in output file %s"),
2527 isize, quoteaf (output_file));
2528 exit_status = EXIT_FAILURE;
2534 start_time = gethrxtime ();
2535 next_time = start_time + XTIME_PRECISION;
2537 exit_status = dd_copy ();
2539 int sync_status = synchronize_output ();
2540 if (sync_status)
2541 exit_status = sync_status;
2543 if (max_records == 0 && max_bytes == 0)
2545 /* Special case to invalidate cache to end of file. */
2546 if (i_nocache && !invalidate_cache (STDIN_FILENO, 0))
2548 error (0, errno, _("failed to discard cache for: %s"),
2549 quotef (input_file));
2550 exit_status = EXIT_FAILURE;
2552 if (o_nocache && !invalidate_cache (STDOUT_FILENO, 0))
2554 error (0, errno, _("failed to discard cache for: %s"),
2555 quotef (output_file));
2556 exit_status = EXIT_FAILURE;
2559 else
2561 /* Invalidate any pending region or to EOF if appropriate. */
2562 if (i_nocache || i_nocache_eof)
2563 invalidate_cache (STDIN_FILENO, 0);
2564 if (o_nocache || o_nocache_eof)
2565 invalidate_cache (STDOUT_FILENO, 0);
2568 finish_up ();
2569 main_exit (exit_status);