dd: synchronize output after write errors
[coreutils.git] / src / dd.c
blob4ddc6db12c810b969e94b0f77cbbff31136342b1
1 /* dd -- convert a file while copying it.
2 Copyright (C) 1985-2022 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 verify (O_FULLBLOCK != 0);
331 verify (O_NOCACHE != 0);
332 verify (O_COUNT_BYTES != 0);
333 verify (O_SKIP_BYTES != 0);
334 verify (O_SEEK_BYTES != 0);
336 #define MULTIPLE_BITS_SET(i) (((i) & ((i) - 1)) != 0)
338 /* Ensure that this is a single-bit value. */
339 verify ( ! MULTIPLE_BITS_SET (O_FULLBLOCK));
340 verify ( ! MULTIPLE_BITS_SET (O_NOCACHE));
341 verify ( ! MULTIPLE_BITS_SET (O_COUNT_BYTES));
342 verify ( ! MULTIPLE_BITS_SET (O_SKIP_BYTES));
343 verify ( ! 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 skip N obs-sized blocks at start of output\n\
566 skip=N skip N ibs-sized blocks at start of input\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.\n\
577 Binary prefixes can be used, too: KiB=K, MiB=M, and so on.\n\
579 Each CONV symbol may be:\n\
581 "), stdout);
582 fputs (_("\
583 ascii from EBCDIC to ASCII\n\
584 ebcdic from ASCII to EBCDIC\n\
585 ibm from ASCII to alternate EBCDIC\n\
586 block pad newline-terminated records with spaces to cbs-size\n\
587 unblock replace trailing spaces in cbs-size records with newline\n\
588 lcase change upper case to lower case\n\
589 ucase change lower case to upper case\n\
590 sparse try to seek rather than write all-NUL output blocks\n\
591 swab swap every pair of input bytes\n\
592 sync pad every input block with NULs to ibs-size; when used\n\
593 with block or unblock, pad with spaces rather than NULs\n\
594 "), stdout);
595 fputs (_("\
596 excl fail if the output file already exists\n\
597 nocreat do not create the output file\n\
598 notrunc do not truncate the output file\n\
599 noerror continue after read errors\n\
600 fdatasync physically write output file data before finishing\n\
601 fsync likewise, but also write metadata\n\
602 "), stdout);
603 fputs (_("\
605 Each FLAG symbol may be:\n\
607 append append mode (makes sense only for output; conv=notrunc suggested)\n\
608 "), stdout);
609 if (O_CIO)
610 fputs (_(" cio use concurrent I/O for data\n"), stdout);
611 if (O_DIRECT)
612 fputs (_(" direct use direct I/O for data\n"), stdout);
613 if (O_DIRECTORY)
614 fputs (_(" directory fail unless a directory\n"), stdout);
615 if (O_DSYNC)
616 fputs (_(" dsync use synchronized I/O for data\n"), stdout);
617 if (O_SYNC)
618 fputs (_(" sync likewise, but also for metadata\n"), stdout);
619 fputs (_(" fullblock accumulate full blocks of input (iflag only)\n"),
620 stdout);
621 if (O_NONBLOCK)
622 fputs (_(" nonblock use non-blocking I/O\n"), stdout);
623 if (O_NOATIME)
624 fputs (_(" noatime do not update access time\n"), stdout);
625 #if HAVE_POSIX_FADVISE
626 if (O_NOCACHE)
627 fputs (_(" nocache Request to drop cache. See also oflag=sync\n"),
628 stdout);
629 #endif
630 if (O_NOCTTY)
631 fputs (_(" noctty do not assign controlling terminal from file\n"),
632 stdout);
633 if (HAVE_WORKING_O_NOFOLLOW)
634 fputs (_(" nofollow do not follow symlinks\n"), stdout);
635 if (O_NOLINKS)
636 fputs (_(" nolinks fail if multiply-linked\n"), stdout);
637 if (O_BINARY)
638 fputs (_(" binary use binary I/O for data\n"), stdout);
639 if (O_TEXT)
640 fputs (_(" text use text I/O for data\n"), stdout);
641 if (O_COUNT_BYTES)
642 fputs (_(" count_bytes treat 'count=N' as a byte count (iflag only)\n\
643 "), stdout);
644 if (O_SKIP_BYTES)
645 fputs (_(" skip_bytes treat 'skip=N' as a byte count (iflag only)\n\
646 "), stdout);
647 if (O_SEEK_BYTES)
648 fputs (_(" seek_bytes treat 'seek=N' as a byte count (oflag only)\n\
649 "), stdout);
652 printf (_("\
654 Sending a %s signal to a running 'dd' process makes it\n\
655 print I/O statistics to standard error and then resume copying.\n\
657 Options are:\n\
659 "), SIGINFO == SIGUSR1 ? "USR1" : "INFO");
662 fputs (HELP_OPTION_DESCRIPTION, stdout);
663 fputs (VERSION_OPTION_DESCRIPTION, stdout);
664 emit_ancillary_info (PROGRAM_NAME);
666 exit (status);
669 /* Common options to use when displaying sizes and rates. */
671 enum { human_opts = (human_autoscale | human_round_to_nearest
672 | human_space_before_unit | human_SI | human_B) };
674 /* Ensure input buffer IBUF is allocated. */
676 static void
677 alloc_ibuf (void)
679 if (ibuf)
680 return;
682 bool extra_byte_for_swab = !!(conversions_mask & C_SWAB);
683 ibuf = alignalloc (page_size, input_blocksize + extra_byte_for_swab);
684 if (!ibuf)
686 char hbuf[LONGEST_HUMAN_READABLE + 1];
687 die (EXIT_FAILURE, 0,
688 _("memory exhausted by input buffer of size %td bytes (%s)"),
689 input_blocksize,
690 human_readable (input_blocksize, hbuf,
691 human_opts | human_base_1024, 1, 1));
695 /* Ensure output buffer OBUF is allocated/initialized. */
697 static void
698 alloc_obuf (void)
700 if (obuf)
701 return;
703 if (conversions_mask & C_TWOBUFS)
705 obuf = alignalloc (page_size, output_blocksize);
706 if (!obuf)
708 char hbuf[LONGEST_HUMAN_READABLE + 1];
709 die (EXIT_FAILURE, 0,
710 _("memory exhausted by output buffer of size %td"
711 " bytes (%s)"),
712 output_blocksize,
713 human_readable (output_blocksize, hbuf,
714 human_opts | human_base_1024, 1, 1));
717 else
719 alloc_ibuf ();
720 obuf = ibuf;
724 static void
725 translate_charset (char const *new_trans)
727 for (int i = 0; i < 256; i++)
728 trans_table[i] = new_trans[trans_table[i]];
729 translation_needed = true;
732 /* Return true if I has more than one bit set. I must be nonnegative. */
734 static inline bool
735 multiple_bits_set (int i)
737 return MULTIPLE_BITS_SET (i);
740 static bool
741 abbreviation_lacks_prefix (char const *message)
743 return message[strlen (message) - 2] == ' ';
746 /* Print transfer statistics. */
748 static void
749 print_xfer_stats (xtime_t progress_time)
751 xtime_t now = progress_time ? progress_time : gethrxtime ();
752 static char const slash_s[] = "/s";
753 char hbuf[3][LONGEST_HUMAN_READABLE + sizeof slash_s];
754 double delta_s;
755 char const *bytes_per_second;
756 char const *si = human_readable (w_bytes, hbuf[0], human_opts, 1, 1);
757 char const *iec = human_readable (w_bytes, hbuf[1],
758 human_opts | human_base_1024, 1, 1);
760 /* Use integer arithmetic to compute the transfer rate,
761 since that makes it easy to use SI abbreviations. */
762 char *bpsbuf = hbuf[2];
763 int bpsbufsize = sizeof hbuf[2];
764 if (start_time < now)
766 double XTIME_PRECISIONe0 = XTIME_PRECISION;
767 xtime_t delta_xtime = now - start_time;
768 delta_s = delta_xtime / XTIME_PRECISIONe0;
769 bytes_per_second = human_readable (w_bytes, bpsbuf, human_opts,
770 XTIME_PRECISION, delta_xtime);
771 strcat (bytes_per_second - bpsbuf + bpsbuf, slash_s);
773 else
775 delta_s = 0;
776 snprintf (bpsbuf, bpsbufsize, "%s B/s", _("Infinity"));
777 bytes_per_second = bpsbuf;
780 if (progress_time)
781 fputc ('\r', stderr);
783 /* Use full seconds when printing progress, since the progress
784 report is output once per second and there is little point
785 displaying any subsecond jitter. Use default precision with %g
786 otherwise, as this provides more-useful output then. With long
787 transfers %g can generate a number with an exponent; that is OK. */
788 char delta_s_buf[24];
789 snprintf (delta_s_buf, sizeof delta_s_buf,
790 progress_time ? "%.0f s" : "%g s", delta_s);
792 int stats_len
793 = (abbreviation_lacks_prefix (si)
794 ? fprintf (stderr,
795 ngettext ("%"PRIdMAX" byte copied, %s, %s",
796 "%"PRIdMAX" bytes copied, %s, %s",
797 select_plural (w_bytes)),
798 w_bytes, delta_s_buf, bytes_per_second)
799 : abbreviation_lacks_prefix (iec)
800 ? fprintf (stderr,
801 _("%"PRIdMAX" bytes (%s) copied, %s, %s"),
802 w_bytes, si, delta_s_buf, bytes_per_second)
803 : fprintf (stderr,
804 _("%"PRIdMAX" bytes (%s, %s) copied, %s, %s"),
805 w_bytes, si, iec, delta_s_buf, bytes_per_second));
807 if (progress_time)
809 /* Erase any trailing junk on the output line by outputting
810 spaces. In theory this could glitch the display because the
811 formatted translation of a line describing a larger file
812 could consume fewer screen columns than the strlen difference
813 from the previously formatted translation. In practice this
814 does not seem to be a problem. */
815 if (0 <= stats_len && stats_len < progress_len)
816 fprintf (stderr, "%*s", progress_len - stats_len, "");
817 progress_len = stats_len;
819 else
820 fputc ('\n', stderr);
822 reported_w_bytes = w_bytes;
825 static void
826 print_stats (void)
828 if (status_level == STATUS_NONE)
829 return;
831 if (0 < progress_len)
833 fputc ('\n', stderr);
834 progress_len = 0;
837 fprintf (stderr,
838 _("%"PRIdMAX"+%"PRIdMAX" records in\n"
839 "%"PRIdMAX"+%"PRIdMAX" records out\n"),
840 r_full, r_partial, w_full, w_partial);
842 if (r_truncate != 0)
843 fprintf (stderr,
844 ngettext ("%"PRIdMAX" truncated record\n",
845 "%"PRIdMAX" truncated records\n",
846 select_plural (r_truncate)),
847 r_truncate);
849 if (status_level == STATUS_NOXFER)
850 return;
852 print_xfer_stats (0);
855 /* An ordinary signal was received; arrange for the program to exit. */
857 static void
858 interrupt_handler (int sig)
860 if (! SA_RESETHAND)
861 signal (sig, SIG_DFL);
862 interrupt_signal = sig;
865 /* An info signal was received; arrange for the program to print status. */
867 static void
868 siginfo_handler (int sig)
870 if (! SA_NOCLDSTOP)
871 signal (sig, siginfo_handler);
872 info_signal_count++;
875 /* Install the signal handlers. */
877 static void
878 install_signal_handlers (void)
880 bool catch_siginfo = ! (SIGINFO == SIGUSR1 && getenv ("POSIXLY_CORRECT"));
882 #if SA_NOCLDSTOP
884 struct sigaction act;
885 sigemptyset (&caught_signals);
886 if (catch_siginfo)
887 sigaddset (&caught_signals, SIGINFO);
888 sigaction (SIGINT, NULL, &act);
889 if (act.sa_handler != SIG_IGN)
890 sigaddset (&caught_signals, SIGINT);
891 act.sa_mask = caught_signals;
893 if (sigismember (&caught_signals, SIGINFO))
895 act.sa_handler = siginfo_handler;
896 /* Note we don't use SA_RESTART here and instead
897 handle EINTR explicitly in iftruncate etc.
898 to avoid blocking on noncommitted read/write calls. */
899 act.sa_flags = 0;
900 sigaction (SIGINFO, &act, NULL);
903 if (sigismember (&caught_signals, SIGINT))
905 act.sa_handler = interrupt_handler;
906 act.sa_flags = SA_NODEFER | SA_RESETHAND;
907 sigaction (SIGINT, &act, NULL);
910 #else
912 if (catch_siginfo)
914 signal (SIGINFO, siginfo_handler);
915 siginterrupt (SIGINFO, 1);
917 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
919 signal (SIGINT, interrupt_handler);
920 siginterrupt (SIGINT, 1);
922 #endif
925 /* Close FD. Return 0 if successful, -1 (setting errno) otherwise.
926 If close fails with errno == EINTR, POSIX says the file descriptor
927 is in an unspecified state, so keep trying to close FD but do not
928 consider EBADF to be an error. Do not process signals. This all
929 differs somewhat from functions like ifdatasync and ifsync. */
930 static int
931 iclose (int fd)
933 if (close (fd) != 0)
935 if (errno != EINTR)
936 return -1;
937 while (close (fd) != 0 && errno != EBADF);
939 return 0;
942 static int synchronize_output (void);
944 static void
945 cleanup (void)
947 #ifdef lint
948 if (ibuf != obuf)
949 alignfree (ibuf);
950 alignfree (obuf);
951 #endif
953 if (!interrupt_signal)
955 int sync_status = synchronize_output ();
956 if (sync_status)
957 exit (sync_status);
960 if (iclose (STDIN_FILENO) != 0)
961 die (EXIT_FAILURE, errno, _("closing input file %s"), quoteaf (input_file));
963 /* Don't remove this call to close, even though close_stdout
964 closes standard output. This close is necessary when cleanup
965 is called as a consequence of signal handling. */
966 if (iclose (STDOUT_FILENO) != 0)
967 die (EXIT_FAILURE, errno,
968 _("closing output file %s"), quoteaf (output_file));
971 /* Process any pending signals. If signals are caught, this function
972 should be called periodically. Ideally there should never be an
973 unbounded amount of time when signals are not being processed. */
975 static void
976 process_signals (void)
978 while (interrupt_signal || info_signal_count)
980 int interrupt;
981 int infos;
982 sigset_t oldset;
984 sigprocmask (SIG_BLOCK, &caught_signals, &oldset);
986 /* Reload interrupt_signal and info_signal_count, in case a new
987 signal was handled before sigprocmask took effect. */
988 interrupt = interrupt_signal;
989 infos = info_signal_count;
991 if (infos)
992 info_signal_count = infos - 1;
994 sigprocmask (SIG_SETMASK, &oldset, NULL);
996 if (interrupt)
997 cleanup ();
998 print_stats ();
999 if (interrupt)
1000 raise (interrupt);
1004 static void
1005 finish_up (void)
1007 /* Process signals first, so that cleanup is called at most once. */
1008 process_signals ();
1009 cleanup ();
1010 print_stats ();
1013 static void
1014 quit (int code)
1016 finish_up ();
1017 exit (code);
1020 /* Return LEN rounded down to a multiple of IO_BUFSIZE
1021 (to minimize calls to the expensive posix_fadvise (,POSIX_FADV_DONTNEED),
1022 while storing the remainder internally per FD.
1023 Pass LEN == 0 to get the current remainder. */
1025 static off_t
1026 cache_round (int fd, off_t len)
1028 static off_t i_pending, o_pending;
1029 off_t *pending = (fd == STDIN_FILENO ? &i_pending : &o_pending);
1031 if (len)
1033 intmax_t c_pending;
1034 if (INT_ADD_WRAPV (*pending, len, &c_pending))
1035 c_pending = INTMAX_MAX;
1036 *pending = c_pending % IO_BUFSIZE;
1037 if (c_pending > *pending)
1038 len = c_pending - *pending;
1039 else
1040 len = 0;
1042 else
1043 len = *pending;
1045 return len;
1048 /* Discard the cache from the current offset of either
1049 STDIN_FILENO or STDOUT_FILENO.
1050 Return true on success. */
1052 static bool
1053 invalidate_cache (int fd, off_t len)
1055 int adv_ret = -1;
1056 off_t offset;
1057 bool nocache_eof = (fd == STDIN_FILENO ? i_nocache_eof : o_nocache_eof);
1059 /* Minimize syscalls. */
1060 off_t clen = cache_round (fd, len);
1061 if (len && !clen)
1062 return true; /* Don't advise this time. */
1063 else if (! len && ! clen && ! nocache_eof)
1064 return true;
1065 off_t pending = len ? cache_round (fd, 0) : 0;
1067 if (fd == STDIN_FILENO)
1069 if (input_seekable)
1070 offset = input_offset;
1071 else
1073 offset = -1;
1074 errno = ESPIPE;
1077 else
1079 static off_t output_offset = -2;
1081 if (output_offset != -1)
1083 if (output_offset < 0)
1084 output_offset = lseek (fd, 0, SEEK_CUR);
1085 else if (len)
1086 output_offset += clen + pending;
1089 offset = output_offset;
1092 if (0 <= offset)
1094 if (! len && clen && nocache_eof)
1096 pending = clen;
1097 clen = 0;
1100 /* Note we're being careful here to only invalidate what
1101 we've read, so as not to dump any read ahead cache.
1102 Note also the kernel is conservative and only invalidates
1103 full pages in the specified range. */
1104 #if HAVE_POSIX_FADVISE
1105 offset = offset - clen - pending;
1106 /* ensure full page specified when invalidating to eof. */
1107 if (clen == 0)
1108 offset -= offset % page_size;
1109 adv_ret = posix_fadvise (fd, offset, clen, POSIX_FADV_DONTNEED);
1110 #else
1111 errno = ENOTSUP;
1112 #endif
1115 return adv_ret != -1 ? true : false;
1118 /* Read from FD into the buffer BUF of size SIZE, processing any
1119 signals that arrive before bytes are read. Return the number of
1120 bytes read if successful, -1 (setting errno) on failure. */
1122 static ssize_t
1123 iread (int fd, char *buf, idx_t size)
1125 ssize_t nread;
1126 static ssize_t prev_nread;
1130 process_signals ();
1131 nread = read (fd, buf, size);
1132 /* Ignore final read error with iflag=direct as that
1133 returns EINVAL due to the non aligned file offset. */
1134 if (nread == -1 && errno == EINVAL
1135 && 0 < prev_nread && prev_nread < size
1136 && (input_flags & O_DIRECT))
1138 errno = 0;
1139 nread = 0;
1142 while (nread < 0 && errno == EINTR);
1144 /* Short read may be due to received signal. */
1145 if (0 < nread && nread < size)
1146 process_signals ();
1148 if (0 < nread && warn_partial_read)
1150 if (0 < prev_nread && prev_nread < size)
1152 idx_t prev = prev_nread;
1153 if (status_level != STATUS_NONE)
1154 error (0, 0, ngettext (("warning: partial read (%td byte); "
1155 "suggest iflag=fullblock"),
1156 ("warning: partial read (%td bytes); "
1157 "suggest iflag=fullblock"),
1158 select_plural (prev)),
1159 prev);
1160 warn_partial_read = false;
1164 prev_nread = nread;
1165 return nread;
1168 /* Wrapper around iread function to accumulate full blocks. */
1169 static ssize_t
1170 iread_fullblock (int fd, char *buf, idx_t size)
1172 ssize_t nread = 0;
1174 while (0 < size)
1176 ssize_t ncurr = iread (fd, buf, size);
1177 if (ncurr < 0)
1178 return ncurr;
1179 if (ncurr == 0)
1180 break;
1181 nread += ncurr;
1182 buf += ncurr;
1183 size -= ncurr;
1186 return nread;
1189 /* Write to FD the buffer BUF of size SIZE, processing any signals
1190 that arrive. Return the number of bytes written, setting errno if
1191 this is less than SIZE. Keep trying if there are partial
1192 writes. */
1194 static idx_t
1195 iwrite (int fd, char const *buf, idx_t size)
1197 idx_t total_written = 0;
1199 if ((output_flags & O_DIRECT) && size < output_blocksize)
1201 int old_flags = fcntl (STDOUT_FILENO, F_GETFL);
1202 if (fcntl (STDOUT_FILENO, F_SETFL, old_flags & ~O_DIRECT) != 0
1203 && status_level != STATUS_NONE)
1204 error (0, errno, _("failed to turn off O_DIRECT: %s"),
1205 quotef (output_file));
1207 /* Since we have just turned off O_DIRECT for the final write,
1208 we try to preserve some of its semantics. */
1210 /* Call invalidate_cache to setup the appropriate offsets
1211 for subsequent calls. */
1212 o_nocache_eof = true;
1213 invalidate_cache (STDOUT_FILENO, 0);
1215 /* Attempt to ensure that that final block is committed
1216 to stable storage as quickly as possible. */
1217 conversions_mask |= C_FSYNC;
1219 /* After the subsequent fsync we'll call invalidate_cache
1220 to attempt to clear all data from the page cache. */
1223 while (total_written < size)
1225 ssize_t nwritten = 0;
1226 process_signals ();
1228 /* Perform a seek for a NUL block if sparse output is enabled. */
1229 final_op_was_seek = false;
1230 if ((conversions_mask & C_SPARSE) && is_nul (buf, size))
1232 if (lseek (fd, size, SEEK_CUR) < 0)
1234 conversions_mask &= ~C_SPARSE;
1235 /* Don't warn about the advisory sparse request. */
1237 else
1239 final_op_was_seek = true;
1240 nwritten = size;
1244 if (!nwritten)
1245 nwritten = write (fd, buf + total_written, size - total_written);
1247 if (nwritten < 0)
1249 if (errno != EINTR)
1250 break;
1252 else if (nwritten == 0)
1254 /* Some buggy drivers return 0 when one tries to write beyond
1255 a device's end. (Example: Linux kernel 1.2.13 on /dev/fd0.)
1256 Set errno to ENOSPC so they get a sensible diagnostic. */
1257 errno = ENOSPC;
1258 break;
1260 else
1261 total_written += nwritten;
1264 if (o_nocache && total_written)
1265 invalidate_cache (fd, total_written);
1267 return total_written;
1270 /* Write, then empty, the output buffer 'obuf'. */
1272 static void
1273 write_output (void)
1275 idx_t nwritten = iwrite (STDOUT_FILENO, obuf, output_blocksize);
1276 w_bytes += nwritten;
1277 if (nwritten != output_blocksize)
1279 error (0, errno, _("writing to %s"), quoteaf (output_file));
1280 if (nwritten != 0)
1281 w_partial++;
1282 quit (EXIT_FAILURE);
1284 else
1285 w_full++;
1286 oc = 0;
1289 /* Restart on EINTR from fdatasync. */
1291 static int
1292 ifdatasync (int fd)
1294 int ret;
1298 process_signals ();
1299 ret = fdatasync (fd);
1301 while (ret < 0 && errno == EINTR);
1303 return ret;
1306 /* Restart on EINTR from fd_reopen. */
1308 static int
1309 ifd_reopen (int desired_fd, char const *file, int flag, mode_t mode)
1311 int ret;
1315 process_signals ();
1316 ret = fd_reopen (desired_fd, file, flag, mode);
1318 while (ret < 0 && errno == EINTR);
1320 return ret;
1323 /* Restart on EINTR from fstat. */
1325 static int
1326 ifstat (int fd, struct stat *st)
1328 int ret;
1332 process_signals ();
1333 ret = fstat (fd, st);
1335 while (ret < 0 && errno == EINTR);
1337 return ret;
1340 /* Restart on EINTR from fsync. */
1342 static int
1343 ifsync (int fd)
1345 int ret;
1349 process_signals ();
1350 ret = fsync (fd);
1352 while (ret < 0 && errno == EINTR);
1354 return ret;
1357 /* Restart on EINTR from ftruncate. */
1359 static int
1360 iftruncate (int fd, off_t length)
1362 int ret;
1366 process_signals ();
1367 ret = ftruncate (fd, length);
1369 while (ret < 0 && errno == EINTR);
1371 return ret;
1374 /* Return true if STR is of the form "PATTERN" or "PATTERNDELIM...". */
1376 ATTRIBUTE_PURE
1377 static bool
1378 operand_matches (char const *str, char const *pattern, char delim)
1380 while (*pattern)
1381 if (*str++ != *pattern++)
1382 return false;
1383 return !*str || *str == delim;
1386 /* Interpret one "conv=..." or similar operand STR according to the
1387 symbols in TABLE, returning the flags specified. If the operand
1388 cannot be parsed, use ERROR_MSGID to generate a diagnostic. */
1390 static int
1391 parse_symbols (char const *str, struct symbol_value const *table,
1392 bool exclusive, char const *error_msgid)
1394 int value = 0;
1396 while (true)
1398 char const *strcomma = strchr (str, ',');
1399 struct symbol_value const *entry;
1401 for (entry = table;
1402 ! (operand_matches (str, entry->symbol, ',') && entry->value);
1403 entry++)
1405 if (! entry->symbol[0])
1407 idx_t slen = strcomma ? strcomma - str : strlen (str);
1408 error (0, 0, "%s: %s", _(error_msgid),
1409 quotearg_n_style_mem (0, locale_quoting_style, str, slen));
1410 usage (EXIT_FAILURE);
1414 if (exclusive)
1415 value = entry->value;
1416 else
1417 value |= entry->value;
1418 if (!strcomma)
1419 break;
1420 str = strcomma + 1;
1423 return value;
1426 /* Return the value of STR, interpreted as a non-negative decimal integer,
1427 optionally multiplied by various values.
1428 If STR does not represent a number in this format,
1429 set *INVALID to a nonzero error value and return
1430 INTMAX_MAX if it is an overflow, an indeterminate value otherwise. */
1432 static intmax_t
1433 parse_integer (char const *str, strtol_error *invalid)
1435 /* Call xstrtoumax, not xstrtoimax, since we don't want to
1436 allow strings like " -0". */
1437 uintmax_t n;
1438 char *suffix;
1439 strtol_error e = xstrtoumax (str, &suffix, 10, &n, "bcEGkKMPTwYZ0");
1441 if ((e & ~LONGINT_OVERFLOW) == LONGINT_INVALID_SUFFIX_CHAR
1442 && *suffix == 'x')
1444 strtol_error invalid2 = LONGINT_OK;
1445 intmax_t result = parse_integer (suffix + 1, &invalid2);
1446 if ((invalid2 & ~LONGINT_OVERFLOW) != LONGINT_OK)
1448 *invalid = invalid2;
1449 return result;
1452 if (INT_MULTIPLY_WRAPV (n, result, &result))
1454 *invalid = LONGINT_OVERFLOW;
1455 return INTMAX_MAX;
1458 if (result == 0)
1460 if (STRPREFIX (str, "0x"))
1461 error (0, 0,
1462 _("warning: %s is a zero multiplier; "
1463 "use %s if that is intended"),
1464 quote_n (0, "0x"), quote_n (1, "00x"));
1466 else if ((e | invalid2) & LONGINT_OVERFLOW)
1468 *invalid = LONGINT_OVERFLOW;
1469 return INTMAX_MAX;
1472 return result;
1475 if (INTMAX_MAX < n)
1477 *invalid = LONGINT_OVERFLOW;
1478 return INTMAX_MAX;
1481 if (e != LONGINT_OK)
1482 *invalid = e;
1483 return n;
1486 /* OPERAND is of the form "X=...". Return true if X is NAME. */
1488 ATTRIBUTE_PURE
1489 static bool
1490 operand_is (char const *operand, char const *name)
1492 return operand_matches (operand, name, '=');
1495 static void
1496 scanargs (int argc, char *const *argv)
1498 idx_t blocksize = 0;
1499 intmax_t count = INTMAX_MAX;
1500 intmax_t skip = 0;
1501 intmax_t seek = 0;
1503 for (int i = optind; i < argc; i++)
1505 char const *name = argv[i];
1506 char const *val = strchr (name, '=');
1508 if (val == NULL)
1510 error (0, 0, _("unrecognized operand %s"),
1511 quote (name));
1512 usage (EXIT_FAILURE);
1514 val++;
1516 if (operand_is (name, "if"))
1517 input_file = val;
1518 else if (operand_is (name, "of"))
1519 output_file = val;
1520 else if (operand_is (name, "conv"))
1521 conversions_mask |= parse_symbols (val, conversions, false,
1522 N_("invalid conversion"));
1523 else if (operand_is (name, "iflag"))
1524 input_flags |= parse_symbols (val, flags, false,
1525 N_("invalid input flag"));
1526 else if (operand_is (name, "oflag"))
1527 output_flags |= parse_symbols (val, flags, false,
1528 N_("invalid output flag"));
1529 else if (operand_is (name, "status"))
1530 status_level = parse_symbols (val, statuses, true,
1531 N_("invalid status level"));
1532 else
1534 strtol_error invalid = LONGINT_OK;
1535 intmax_t n = parse_integer (val, &invalid);
1536 intmax_t n_min = 0;
1537 intmax_t n_max = INTMAX_MAX;
1538 idx_t *converted_idx = NULL;
1540 /* Maximum blocksize. Keep it smaller than IDX_MAX, so that
1541 it fits into blocksize vars even if 1 is added for conv=swab.
1542 Do not exceed SSIZE_MAX, for the benefit of system calls
1543 like "read". And do not exceed OFF_T_MAX, for the
1544 benefit of the large-offset seek code. */
1545 idx_t max_blocksize = MIN (IDX_MAX - 1, MIN (SSIZE_MAX, OFF_T_MAX));
1547 if (operand_is (name, "ibs"))
1549 n_min = 1;
1550 n_max = max_blocksize;
1551 converted_idx = &input_blocksize;
1553 else if (operand_is (name, "obs"))
1555 n_min = 1;
1556 n_max = max_blocksize;
1557 converted_idx = &output_blocksize;
1559 else if (operand_is (name, "bs"))
1561 n_min = 1;
1562 n_max = max_blocksize;
1563 converted_idx = &blocksize;
1565 else if (operand_is (name, "cbs"))
1567 n_min = 1;
1568 n_max = MIN (SIZE_MAX, IDX_MAX);
1569 converted_idx = &conversion_blocksize;
1571 else if (operand_is (name, "skip"))
1572 skip = n;
1573 else if (operand_is (name, "seek"))
1574 seek = n;
1575 else if (operand_is (name, "count"))
1576 count = n;
1577 else
1579 error (0, 0, _("unrecognized operand %s"),
1580 quote (name));
1581 usage (EXIT_FAILURE);
1584 if (n < n_min)
1585 invalid = LONGINT_INVALID;
1586 else if (n_max < n)
1587 invalid = LONGINT_OVERFLOW;
1589 if (invalid != LONGINT_OK)
1590 die (EXIT_FAILURE, invalid == LONGINT_OVERFLOW ? EOVERFLOW : 0,
1591 "%s: %s", _("invalid number"), quote (val));
1592 else if (converted_idx)
1593 *converted_idx = n;
1597 if (blocksize)
1598 input_blocksize = output_blocksize = blocksize;
1599 else
1601 /* POSIX says dd aggregates partial reads into
1602 output_blocksize if bs= is not specified. */
1603 conversions_mask |= C_TWOBUFS;
1606 if (input_blocksize == 0)
1607 input_blocksize = DEFAULT_BLOCKSIZE;
1608 if (output_blocksize == 0)
1609 output_blocksize = DEFAULT_BLOCKSIZE;
1610 if (conversion_blocksize == 0)
1611 conversions_mask &= ~(C_BLOCK | C_UNBLOCK);
1613 if (input_flags & (O_DSYNC | O_SYNC))
1614 input_flags |= O_RSYNC;
1616 if (output_flags & O_FULLBLOCK)
1618 error (0, 0, "%s: %s", _("invalid output flag"), quote ("fullblock"));
1619 usage (EXIT_FAILURE);
1622 if (input_flags & O_SEEK_BYTES)
1624 error (0, 0, "%s: %s", _("invalid input flag"), quote ("seek_bytes"));
1625 usage (EXIT_FAILURE);
1628 if (output_flags & (O_COUNT_BYTES | O_SKIP_BYTES))
1630 error (0, 0, "%s: %s", _("invalid output flag"),
1631 quote (output_flags & O_COUNT_BYTES
1632 ? "count_bytes" : "skip_bytes"));
1633 usage (EXIT_FAILURE);
1636 if (input_flags & O_SKIP_BYTES && skip != 0)
1638 skip_records = skip / input_blocksize;
1639 skip_bytes = skip % input_blocksize;
1641 else if (skip != 0)
1642 skip_records = skip;
1644 if (input_flags & O_COUNT_BYTES && count != INTMAX_MAX)
1646 max_records = count / input_blocksize;
1647 max_bytes = count % input_blocksize;
1649 else if (count != INTMAX_MAX)
1650 max_records = count;
1652 if (output_flags & O_SEEK_BYTES && seek != 0)
1654 seek_records = seek / output_blocksize;
1655 seek_bytes = seek % output_blocksize;
1657 else if (seek != 0)
1658 seek_records = seek;
1660 /* Warn about partial reads if bs=SIZE is given and iflag=fullblock
1661 is not, and if counting or skipping bytes or using direct I/O.
1662 This helps to avoid confusion with miscounts, and to avoid issues
1663 with direct I/O on GNU/Linux. */
1664 warn_partial_read =
1665 (! (conversions_mask & C_TWOBUFS) && ! (input_flags & O_FULLBLOCK)
1666 && (skip_records
1667 || (0 < max_records && max_records < INTMAX_MAX)
1668 || (input_flags | output_flags) & O_DIRECT));
1670 iread_fnc = ((input_flags & O_FULLBLOCK)
1671 ? iread_fullblock
1672 : iread);
1673 input_flags &= ~O_FULLBLOCK;
1675 if (multiple_bits_set (conversions_mask & (C_ASCII | C_EBCDIC | C_IBM)))
1676 die (EXIT_FAILURE, 0, _("cannot combine any two of {ascii,ebcdic,ibm}"));
1677 if (multiple_bits_set (conversions_mask & (C_BLOCK | C_UNBLOCK)))
1678 die (EXIT_FAILURE, 0, _("cannot combine block and unblock"));
1679 if (multiple_bits_set (conversions_mask & (C_LCASE | C_UCASE)))
1680 die (EXIT_FAILURE, 0, _("cannot combine lcase and ucase"));
1681 if (multiple_bits_set (conversions_mask & (C_EXCL | C_NOCREAT)))
1682 die (EXIT_FAILURE, 0, _("cannot combine excl and nocreat"));
1683 if (multiple_bits_set (input_flags & (O_DIRECT | O_NOCACHE))
1684 || multiple_bits_set (output_flags & (O_DIRECT | O_NOCACHE)))
1685 die (EXIT_FAILURE, 0, _("cannot combine direct and nocache"));
1687 if (input_flags & O_NOCACHE)
1689 i_nocache = true;
1690 i_nocache_eof = (max_records == 0 && max_bytes == 0);
1691 input_flags &= ~O_NOCACHE;
1693 if (output_flags & O_NOCACHE)
1695 o_nocache = true;
1696 o_nocache_eof = (max_records == 0 && max_bytes == 0);
1697 output_flags &= ~O_NOCACHE;
1701 /* Fix up translation table. */
1703 static void
1704 apply_translations (void)
1706 int i;
1708 if (conversions_mask & C_ASCII)
1709 translate_charset (ebcdic_to_ascii);
1711 if (conversions_mask & C_UCASE)
1713 for (i = 0; i < 256; i++)
1714 trans_table[i] = toupper (trans_table[i]);
1715 translation_needed = true;
1717 else if (conversions_mask & C_LCASE)
1719 for (i = 0; i < 256; i++)
1720 trans_table[i] = tolower (trans_table[i]);
1721 translation_needed = true;
1724 if (conversions_mask & C_EBCDIC)
1726 translate_charset (ascii_to_ebcdic);
1727 newline_character = ascii_to_ebcdic['\n'];
1728 space_character = ascii_to_ebcdic[' '];
1730 else if (conversions_mask & C_IBM)
1732 translate_charset (ascii_to_ibm);
1733 newline_character = ascii_to_ibm['\n'];
1734 space_character = ascii_to_ibm[' '];
1738 /* Apply the character-set translations specified by the user
1739 to the NREAD bytes in BUF. */
1741 static void
1742 translate_buffer (char *buf, idx_t nread)
1744 idx_t i;
1745 char *cp;
1746 for (i = nread, cp = buf; i; i--, cp++)
1747 *cp = trans_table[to_uchar (*cp)];
1750 /* Swap *NREAD bytes in BUF, which should have room for an extra byte
1751 after the end because the swapping is not in-place. If *SAVED_BYTE
1752 is nonnegative, also swap that initial byte from the previous call.
1753 Save the last byte into into *SAVED_BYTE if needed to make the
1754 resulting *NREAD even, and set *SAVED_BYTE to -1 otherwise.
1755 Return the buffer's adjusted start, either BUF or BUF + 1. */
1757 static char *
1758 swab_buffer (char *buf, idx_t *nread, int *saved_byte)
1760 if (*nread == 0)
1761 return buf;
1763 /* Update *SAVED_BYTE, and set PREV_SAVED to its old value. */
1764 int prev_saved = *saved_byte;
1765 if ((prev_saved < 0) == (*nread & 1))
1767 unsigned char c = buf[--*nread];
1768 *saved_byte = c;
1770 else
1771 *saved_byte = -1;
1773 /* Do the byte-swapping by moving every other byte two
1774 positions toward the end, working from the end of the buffer
1775 toward the beginning. This way we move only half the data. */
1776 for (idx_t i = *nread; 1 < i; i -= 2)
1777 buf[i] = buf[i - 2];
1779 if (prev_saved < 0)
1780 return buf + 1;
1782 buf[1] = prev_saved;
1783 ++*nread;
1784 return buf;
1787 /* Add OFFSET to the input offset, setting the overflow flag if
1788 necessary. */
1790 static void
1791 advance_input_offset (intmax_t offset)
1793 if (0 <= input_offset && INT_ADD_WRAPV (input_offset, offset, &input_offset))
1794 input_offset = -1;
1797 /* Throw away RECORDS blocks of BLOCKSIZE bytes plus BYTES bytes on
1798 file descriptor FDESC, which is open with read permission for FILE.
1799 Store up to BLOCKSIZE bytes of the data at a time in IBUF or OBUF, if
1800 necessary. RECORDS or BYTES must be nonzero. If FDESC is
1801 STDIN_FILENO, advance the input offset. Return the number of
1802 records remaining, i.e., that were not skipped because EOF was
1803 reached. If FDESC is STDOUT_FILENO, on return, BYTES is the
1804 remaining bytes in addition to the remaining records. */
1806 static intmax_t
1807 skip (int fdesc, char const *file, intmax_t records, idx_t blocksize,
1808 idx_t *bytes)
1810 /* Try lseek and if an error indicates it was an inappropriate operation --
1811 or if the file offset is not representable as an off_t --
1812 fall back on using read. */
1814 errno = 0;
1815 off_t offset;
1816 if (! INT_MULTIPLY_WRAPV (records, blocksize, &offset)
1817 && ! INT_ADD_WRAPV (offset, *bytes, &offset)
1818 && 0 <= lseek (fdesc, offset, SEEK_CUR))
1820 if (fdesc == STDIN_FILENO)
1822 struct stat st;
1823 if (ifstat (STDIN_FILENO, &st) != 0)
1824 die (EXIT_FAILURE, errno, _("cannot fstat %s"), quoteaf (file));
1825 if (usable_st_size (&st) && 0 <= input_offset
1826 && st.st_size - input_offset < offset)
1828 /* When skipping past EOF, return the number of _full_ blocks
1829 * that are not skipped, and set offset to EOF, so the caller
1830 * can determine the requested skip was not satisfied. */
1831 records = ( offset - st.st_size ) / blocksize;
1832 offset = st.st_size - input_offset;
1834 else
1835 records = 0;
1836 advance_input_offset (offset);
1838 else
1840 records = 0;
1841 *bytes = 0;
1843 return records;
1845 else
1847 int lseek_errno = errno;
1849 /* The seek request may have failed above if it was too big
1850 (> device size, > max file size, etc.)
1851 Or it may not have been done at all (> OFF_T_MAX).
1852 Therefore try to seek to the end of the file,
1853 to avoid redundant reading. */
1854 if (lseek (fdesc, 0, SEEK_END) >= 0)
1856 /* File is seekable, and we're at the end of it, and
1857 size <= OFF_T_MAX. So there's no point using read to advance. */
1859 if (!lseek_errno)
1861 /* The original seek was not attempted as offset > OFF_T_MAX.
1862 We should error for write as can't get to the desired
1863 location, even if OFF_T_MAX < max file size.
1864 For read we're not going to read any data anyway,
1865 so we should error for consistency.
1866 It would be nice to not error for /dev/{zero,null}
1867 for any offset, but that's not a significant issue. */
1868 lseek_errno = EOVERFLOW;
1871 if (fdesc == STDIN_FILENO)
1872 error (0, lseek_errno, _("%s: cannot skip"), quotef (file));
1873 else
1874 error (0, lseek_errno, _("%s: cannot seek"), quotef (file));
1875 /* If the file has a specific size and we've asked
1876 to skip/seek beyond the max allowable, then quit. */
1877 quit (EXIT_FAILURE);
1879 /* else file_size && offset > OFF_T_MAX or file ! seekable */
1881 char *buf;
1882 if (fdesc == STDIN_FILENO)
1884 alloc_ibuf ();
1885 buf = ibuf;
1887 else
1889 alloc_obuf ();
1890 buf = obuf;
1895 ssize_t nread = iread_fnc (fdesc, buf, records ? blocksize : *bytes);
1896 if (nread < 0)
1898 if (fdesc == STDIN_FILENO)
1900 error (0, errno, _("error reading %s"), quoteaf (file));
1901 if (conversions_mask & C_NOERROR)
1902 print_stats ();
1904 else
1905 error (0, lseek_errno, _("%s: cannot seek"), quotef (file));
1906 quit (EXIT_FAILURE);
1908 else if (nread == 0)
1909 break;
1910 else if (fdesc == STDIN_FILENO)
1911 advance_input_offset (nread);
1913 if (records != 0)
1914 records--;
1915 else
1916 *bytes = 0;
1918 while (records || *bytes);
1920 return records;
1924 /* Advance the input by NBYTES if possible, after a read error.
1925 The input file offset may or may not have advanced after the failed
1926 read; adjust it to point just after the bad record regardless.
1927 Return true if successful, or if the input is already known to not
1928 be seekable. */
1930 static bool
1931 advance_input_after_read_error (idx_t nbytes)
1933 if (! input_seekable)
1935 if (input_seek_errno == ESPIPE)
1936 return true;
1937 errno = input_seek_errno;
1939 else
1941 off_t offset;
1942 advance_input_offset (nbytes);
1943 if (input_offset < 0)
1945 error (0, 0, _("offset overflow while reading file %s"),
1946 quoteaf (input_file));
1947 return false;
1949 offset = lseek (STDIN_FILENO, 0, SEEK_CUR);
1950 if (0 <= offset)
1952 off_t diff;
1953 if (offset == input_offset)
1954 return true;
1955 diff = input_offset - offset;
1956 if (! (0 <= diff && diff <= nbytes) && status_level != STATUS_NONE)
1957 error (0, 0, _("warning: invalid file offset after failed read"));
1958 if (0 <= lseek (STDIN_FILENO, diff, SEEK_CUR))
1959 return true;
1960 if (errno == 0)
1961 error (0, 0, _("cannot work around kernel bug after all"));
1965 error (0, errno, _("%s: cannot seek"), quotef (input_file));
1966 return false;
1969 /* Copy NREAD bytes of BUF, with no conversions. */
1971 static void
1972 copy_simple (char const *buf, idx_t nread)
1974 char const *start = buf; /* First uncopied char in BUF. */
1978 idx_t nfree = MIN (nread, output_blocksize - oc);
1980 memcpy (obuf + oc, start, nfree);
1982 nread -= nfree; /* Update the number of bytes left to copy. */
1983 start += nfree;
1984 oc += nfree;
1985 if (oc >= output_blocksize)
1986 write_output ();
1988 while (nread != 0);
1991 /* Copy NREAD bytes of BUF, doing conv=block
1992 (pad newline-terminated records to 'conversion_blocksize',
1993 replacing the newline with trailing spaces). */
1995 static void
1996 copy_with_block (char const *buf, idx_t nread)
1998 for (idx_t i = nread; i; i--, buf++)
2000 if (*buf == newline_character)
2002 if (col < conversion_blocksize)
2004 idx_t j;
2005 for (j = col; j < conversion_blocksize; j++)
2006 output_char (space_character);
2008 col = 0;
2010 else
2012 if (col == conversion_blocksize)
2013 r_truncate++;
2014 else if (col < conversion_blocksize)
2015 output_char (*buf);
2016 col++;
2021 /* Copy NREAD bytes of BUF, doing conv=unblock
2022 (replace trailing spaces in 'conversion_blocksize'-sized records
2023 with a newline). */
2025 static void
2026 copy_with_unblock (char const *buf, idx_t nread)
2028 static idx_t pending_spaces = 0;
2030 for (idx_t i = 0; i < nread; i++)
2032 char c = buf[i];
2034 if (col++ >= conversion_blocksize)
2036 col = pending_spaces = 0; /* Wipe out any pending spaces. */
2037 i--; /* Push the char back; get it later. */
2038 output_char (newline_character);
2040 else if (c == space_character)
2041 pending_spaces++;
2042 else
2044 /* 'c' is the character after a run of spaces that were not
2045 at the end of the conversion buffer. Output them. */
2046 while (pending_spaces)
2048 output_char (space_character);
2049 --pending_spaces;
2051 output_char (c);
2056 /* Set the file descriptor flags for FD that correspond to the nonzero bits
2057 in ADD_FLAGS. The file's name is NAME. */
2059 static void
2060 set_fd_flags (int fd, int add_flags, char const *name)
2062 /* Ignore file creation flags that are no-ops on file descriptors. */
2063 add_flags &= ~ (O_NOCTTY | O_NOFOLLOW);
2065 if (add_flags)
2067 int old_flags = fcntl (fd, F_GETFL);
2068 int new_flags = old_flags | add_flags;
2069 bool ok = true;
2070 if (old_flags < 0)
2071 ok = false;
2072 else if (old_flags != new_flags)
2074 if (new_flags & (O_DIRECTORY | O_NOLINKS))
2076 /* NEW_FLAGS contains at least one file creation flag that
2077 requires some checking of the open file descriptor. */
2078 struct stat st;
2079 if (ifstat (fd, &st) != 0)
2080 ok = false;
2081 else if ((new_flags & O_DIRECTORY) && ! S_ISDIR (st.st_mode))
2083 errno = ENOTDIR;
2084 ok = false;
2086 else if ((new_flags & O_NOLINKS) && 1 < st.st_nlink)
2088 errno = EMLINK;
2089 ok = false;
2091 new_flags &= ~ (O_DIRECTORY | O_NOLINKS);
2094 if (ok && old_flags != new_flags
2095 && fcntl (fd, F_SETFL, new_flags) == -1)
2096 ok = false;
2099 if (!ok)
2100 die (EXIT_FAILURE, errno, _("setting flags for %s"), quoteaf (name));
2104 /* The main loop. */
2106 static int
2107 dd_copy (void)
2109 char *bufstart; /* Input buffer. */
2110 ssize_t nread; /* Bytes read in the current block. */
2112 /* If nonzero, then the previously read block was partial and
2113 PARTREAD was its size. */
2114 idx_t partread = 0;
2116 int exit_status = EXIT_SUCCESS;
2117 idx_t n_bytes_read;
2119 if (skip_records != 0 || skip_bytes != 0)
2121 intmax_t us_bytes;
2122 bool us_bytes_overflow =
2123 (INT_MULTIPLY_WRAPV (skip_records, input_blocksize, &us_bytes)
2124 || INT_ADD_WRAPV (skip_bytes, us_bytes, &us_bytes));
2125 off_t input_offset0 = input_offset;
2126 intmax_t us_blocks = skip (STDIN_FILENO, input_file,
2127 skip_records, input_blocksize, &skip_bytes);
2129 /* POSIX doesn't say what to do when dd detects it has been
2130 asked to skip past EOF, so I assume it's non-fatal.
2131 There are 3 reasons why there might be unskipped blocks/bytes:
2132 1. file is too small
2133 2. pipe has not enough data
2134 3. partial reads */
2135 if ((us_blocks
2136 || (0 <= input_offset
2137 && (us_bytes_overflow
2138 || us_bytes != input_offset - input_offset0)))
2139 && status_level != STATUS_NONE)
2141 error (0, 0,
2142 _("%s: cannot skip to specified offset"), quotef (input_file));
2146 if (seek_records != 0 || seek_bytes != 0)
2148 idx_t bytes = seek_bytes;
2149 intmax_t write_records = skip (STDOUT_FILENO, output_file,
2150 seek_records, output_blocksize, &bytes);
2152 if (write_records != 0 || bytes != 0)
2154 memset (obuf, 0, write_records ? output_blocksize : bytes);
2158 idx_t size = write_records ? output_blocksize : bytes;
2159 if (iwrite (STDOUT_FILENO, obuf, size) != size)
2161 error (0, errno, _("writing to %s"), quoteaf (output_file));
2162 quit (EXIT_FAILURE);
2165 if (write_records != 0)
2166 write_records--;
2167 else
2168 bytes = 0;
2170 while (write_records || bytes);
2174 if (max_records == 0 && max_bytes == 0)
2175 return exit_status;
2177 alloc_ibuf ();
2178 alloc_obuf ();
2179 int saved_byte = -1;
2181 while (true)
2183 if (status_level == STATUS_PROGRESS)
2185 xtime_t progress_time = gethrxtime ();
2186 if (next_time <= progress_time)
2188 print_xfer_stats (progress_time);
2189 next_time += XTIME_PRECISION;
2193 if (r_partial + r_full >= max_records + !!max_bytes)
2194 break;
2196 /* Zero the buffer before reading, so that if we get a read error,
2197 whatever data we are able to read is followed by zeros.
2198 This minimizes data loss. */
2199 if ((conversions_mask & C_SYNC) && (conversions_mask & C_NOERROR))
2200 memset (ibuf,
2201 (conversions_mask & (C_BLOCK | C_UNBLOCK)) ? ' ' : '\0',
2202 input_blocksize);
2204 if (r_partial + r_full >= max_records)
2205 nread = iread_fnc (STDIN_FILENO, ibuf, max_bytes);
2206 else
2207 nread = iread_fnc (STDIN_FILENO, ibuf, input_blocksize);
2209 if (nread > 0)
2211 advance_input_offset (nread);
2212 if (i_nocache)
2213 invalidate_cache (STDIN_FILENO, nread);
2215 else if (nread == 0)
2217 i_nocache_eof |= i_nocache;
2218 o_nocache_eof |= o_nocache && ! (conversions_mask & C_NOTRUNC);
2219 break; /* EOF. */
2221 else
2223 if (!(conversions_mask & C_NOERROR) || status_level != STATUS_NONE)
2224 error (0, errno, _("error reading %s"), quoteaf (input_file));
2226 if (conversions_mask & C_NOERROR)
2228 print_stats ();
2229 idx_t bad_portion = input_blocksize - partread;
2231 /* We already know this data is not cached,
2232 but call this so that correct offsets are maintained. */
2233 invalidate_cache (STDIN_FILENO, bad_portion);
2235 /* Seek past the bad block if possible. */
2236 if (!advance_input_after_read_error (bad_portion))
2238 exit_status = EXIT_FAILURE;
2240 /* Suppress duplicate diagnostics. */
2241 input_seekable = false;
2242 input_seek_errno = ESPIPE;
2244 if ((conversions_mask & C_SYNC) && !partread)
2245 /* Replace the missing input with null bytes and
2246 proceed normally. */
2247 nread = 0;
2248 else
2249 continue;
2251 else
2253 /* Write any partial block. */
2254 exit_status = EXIT_FAILURE;
2255 break;
2259 n_bytes_read = nread;
2261 if (n_bytes_read < input_blocksize)
2263 r_partial++;
2264 partread = n_bytes_read;
2265 if (conversions_mask & C_SYNC)
2267 if (!(conversions_mask & C_NOERROR))
2268 /* If C_NOERROR, we zeroed the block before reading. */
2269 memset (ibuf + n_bytes_read,
2270 (conversions_mask & (C_BLOCK | C_UNBLOCK)) ? ' ' : '\0',
2271 input_blocksize - n_bytes_read);
2272 n_bytes_read = input_blocksize;
2275 else
2277 r_full++;
2278 partread = 0;
2281 if (ibuf == obuf) /* If not C_TWOBUFS. */
2283 idx_t nwritten = iwrite (STDOUT_FILENO, obuf, n_bytes_read);
2284 w_bytes += nwritten;
2285 if (nwritten != n_bytes_read)
2287 error (0, errno, _("error writing %s"), quoteaf (output_file));
2288 return EXIT_FAILURE;
2290 else if (n_bytes_read == input_blocksize)
2291 w_full++;
2292 else
2293 w_partial++;
2294 continue;
2297 /* Do any translations on the whole buffer at once. */
2299 if (translation_needed)
2300 translate_buffer (ibuf, n_bytes_read);
2302 if (conversions_mask & C_SWAB)
2303 bufstart = swab_buffer (ibuf, &n_bytes_read, &saved_byte);
2304 else
2305 bufstart = ibuf;
2307 if (conversions_mask & C_BLOCK)
2308 copy_with_block (bufstart, n_bytes_read);
2309 else if (conversions_mask & C_UNBLOCK)
2310 copy_with_unblock (bufstart, n_bytes_read);
2311 else
2312 copy_simple (bufstart, n_bytes_read);
2315 /* If we have a char left as a result of conv=swab, output it. */
2316 if (0 <= saved_byte)
2318 char saved_char = saved_byte;
2319 if (conversions_mask & C_BLOCK)
2320 copy_with_block (&saved_char, 1);
2321 else if (conversions_mask & C_UNBLOCK)
2322 copy_with_unblock (&saved_char, 1);
2323 else
2324 output_char (saved_char);
2327 if ((conversions_mask & C_BLOCK) && col > 0)
2329 /* If the final input line didn't end with a '\n', pad
2330 the output block to 'conversion_blocksize' chars. */
2331 for (idx_t i = col; i < conversion_blocksize; i++)
2332 output_char (space_character);
2335 if (col && (conversions_mask & C_UNBLOCK))
2337 /* If there was any output, add a final '\n'. */
2338 output_char (newline_character);
2341 /* Write out the last block. */
2342 if (oc != 0)
2344 idx_t nwritten = iwrite (STDOUT_FILENO, obuf, oc);
2345 w_bytes += nwritten;
2346 if (nwritten != 0)
2347 w_partial++;
2348 if (nwritten != oc)
2350 error (0, errno, _("error writing %s"), quoteaf (output_file));
2351 return EXIT_FAILURE;
2355 /* If the last write was converted to a seek, then for a regular file
2356 or shared memory object, ftruncate to extend the size. */
2357 if (final_op_was_seek)
2359 struct stat stdout_stat;
2360 if (ifstat (STDOUT_FILENO, &stdout_stat) != 0)
2362 error (0, errno, _("cannot fstat %s"), quoteaf (output_file));
2363 return EXIT_FAILURE;
2365 if (S_ISREG (stdout_stat.st_mode) || S_TYPEISSHM (&stdout_stat))
2367 off_t output_offset = lseek (STDOUT_FILENO, 0, SEEK_CUR);
2368 if (0 <= output_offset && stdout_stat.st_size < output_offset)
2370 if (iftruncate (STDOUT_FILENO, output_offset) != 0)
2372 error (0, errno,
2373 _("failed to truncate to %" PRIdMAX " bytes"
2374 " in output file %s"),
2375 (intmax_t) output_offset, quoteaf (output_file));
2376 return EXIT_FAILURE;
2382 /* fdatasync/fsync can take a long time, so issue a final progress
2383 indication now if progress has been made since the previous indication. */
2384 if (conversions_mask & (C_FDATASYNC | C_FSYNC)
2385 && status_level == STATUS_PROGRESS
2386 && 0 <= reported_w_bytes && reported_w_bytes < w_bytes)
2387 print_xfer_stats (0);
2389 return exit_status;
2392 /* Synchronize output according to conversions_mask.
2393 Do this even if w_bytes is zero, as fsync and fdatasync
2394 flush out write requests from other processes too.
2395 Clear bits in conversions_mask so that synchronization is done only once.
2396 Return zero if successful, an exit status otherwise. */
2398 static int
2399 synchronize_output (void)
2401 int exit_status = 0;
2402 int mask = conversions_mask;
2403 conversions_mask &= ~ (C_FDATASYNC | C_FSYNC);
2405 if ((mask & C_FDATASYNC) && ifdatasync (STDOUT_FILENO) != 0)
2407 if (errno != ENOSYS && errno != EINVAL)
2409 error (0, errno, _("fdatasync failed for %s"), quoteaf (output_file));
2410 exit_status = EXIT_FAILURE;
2412 mask |= C_FSYNC;
2415 if ((mask & C_FSYNC) && ifsync (STDOUT_FILENO) != 0)
2417 error (0, errno, _("fsync failed for %s"), quoteaf (output_file));
2418 return EXIT_FAILURE;
2421 return exit_status;
2425 main (int argc, char **argv)
2427 int i;
2428 int exit_status;
2429 off_t offset;
2431 install_signal_handlers ();
2433 initialize_main (&argc, &argv);
2434 set_program_name (argv[0]);
2435 setlocale (LC_ALL, "");
2436 bindtextdomain (PACKAGE, LOCALEDIR);
2437 textdomain (PACKAGE);
2439 /* Arrange to close stdout if parse_long_options exits. */
2440 atexit (maybe_close_stdout);
2442 page_size = getpagesize ();
2444 parse_gnu_standard_options_only (argc, argv, PROGRAM_NAME, PACKAGE, Version,
2445 true, usage, AUTHORS, (char const *) NULL);
2446 close_stdout_required = false;
2448 /* Initialize translation table to identity translation. */
2449 for (i = 0; i < 256; i++)
2450 trans_table[i] = i;
2452 /* Decode arguments. */
2453 scanargs (argc, argv);
2455 apply_translations ();
2457 if (input_file == NULL)
2459 input_file = _("standard input");
2460 set_fd_flags (STDIN_FILENO, input_flags, input_file);
2462 else
2464 if (ifd_reopen (STDIN_FILENO, input_file, O_RDONLY | input_flags, 0) < 0)
2465 die (EXIT_FAILURE, errno, _("failed to open %s"),
2466 quoteaf (input_file));
2469 offset = lseek (STDIN_FILENO, 0, SEEK_CUR);
2470 input_seekable = (0 <= offset);
2471 input_offset = MAX (0, offset);
2472 input_seek_errno = errno;
2474 if (output_file == NULL)
2476 output_file = _("standard output");
2477 set_fd_flags (STDOUT_FILENO, output_flags, output_file);
2479 else
2481 mode_t perms = MODE_RW_UGO;
2482 int opts
2483 = (output_flags
2484 | (conversions_mask & C_NOCREAT ? 0 : O_CREAT)
2485 | (conversions_mask & C_EXCL ? O_EXCL : 0)
2486 | (seek_records || (conversions_mask & C_NOTRUNC) ? 0 : O_TRUNC));
2488 off_t size;
2489 if ((INT_MULTIPLY_WRAPV (seek_records, output_blocksize, &size)
2490 || INT_ADD_WRAPV (seek_bytes, size, &size))
2491 && !(conversions_mask & C_NOTRUNC))
2492 die (EXIT_FAILURE, 0,
2493 _("offset too large: "
2494 "cannot truncate to a length of seek=%"PRIdMAX""
2495 " (%td-byte) blocks"),
2496 seek_records, output_blocksize);
2498 /* Open the output file with *read* access only if we might
2499 need to read to satisfy a 'seek=' request. If we can't read
2500 the file, go ahead with write-only access; it might work. */
2501 if ((! seek_records
2502 || ifd_reopen (STDOUT_FILENO, output_file, O_RDWR | opts, perms) < 0)
2503 && (ifd_reopen (STDOUT_FILENO, output_file, O_WRONLY | opts, perms)
2504 < 0))
2505 die (EXIT_FAILURE, errno, _("failed to open %s"),
2506 quoteaf (output_file));
2508 if (seek_records != 0 && !(conversions_mask & C_NOTRUNC))
2510 if (iftruncate (STDOUT_FILENO, size) != 0)
2512 /* Complain only when ftruncate fails on a regular file, a
2513 directory, or a shared memory object, as POSIX 1003.1-2004
2514 specifies ftruncate's behavior only for these file types.
2515 For example, do not complain when Linux kernel 2.4 ftruncate
2516 fails on /dev/fd0. */
2517 int ftruncate_errno = errno;
2518 struct stat stdout_stat;
2519 if (ifstat (STDOUT_FILENO, &stdout_stat) != 0)
2521 error (0, errno, _("cannot fstat %s"),
2522 quoteaf (output_file));
2523 exit_status = EXIT_FAILURE;
2525 else if (S_ISREG (stdout_stat.st_mode)
2526 || S_ISDIR (stdout_stat.st_mode)
2527 || S_TYPEISSHM (&stdout_stat))
2529 intmax_t isize = size;
2530 error (0, ftruncate_errno,
2531 _("failed to truncate to %"PRIdMAX" bytes"
2532 " in output file %s"),
2533 isize, quoteaf (output_file));
2534 exit_status = EXIT_FAILURE;
2540 start_time = gethrxtime ();
2541 next_time = start_time + XTIME_PRECISION;
2543 exit_status = dd_copy ();
2545 int sync_status = synchronize_output ();
2546 if (sync_status)
2547 exit_status = sync_status;
2549 if (max_records == 0 && max_bytes == 0)
2551 /* Special case to invalidate cache to end of file. */
2552 if (i_nocache && !invalidate_cache (STDIN_FILENO, 0))
2554 error (0, errno, _("failed to discard cache for: %s"),
2555 quotef (input_file));
2556 exit_status = EXIT_FAILURE;
2558 if (o_nocache && !invalidate_cache (STDOUT_FILENO, 0))
2560 error (0, errno, _("failed to discard cache for: %s"),
2561 quotef (output_file));
2562 exit_status = EXIT_FAILURE;
2565 else
2567 /* Invalidate any pending region or to EOF if appropriate. */
2568 if (i_nocache || i_nocache_eof)
2569 invalidate_cache (STDIN_FILENO, 0);
2570 if (o_nocache || o_nocache_eof)
2571 invalidate_cache (STDOUT_FILENO, 0);
2574 finish_up ();
2575 return exit_status;