doc: clarify the operation of wc -L
[coreutils.git] / src / dd.c
blob6b09bc6452327736381c6e98809822dc95118e30
1 /* dd -- convert a file while copying it.
2 Copyright (C) 1985-2015 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 <http://www.gnu.org/licenses/>. */
17 /* Written by Paul Rubin, David MacKenzie, and Stuart Kemp. */
19 #include <config.h>
21 #define SWAB_ALIGN_OFFSET 2
23 #include <assert.h>
24 #include <sys/types.h>
25 #include <signal.h>
26 #include <getopt.h>
28 #include "system.h"
29 #include "close-stream.h"
30 #include "error.h"
31 #include "fd-reopen.h"
32 #include "gethrxtime.h"
33 #include "human.h"
34 #include "long-options.h"
35 #include "quote.h"
36 #include "quotearg.h"
37 #include "verror.h"
38 #include "xstrtol.h"
39 #include "xtime.h"
41 /* The official name of this program (e.g., no 'g' prefix). */
42 #define PROGRAM_NAME "dd"
44 #define AUTHORS \
45 proper_name ("Paul Rubin"), \
46 proper_name ("David MacKenzie"), \
47 proper_name ("Stuart Kemp")
49 /* Use SA_NOCLDSTOP as a proxy for whether the sigaction machinery is
50 present. */
51 #ifndef SA_NOCLDSTOP
52 # define SA_NOCLDSTOP 0
53 # define sigprocmask(How, Set, Oset) /* empty */
54 # define sigset_t int
55 # if ! HAVE_SIGINTERRUPT
56 # define siginterrupt(sig, flag) /* empty */
57 # endif
58 #endif
60 /* NonStop circa 2011 lacks SA_RESETHAND; see Bug#9076. */
61 #ifndef SA_RESETHAND
62 # define SA_RESETHAND 0
63 #endif
65 #ifndef SIGINFO
66 # define SIGINFO SIGUSR1
67 #endif
69 /* This may belong in GNULIB's fcntl module instead.
70 Define O_CIO to 0 if it is not supported by this OS. */
71 #ifndef O_CIO
72 # define O_CIO 0
73 #endif
75 /* On AIX 5.1 and AIX 5.2, O_NOCACHE is defined via <fcntl.h>
76 and would interfere with our use of that name, below. */
77 #undef O_NOCACHE
79 #if ! HAVE_FDATASYNC
80 # define fdatasync(fd) (errno = ENOSYS, -1)
81 #endif
83 #define output_char(c) \
84 do \
85 { \
86 obuf[oc++] = (c); \
87 if (oc >= output_blocksize) \
88 write_output (); \
89 } \
90 while (0)
92 /* Default input and output blocksize. */
93 #define DEFAULT_BLOCKSIZE 512
95 /* How many bytes to add to the input and output block sizes before invoking
96 malloc. See dd_copy for details. INPUT_BLOCK_SLOP must be no less than
97 OUTPUT_BLOCK_SLOP. */
98 #define INPUT_BLOCK_SLOP (2 * SWAB_ALIGN_OFFSET + 2 * page_size - 1)
99 #define OUTPUT_BLOCK_SLOP (page_size - 1)
101 /* Maximum blocksize for the given SLOP.
102 Keep it smaller than SIZE_MAX - SLOP, so that we can
103 allocate buffers that size. Keep it smaller than SSIZE_MAX, for
104 the benefit of system calls like "read". And keep it smaller than
105 OFF_T_MAX, for the benefit of the large-offset seek code. */
106 #define MAX_BLOCKSIZE(slop) MIN (SIZE_MAX - (slop), MIN (SSIZE_MAX, OFF_T_MAX))
108 /* Conversions bit masks. */
109 enum
111 C_ASCII = 01,
113 C_EBCDIC = 02,
114 C_IBM = 04,
115 C_BLOCK = 010,
116 C_UNBLOCK = 020,
117 C_LCASE = 040,
118 C_UCASE = 0100,
119 C_SWAB = 0200,
120 C_NOERROR = 0400,
121 C_NOTRUNC = 01000,
122 C_SYNC = 02000,
124 /* Use separate input and output buffers, and combine partial
125 input blocks. */
126 C_TWOBUFS = 04000,
128 C_NOCREAT = 010000,
129 C_EXCL = 020000,
130 C_FDATASYNC = 040000,
131 C_FSYNC = 0100000,
133 C_SPARSE = 0200000
136 /* Status levels. */
137 enum
139 STATUS_NONE = 1,
140 STATUS_NOXFER = 2,
141 STATUS_DEFAULT = 3,
142 STATUS_PROGRESS = 4
145 /* The name of the input file, or NULL for the standard input. */
146 static char const *input_file = NULL;
148 /* The name of the output file, or NULL for the standard output. */
149 static char const *output_file = NULL;
151 /* The page size on this host. */
152 static size_t page_size;
154 /* The number of bytes in which atomic reads are done. */
155 static size_t input_blocksize = 0;
157 /* The number of bytes in which atomic writes are done. */
158 static size_t output_blocksize = 0;
160 /* Conversion buffer size, in bytes. 0 prevents conversions. */
161 static size_t conversion_blocksize = 0;
163 /* Skip this many records of 'input_blocksize' bytes before input. */
164 static uintmax_t skip_records = 0;
166 /* Skip this many bytes before input in addition of 'skip_records'
167 records. */
168 static size_t skip_bytes = 0;
170 /* Skip this many records of 'output_blocksize' bytes before output. */
171 static uintmax_t seek_records = 0;
173 /* Skip this many bytes in addition to 'seek_records' records before
174 output. */
175 static uintmax_t seek_bytes = 0;
177 /* Whether the final output was done with a seek (rather than a write). */
178 static bool final_op_was_seek;
180 /* Copy only this many records. The default is effectively infinity. */
181 static uintmax_t max_records = (uintmax_t) -1;
183 /* Copy this many bytes in addition to 'max_records' records. */
184 static size_t max_bytes = 0;
186 /* Bit vector of conversions to apply. */
187 static int conversions_mask = 0;
189 /* Open flags for the input and output files. */
190 static int input_flags = 0;
191 static int output_flags = 0;
193 /* Status flags for what is printed to stderr. */
194 static int status_level = STATUS_DEFAULT;
196 /* If nonzero, filter characters through the translation table. */
197 static bool translation_needed = false;
199 /* Number of partial blocks written. */
200 static uintmax_t w_partial = 0;
202 /* Number of full blocks written. */
203 static uintmax_t w_full = 0;
205 /* Number of partial blocks read. */
206 static uintmax_t r_partial = 0;
208 /* Number of full blocks read. */
209 static uintmax_t r_full = 0;
211 /* Number of bytes written. */
212 static uintmax_t w_bytes = 0;
214 /* Time that dd started. */
215 static xtime_t start_time;
217 /* Previous time for periodic progress. */
218 static xtime_t previous_time;
220 /* Whether a '\n' is pending after writing progress. */
221 static bool newline_pending;
223 /* True if input is seekable. */
224 static bool input_seekable;
226 /* Error number corresponding to initial attempt to lseek input.
227 If ESPIPE, do not issue any more diagnostics about it. */
228 static int input_seek_errno;
230 /* File offset of the input, in bytes, along with a flag recording
231 whether it overflowed. */
232 static uintmax_t input_offset;
233 static bool input_offset_overflow;
235 /* True if a partial read should be diagnosed. */
236 static bool warn_partial_read;
238 /* Records truncated by conv=block. */
239 static uintmax_t r_truncate = 0;
241 /* Output representation of newline and space characters.
242 They change if we're converting to EBCDIC. */
243 static char newline_character = '\n';
244 static char space_character = ' ';
246 /* Input buffer. */
247 static char *ibuf;
249 /* Output buffer. */
250 static char *obuf;
252 /* Current index into 'obuf'. */
253 static size_t oc = 0;
255 /* Index into current line, for 'conv=block' and 'conv=unblock'. */
256 static size_t col = 0;
258 /* The set of signals that are caught. */
259 static sigset_t caught_signals;
261 /* If nonzero, the value of the pending fatal signal. */
262 static sig_atomic_t volatile interrupt_signal;
264 /* A count of the number of pending info signals that have been received. */
265 static sig_atomic_t volatile info_signal_count;
267 /* Whether to discard cache for input or output. */
268 static bool i_nocache, o_nocache;
270 /* Function used for read (to handle iflag=fullblock parameter). */
271 static ssize_t (*iread_fnc) (int fd, char *buf, size_t size);
273 /* A longest symbol in the struct symbol_values tables below. */
274 #define LONGEST_SYMBOL "count_bytes"
276 /* A symbol and the corresponding integer value. */
277 struct symbol_value
279 char symbol[sizeof LONGEST_SYMBOL];
280 int value;
283 /* Conversion symbols, for conv="...". */
284 static struct symbol_value const conversions[] =
286 {"ascii", C_ASCII | C_UNBLOCK | C_TWOBUFS}, /* EBCDIC to ASCII. */
287 {"ebcdic", C_EBCDIC | C_BLOCK | C_TWOBUFS}, /* ASCII to EBCDIC. */
288 {"ibm", C_IBM | C_BLOCK | C_TWOBUFS}, /* Different ASCII to EBCDIC. */
289 {"block", C_BLOCK | C_TWOBUFS}, /* Variable to fixed length records. */
290 {"unblock", C_UNBLOCK | C_TWOBUFS}, /* Fixed to variable length records. */
291 {"lcase", C_LCASE | C_TWOBUFS}, /* Translate upper to lower case. */
292 {"ucase", C_UCASE | C_TWOBUFS}, /* Translate lower to upper case. */
293 {"sparse", C_SPARSE}, /* Try to sparsely write output. */
294 {"swab", C_SWAB | C_TWOBUFS}, /* Swap bytes of input. */
295 {"noerror", C_NOERROR}, /* Ignore i/o errors. */
296 {"nocreat", C_NOCREAT}, /* Do not create output file. */
297 {"excl", C_EXCL}, /* Fail if the output file already exists. */
298 {"notrunc", C_NOTRUNC}, /* Do not truncate output file. */
299 {"sync", C_SYNC}, /* Pad input records to ibs with NULs. */
300 {"fdatasync", C_FDATASYNC}, /* Synchronize output data before finishing. */
301 {"fsync", C_FSYNC}, /* Also synchronize output metadata. */
302 {"", 0}
305 #define FFS_MASK(x) ((x) ^ ((x) & ((x) - 1)))
306 enum
308 /* Compute a value that's bitwise disjoint from the union
309 of all O_ values. */
310 v = ~(0
311 | O_APPEND
312 | O_BINARY
313 | O_CIO
314 | O_DIRECT
315 | O_DIRECTORY
316 | O_DSYNC
317 | O_NOATIME
318 | O_NOCTTY
319 | O_NOFOLLOW
320 | O_NOLINKS
321 | O_NONBLOCK
322 | O_SYNC
323 | O_TEXT
326 /* Use its lowest bits for private flags. */
327 O_FULLBLOCK = FFS_MASK (v),
328 v2 = v ^ O_FULLBLOCK,
330 O_NOCACHE = FFS_MASK (v2),
331 v3 = v2 ^ O_NOCACHE,
333 O_COUNT_BYTES = FFS_MASK (v3),
334 v4 = v3 ^ O_COUNT_BYTES,
336 O_SKIP_BYTES = FFS_MASK (v4),
337 v5 = v4 ^ O_SKIP_BYTES,
339 O_SEEK_BYTES = FFS_MASK (v5)
342 /* Ensure that we got something. */
343 verify (O_FULLBLOCK != 0);
344 verify (O_NOCACHE != 0);
345 verify (O_COUNT_BYTES != 0);
346 verify (O_SKIP_BYTES != 0);
347 verify (O_SEEK_BYTES != 0);
349 #define MULTIPLE_BITS_SET(i) (((i) & ((i) - 1)) != 0)
351 /* Ensure that this is a single-bit value. */
352 verify ( ! MULTIPLE_BITS_SET (O_FULLBLOCK));
353 verify ( ! MULTIPLE_BITS_SET (O_NOCACHE));
354 verify ( ! MULTIPLE_BITS_SET (O_COUNT_BYTES));
355 verify ( ! MULTIPLE_BITS_SET (O_SKIP_BYTES));
356 verify ( ! MULTIPLE_BITS_SET (O_SEEK_BYTES));
358 /* Flags, for iflag="..." and oflag="...". */
359 static struct symbol_value const flags[] =
361 {"append", O_APPEND},
362 {"binary", O_BINARY},
363 {"cio", O_CIO},
364 {"direct", O_DIRECT},
365 {"directory", O_DIRECTORY},
366 {"dsync", O_DSYNC},
367 {"noatime", O_NOATIME},
368 {"nocache", O_NOCACHE}, /* Discard cache. */
369 {"noctty", O_NOCTTY},
370 {"nofollow", HAVE_WORKING_O_NOFOLLOW ? O_NOFOLLOW : 0},
371 {"nolinks", O_NOLINKS},
372 {"nonblock", O_NONBLOCK},
373 {"sync", O_SYNC},
374 {"text", O_TEXT},
375 {"fullblock", O_FULLBLOCK}, /* Accumulate full blocks from input. */
376 {"count_bytes", O_COUNT_BYTES},
377 {"skip_bytes", O_SKIP_BYTES},
378 {"seek_bytes", O_SEEK_BYTES},
379 {"", 0}
382 /* Status, for status="...". */
383 static struct symbol_value const statuses[] =
385 {"none", STATUS_NONE},
386 {"noxfer", STATUS_NOXFER},
387 {"progress", STATUS_PROGRESS},
388 {"", 0}
391 /* Translation table formed by applying successive transformations. */
392 static unsigned char trans_table[256];
394 /* Standard translation tables, taken from POSIX 1003.1-2013.
395 Beware of imitations; there are lots of ASCII<->EBCDIC tables
396 floating around the net, perhaps valid for some applications but
397 not correct here. */
399 static char const ascii_to_ebcdic[] =
401 '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
402 '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
403 '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
404 '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
405 '\100', '\132', '\177', '\173', '\133', '\154', '\120', '\175',
406 '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
407 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
408 '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
409 '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
410 '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
411 '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
412 '\347', '\350', '\351', '\255', '\340', '\275', '\232', '\155',
413 '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
414 '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
415 '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
416 '\247', '\250', '\251', '\300', '\117', '\320', '\137', '\007',
417 '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
418 '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
419 '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
420 '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
421 '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
422 '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
423 '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
424 '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
425 '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
426 '\216', '\217', '\220', '\152', '\233', '\234', '\235', '\236',
427 '\237', '\240', '\252', '\253', '\254', '\112', '\256', '\257',
428 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
429 '\270', '\271', '\272', '\273', '\274', '\241', '\276', '\277',
430 '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
431 '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
432 '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
435 static char const ascii_to_ibm[] =
437 '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
438 '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
439 '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
440 '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
441 '\100', '\132', '\177', '\173', '\133', '\154', '\120', '\175',
442 '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
443 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
444 '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
445 '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
446 '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
447 '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
448 '\347', '\350', '\351', '\255', '\340', '\275', '\137', '\155',
449 '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
450 '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
451 '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
452 '\247', '\250', '\251', '\300', '\117', '\320', '\241', '\007',
453 '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
454 '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
455 '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
456 '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
457 '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
458 '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
459 '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
460 '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
461 '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
462 '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',
463 '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',
464 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
465 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
466 '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
467 '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
468 '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
471 static char const ebcdic_to_ascii[] =
473 '\000', '\001', '\002', '\003', '\234', '\011', '\206', '\177',
474 '\227', '\215', '\216', '\013', '\014', '\015', '\016', '\017',
475 '\020', '\021', '\022', '\023', '\235', '\205', '\010', '\207',
476 '\030', '\031', '\222', '\217', '\034', '\035', '\036', '\037',
477 '\200', '\201', '\202', '\203', '\204', '\012', '\027', '\033',
478 '\210', '\211', '\212', '\213', '\214', '\005', '\006', '\007',
479 '\220', '\221', '\026', '\223', '\224', '\225', '\226', '\004',
480 '\230', '\231', '\232', '\233', '\024', '\025', '\236', '\032',
481 '\040', '\240', '\241', '\242', '\243', '\244', '\245', '\246',
482 '\247', '\250', '\325', '\056', '\074', '\050', '\053', '\174',
483 '\046', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
484 '\260', '\261', '\041', '\044', '\052', '\051', '\073', '\176',
485 '\055', '\057', '\262', '\263', '\264', '\265', '\266', '\267',
486 '\270', '\271', '\313', '\054', '\045', '\137', '\076', '\077',
487 '\272', '\273', '\274', '\275', '\276', '\277', '\300', '\301',
488 '\302', '\140', '\072', '\043', '\100', '\047', '\075', '\042',
489 '\303', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
490 '\150', '\151', '\304', '\305', '\306', '\307', '\310', '\311',
491 '\312', '\152', '\153', '\154', '\155', '\156', '\157', '\160',
492 '\161', '\162', '\136', '\314', '\315', '\316', '\317', '\320',
493 '\321', '\345', '\163', '\164', '\165', '\166', '\167', '\170',
494 '\171', '\172', '\322', '\323', '\324', '\133', '\326', '\327',
495 '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
496 '\340', '\341', '\342', '\343', '\344', '\135', '\346', '\347',
497 '\173', '\101', '\102', '\103', '\104', '\105', '\106', '\107',
498 '\110', '\111', '\350', '\351', '\352', '\353', '\354', '\355',
499 '\175', '\112', '\113', '\114', '\115', '\116', '\117', '\120',
500 '\121', '\122', '\356', '\357', '\360', '\361', '\362', '\363',
501 '\134', '\237', '\123', '\124', '\125', '\126', '\127', '\130',
502 '\131', '\132', '\364', '\365', '\366', '\367', '\370', '\371',
503 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
504 '\070', '\071', '\372', '\373', '\374', '\375', '\376', '\377'
507 /* True if we need to close the standard output *stream*. */
508 static bool close_stdout_required = true;
510 /* The only reason to close the standard output *stream* is if
511 parse_long_options fails (as it does for --help or --version).
512 In any other case, dd uses only the STDOUT_FILENO file descriptor,
513 and the "cleanup" function calls "close (STDOUT_FILENO)".
514 Closing the file descriptor and then letting the usual atexit-run
515 close_stdout function call "fclose (stdout)" would result in a
516 harmless failure of the close syscall (with errno EBADF).
517 This function serves solely to avoid the unnecessary close_stdout
518 call, once parse_long_options has succeeded.
519 Meanwhile, we guarantee that the standard error stream is flushed,
520 by inlining the last half of close_stdout as needed. */
521 static void
522 maybe_close_stdout (void)
524 if (close_stdout_required)
525 close_stdout ();
526 else if (close_stream (stderr) != 0)
527 _exit (EXIT_FAILURE);
530 /* Like error() but handle any pending newline. */
532 static void _GL_ATTRIBUTE_FORMAT ((__printf__, 3, 4))
533 nl_error (int status, int errnum, const char *fmt, ...)
535 if (newline_pending)
537 fputc ('\n', stderr);
538 newline_pending = false;
541 va_list ap;
542 va_start (ap, fmt);
543 verror (status, errnum, fmt, ap);
544 va_end (ap);
547 #define error nl_error
549 void
550 usage (int status)
552 if (status != EXIT_SUCCESS)
553 emit_try_help ();
554 else
556 printf (_("\
557 Usage: %s [OPERAND]...\n\
558 or: %s OPTION\n\
560 program_name, program_name);
561 fputs (_("\
562 Copy a file, converting and formatting according to the operands.\n\
564 bs=BYTES read and write up to BYTES bytes at a time\n\
565 cbs=BYTES convert BYTES bytes at a time\n\
566 conv=CONVS convert the file as per the comma separated symbol list\n\
567 count=N copy only N input blocks\n\
568 ibs=BYTES read up to BYTES bytes at a time (default: 512)\n\
569 "), stdout);
570 fputs (_("\
571 if=FILE read from FILE instead of stdin\n\
572 iflag=FLAGS read as per the comma separated symbol list\n\
573 obs=BYTES write BYTES bytes at a time (default: 512)\n\
574 of=FILE write to FILE instead of stdout\n\
575 oflag=FLAGS write as per the comma separated symbol list\n\
576 seek=N skip N obs-sized blocks at start of output\n\
577 skip=N skip N ibs-sized blocks at start of input\n\
578 status=LEVEL The LEVEL of information to print to stderr;\n\
579 'none' suppresses everything but error messages,\n\
580 'noxfer' suppresses the final transfer statistics,\n\
581 'progress' shows periodic transfer statistics\n\
582 "), stdout);
583 fputs (_("\
585 N and BYTES may be followed by the following multiplicative suffixes:\n\
586 c =1, w =2, b =512, kB =1000, K =1024, MB =1000*1000, M =1024*1024, xM =M\n\
587 GB =1000*1000*1000, G =1024*1024*1024, and so on for T, P, E, Z, Y.\n\
589 Each CONV symbol may be:\n\
591 "), stdout);
592 fputs (_("\
593 ascii from EBCDIC to ASCII\n\
594 ebcdic from ASCII to EBCDIC\n\
595 ibm from ASCII to alternate EBCDIC\n\
596 block pad newline-terminated records with spaces to cbs-size\n\
597 unblock replace trailing spaces in cbs-size records with newline\n\
598 lcase change upper case to lower case\n\
599 ucase change lower case to upper case\n\
600 sparse try to seek rather than write the output for NUL input blocks\n\
601 swab swap every pair of input bytes\n\
602 sync pad every input block with NULs to ibs-size; when used\n\
603 with block or unblock, pad with spaces rather than NULs\n\
604 "), stdout);
605 fputs (_("\
606 excl fail if the output file already exists\n\
607 nocreat do not create the output file\n\
608 notrunc do not truncate the output file\n\
609 noerror continue after read errors\n\
610 fdatasync physically write output file data before finishing\n\
611 fsync likewise, but also write metadata\n\
612 "), stdout);
613 fputs (_("\
615 Each FLAG symbol may be:\n\
617 append append mode (makes sense only for output; conv=notrunc suggested)\n\
618 "), stdout);
619 if (O_CIO)
620 fputs (_(" cio use concurrent I/O for data\n"), stdout);
621 if (O_DIRECT)
622 fputs (_(" direct use direct I/O for data\n"), stdout);
623 if (O_DIRECTORY)
624 fputs (_(" directory fail unless a directory\n"), stdout);
625 if (O_DSYNC)
626 fputs (_(" dsync use synchronized I/O for data\n"), stdout);
627 if (O_SYNC)
628 fputs (_(" sync likewise, but also for metadata\n"), stdout);
629 fputs (_(" fullblock accumulate full blocks of input (iflag only)\n"),
630 stdout);
631 if (O_NONBLOCK)
632 fputs (_(" nonblock use non-blocking I/O\n"), stdout);
633 if (O_NOATIME)
634 fputs (_(" noatime do not update access time\n"), stdout);
635 #if HAVE_POSIX_FADVISE
636 if (O_NOCACHE)
637 fputs (_(" nocache discard cached data\n"), stdout);
638 #endif
639 if (O_NOCTTY)
640 fputs (_(" noctty do not assign controlling terminal from file\n"),
641 stdout);
642 if (HAVE_WORKING_O_NOFOLLOW)
643 fputs (_(" nofollow do not follow symlinks\n"), stdout);
644 if (O_NOLINKS)
645 fputs (_(" nolinks fail if multiply-linked\n"), stdout);
646 if (O_BINARY)
647 fputs (_(" binary use binary I/O for data\n"), stdout);
648 if (O_TEXT)
649 fputs (_(" text use text I/O for data\n"), stdout);
650 if (O_COUNT_BYTES)
651 fputs (_(" count_bytes treat 'count=N' as a byte count (iflag only)\n\
652 "), stdout);
653 if (O_SKIP_BYTES)
654 fputs (_(" skip_bytes treat 'skip=N' as a byte count (iflag only)\n\
655 "), stdout);
656 if (O_SEEK_BYTES)
657 fputs (_(" seek_bytes treat 'seek=N' as a byte count (oflag only)\n\
658 "), stdout);
661 printf (_("\
663 Sending a %s signal to a running 'dd' process makes it\n\
664 print I/O statistics to standard error and then resume copying.\n\
666 Options are:\n\
668 "), SIGINFO == SIGUSR1 ? "USR1" : "INFO");
671 fputs (HELP_OPTION_DESCRIPTION, stdout);
672 fputs (VERSION_OPTION_DESCRIPTION, stdout);
673 emit_ancillary_info (PROGRAM_NAME);
675 exit (status);
678 static char *
679 human_size (size_t n)
681 static char hbuf[LONGEST_HUMAN_READABLE + 1];
682 int human_opts =
683 (human_autoscale | human_round_to_nearest | human_base_1024
684 | human_space_before_unit | human_SI | human_B);
685 return human_readable (n, hbuf, human_opts, 1, 1);
688 /* Ensure input buffer IBUF is allocated. */
690 static void
691 alloc_ibuf (void)
693 if (ibuf)
694 return;
696 char *real_buf = malloc (input_blocksize + INPUT_BLOCK_SLOP);
697 if (!real_buf)
698 error (EXIT_FAILURE, 0,
699 _("memory exhausted by input buffer of size %"PRIuMAX" bytes (%s)"),
700 (uintmax_t) input_blocksize, human_size (input_blocksize));
702 real_buf += SWAB_ALIGN_OFFSET; /* allow space for swab */
704 ibuf = ptr_align (real_buf, page_size);
707 /* Ensure output buffer OBUF is allocated/initialized. */
709 static void
710 alloc_obuf (void)
712 if (obuf)
713 return;
715 if (conversions_mask & C_TWOBUFS)
717 /* Page-align the output buffer, too. */
718 char *real_obuf = malloc (output_blocksize + OUTPUT_BLOCK_SLOP);
719 if (!real_obuf)
720 error (EXIT_FAILURE, 0,
721 _("memory exhausted by output buffer of size %"PRIuMAX
722 " bytes (%s)"),
723 (uintmax_t) output_blocksize, human_size (output_blocksize));
724 obuf = ptr_align (real_obuf, page_size);
726 else
728 alloc_ibuf ();
729 obuf = ibuf;
732 /* Write a sentinel to the slop after the buffer,
733 to allow efficient checking for NUL blocks. */
734 assert (sizeof (uintptr_t) <= OUTPUT_BLOCK_SLOP);
735 memset (obuf + output_blocksize, 1, sizeof (uintptr_t));
738 static void
739 translate_charset (char const *new_trans)
741 int i;
743 for (i = 0; i < 256; i++)
744 trans_table[i] = new_trans[trans_table[i]];
745 translation_needed = true;
748 /* Return true if I has more than one bit set. I must be nonnegative. */
750 static inline bool
751 multiple_bits_set (int i)
753 return MULTIPLE_BITS_SET (i);
756 /* Print transfer statistics. */
758 static void
759 print_xfer_stats (xtime_t progress_time) {
760 char hbuf[LONGEST_HUMAN_READABLE + 1];
761 int human_opts =
762 (human_autoscale | human_round_to_nearest
763 | human_space_before_unit | human_SI | human_B);
764 double delta_s;
765 char const *bytes_per_second;
767 if (progress_time)
768 fputc ('\r', stderr);
770 /* Use integer arithmetic to compute the transfer rate,
771 since that makes it easy to use SI abbreviations. */
773 fprintf (stderr,
774 ngettext ("%"PRIuMAX" byte (%s) copied",
775 "%"PRIuMAX" bytes (%s) copied",
776 select_plural (w_bytes)),
777 w_bytes,
778 human_readable (w_bytes, hbuf, human_opts, 1, 1));
780 xtime_t now = progress_time ? progress_time : gethrxtime ();
782 if (start_time < now)
784 double XTIME_PRECISIONe0 = XTIME_PRECISION;
785 uintmax_t delta_xtime = now;
786 delta_xtime -= start_time;
787 delta_s = delta_xtime / XTIME_PRECISIONe0;
788 bytes_per_second = human_readable (w_bytes, hbuf, human_opts,
789 XTIME_PRECISION, delta_xtime);
791 else
793 delta_s = 0;
794 bytes_per_second = _("Infinity B");
797 /* TRANSLATORS: The two instances of "s" in this string are the SI
798 symbol "s" (meaning second), and should not be translated.
800 This format used to be:
802 ngettext (", %g second, %s/s\n", ", %g seconds, %s/s\n", delta_s == 1)
804 but that was incorrect for languages like Polish. To fix this
805 bug we now use SI symbols even though they're a bit more
806 confusing in English. */
807 char const *time_fmt = _(", %g s, %s/s\n");
808 if (progress_time)
809 time_fmt = _(", %.6f s, %s/s"); /* OK with '\r' as increasing width. */
810 fprintf (stderr, time_fmt, delta_s, bytes_per_second);
812 newline_pending = !!progress_time;
815 static void
816 print_stats (void)
818 if (status_level == STATUS_NONE)
819 return;
821 if (newline_pending)
823 fputc ('\n', stderr);
824 newline_pending = false;
827 fprintf (stderr,
828 _("%"PRIuMAX"+%"PRIuMAX" records in\n"
829 "%"PRIuMAX"+%"PRIuMAX" records out\n"),
830 r_full, r_partial, w_full, w_partial);
832 if (r_truncate != 0)
833 fprintf (stderr,
834 ngettext ("%"PRIuMAX" truncated record\n",
835 "%"PRIuMAX" truncated records\n",
836 select_plural (r_truncate)),
837 r_truncate);
839 if (status_level == STATUS_NOXFER)
840 return;
842 print_xfer_stats (0);
845 /* An ordinary signal was received; arrange for the program to exit. */
847 static void
848 interrupt_handler (int sig)
850 if (! SA_RESETHAND)
851 signal (sig, SIG_DFL);
852 interrupt_signal = sig;
855 /* An info signal was received; arrange for the program to print status. */
857 static void
858 siginfo_handler (int sig)
860 if (! SA_NOCLDSTOP)
861 signal (sig, siginfo_handler);
862 info_signal_count++;
865 /* Install the signal handlers. */
867 static void
868 install_signal_handlers (void)
870 bool catch_siginfo = ! (SIGINFO == SIGUSR1 && getenv ("POSIXLY_CORRECT"));
872 #if SA_NOCLDSTOP
874 struct sigaction act;
875 sigemptyset (&caught_signals);
876 if (catch_siginfo)
877 sigaddset (&caught_signals, SIGINFO);
878 sigaction (SIGINT, NULL, &act);
879 if (act.sa_handler != SIG_IGN)
880 sigaddset (&caught_signals, SIGINT);
881 act.sa_mask = caught_signals;
883 if (sigismember (&caught_signals, SIGINFO))
885 act.sa_handler = siginfo_handler;
886 /* Note we don't use SA_RESTART here and instead
887 handle EINTR explicitly in iftruncate() etc.
888 to avoid blocking on noncommitted read()/write() calls. */
889 act.sa_flags = 0;
890 sigaction (SIGINFO, &act, NULL);
893 if (sigismember (&caught_signals, SIGINT))
895 act.sa_handler = interrupt_handler;
896 act.sa_flags = SA_NODEFER | SA_RESETHAND;
897 sigaction (SIGINT, &act, NULL);
900 #else
902 if (catch_siginfo)
904 signal (SIGINFO, siginfo_handler);
905 siginterrupt (SIGINFO, 1);
907 if (signal (SIGINT, SIG_IGN) != SIG_IGN)
909 signal (SIGINT, interrupt_handler);
910 siginterrupt (SIGINT, 1);
912 #endif
915 static void
916 cleanup (void)
918 if (close (STDIN_FILENO) < 0)
919 error (EXIT_FAILURE, errno,
920 _("closing input file %s"), quote (input_file));
922 /* Don't remove this call to close, even though close_stdout
923 closes standard output. This close is necessary when cleanup
924 is called as part of a signal handler. */
925 if (close (STDOUT_FILENO) < 0)
926 error (EXIT_FAILURE, errno,
927 _("closing output file %s"), quote (output_file));
930 /* Process any pending signals. If signals are caught, this function
931 should be called periodically. Ideally there should never be an
932 unbounded amount of time when signals are not being processed. */
934 static void
935 process_signals (void)
937 while (interrupt_signal || info_signal_count)
939 int interrupt;
940 int infos;
941 sigset_t oldset;
943 sigprocmask (SIG_BLOCK, &caught_signals, &oldset);
945 /* Reload interrupt_signal and info_signal_count, in case a new
946 signal was handled before sigprocmask took effect. */
947 interrupt = interrupt_signal;
948 infos = info_signal_count;
950 if (infos)
951 info_signal_count = infos - 1;
953 sigprocmask (SIG_SETMASK, &oldset, NULL);
955 if (interrupt)
956 cleanup ();
957 print_stats ();
958 if (interrupt)
959 raise (interrupt);
963 static void
964 finish_up (void)
966 cleanup ();
967 print_stats ();
968 process_signals ();
971 static void ATTRIBUTE_NORETURN
972 quit (int code)
974 finish_up ();
975 exit (code);
978 /* Return LEN rounded down to a multiple of PAGE_SIZE
979 while storing the remainder internally per FD.
980 Pass LEN == 0 to get the current remainder. */
982 static off_t
983 cache_round (int fd, off_t len)
985 static off_t i_pending, o_pending;
986 off_t *pending = (fd == STDIN_FILENO ? &i_pending : &o_pending);
988 if (len)
990 uintmax_t c_pending = *pending + len;
991 *pending = c_pending % page_size;
992 if (c_pending > *pending)
993 len = c_pending - *pending;
994 else
995 len = 0;
997 else
998 len = *pending;
1000 return len;
1003 /* Discard the cache from the current offset of either
1004 STDIN_FILENO or STDOUT_FILENO.
1005 Return true on success. */
1007 static bool
1008 invalidate_cache (int fd, off_t len)
1010 int adv_ret = -1;
1012 /* Minimize syscalls. */
1013 off_t clen = cache_round (fd, len);
1014 if (len && !clen)
1015 return true; /* Don't advise this time. */
1016 if (!len && !clen && max_records)
1017 return true; /* Nothing pending. */
1018 off_t pending = len ? cache_round (fd, 0) : 0;
1020 if (fd == STDIN_FILENO)
1022 if (input_seekable)
1024 /* Note we're being careful here to only invalidate what
1025 we've read, so as not to dump any read ahead cache. */
1026 #if HAVE_POSIX_FADVISE
1027 adv_ret = posix_fadvise (fd, input_offset - clen - pending, clen,
1028 POSIX_FADV_DONTNEED);
1029 #else
1030 errno = ENOTSUP;
1031 #endif
1033 else
1034 errno = ESPIPE;
1036 else if (fd == STDOUT_FILENO)
1038 static off_t output_offset = -2;
1040 if (output_offset != -1)
1042 if (0 > output_offset)
1044 output_offset = lseek (fd, 0, SEEK_CUR);
1045 output_offset -= clen + pending;
1047 if (0 <= output_offset)
1049 #if HAVE_POSIX_FADVISE
1050 adv_ret = posix_fadvise (fd, output_offset, clen,
1051 POSIX_FADV_DONTNEED);
1052 #else
1053 errno = ENOTSUP;
1054 #endif
1055 output_offset += clen + pending;
1060 return adv_ret != -1 ? true : false;
1063 /* Read from FD into the buffer BUF of size SIZE, processing any
1064 signals that arrive before bytes are read. Return the number of
1065 bytes read if successful, -1 (setting errno) on failure. */
1067 static ssize_t
1068 iread (int fd, char *buf, size_t size)
1070 ssize_t nread;
1074 process_signals ();
1075 nread = read (fd, buf, size);
1077 while (nread < 0 && errno == EINTR);
1079 /* Short read may be due to received signal. */
1080 if (0 < nread && nread < size)
1081 process_signals ();
1083 if (0 < nread && warn_partial_read)
1085 static ssize_t prev_nread;
1087 if (0 < prev_nread && prev_nread < size)
1089 uintmax_t prev = prev_nread;
1090 if (status_level != STATUS_NONE)
1091 error (0, 0, ngettext (("warning: partial read (%"PRIuMAX" byte); "
1092 "suggest iflag=fullblock"),
1093 ("warning: partial read (%"PRIuMAX" bytes); "
1094 "suggest iflag=fullblock"),
1095 select_plural (prev)),
1096 prev);
1097 warn_partial_read = false;
1100 prev_nread = nread;
1103 return nread;
1106 /* Wrapper around iread function to accumulate full blocks. */
1107 static ssize_t
1108 iread_fullblock (int fd, char *buf, size_t size)
1110 ssize_t nread = 0;
1112 while (0 < size)
1114 ssize_t ncurr = iread (fd, buf, size);
1115 if (ncurr < 0)
1116 return ncurr;
1117 if (ncurr == 0)
1118 break;
1119 nread += ncurr;
1120 buf += ncurr;
1121 size -= ncurr;
1124 return nread;
1127 /* Write to FD the buffer BUF of size SIZE, processing any signals
1128 that arrive. Return the number of bytes written, setting errno if
1129 this is less than SIZE. Keep trying if there are partial
1130 writes. */
1132 static size_t
1133 iwrite (int fd, char const *buf, size_t size)
1135 size_t total_written = 0;
1137 if ((output_flags & O_DIRECT) && size < output_blocksize)
1139 int old_flags = fcntl (STDOUT_FILENO, F_GETFL);
1140 if (fcntl (STDOUT_FILENO, F_SETFL, old_flags & ~O_DIRECT) != 0
1141 && status_level != STATUS_NONE)
1142 error (0, errno, _("failed to turn off O_DIRECT: %s"),
1143 quote (output_file));
1145 /* Since we have just turned off O_DIRECT for the final write,
1146 here we try to preserve some of its semantics. First, use
1147 posix_fadvise to tell the system not to pollute the buffer
1148 cache with this data. Don't bother to diagnose lseek or
1149 posix_fadvise failure. */
1150 invalidate_cache (STDOUT_FILENO, 0);
1152 /* Attempt to ensure that that final block is committed
1153 to disk as quickly as possible. */
1154 conversions_mask |= C_FSYNC;
1157 while (total_written < size)
1159 ssize_t nwritten = 0;
1160 process_signals ();
1162 /* Perform a seek for a NUL block if sparse output is enabled. */
1163 final_op_was_seek = false;
1164 if ((conversions_mask & C_SPARSE) && is_nul (buf, size))
1166 if (lseek (fd, size, SEEK_CUR) < 0)
1168 conversions_mask &= ~C_SPARSE;
1169 /* Don't warn about the advisory sparse request. */
1171 else
1173 final_op_was_seek = true;
1174 nwritten = size;
1178 if (!nwritten)
1179 nwritten = write (fd, buf + total_written, size - total_written);
1181 if (nwritten < 0)
1183 if (errno != EINTR)
1184 break;
1186 else if (nwritten == 0)
1188 /* Some buggy drivers return 0 when one tries to write beyond
1189 a device's end. (Example: Linux kernel 1.2.13 on /dev/fd0.)
1190 Set errno to ENOSPC so they get a sensible diagnostic. */
1191 errno = ENOSPC;
1192 break;
1194 else
1195 total_written += nwritten;
1198 if (o_nocache && total_written)
1199 invalidate_cache (fd, total_written);
1201 return total_written;
1204 /* Write, then empty, the output buffer 'obuf'. */
1206 static void
1207 write_output (void)
1209 size_t nwritten = iwrite (STDOUT_FILENO, obuf, output_blocksize);
1210 w_bytes += nwritten;
1211 if (nwritten != output_blocksize)
1213 error (0, errno, _("writing to %s"), quote (output_file));
1214 if (nwritten != 0)
1215 w_partial++;
1216 quit (EXIT_FAILURE);
1218 else
1219 w_full++;
1220 oc = 0;
1223 /* Restart on EINTR from fd_reopen(). */
1225 static int
1226 ifd_reopen (int desired_fd, char const *file, int flag, mode_t mode)
1228 int ret;
1232 process_signals ();
1233 ret = fd_reopen (desired_fd, file, flag, mode);
1235 while (ret < 0 && errno == EINTR);
1237 return ret;
1240 /* Restart on EINTR from ftruncate(). */
1242 static int
1243 iftruncate (int fd, off_t length)
1245 int ret;
1249 process_signals ();
1250 ret = ftruncate (fd, length);
1252 while (ret < 0 && errno == EINTR);
1254 return ret;
1257 /* Return true if STR is of the form "PATTERN" or "PATTERNDELIM...". */
1259 static bool _GL_ATTRIBUTE_PURE
1260 operand_matches (char const *str, char const *pattern, char delim)
1262 while (*pattern)
1263 if (*str++ != *pattern++)
1264 return false;
1265 return !*str || *str == delim;
1268 /* Interpret one "conv=..." or similar operand STR according to the
1269 symbols in TABLE, returning the flags specified. If the operand
1270 cannot be parsed, use ERROR_MSGID to generate a diagnostic. */
1272 static int
1273 parse_symbols (char const *str, struct symbol_value const *table,
1274 bool exclusive, char const *error_msgid)
1276 int value = 0;
1278 while (true)
1280 char const *strcomma = strchr (str, ',');
1281 struct symbol_value const *entry;
1283 for (entry = table;
1284 ! (operand_matches (str, entry->symbol, ',') && entry->value);
1285 entry++)
1287 if (! entry->symbol[0])
1289 size_t slen = strcomma ? strcomma - str : strlen (str);
1290 error (0, 0, "%s: %s", _(error_msgid),
1291 quotearg_n_style_mem (0, locale_quoting_style, str, slen));
1292 usage (EXIT_FAILURE);
1296 if (exclusive)
1297 value = entry->value;
1298 else
1299 value |= entry->value;
1300 if (!strcomma)
1301 break;
1302 str = strcomma + 1;
1305 return value;
1308 /* Return the value of STR, interpreted as a non-negative decimal integer,
1309 optionally multiplied by various values.
1310 Set *INVALID to a nonzero error value if STR does not represent a
1311 number in this format. */
1313 static uintmax_t
1314 parse_integer (const char *str, strtol_error *invalid)
1316 uintmax_t n;
1317 char *suffix;
1318 strtol_error e = xstrtoumax (str, &suffix, 10, &n, "bcEGkKMPTwYZ0");
1320 if (e == LONGINT_INVALID_SUFFIX_CHAR && *suffix == 'x')
1322 uintmax_t multiplier = parse_integer (suffix + 1, invalid);
1324 if (multiplier != 0 && n * multiplier / multiplier != n)
1326 *invalid = LONGINT_OVERFLOW;
1327 return 0;
1330 n *= multiplier;
1332 else if (e != LONGINT_OK)
1334 *invalid = e;
1335 return 0;
1338 return n;
1341 /* OPERAND is of the form "X=...". Return true if X is NAME. */
1343 static bool _GL_ATTRIBUTE_PURE
1344 operand_is (char const *operand, char const *name)
1346 return operand_matches (operand, name, '=');
1349 static void
1350 scanargs (int argc, char *const *argv)
1352 int i;
1353 size_t blocksize = 0;
1354 uintmax_t count = (uintmax_t) -1;
1355 uintmax_t skip = 0;
1356 uintmax_t seek = 0;
1358 for (i = optind; i < argc; i++)
1360 char const *name = argv[i];
1361 char const *val = strchr (name, '=');
1363 if (val == NULL)
1365 error (0, 0, _("unrecognized operand %s"), quote (name));
1366 usage (EXIT_FAILURE);
1368 val++;
1370 if (operand_is (name, "if"))
1371 input_file = val;
1372 else if (operand_is (name, "of"))
1373 output_file = val;
1374 else if (operand_is (name, "conv"))
1375 conversions_mask |= parse_symbols (val, conversions, false,
1376 N_("invalid conversion"));
1377 else if (operand_is (name, "iflag"))
1378 input_flags |= parse_symbols (val, flags, false,
1379 N_("invalid input flag"));
1380 else if (operand_is (name, "oflag"))
1381 output_flags |= parse_symbols (val, flags, false,
1382 N_("invalid output flag"));
1383 else if (operand_is (name, "status"))
1384 status_level = parse_symbols (val, statuses, true,
1385 N_("invalid status level"));
1386 else
1388 strtol_error invalid = LONGINT_OK;
1389 uintmax_t n = parse_integer (val, &invalid);
1390 uintmax_t n_min = 0;
1391 uintmax_t n_max = UINTMAX_MAX;
1393 if (operand_is (name, "ibs"))
1395 n_min = 1;
1396 n_max = MAX_BLOCKSIZE (INPUT_BLOCK_SLOP);
1397 input_blocksize = n;
1399 else if (operand_is (name, "obs"))
1401 n_min = 1;
1402 n_max = MAX_BLOCKSIZE (OUTPUT_BLOCK_SLOP);
1403 output_blocksize = n;
1405 else if (operand_is (name, "bs"))
1407 n_min = 1;
1408 n_max = MAX_BLOCKSIZE (INPUT_BLOCK_SLOP);
1409 blocksize = n;
1411 else if (operand_is (name, "cbs"))
1413 n_min = 1;
1414 n_max = SIZE_MAX;
1415 conversion_blocksize = n;
1417 else if (operand_is (name, "skip"))
1418 skip = n;
1419 else if (operand_is (name, "seek"))
1420 seek = n;
1421 else if (operand_is (name, "count"))
1422 count = n;
1423 else
1425 error (0, 0, _("unrecognized operand %s"), quote (name));
1426 usage (EXIT_FAILURE);
1429 if (n < n_min)
1430 invalid = LONGINT_INVALID;
1431 else if (n_max < n)
1432 invalid = LONGINT_OVERFLOW;
1434 if (invalid != LONGINT_OK)
1435 error (EXIT_FAILURE, invalid == LONGINT_OVERFLOW ? EOVERFLOW : 0,
1436 "%s: %s", _("invalid number"), quote (val));
1440 if (blocksize)
1441 input_blocksize = output_blocksize = blocksize;
1442 else
1444 /* POSIX says dd aggregates partial reads into
1445 output_blocksize if bs= is not specified. */
1446 conversions_mask |= C_TWOBUFS;
1449 if (input_blocksize == 0)
1450 input_blocksize = DEFAULT_BLOCKSIZE;
1451 if (output_blocksize == 0)
1452 output_blocksize = DEFAULT_BLOCKSIZE;
1453 if (conversion_blocksize == 0)
1454 conversions_mask &= ~(C_BLOCK | C_UNBLOCK);
1456 if (input_flags & (O_DSYNC | O_SYNC))
1457 input_flags |= O_RSYNC;
1459 if (output_flags & O_FULLBLOCK)
1461 error (0, 0, "%s: %s", _("invalid output flag"), "'fullblock'");
1462 usage (EXIT_FAILURE);
1465 if (input_flags & O_SEEK_BYTES)
1467 error (0, 0, "%s: %s", _("invalid input flag"), "'seek_bytes'");
1468 usage (EXIT_FAILURE);
1471 if (output_flags & (O_COUNT_BYTES | O_SKIP_BYTES))
1473 error (0, 0, "%s: %s", _("invalid output flag"),
1474 output_flags & O_COUNT_BYTES ? "'count_bytes'" : "'skip_bytes'");
1475 usage (EXIT_FAILURE);
1478 if (input_flags & O_SKIP_BYTES && skip != 0)
1480 skip_records = skip / input_blocksize;
1481 skip_bytes = skip % input_blocksize;
1483 else if (skip != 0)
1484 skip_records = skip;
1486 if (input_flags & O_COUNT_BYTES && count != (uintmax_t) -1)
1488 max_records = count / input_blocksize;
1489 max_bytes = count % input_blocksize;
1491 else if (count != (uintmax_t) -1)
1492 max_records = count;
1494 if (output_flags & O_SEEK_BYTES && seek != 0)
1496 seek_records = seek / output_blocksize;
1497 seek_bytes = seek % output_blocksize;
1499 else if (seek != 0)
1500 seek_records = seek;
1502 /* Warn about partial reads if bs=SIZE is given and iflag=fullblock
1503 is not, and if counting or skipping bytes or using direct I/O.
1504 This helps to avoid confusion with miscounts, and to avoid issues
1505 with direct I/O on GNU/Linux. */
1506 warn_partial_read =
1507 (! (conversions_mask & C_TWOBUFS) && ! (input_flags & O_FULLBLOCK)
1508 && (skip_records
1509 || (0 < max_records && max_records < (uintmax_t) -1)
1510 || (input_flags | output_flags) & O_DIRECT));
1512 iread_fnc = ((input_flags & O_FULLBLOCK)
1513 ? iread_fullblock
1514 : iread);
1515 input_flags &= ~O_FULLBLOCK;
1517 if (multiple_bits_set (conversions_mask & (C_ASCII | C_EBCDIC | C_IBM)))
1518 error (EXIT_FAILURE, 0, _("cannot combine any two of {ascii,ebcdic,ibm}"));
1519 if (multiple_bits_set (conversions_mask & (C_BLOCK | C_UNBLOCK)))
1520 error (EXIT_FAILURE, 0, _("cannot combine block and unblock"));
1521 if (multiple_bits_set (conversions_mask & (C_LCASE | C_UCASE)))
1522 error (EXIT_FAILURE, 0, _("cannot combine lcase and ucase"));
1523 if (multiple_bits_set (conversions_mask & (C_EXCL | C_NOCREAT)))
1524 error (EXIT_FAILURE, 0, _("cannot combine excl and nocreat"));
1525 if (multiple_bits_set (input_flags & (O_DIRECT | O_NOCACHE))
1526 || multiple_bits_set (output_flags & (O_DIRECT | O_NOCACHE)))
1527 error (EXIT_FAILURE, 0, _("cannot combine direct and nocache"));
1529 if (input_flags & O_NOCACHE)
1531 i_nocache = true;
1532 input_flags &= ~O_NOCACHE;
1534 if (output_flags & O_NOCACHE)
1536 o_nocache = true;
1537 output_flags &= ~O_NOCACHE;
1541 /* Fix up translation table. */
1543 static void
1544 apply_translations (void)
1546 int i;
1548 if (conversions_mask & C_ASCII)
1549 translate_charset (ebcdic_to_ascii);
1551 if (conversions_mask & C_UCASE)
1553 for (i = 0; i < 256; i++)
1554 trans_table[i] = toupper (trans_table[i]);
1555 translation_needed = true;
1557 else if (conversions_mask & C_LCASE)
1559 for (i = 0; i < 256; i++)
1560 trans_table[i] = tolower (trans_table[i]);
1561 translation_needed = true;
1564 if (conversions_mask & C_EBCDIC)
1566 translate_charset (ascii_to_ebcdic);
1567 newline_character = ascii_to_ebcdic['\n'];
1568 space_character = ascii_to_ebcdic[' '];
1570 else if (conversions_mask & C_IBM)
1572 translate_charset (ascii_to_ibm);
1573 newline_character = ascii_to_ibm['\n'];
1574 space_character = ascii_to_ibm[' '];
1578 /* Apply the character-set translations specified by the user
1579 to the NREAD bytes in BUF. */
1581 static void
1582 translate_buffer (char *buf, size_t nread)
1584 char *cp;
1585 size_t i;
1587 for (i = nread, cp = buf; i; i--, cp++)
1588 *cp = trans_table[to_uchar (*cp)];
1591 /* If true, the last char from the previous call to 'swab_buffer'
1592 is saved in 'saved_char'. */
1593 static bool char_is_saved = false;
1595 /* Odd char from previous call. */
1596 static char saved_char;
1598 /* Swap NREAD bytes in BUF, plus possibly an initial char from the
1599 previous call. If NREAD is odd, save the last char for the
1600 next call. Return the new start of the BUF buffer. */
1602 static char *
1603 swab_buffer (char *buf, size_t *nread)
1605 char *bufstart = buf;
1606 char *cp;
1607 size_t i;
1609 /* Is a char left from last time? */
1610 if (char_is_saved)
1612 *--bufstart = saved_char;
1613 (*nread)++;
1614 char_is_saved = false;
1617 if (*nread & 1)
1619 /* An odd number of chars are in the buffer. */
1620 saved_char = bufstart[--*nread];
1621 char_is_saved = true;
1624 /* Do the byte-swapping by moving every second character two
1625 positions toward the end, working from the end of the buffer
1626 toward the beginning. This way we only move half of the data. */
1628 cp = bufstart + *nread; /* Start one char past the last. */
1629 for (i = *nread / 2; i; i--, cp -= 2)
1630 *cp = *(cp - 2);
1632 return ++bufstart;
1635 /* Add OFFSET to the input offset, setting the overflow flag if
1636 necessary. */
1638 static void
1639 advance_input_offset (uintmax_t offset)
1641 input_offset += offset;
1642 if (input_offset < offset)
1643 input_offset_overflow = true;
1646 /* This is a wrapper for lseek. It detects and warns about a kernel
1647 bug that makes lseek a no-op for tape devices, even though the kernel
1648 lseek return value suggests that the function succeeded.
1650 The parameters are the same as those of the lseek function, but
1651 with the addition of FILENAME, the name of the file associated with
1652 descriptor FDESC. The file name is used solely in the warning that's
1653 printed when the bug is detected. Return the same value that lseek
1654 would have returned, but when the lseek bug is detected, return -1
1655 to indicate that lseek failed.
1657 The offending behavior has been confirmed with an Exabyte SCSI tape
1658 drive accessed via /dev/nst0 on both Linux 2.2.17 and 2.4.16 kernels. */
1660 #ifdef __linux__
1662 # include <sys/mtio.h>
1664 # define MT_SAME_POSITION(P, Q) \
1665 ((P).mt_resid == (Q).mt_resid \
1666 && (P).mt_fileno == (Q).mt_fileno \
1667 && (P).mt_blkno == (Q).mt_blkno)
1669 static off_t
1670 skip_via_lseek (char const *filename, int fdesc, off_t offset, int whence)
1672 struct mtget s1;
1673 struct mtget s2;
1674 bool got_original_tape_position = (ioctl (fdesc, MTIOCGET, &s1) == 0);
1675 /* known bad device type */
1676 /* && s.mt_type == MT_ISSCSI2 */
1678 off_t new_position = lseek (fdesc, offset, whence);
1679 if (0 <= new_position
1680 && got_original_tape_position
1681 && ioctl (fdesc, MTIOCGET, &s2) == 0
1682 && MT_SAME_POSITION (s1, s2))
1684 if (status_level != STATUS_NONE)
1685 error (0, 0, _("warning: working around lseek kernel bug for file "
1686 "(%s)\n of mt_type=0x%0lx -- "
1687 "see <sys/mtio.h> for the list of types"),
1688 filename, s2.mt_type + 0Lu);
1689 errno = 0;
1690 new_position = -1;
1693 return new_position;
1695 #else
1696 # define skip_via_lseek(Filename, Fd, Offset, Whence) lseek (Fd, Offset, Whence)
1697 #endif
1699 /* Throw away RECORDS blocks of BLOCKSIZE bytes plus BYTES bytes on
1700 file descriptor FDESC, which is open with read permission for FILE.
1701 Store up to BLOCKSIZE bytes of the data at a time in IBUF or OBUF, if
1702 necessary. RECORDS or BYTES must be nonzero. If FDESC is
1703 STDIN_FILENO, advance the input offset. Return the number of
1704 records remaining, i.e., that were not skipped because EOF was
1705 reached. If FDESC is STDOUT_FILENO, on return, BYTES is the
1706 remaining bytes in addition to the remaining records. */
1708 static uintmax_t
1709 skip (int fdesc, char const *file, uintmax_t records, size_t blocksize,
1710 size_t *bytes)
1712 uintmax_t offset = records * blocksize + *bytes;
1714 /* Try lseek and if an error indicates it was an inappropriate operation --
1715 or if the file offset is not representable as an off_t --
1716 fall back on using read. */
1718 errno = 0;
1719 if (records <= OFF_T_MAX / blocksize
1720 && 0 <= skip_via_lseek (file, fdesc, offset, SEEK_CUR))
1722 if (fdesc == STDIN_FILENO)
1724 struct stat st;
1725 if (fstat (STDIN_FILENO, &st) != 0)
1726 error (EXIT_FAILURE, errno, _("cannot fstat %s"), quote (file));
1727 if (usable_st_size (&st) && st.st_size < input_offset + offset)
1729 /* When skipping past EOF, return the number of _full_ blocks
1730 * that are not skipped, and set offset to EOF, so the caller
1731 * can determine the requested skip was not satisfied. */
1732 records = ( offset - st.st_size ) / blocksize;
1733 offset = st.st_size - input_offset;
1735 else
1736 records = 0;
1737 advance_input_offset (offset);
1739 else
1741 records = 0;
1742 *bytes = 0;
1744 return records;
1746 else
1748 int lseek_errno = errno;
1750 /* The seek request may have failed above if it was too big
1751 (> device size, > max file size, etc.)
1752 Or it may not have been done at all (> OFF_T_MAX).
1753 Therefore try to seek to the end of the file,
1754 to avoid redundant reading. */
1755 if ((skip_via_lseek (file, fdesc, 0, SEEK_END)) >= 0)
1757 /* File is seekable, and we're at the end of it, and
1758 size <= OFF_T_MAX. So there's no point using read to advance. */
1760 if (!lseek_errno)
1762 /* The original seek was not attempted as offset > OFF_T_MAX.
1763 We should error for write as can't get to the desired
1764 location, even if OFF_T_MAX < max file size.
1765 For read we're not going to read any data anyway,
1766 so we should error for consistency.
1767 It would be nice to not error for /dev/{zero,null}
1768 for any offset, but that's not a significant issue. */
1769 lseek_errno = EOVERFLOW;
1772 if (fdesc == STDIN_FILENO)
1773 error (0, lseek_errno, _("%s: cannot skip"), quote (file));
1774 else
1775 error (0, lseek_errno, _("%s: cannot seek"), quote (file));
1776 /* If the file has a specific size and we've asked
1777 to skip/seek beyond the max allowable, then quit. */
1778 quit (EXIT_FAILURE);
1780 /* else file_size && offset > OFF_T_MAX or file ! seekable */
1782 char *buf;
1783 if (fdesc == STDIN_FILENO)
1785 alloc_ibuf ();
1786 buf = ibuf;
1788 else
1790 alloc_obuf ();
1791 buf = obuf;
1796 ssize_t nread = iread_fnc (fdesc, buf, records ? blocksize : *bytes);
1797 if (nread < 0)
1799 if (fdesc == STDIN_FILENO)
1801 error (0, errno, _("error reading %s"), quote (file));
1802 if (conversions_mask & C_NOERROR)
1803 print_stats ();
1805 else
1806 error (0, lseek_errno, _("%s: cannot seek"), quote (file));
1807 quit (EXIT_FAILURE);
1809 else if (nread == 0)
1810 break;
1811 else if (fdesc == STDIN_FILENO)
1812 advance_input_offset (nread);
1814 if (records != 0)
1815 records--;
1816 else
1817 *bytes = 0;
1819 while (records || *bytes);
1821 return records;
1825 /* Advance the input by NBYTES if possible, after a read error.
1826 The input file offset may or may not have advanced after the failed
1827 read; adjust it to point just after the bad record regardless.
1828 Return true if successful, or if the input is already known to not
1829 be seekable. */
1831 static bool
1832 advance_input_after_read_error (size_t nbytes)
1834 if (! input_seekable)
1836 if (input_seek_errno == ESPIPE)
1837 return true;
1838 errno = input_seek_errno;
1840 else
1842 off_t offset;
1843 advance_input_offset (nbytes);
1844 input_offset_overflow |= (OFF_T_MAX < input_offset);
1845 if (input_offset_overflow)
1847 error (0, 0, _("offset overflow while reading file %s"),
1848 quote (input_file));
1849 return false;
1851 offset = lseek (STDIN_FILENO, 0, SEEK_CUR);
1852 if (0 <= offset)
1854 off_t diff;
1855 if (offset == input_offset)
1856 return true;
1857 diff = input_offset - offset;
1858 if (! (0 <= diff && diff <= nbytes) && status_level != STATUS_NONE)
1859 error (0, 0, _("warning: invalid file offset after failed read"));
1860 if (0 <= skip_via_lseek (input_file, STDIN_FILENO, diff, SEEK_CUR))
1861 return true;
1862 if (errno == 0)
1863 error (0, 0, _("cannot work around kernel bug after all"));
1867 error (0, errno, _("%s: cannot seek"), quote (input_file));
1868 return false;
1871 /* Copy NREAD bytes of BUF, with no conversions. */
1873 static void
1874 copy_simple (char const *buf, size_t nread)
1876 const char *start = buf; /* First uncopied char in BUF. */
1880 size_t nfree = MIN (nread, output_blocksize - oc);
1882 memcpy (obuf + oc, start, nfree);
1884 nread -= nfree; /* Update the number of bytes left to copy. */
1885 start += nfree;
1886 oc += nfree;
1887 if (oc >= output_blocksize)
1888 write_output ();
1890 while (nread != 0);
1893 /* Copy NREAD bytes of BUF, doing conv=block
1894 (pad newline-terminated records to 'conversion_blocksize',
1895 replacing the newline with trailing spaces). */
1897 static void
1898 copy_with_block (char const *buf, size_t nread)
1900 size_t i;
1902 for (i = nread; i; i--, buf++)
1904 if (*buf == newline_character)
1906 if (col < conversion_blocksize)
1908 size_t j;
1909 for (j = col; j < conversion_blocksize; j++)
1910 output_char (space_character);
1912 col = 0;
1914 else
1916 if (col == conversion_blocksize)
1917 r_truncate++;
1918 else if (col < conversion_blocksize)
1919 output_char (*buf);
1920 col++;
1925 /* Copy NREAD bytes of BUF, doing conv=unblock
1926 (replace trailing spaces in 'conversion_blocksize'-sized records
1927 with a newline). */
1929 static void
1930 copy_with_unblock (char const *buf, size_t nread)
1932 size_t i;
1933 char c;
1934 static size_t pending_spaces = 0;
1936 for (i = 0; i < nread; i++)
1938 c = buf[i];
1940 if (col++ >= conversion_blocksize)
1942 col = pending_spaces = 0; /* Wipe out any pending spaces. */
1943 i--; /* Push the char back; get it later. */
1944 output_char (newline_character);
1946 else if (c == space_character)
1947 pending_spaces++;
1948 else
1950 /* 'c' is the character after a run of spaces that were not
1951 at the end of the conversion buffer. Output them. */
1952 while (pending_spaces)
1954 output_char (space_character);
1955 --pending_spaces;
1957 output_char (c);
1962 /* Set the file descriptor flags for FD that correspond to the nonzero bits
1963 in ADD_FLAGS. The file's name is NAME. */
1965 static void
1966 set_fd_flags (int fd, int add_flags, char const *name)
1968 /* Ignore file creation flags that are no-ops on file descriptors. */
1969 add_flags &= ~ (O_NOCTTY | O_NOFOLLOW);
1971 if (add_flags)
1973 int old_flags = fcntl (fd, F_GETFL);
1974 int new_flags = old_flags | add_flags;
1975 bool ok = true;
1976 if (old_flags < 0)
1977 ok = false;
1978 else if (old_flags != new_flags)
1980 if (new_flags & (O_DIRECTORY | O_NOLINKS))
1982 /* NEW_FLAGS contains at least one file creation flag that
1983 requires some checking of the open file descriptor. */
1984 struct stat st;
1985 if (fstat (fd, &st) != 0)
1986 ok = false;
1987 else if ((new_flags & O_DIRECTORY) && ! S_ISDIR (st.st_mode))
1989 errno = ENOTDIR;
1990 ok = false;
1992 else if ((new_flags & O_NOLINKS) && 1 < st.st_nlink)
1994 errno = EMLINK;
1995 ok = false;
1997 new_flags &= ~ (O_DIRECTORY | O_NOLINKS);
2000 if (ok && old_flags != new_flags
2001 && fcntl (fd, F_SETFL, new_flags) == -1)
2002 ok = false;
2005 if (!ok)
2006 error (EXIT_FAILURE, errno, _("setting flags for %s"), quote (name));
2010 /* The main loop. */
2012 static int
2013 dd_copy (void)
2015 char *bufstart; /* Input buffer. */
2016 ssize_t nread; /* Bytes read in the current block. */
2018 /* If nonzero, then the previously read block was partial and
2019 PARTREAD was its size. */
2020 size_t partread = 0;
2022 int exit_status = EXIT_SUCCESS;
2023 size_t n_bytes_read;
2025 /* Leave at least one extra byte at the beginning and end of 'ibuf'
2026 for conv=swab, but keep the buffer address even. But some peculiar
2027 device drivers work only with word-aligned buffers, so leave an
2028 extra two bytes. */
2030 /* Some devices require alignment on a sector or page boundary
2031 (e.g. character disk devices). Align the input buffer to a
2032 page boundary to cover all bases. Note that due to the swab
2033 algorithm, we must have at least one byte in the page before
2034 the input buffer; thus we allocate 2 pages of slop in the
2035 real buffer. 8k above the blocksize shouldn't bother anyone.
2037 The page alignment is necessary on any Linux kernel that supports
2038 either the SGI raw I/O patch or Steven Tweedies raw I/O patch.
2039 It is necessary when accessing raw (i.e., character special) disk
2040 devices on Unixware or other SVR4-derived system. */
2042 if (skip_records != 0 || skip_bytes != 0)
2044 uintmax_t us_bytes = input_offset + (skip_records * input_blocksize)
2045 + skip_bytes;
2046 uintmax_t us_blocks = skip (STDIN_FILENO, input_file,
2047 skip_records, input_blocksize, &skip_bytes);
2048 us_bytes -= input_offset;
2050 /* POSIX doesn't say what to do when dd detects it has been
2051 asked to skip past EOF, so I assume it's non-fatal.
2052 There are 3 reasons why there might be unskipped blocks/bytes:
2053 1. file is too small
2054 2. pipe has not enough data
2055 3. partial reads */
2056 if ((us_blocks || (!input_offset_overflow && us_bytes))
2057 && status_level != STATUS_NONE)
2059 error (0, 0,
2060 _("%s: cannot skip to specified offset"), quote (input_file));
2064 if (seek_records != 0 || seek_bytes != 0)
2066 size_t bytes = seek_bytes;
2067 uintmax_t write_records = skip (STDOUT_FILENO, output_file,
2068 seek_records, output_blocksize, &bytes);
2070 if (write_records != 0 || bytes != 0)
2072 memset (obuf, 0, write_records ? output_blocksize : bytes);
2076 size_t size = write_records ? output_blocksize : bytes;
2077 if (iwrite (STDOUT_FILENO, obuf, size) != size)
2079 error (0, errno, _("writing to %s"), quote (output_file));
2080 quit (EXIT_FAILURE);
2083 if (write_records != 0)
2084 write_records--;
2085 else
2086 bytes = 0;
2088 while (write_records || bytes);
2092 if (max_records == 0 && max_bytes == 0)
2093 return exit_status;
2095 alloc_ibuf ();
2096 alloc_obuf ();
2098 while (1)
2100 if (status_level == STATUS_PROGRESS)
2102 xtime_t progress_time = gethrxtime ();
2103 uintmax_t delta_xtime = progress_time;
2104 delta_xtime -= previous_time;
2105 double XTIME_PRECISIONe0 = XTIME_PRECISION;
2106 if (delta_xtime / XTIME_PRECISIONe0 > 1)
2108 print_xfer_stats (progress_time);
2109 previous_time = progress_time;
2113 if (r_partial + r_full >= max_records + !!max_bytes)
2114 break;
2116 /* Zero the buffer before reading, so that if we get a read error,
2117 whatever data we are able to read is followed by zeros.
2118 This minimizes data loss. */
2119 if ((conversions_mask & C_SYNC) && (conversions_mask & C_NOERROR))
2120 memset (ibuf,
2121 (conversions_mask & (C_BLOCK | C_UNBLOCK)) ? ' ' : '\0',
2122 input_blocksize);
2124 if (r_partial + r_full >= max_records)
2125 nread = iread_fnc (STDIN_FILENO, ibuf, max_bytes);
2126 else
2127 nread = iread_fnc (STDIN_FILENO, ibuf, input_blocksize);
2129 if (nread >= 0 && i_nocache)
2130 invalidate_cache (STDIN_FILENO, nread);
2132 if (nread == 0)
2133 break; /* EOF. */
2135 if (nread < 0)
2137 if (!(conversions_mask & C_NOERROR) || status_level != STATUS_NONE)
2138 error (0, errno, _("error reading %s"), quote (input_file));
2140 if (conversions_mask & C_NOERROR)
2142 print_stats ();
2143 size_t bad_portion = input_blocksize - partread;
2145 /* We already know this data is not cached,
2146 but call this so that correct offsets are maintained. */
2147 invalidate_cache (STDIN_FILENO, bad_portion);
2149 /* Seek past the bad block if possible. */
2150 if (!advance_input_after_read_error (bad_portion))
2152 exit_status = EXIT_FAILURE;
2154 /* Suppress duplicate diagnostics. */
2155 input_seekable = false;
2156 input_seek_errno = ESPIPE;
2158 if ((conversions_mask & C_SYNC) && !partread)
2159 /* Replace the missing input with null bytes and
2160 proceed normally. */
2161 nread = 0;
2162 else
2163 continue;
2165 else
2167 /* Write any partial block. */
2168 exit_status = EXIT_FAILURE;
2169 break;
2173 n_bytes_read = nread;
2174 advance_input_offset (nread);
2176 if (n_bytes_read < input_blocksize)
2178 r_partial++;
2179 partread = n_bytes_read;
2180 if (conversions_mask & C_SYNC)
2182 if (!(conversions_mask & C_NOERROR))
2183 /* If C_NOERROR, we zeroed the block before reading. */
2184 memset (ibuf + n_bytes_read,
2185 (conversions_mask & (C_BLOCK | C_UNBLOCK)) ? ' ' : '\0',
2186 input_blocksize - n_bytes_read);
2187 n_bytes_read = input_blocksize;
2190 else
2192 r_full++;
2193 partread = 0;
2196 if (ibuf == obuf) /* If not C_TWOBUFS. */
2198 size_t nwritten = iwrite (STDOUT_FILENO, obuf, n_bytes_read);
2199 w_bytes += nwritten;
2200 if (nwritten != n_bytes_read)
2202 error (0, errno, _("error writing %s"), quote (output_file));
2203 return EXIT_FAILURE;
2205 else if (n_bytes_read == input_blocksize)
2206 w_full++;
2207 else
2208 w_partial++;
2209 continue;
2212 /* Do any translations on the whole buffer at once. */
2214 if (translation_needed)
2215 translate_buffer (ibuf, n_bytes_read);
2217 if (conversions_mask & C_SWAB)
2218 bufstart = swab_buffer (ibuf, &n_bytes_read);
2219 else
2220 bufstart = ibuf;
2222 if (conversions_mask & C_BLOCK)
2223 copy_with_block (bufstart, n_bytes_read);
2224 else if (conversions_mask & C_UNBLOCK)
2225 copy_with_unblock (bufstart, n_bytes_read);
2226 else
2227 copy_simple (bufstart, n_bytes_read);
2230 /* If we have a char left as a result of conv=swab, output it. */
2231 if (char_is_saved)
2233 if (conversions_mask & C_BLOCK)
2234 copy_with_block (&saved_char, 1);
2235 else if (conversions_mask & C_UNBLOCK)
2236 copy_with_unblock (&saved_char, 1);
2237 else
2238 output_char (saved_char);
2241 if ((conversions_mask & C_BLOCK) && col > 0)
2243 /* If the final input line didn't end with a '\n', pad
2244 the output block to 'conversion_blocksize' chars. */
2245 size_t i;
2246 for (i = col; i < conversion_blocksize; i++)
2247 output_char (space_character);
2250 if (col && (conversions_mask & C_UNBLOCK))
2252 /* If there was any output, add a final '\n'. */
2253 output_char (newline_character);
2256 /* Write out the last block. */
2257 if (oc != 0)
2259 size_t nwritten = iwrite (STDOUT_FILENO, obuf, oc);
2260 w_bytes += nwritten;
2261 if (nwritten != 0)
2262 w_partial++;
2263 if (nwritten != oc)
2265 error (0, errno, _("error writing %s"), quote (output_file));
2266 return EXIT_FAILURE;
2270 /* If the last write was converted to a seek, then for a regular file
2271 or shared memory object, ftruncate to extend the size. */
2272 if (final_op_was_seek)
2274 struct stat stdout_stat;
2275 if (fstat (STDOUT_FILENO, &stdout_stat) != 0)
2277 error (0, errno, _("cannot fstat %s"), quote (output_file));
2278 return EXIT_FAILURE;
2280 if (S_ISREG (stdout_stat.st_mode) || S_TYPEISSHM (&stdout_stat))
2282 off_t output_offset = lseek (STDOUT_FILENO, 0, SEEK_CUR);
2283 if (output_offset > stdout_stat.st_size)
2285 if (iftruncate (STDOUT_FILENO, output_offset) != 0)
2287 error (0, errno,
2288 _("failed to truncate to %" PRIdMAX " bytes"
2289 " in output file %s"),
2290 (intmax_t) output_offset, quote (output_file));
2291 return EXIT_FAILURE;
2297 if ((conversions_mask & C_FDATASYNC) && fdatasync (STDOUT_FILENO) != 0)
2299 if (errno != ENOSYS && errno != EINVAL)
2301 error (0, errno, _("fdatasync failed for %s"), quote (output_file));
2302 exit_status = EXIT_FAILURE;
2304 conversions_mask |= C_FSYNC;
2307 if (conversions_mask & C_FSYNC)
2308 while (fsync (STDOUT_FILENO) != 0)
2309 if (errno != EINTR)
2311 error (0, errno, _("fsync failed for %s"), quote (output_file));
2312 return EXIT_FAILURE;
2315 return exit_status;
2319 main (int argc, char **argv)
2321 int i;
2322 int exit_status;
2323 off_t offset;
2325 install_signal_handlers ();
2327 initialize_main (&argc, &argv);
2328 set_program_name (argv[0]);
2329 setlocale (LC_ALL, "");
2330 bindtextdomain (PACKAGE, LOCALEDIR);
2331 textdomain (PACKAGE);
2333 /* Arrange to close stdout if parse_long_options exits. */
2334 atexit (maybe_close_stdout);
2336 page_size = getpagesize ();
2338 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, Version,
2339 usage, AUTHORS, (char const *) NULL);
2340 close_stdout_required = false;
2342 if (getopt_long (argc, argv, "", NULL, NULL) != -1)
2343 usage (EXIT_FAILURE);
2345 /* Initialize translation table to identity translation. */
2346 for (i = 0; i < 256; i++)
2347 trans_table[i] = i;
2349 /* Decode arguments. */
2350 scanargs (argc, argv);
2352 apply_translations ();
2354 if (input_file == NULL)
2356 input_file = _("standard input");
2357 set_fd_flags (STDIN_FILENO, input_flags, input_file);
2359 else
2361 if (ifd_reopen (STDIN_FILENO, input_file, O_RDONLY | input_flags, 0) < 0)
2362 error (EXIT_FAILURE, errno, _("failed to open %s"), quote (input_file));
2365 offset = lseek (STDIN_FILENO, 0, SEEK_CUR);
2366 input_seekable = (0 <= offset);
2367 input_offset = MAX (0, offset);
2368 input_seek_errno = errno;
2370 if (output_file == NULL)
2372 output_file = _("standard output");
2373 set_fd_flags (STDOUT_FILENO, output_flags, output_file);
2375 else
2377 mode_t perms = MODE_RW_UGO;
2378 int opts
2379 = (output_flags
2380 | (conversions_mask & C_NOCREAT ? 0 : O_CREAT)
2381 | (conversions_mask & C_EXCL ? O_EXCL : 0)
2382 | (seek_records || (conversions_mask & C_NOTRUNC) ? 0 : O_TRUNC));
2384 /* Open the output file with *read* access only if we might
2385 need to read to satisfy a 'seek=' request. If we can't read
2386 the file, go ahead with write-only access; it might work. */
2387 if ((! seek_records
2388 || ifd_reopen (STDOUT_FILENO, output_file, O_RDWR | opts, perms) < 0)
2389 && (ifd_reopen (STDOUT_FILENO, output_file, O_WRONLY | opts, perms)
2390 < 0))
2391 error (EXIT_FAILURE, errno, _("failed to open %s"),
2392 quote (output_file));
2394 if (seek_records != 0 && !(conversions_mask & C_NOTRUNC))
2396 uintmax_t size = seek_records * output_blocksize + seek_bytes;
2397 unsigned long int obs = output_blocksize;
2399 if (OFF_T_MAX / output_blocksize < seek_records)
2400 error (EXIT_FAILURE, 0,
2401 _("offset too large: "
2402 "cannot truncate to a length of seek=%"PRIuMAX""
2403 " (%lu-byte) blocks"),
2404 seek_records, obs);
2406 if (iftruncate (STDOUT_FILENO, size) != 0)
2408 /* Complain only when ftruncate fails on a regular file, a
2409 directory, or a shared memory object, as POSIX 1003.1-2004
2410 specifies ftruncate's behavior only for these file types.
2411 For example, do not complain when Linux kernel 2.4 ftruncate
2412 fails on /dev/fd0. */
2413 int ftruncate_errno = errno;
2414 struct stat stdout_stat;
2415 if (fstat (STDOUT_FILENO, &stdout_stat) != 0)
2416 error (EXIT_FAILURE, errno, _("cannot fstat %s"),
2417 quote (output_file));
2418 if (S_ISREG (stdout_stat.st_mode)
2419 || S_ISDIR (stdout_stat.st_mode)
2420 || S_TYPEISSHM (&stdout_stat))
2421 error (EXIT_FAILURE, ftruncate_errno,
2422 _("failed to truncate to %"PRIuMAX" bytes"
2423 " in output file %s"),
2424 size, quote (output_file));
2429 start_time = previous_time = gethrxtime ();
2431 exit_status = dd_copy ();
2433 if (max_records == 0 && max_bytes == 0)
2435 /* Special case to invalidate cache to end of file. */
2436 if (i_nocache && !invalidate_cache (STDIN_FILENO, 0))
2438 error (0, errno, _("failed to discard cache for: %s"),
2439 quote (input_file));
2440 exit_status = EXIT_FAILURE;
2442 if (o_nocache && !invalidate_cache (STDOUT_FILENO, 0))
2444 error (0, errno, _("failed to discard cache for: %s"),
2445 quote (output_file));
2446 exit_status = EXIT_FAILURE;
2449 else if (max_records != (uintmax_t) -1)
2451 /* Invalidate any pending region less than page size,
2452 in case the kernel might round up. */
2453 if (i_nocache)
2454 invalidate_cache (STDIN_FILENO, 0);
2455 if (o_nocache)
2456 invalidate_cache (STDOUT_FILENO, 0);
2459 finish_up ();
2460 return exit_status;