1 /* dd -- convert a file while copying it.
2 Copyright (C) 1985-2019 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Paul Rubin, David MacKenzie, and Stuart Kemp. */
21 #define SWAB_ALIGN_OFFSET 2
23 #include <sys/types.h>
27 #include "close-stream.h"
30 #include "fd-reopen.h"
31 #include "gethrxtime.h"
33 #include "ioblksize.h"
34 #include "long-options.h"
40 /* The official name of this program (e.g., no 'g' prefix). */
41 #define PROGRAM_NAME "dd"
44 proper_name ("Paul Rubin"), \
45 proper_name ("David MacKenzie"), \
46 proper_name ("Stuart Kemp")
48 /* Use SA_NOCLDSTOP as a proxy for whether the sigaction machinery is
51 # define SA_NOCLDSTOP 0
52 # define sigprocmask(How, Set, Oset) /* empty */
54 # if ! HAVE_SIGINTERRUPT
55 # define siginterrupt(sig, flag) /* empty */
59 /* NonStop circa 2011 lacks SA_RESETHAND; see Bug#9076. */
61 # define SA_RESETHAND 0
65 # define SIGINFO SIGUSR1
68 /* This may belong in GNULIB's fcntl module instead.
69 Define O_CIO to 0 if it is not supported by this OS. */
74 /* On AIX 5.1 and AIX 5.2, O_NOCACHE is defined via <fcntl.h>
75 and would interfere with our use of that name, below. */
79 # define fdatasync(fd) (errno = ENOSYS, -1)
82 #define output_char(c) \
86 if (oc >= output_blocksize) \
91 /* Default input and output blocksize. */
92 #define DEFAULT_BLOCKSIZE 512
94 /* How many bytes to add to the input and output block sizes before invoking
95 malloc. See dd_copy for details. INPUT_BLOCK_SLOP must be no less than
97 #define INPUT_BLOCK_SLOP (2 * SWAB_ALIGN_OFFSET + 2 * page_size - 1)
98 #define OUTPUT_BLOCK_SLOP (page_size - 1)
100 /* Maximum blocksize for the given SLOP.
101 Keep it smaller than SIZE_MAX - SLOP, so that we can
102 allocate buffers that size. Keep it smaller than SSIZE_MAX, for
103 the benefit of system calls like "read". And keep it smaller than
104 OFF_T_MAX, for the benefit of the large-offset seek code. */
105 #define MAX_BLOCKSIZE(slop) MIN (SIZE_MAX - (slop), MIN (SSIZE_MAX, OFF_T_MAX))
107 /* Conversions bit masks. */
123 /* Use separate input and output buffers, and combine partial
129 C_FDATASYNC
= 040000,
144 /* The name of the input file, or NULL for the standard input. */
145 static char const *input_file
= NULL
;
147 /* The name of the output file, or NULL for the standard output. */
148 static char const *output_file
= NULL
;
150 /* The page size on this host. */
151 static size_t page_size
;
153 /* The number of bytes in which atomic reads are done. */
154 static size_t input_blocksize
= 0;
156 /* The number of bytes in which atomic writes are done. */
157 static size_t output_blocksize
= 0;
159 /* Conversion buffer size, in bytes. 0 prevents conversions. */
160 static size_t conversion_blocksize
= 0;
162 /* Skip this many records of 'input_blocksize' bytes before input. */
163 static uintmax_t skip_records
= 0;
165 /* Skip this many bytes before input in addition of 'skip_records'
167 static size_t skip_bytes
= 0;
169 /* Skip this many records of 'output_blocksize' bytes before output. */
170 static uintmax_t seek_records
= 0;
172 /* Skip this many bytes in addition to 'seek_records' records before
174 static uintmax_t seek_bytes
= 0;
176 /* Whether the final output was done with a seek (rather than a write). */
177 static bool final_op_was_seek
;
179 /* Copy only this many records. The default is effectively infinity. */
180 static uintmax_t max_records
= (uintmax_t) -1;
182 /* Copy this many bytes in addition to 'max_records' records. */
183 static size_t max_bytes
= 0;
185 /* Bit vector of conversions to apply. */
186 static int conversions_mask
= 0;
188 /* Open flags for the input and output files. */
189 static int input_flags
= 0;
190 static int output_flags
= 0;
192 /* Status flags for what is printed to stderr. */
193 static int status_level
= STATUS_DEFAULT
;
195 /* If nonzero, filter characters through the translation table. */
196 static bool translation_needed
= false;
198 /* Number of partial blocks written. */
199 static uintmax_t w_partial
= 0;
201 /* Number of full blocks written. */
202 static uintmax_t w_full
= 0;
204 /* Number of partial blocks read. */
205 static uintmax_t r_partial
= 0;
207 /* Number of full blocks read. */
208 static uintmax_t r_full
= 0;
210 /* Number of bytes written. */
211 static uintmax_t w_bytes
= 0;
213 /* Time that dd started. */
214 static xtime_t start_time
;
216 /* Next time to report periodic progress. */
217 static xtime_t next_time
;
219 /* If positive, the number of bytes output in the current progress line. */
220 static int progress_len
;
222 /* True if input is seekable. */
223 static bool input_seekable
;
225 /* Error number corresponding to initial attempt to lseek input.
226 If ESPIPE, do not issue any more diagnostics about it. */
227 static int input_seek_errno
;
229 /* File offset of the input, in bytes, along with a flag recording
230 whether it overflowed. */
231 static uintmax_t input_offset
;
232 static bool input_offset_overflow
;
234 /* True if a partial read should be diagnosed. */
235 static bool warn_partial_read
;
237 /* Records truncated by conv=block. */
238 static uintmax_t r_truncate
= 0;
240 /* Output representation of newline and space characters.
241 They change if we're converting to EBCDIC. */
242 static char newline_character
= '\n';
243 static char space_character
= ' ';
251 /* Current index into 'obuf'. */
252 static size_t oc
= 0;
254 /* Index into current line, for 'conv=block' and 'conv=unblock'. */
255 static size_t col
= 0;
257 /* The set of signals that are caught. */
258 static sigset_t caught_signals
;
260 /* If nonzero, the value of the pending fatal signal. */
261 static sig_atomic_t volatile interrupt_signal
;
263 /* A count of the number of pending info signals that have been received. */
264 static sig_atomic_t volatile info_signal_count
;
266 /* Whether to discard cache for input or output. */
267 static bool i_nocache
, o_nocache
;
269 /* Whether to instruct the kernel to discard the complete file. */
270 static bool i_nocache_eof
, o_nocache_eof
;
272 /* Function used for read (to handle iflag=fullblock parameter). */
273 static ssize_t (*iread_fnc
) (int fd
, char *buf
, size_t size
);
275 /* A longest symbol in the struct symbol_values tables below. */
276 #define LONGEST_SYMBOL "count_bytes"
278 /* A symbol and the corresponding integer value. */
281 char symbol
[sizeof LONGEST_SYMBOL
];
285 /* Conversion symbols, for conv="...". */
286 static struct symbol_value
const conversions
[] =
288 {"ascii", C_ASCII
| C_UNBLOCK
| C_TWOBUFS
}, /* EBCDIC to ASCII. */
289 {"ebcdic", C_EBCDIC
| C_BLOCK
| C_TWOBUFS
}, /* ASCII to EBCDIC. */
290 {"ibm", C_IBM
| C_BLOCK
| C_TWOBUFS
}, /* Different ASCII to EBCDIC. */
291 {"block", C_BLOCK
| C_TWOBUFS
}, /* Variable to fixed length records. */
292 {"unblock", C_UNBLOCK
| C_TWOBUFS
}, /* Fixed to variable length records. */
293 {"lcase", C_LCASE
| C_TWOBUFS
}, /* Translate upper to lower case. */
294 {"ucase", C_UCASE
| C_TWOBUFS
}, /* Translate lower to upper case. */
295 {"sparse", C_SPARSE
}, /* Try to sparsely write output. */
296 {"swab", C_SWAB
| C_TWOBUFS
}, /* Swap bytes of input. */
297 {"noerror", C_NOERROR
}, /* Ignore i/o errors. */
298 {"nocreat", C_NOCREAT
}, /* Do not create output file. */
299 {"excl", C_EXCL
}, /* Fail if the output file already exists. */
300 {"notrunc", C_NOTRUNC
}, /* Do not truncate output file. */
301 {"sync", C_SYNC
}, /* Pad input records to ibs with NULs. */
302 {"fdatasync", C_FDATASYNC
}, /* Synchronize output data before finishing. */
303 {"fsync", C_FSYNC
}, /* Also synchronize output metadata. */
307 #define FFS_MASK(x) ((x) ^ ((x) & ((x) - 1)))
310 /* Compute a value that's bitwise disjoint from the union
328 /* Use its lowest bits for private flags. */
329 O_FULLBLOCK
= FFS_MASK (v
),
330 v2
= v
^ O_FULLBLOCK
,
332 O_NOCACHE
= FFS_MASK (v2
),
335 O_COUNT_BYTES
= FFS_MASK (v3
),
336 v4
= v3
^ O_COUNT_BYTES
,
338 O_SKIP_BYTES
= FFS_MASK (v4
),
339 v5
= v4
^ O_SKIP_BYTES
,
341 O_SEEK_BYTES
= FFS_MASK (v5
)
344 /* Ensure that we got something. */
345 verify (O_FULLBLOCK
!= 0);
346 verify (O_NOCACHE
!= 0);
347 verify (O_COUNT_BYTES
!= 0);
348 verify (O_SKIP_BYTES
!= 0);
349 verify (O_SEEK_BYTES
!= 0);
351 #define MULTIPLE_BITS_SET(i) (((i) & ((i) - 1)) != 0)
353 /* Ensure that this is a single-bit value. */
354 verify ( ! MULTIPLE_BITS_SET (O_FULLBLOCK
));
355 verify ( ! MULTIPLE_BITS_SET (O_NOCACHE
));
356 verify ( ! MULTIPLE_BITS_SET (O_COUNT_BYTES
));
357 verify ( ! MULTIPLE_BITS_SET (O_SKIP_BYTES
));
358 verify ( ! MULTIPLE_BITS_SET (O_SEEK_BYTES
));
360 /* Flags, for iflag="..." and oflag="...". */
361 static struct symbol_value
const flags
[] =
363 {"append", O_APPEND
},
364 {"binary", O_BINARY
},
366 {"direct", O_DIRECT
},
367 {"directory", O_DIRECTORY
},
369 {"noatime", O_NOATIME
},
370 {"nocache", O_NOCACHE
}, /* Discard cache. */
371 {"noctty", O_NOCTTY
},
372 {"nofollow", HAVE_WORKING_O_NOFOLLOW
? O_NOFOLLOW
: 0},
373 {"nolinks", O_NOLINKS
},
374 {"nonblock", O_NONBLOCK
},
377 {"fullblock", O_FULLBLOCK
}, /* Accumulate full blocks from input. */
378 {"count_bytes", O_COUNT_BYTES
},
379 {"skip_bytes", O_SKIP_BYTES
},
380 {"seek_bytes", O_SEEK_BYTES
},
384 /* Status, for status="...". */
385 static struct symbol_value
const statuses
[] =
387 {"none", STATUS_NONE
},
388 {"noxfer", STATUS_NOXFER
},
389 {"progress", STATUS_PROGRESS
},
393 /* Translation table formed by applying successive transformations. */
394 static unsigned char trans_table
[256];
396 /* Standard translation tables, taken from POSIX 1003.1-2013.
397 Beware of imitations; there are lots of ASCII<->EBCDIC tables
398 floating around the net, perhaps valid for some applications but
401 static char const ascii_to_ebcdic
[] =
403 '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
404 '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
405 '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
406 '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
407 '\100', '\132', '\177', '\173', '\133', '\154', '\120', '\175',
408 '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
409 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
410 '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
411 '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
412 '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
413 '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
414 '\347', '\350', '\351', '\255', '\340', '\275', '\232', '\155',
415 '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
416 '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
417 '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
418 '\247', '\250', '\251', '\300', '\117', '\320', '\137', '\007',
419 '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
420 '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
421 '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
422 '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
423 '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
424 '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
425 '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
426 '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
427 '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
428 '\216', '\217', '\220', '\152', '\233', '\234', '\235', '\236',
429 '\237', '\240', '\252', '\253', '\254', '\112', '\256', '\257',
430 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
431 '\270', '\271', '\272', '\273', '\274', '\241', '\276', '\277',
432 '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
433 '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
434 '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
437 static char const ascii_to_ibm
[] =
439 '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
440 '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
441 '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
442 '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
443 '\100', '\132', '\177', '\173', '\133', '\154', '\120', '\175',
444 '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
445 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
446 '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
447 '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
448 '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
449 '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
450 '\347', '\350', '\351', '\255', '\340', '\275', '\137', '\155',
451 '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
452 '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
453 '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
454 '\247', '\250', '\251', '\300', '\117', '\320', '\241', '\007',
455 '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
456 '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
457 '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
458 '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
459 '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
460 '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
461 '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
462 '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
463 '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
464 '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',
465 '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',
466 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
467 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
468 '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
469 '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
470 '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
473 static char const ebcdic_to_ascii
[] =
475 '\000', '\001', '\002', '\003', '\234', '\011', '\206', '\177',
476 '\227', '\215', '\216', '\013', '\014', '\015', '\016', '\017',
477 '\020', '\021', '\022', '\023', '\235', '\205', '\010', '\207',
478 '\030', '\031', '\222', '\217', '\034', '\035', '\036', '\037',
479 '\200', '\201', '\202', '\203', '\204', '\012', '\027', '\033',
480 '\210', '\211', '\212', '\213', '\214', '\005', '\006', '\007',
481 '\220', '\221', '\026', '\223', '\224', '\225', '\226', '\004',
482 '\230', '\231', '\232', '\233', '\024', '\025', '\236', '\032',
483 '\040', '\240', '\241', '\242', '\243', '\244', '\245', '\246',
484 '\247', '\250', '\325', '\056', '\074', '\050', '\053', '\174',
485 '\046', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
486 '\260', '\261', '\041', '\044', '\052', '\051', '\073', '\176',
487 '\055', '\057', '\262', '\263', '\264', '\265', '\266', '\267',
488 '\270', '\271', '\313', '\054', '\045', '\137', '\076', '\077',
489 '\272', '\273', '\274', '\275', '\276', '\277', '\300', '\301',
490 '\302', '\140', '\072', '\043', '\100', '\047', '\075', '\042',
491 '\303', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
492 '\150', '\151', '\304', '\305', '\306', '\307', '\310', '\311',
493 '\312', '\152', '\153', '\154', '\155', '\156', '\157', '\160',
494 '\161', '\162', '\136', '\314', '\315', '\316', '\317', '\320',
495 '\321', '\345', '\163', '\164', '\165', '\166', '\167', '\170',
496 '\171', '\172', '\322', '\323', '\324', '\133', '\326', '\327',
497 '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
498 '\340', '\341', '\342', '\343', '\344', '\135', '\346', '\347',
499 '\173', '\101', '\102', '\103', '\104', '\105', '\106', '\107',
500 '\110', '\111', '\350', '\351', '\352', '\353', '\354', '\355',
501 '\175', '\112', '\113', '\114', '\115', '\116', '\117', '\120',
502 '\121', '\122', '\356', '\357', '\360', '\361', '\362', '\363',
503 '\134', '\237', '\123', '\124', '\125', '\126', '\127', '\130',
504 '\131', '\132', '\364', '\365', '\366', '\367', '\370', '\371',
505 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
506 '\070', '\071', '\372', '\373', '\374', '\375', '\376', '\377'
509 /* True if we need to close the standard output *stream*. */
510 static bool close_stdout_required
= true;
512 /* The only reason to close the standard output *stream* is if
513 parse_long_options fails (as it does for --help or --version).
514 In any other case, dd uses only the STDOUT_FILENO file descriptor,
515 and the "cleanup" function calls "close (STDOUT_FILENO)".
516 Closing the file descriptor and then letting the usual atexit-run
517 close_stdout function call "fclose (stdout)" would result in a
518 harmless failure of the close syscall (with errno EBADF).
519 This function serves solely to avoid the unnecessary close_stdout
520 call, once parse_long_options has succeeded.
521 Meanwhile, we guarantee that the standard error stream is flushed,
522 by inlining the last half of close_stdout as needed. */
524 maybe_close_stdout (void)
526 if (close_stdout_required
)
528 else if (close_stream (stderr
) != 0)
529 _exit (EXIT_FAILURE
);
532 /* Like the 'error' function but handle any pending newline. */
534 static void _GL_ATTRIBUTE_FORMAT ((__printf__
, 3, 4))
535 nl_error (int status
, int errnum
, const char *fmt
, ...)
537 if (0 < progress_len
)
539 fputc ('\n', stderr
);
545 verror (status
, errnum
, fmt
, ap
);
549 #define error nl_error
554 if (status
!= EXIT_SUCCESS
)
559 Usage: %s [OPERAND]...\n\
562 program_name
, program_name
);
564 Copy a file, converting and formatting according to the operands.\n\
566 bs=BYTES read and write up to BYTES bytes at a time (default: 512);\n\
567 overrides ibs and obs\n\
568 cbs=BYTES convert BYTES bytes at a time\n\
569 conv=CONVS convert the file as per the comma separated symbol list\n\
570 count=N copy only N input blocks\n\
571 ibs=BYTES read up to BYTES bytes at a time (default: 512)\n\
574 if=FILE read from FILE instead of stdin\n\
575 iflag=FLAGS read as per the comma separated symbol list\n\
576 obs=BYTES write BYTES bytes at a time (default: 512)\n\
577 of=FILE write to FILE instead of stdout\n\
578 oflag=FLAGS write as per the comma separated symbol list\n\
579 seek=N skip N obs-sized blocks at start of output\n\
580 skip=N skip N ibs-sized blocks at start of input\n\
581 status=LEVEL The LEVEL of information to print to stderr;\n\
582 'none' suppresses everything but error messages,\n\
583 'noxfer' suppresses the final transfer statistics,\n\
584 'progress' shows periodic transfer statistics\n\
588 N and BYTES may be followed by the following multiplicative suffixes:\n\
589 c=1, w=2, b=512, kB=1000, K=1024, MB=1000*1000, M=1024*1024, xM=M,\n\
590 GB=1000*1000*1000, G=1024*1024*1024, and so on for T, P, E, Z, Y.\n\
591 Binary prefixes can be used, too: KiB=K, MiB=M, and so on.\n\
593 Each CONV symbol may be:\n\
597 ascii from EBCDIC to ASCII\n\
598 ebcdic from ASCII to EBCDIC\n\
599 ibm from ASCII to alternate EBCDIC\n\
600 block pad newline-terminated records with spaces to cbs-size\n\
601 unblock replace trailing spaces in cbs-size records with newline\n\
602 lcase change upper case to lower case\n\
603 ucase change lower case to upper case\n\
604 sparse try to seek rather than write all-NUL output blocks\n\
605 swab swap every pair of input bytes\n\
606 sync pad every input block with NULs to ibs-size; when used\n\
607 with block or unblock, pad with spaces rather than NULs\n\
610 excl fail if the output file already exists\n\
611 nocreat do not create the output file\n\
612 notrunc do not truncate the output file\n\
613 noerror continue after read errors\n\
614 fdatasync physically write output file data before finishing\n\
615 fsync likewise, but also write metadata\n\
619 Each FLAG symbol may be:\n\
621 append append mode (makes sense only for output; conv=notrunc suggested)\n\
624 fputs (_(" cio use concurrent I/O for data\n"), stdout
);
626 fputs (_(" direct use direct I/O for data\n"), stdout
);
628 fputs (_(" directory fail unless a directory\n"), stdout
);
630 fputs (_(" dsync use synchronized I/O for data\n"), stdout
);
632 fputs (_(" sync likewise, but also for metadata\n"), stdout
);
633 fputs (_(" fullblock accumulate full blocks of input (iflag only)\n"),
636 fputs (_(" nonblock use non-blocking I/O\n"), stdout
);
638 fputs (_(" noatime do not update access time\n"), stdout
);
639 #if HAVE_POSIX_FADVISE
641 fputs (_(" nocache Request to drop cache. See also oflag=sync\n"),
645 fputs (_(" noctty do not assign controlling terminal from file\n"),
647 if (HAVE_WORKING_O_NOFOLLOW
)
648 fputs (_(" nofollow do not follow symlinks\n"), stdout
);
650 fputs (_(" nolinks fail if multiply-linked\n"), stdout
);
652 fputs (_(" binary use binary I/O for data\n"), stdout
);
654 fputs (_(" text use text I/O for data\n"), stdout
);
656 fputs (_(" count_bytes treat 'count=N' as a byte count (iflag only)\n\
659 fputs (_(" skip_bytes treat 'skip=N' as a byte count (iflag only)\n\
662 fputs (_(" seek_bytes treat 'seek=N' as a byte count (oflag only)\n\
668 Sending a %s signal to a running 'dd' process makes it\n\
669 print I/O statistics to standard error and then resume copying.\n\
673 "), SIGINFO
== SIGUSR1
? "USR1" : "INFO");
676 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
677 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
678 emit_ancillary_info (PROGRAM_NAME
);
683 /* Common options to use when displaying sizes and rates. */
685 enum { human_opts
= (human_autoscale
| human_round_to_nearest
686 | human_space_before_unit
| human_SI
| human_B
) };
688 /* Ensure input buffer IBUF is allocated. */
696 char *real_buf
= malloc (input_blocksize
+ INPUT_BLOCK_SLOP
);
699 uintmax_t ibs
= input_blocksize
;
700 char hbuf
[LONGEST_HUMAN_READABLE
+ 1];
701 die (EXIT_FAILURE
, 0,
702 _("memory exhausted by input buffer of size %"PRIuMAX
" bytes (%s)"),
704 human_readable (input_blocksize
, hbuf
,
705 human_opts
| human_base_1024
, 1, 1));
708 real_buf
+= SWAB_ALIGN_OFFSET
; /* allow space for swab */
710 ibuf
= ptr_align (real_buf
, page_size
);
713 /* Ensure output buffer OBUF is allocated/initialized. */
721 if (conversions_mask
& C_TWOBUFS
)
723 /* Page-align the output buffer, too. */
724 char *real_obuf
= malloc (output_blocksize
+ OUTPUT_BLOCK_SLOP
);
727 uintmax_t obs
= output_blocksize
;
728 char hbuf
[LONGEST_HUMAN_READABLE
+ 1];
729 die (EXIT_FAILURE
, 0,
730 _("memory exhausted by output buffer of size %"PRIuMAX
733 human_readable (output_blocksize
, hbuf
,
734 human_opts
| human_base_1024
, 1, 1));
736 obuf
= ptr_align (real_obuf
, page_size
);
746 translate_charset (char const *new_trans
)
748 for (int i
= 0; i
< 256; i
++)
749 trans_table
[i
] = new_trans
[trans_table
[i
]];
750 translation_needed
= true;
753 /* Return true if I has more than one bit set. I must be nonnegative. */
756 multiple_bits_set (int i
)
758 return MULTIPLE_BITS_SET (i
);
762 abbreviation_lacks_prefix (char const *message
)
764 return message
[strlen (message
) - 2] == ' ';
767 /* Print transfer statistics. */
770 print_xfer_stats (xtime_t progress_time
)
772 xtime_t now
= progress_time
? progress_time
: gethrxtime ();
773 static char const slash_s
[] = "/s";
774 char hbuf
[3][LONGEST_HUMAN_READABLE
+ sizeof slash_s
];
776 char const *bytes_per_second
;
777 char const *si
= human_readable (w_bytes
, hbuf
[0], human_opts
, 1, 1);
778 char const *iec
= human_readable (w_bytes
, hbuf
[1],
779 human_opts
| human_base_1024
, 1, 1);
781 /* Use integer arithmetic to compute the transfer rate,
782 since that makes it easy to use SI abbreviations. */
783 char *bpsbuf
= hbuf
[2];
784 int bpsbufsize
= sizeof hbuf
[2];
785 if (start_time
< now
)
787 double XTIME_PRECISIONe0
= XTIME_PRECISION
;
788 uintmax_t delta_xtime
= now
;
789 delta_xtime
-= start_time
;
790 delta_s
= delta_xtime
/ XTIME_PRECISIONe0
;
791 bytes_per_second
= human_readable (w_bytes
, bpsbuf
, human_opts
,
792 XTIME_PRECISION
, delta_xtime
);
793 strcat (bytes_per_second
- bpsbuf
+ bpsbuf
, slash_s
);
798 snprintf (bpsbuf
, bpsbufsize
, "%s B/s", _("Infinity"));
799 bytes_per_second
= bpsbuf
;
803 fputc ('\r', stderr
);
805 /* Use full seconds when printing progress, since the progress
806 report is output once per second and there is little point
807 displaying any subsecond jitter. Use default precision with %g
808 otherwise, as this provides more-useful output then. With long
809 transfers %g can generate a number with an exponent; that is OK. */
810 char delta_s_buf
[24];
811 snprintf (delta_s_buf
, sizeof delta_s_buf
,
812 progress_time
? "%.0f s" : "%g s", delta_s
);
815 = (abbreviation_lacks_prefix (si
)
817 ngettext ("%"PRIuMAX
" byte copied, %s, %s",
818 "%"PRIuMAX
" bytes copied, %s, %s",
819 select_plural (w_bytes
)),
820 w_bytes
, delta_s_buf
, bytes_per_second
)
821 : abbreviation_lacks_prefix (iec
)
823 _("%"PRIuMAX
" bytes (%s) copied, %s, %s"),
824 w_bytes
, si
, delta_s_buf
, bytes_per_second
)
826 _("%"PRIuMAX
" bytes (%s, %s) copied, %s, %s"),
827 w_bytes
, si
, iec
, delta_s_buf
, bytes_per_second
));
831 /* Erase any trailing junk on the output line by outputting
832 spaces. In theory this could glitch the display because the
833 formatted translation of a line describing a larger file
834 could consume fewer screen columns than the strlen difference
835 from the previously formatted translation. In practice this
836 does not seem to be a problem. */
837 if (0 <= stats_len
&& stats_len
< progress_len
)
838 fprintf (stderr
, "%*s", progress_len
- stats_len
, "");
839 progress_len
= stats_len
;
842 fputc ('\n', stderr
);
848 if (status_level
== STATUS_NONE
)
851 if (0 < progress_len
)
853 fputc ('\n', stderr
);
858 _("%"PRIuMAX
"+%"PRIuMAX
" records in\n"
859 "%"PRIuMAX
"+%"PRIuMAX
" records out\n"),
860 r_full
, r_partial
, w_full
, w_partial
);
864 ngettext ("%"PRIuMAX
" truncated record\n",
865 "%"PRIuMAX
" truncated records\n",
866 select_plural (r_truncate
)),
869 if (status_level
== STATUS_NOXFER
)
872 print_xfer_stats (0);
875 /* An ordinary signal was received; arrange for the program to exit. */
878 interrupt_handler (int sig
)
881 signal (sig
, SIG_DFL
);
882 interrupt_signal
= sig
;
885 /* An info signal was received; arrange for the program to print status. */
888 siginfo_handler (int sig
)
891 signal (sig
, siginfo_handler
);
895 /* Install the signal handlers. */
898 install_signal_handlers (void)
900 bool catch_siginfo
= ! (SIGINFO
== SIGUSR1
&& getenv ("POSIXLY_CORRECT"));
904 struct sigaction act
;
905 sigemptyset (&caught_signals
);
907 sigaddset (&caught_signals
, SIGINFO
);
908 sigaction (SIGINT
, NULL
, &act
);
909 if (act
.sa_handler
!= SIG_IGN
)
910 sigaddset (&caught_signals
, SIGINT
);
911 act
.sa_mask
= caught_signals
;
913 if (sigismember (&caught_signals
, SIGINFO
))
915 act
.sa_handler
= siginfo_handler
;
916 /* Note we don't use SA_RESTART here and instead
917 handle EINTR explicitly in iftruncate etc.
918 to avoid blocking on noncommitted read/write calls. */
920 sigaction (SIGINFO
, &act
, NULL
);
923 if (sigismember (&caught_signals
, SIGINT
))
925 act
.sa_handler
= interrupt_handler
;
926 act
.sa_flags
= SA_NODEFER
| SA_RESETHAND
;
927 sigaction (SIGINT
, &act
, NULL
);
934 signal (SIGINFO
, siginfo_handler
);
935 siginterrupt (SIGINFO
, 1);
937 if (signal (SIGINT
, SIG_IGN
) != SIG_IGN
)
939 signal (SIGINT
, interrupt_handler
);
940 siginterrupt (SIGINT
, 1);
945 /* Close FD. Return 0 if successful, -1 (setting errno) otherwise.
946 If close fails with errno == EINTR, POSIX says the file descriptor
947 is in an unspecified state, so keep trying to close FD but do not
948 consider EBADF to be an error. Do not process signals. This all
949 differs somewhat from functions like ifdatasync and ifsync. */
957 while (close (fd
) != 0 && errno
!= EBADF
);
965 if (iclose (STDIN_FILENO
) != 0)
966 die (EXIT_FAILURE
, errno
, _("closing input file %s"), quoteaf (input_file
));
968 /* Don't remove this call to close, even though close_stdout
969 closes standard output. This close is necessary when cleanup
970 is called as a consequence of signal handling. */
971 if (iclose (STDOUT_FILENO
) != 0)
972 die (EXIT_FAILURE
, errno
,
973 _("closing output file %s"), quoteaf (output_file
));
976 /* Process any pending signals. If signals are caught, this function
977 should be called periodically. Ideally there should never be an
978 unbounded amount of time when signals are not being processed. */
981 process_signals (void)
983 while (interrupt_signal
|| info_signal_count
)
989 sigprocmask (SIG_BLOCK
, &caught_signals
, &oldset
);
991 /* Reload interrupt_signal and info_signal_count, in case a new
992 signal was handled before sigprocmask took effect. */
993 interrupt
= interrupt_signal
;
994 infos
= info_signal_count
;
997 info_signal_count
= infos
- 1;
999 sigprocmask (SIG_SETMASK
, &oldset
, NULL
);
1012 /* Process signals first, so that cleanup is called at most once. */
1018 static void ATTRIBUTE_NORETURN
1025 /* Return LEN rounded down to a multiple of IO_BUFSIZE
1026 (to minimize calls to the expensive posix_fadvise(,POSIX_FADV_DONTNEED),
1027 while storing the remainder internally per FD.
1028 Pass LEN == 0 to get the current remainder. */
1031 cache_round (int fd
, off_t len
)
1033 static off_t i_pending
, o_pending
;
1034 off_t
*pending
= (fd
== STDIN_FILENO
? &i_pending
: &o_pending
);
1038 uintmax_t c_pending
= *pending
+ len
;
1039 *pending
= c_pending
% IO_BUFSIZE
;
1040 if (c_pending
> *pending
)
1041 len
= c_pending
- *pending
;
1051 /* Discard the cache from the current offset of either
1052 STDIN_FILENO or STDOUT_FILENO.
1053 Return true on success. */
1056 invalidate_cache (int fd
, off_t len
)
1060 bool nocache_eof
= (fd
== STDIN_FILENO
? i_nocache_eof
: o_nocache_eof
);
1062 /* Minimize syscalls. */
1063 off_t clen
= cache_round (fd
, len
);
1065 return true; /* Don't advise this time. */
1066 else if (! len
&& ! clen
&& ! nocache_eof
)
1068 off_t pending
= len
? cache_round (fd
, 0) : 0;
1070 if (fd
== STDIN_FILENO
)
1073 offset
= input_offset
;
1082 static off_t output_offset
= -2;
1084 if (output_offset
!= -1)
1086 if (output_offset
< 0)
1087 output_offset
= lseek (fd
, 0, SEEK_CUR
);
1089 output_offset
+= clen
+ pending
;
1092 offset
= output_offset
;
1097 if (! len
&& clen
&& nocache_eof
)
1103 /* Note we're being careful here to only invalidate what
1104 we've read, so as not to dump any read ahead cache.
1105 Note also the kernel is conservative and only invalidates
1106 full pages in the specified range. */
1107 #if HAVE_POSIX_FADVISE
1108 offset
= offset
- clen
- pending
;
1109 /* ensure full page specified when invalidating to eof. */
1111 offset
-= offset
% page_size
;
1112 adv_ret
= posix_fadvise (fd
, offset
, clen
, POSIX_FADV_DONTNEED
);
1118 return adv_ret
!= -1 ? true : false;
1121 /* Read from FD into the buffer BUF of size SIZE, processing any
1122 signals that arrive before bytes are read. Return the number of
1123 bytes read if successful, -1 (setting errno) on failure. */
1126 iread (int fd
, char *buf
, size_t size
)
1129 static ssize_t prev_nread
;
1134 nread
= read (fd
, buf
, size
);
1135 /* Ignore final read error with iflag=direct as that
1136 returns EINVAL due to the non aligned file offset. */
1137 if (nread
== -1 && errno
== EINVAL
1138 && 0 < prev_nread
&& prev_nread
< size
1139 && (input_flags
& O_DIRECT
))
1145 while (nread
< 0 && errno
== EINTR
);
1147 /* Short read may be due to received signal. */
1148 if (0 < nread
&& nread
< size
)
1151 if (0 < nread
&& warn_partial_read
)
1153 if (0 < prev_nread
&& prev_nread
< size
)
1155 uintmax_t prev
= prev_nread
;
1156 if (status_level
!= STATUS_NONE
)
1157 error (0, 0, ngettext (("warning: partial read (%"PRIuMAX
" byte); "
1158 "suggest iflag=fullblock"),
1159 ("warning: partial read (%"PRIuMAX
" bytes); "
1160 "suggest iflag=fullblock"),
1161 select_plural (prev
)),
1163 warn_partial_read
= false;
1171 /* Wrapper around iread function to accumulate full blocks. */
1173 iread_fullblock (int fd
, char *buf
, size_t size
)
1179 ssize_t ncurr
= iread (fd
, buf
, size
);
1192 /* Write to FD the buffer BUF of size SIZE, processing any signals
1193 that arrive. Return the number of bytes written, setting errno if
1194 this is less than SIZE. Keep trying if there are partial
1198 iwrite (int fd
, char const *buf
, size_t size
)
1200 size_t total_written
= 0;
1202 if ((output_flags
& O_DIRECT
) && size
< output_blocksize
)
1204 int old_flags
= fcntl (STDOUT_FILENO
, F_GETFL
);
1205 if (fcntl (STDOUT_FILENO
, F_SETFL
, old_flags
& ~O_DIRECT
) != 0
1206 && status_level
!= STATUS_NONE
)
1207 error (0, errno
, _("failed to turn off O_DIRECT: %s"),
1208 quotef (output_file
));
1210 /* Since we have just turned off O_DIRECT for the final write,
1211 we try to preserve some of its semantics. */
1213 /* Call invalidate_cache to setup the appropriate offsets
1214 for subsequent calls. */
1215 o_nocache_eof
= true;
1216 invalidate_cache (STDOUT_FILENO
, 0);
1218 /* Attempt to ensure that that final block is committed
1219 to disk as quickly as possible. */
1220 conversions_mask
|= C_FSYNC
;
1222 /* After the subsequent fsync we'll call invalidate_cache
1223 to attempt to clear all data from the page cache. */
1226 while (total_written
< size
)
1228 ssize_t nwritten
= 0;
1231 /* Perform a seek for a NUL block if sparse output is enabled. */
1232 final_op_was_seek
= false;
1233 if ((conversions_mask
& C_SPARSE
) && is_nul (buf
, size
))
1235 if (lseek (fd
, size
, SEEK_CUR
) < 0)
1237 conversions_mask
&= ~C_SPARSE
;
1238 /* Don't warn about the advisory sparse request. */
1242 final_op_was_seek
= true;
1248 nwritten
= write (fd
, buf
+ total_written
, size
- total_written
);
1255 else if (nwritten
== 0)
1257 /* Some buggy drivers return 0 when one tries to write beyond
1258 a device's end. (Example: Linux kernel 1.2.13 on /dev/fd0.)
1259 Set errno to ENOSPC so they get a sensible diagnostic. */
1264 total_written
+= nwritten
;
1267 if (o_nocache
&& total_written
)
1268 invalidate_cache (fd
, total_written
);
1270 return total_written
;
1273 /* Write, then empty, the output buffer 'obuf'. */
1278 size_t nwritten
= iwrite (STDOUT_FILENO
, obuf
, output_blocksize
);
1279 w_bytes
+= nwritten
;
1280 if (nwritten
!= output_blocksize
)
1282 error (0, errno
, _("writing to %s"), quoteaf (output_file
));
1285 quit (EXIT_FAILURE
);
1292 /* Restart on EINTR from fdatasync. */
1302 ret
= fdatasync (fd
);
1304 while (ret
< 0 && errno
== EINTR
);
1309 /* Restart on EINTR from fd_reopen. */
1312 ifd_reopen (int desired_fd
, char const *file
, int flag
, mode_t mode
)
1319 ret
= fd_reopen (desired_fd
, file
, flag
, mode
);
1321 while (ret
< 0 && errno
== EINTR
);
1326 /* Restart on EINTR from fstat. */
1329 ifstat (int fd
, struct stat
*st
)
1336 ret
= fstat (fd
, st
);
1338 while (ret
< 0 && errno
== EINTR
);
1343 /* Restart on EINTR from fsync. */
1355 while (ret
< 0 && errno
== EINTR
);
1360 /* Restart on EINTR from ftruncate. */
1363 iftruncate (int fd
, off_t length
)
1370 ret
= ftruncate (fd
, length
);
1372 while (ret
< 0 && errno
== EINTR
);
1377 /* Return true if STR is of the form "PATTERN" or "PATTERNDELIM...". */
1379 static bool _GL_ATTRIBUTE_PURE
1380 operand_matches (char const *str
, char const *pattern
, char delim
)
1383 if (*str
++ != *pattern
++)
1385 return !*str
|| *str
== delim
;
1388 /* Interpret one "conv=..." or similar operand STR according to the
1389 symbols in TABLE, returning the flags specified. If the operand
1390 cannot be parsed, use ERROR_MSGID to generate a diagnostic. */
1393 parse_symbols (char const *str
, struct symbol_value
const *table
,
1394 bool exclusive
, char const *error_msgid
)
1400 char const *strcomma
= strchr (str
, ',');
1401 struct symbol_value
const *entry
;
1404 ! (operand_matches (str
, entry
->symbol
, ',') && entry
->value
);
1407 if (! entry
->symbol
[0])
1409 size_t slen
= strcomma
? strcomma
- str
: strlen (str
);
1410 error (0, 0, "%s: %s", _(error_msgid
),
1411 quotearg_n_style_mem (0, locale_quoting_style
, str
, slen
));
1412 usage (EXIT_FAILURE
);
1417 value
= entry
->value
;
1419 value
|= entry
->value
;
1428 /* Return the value of STR, interpreted as a non-negative decimal integer,
1429 optionally multiplied by various values.
1430 Set *INVALID to a nonzero error value if STR does not represent a
1431 number in this format. */
1434 parse_integer (const char *str
, strtol_error
*invalid
)
1438 strtol_error e
= xstrtoumax (str
, &suffix
, 10, &n
, "bcEGkKMPTwYZ0");
1440 if (e
== LONGINT_INVALID_SUFFIX_CHAR
&& *suffix
== 'x')
1442 uintmax_t multiplier
= parse_integer (suffix
+ 1, invalid
);
1444 if (multiplier
!= 0 && n
* multiplier
/ multiplier
!= n
)
1446 *invalid
= LONGINT_OVERFLOW
;
1450 if (n
== 0 && STRPREFIX (str
, "0x"))
1452 _("warning: %s is a zero multiplier; "
1453 "use %s if that is intended"),
1454 quote_n (0, "0x"), quote_n (1, "00x"));
1458 else if (e
!= LONGINT_OK
)
1467 /* OPERAND is of the form "X=...". Return true if X is NAME. */
1469 static bool _GL_ATTRIBUTE_PURE
1470 operand_is (char const *operand
, char const *name
)
1472 return operand_matches (operand
, name
, '=');
1476 scanargs (int argc
, char *const *argv
)
1478 size_t blocksize
= 0;
1479 uintmax_t count
= (uintmax_t) -1;
1483 for (int i
= optind
; i
< argc
; i
++)
1485 char const *name
= argv
[i
];
1486 char const *val
= strchr (name
, '=');
1490 error (0, 0, _("unrecognized operand %s"),
1492 usage (EXIT_FAILURE
);
1496 if (operand_is (name
, "if"))
1498 else if (operand_is (name
, "of"))
1500 else if (operand_is (name
, "conv"))
1501 conversions_mask
|= parse_symbols (val
, conversions
, false,
1502 N_("invalid conversion"));
1503 else if (operand_is (name
, "iflag"))
1504 input_flags
|= parse_symbols (val
, flags
, false,
1505 N_("invalid input flag"));
1506 else if (operand_is (name
, "oflag"))
1507 output_flags
|= parse_symbols (val
, flags
, false,
1508 N_("invalid output flag"));
1509 else if (operand_is (name
, "status"))
1510 status_level
= parse_symbols (val
, statuses
, true,
1511 N_("invalid status level"));
1514 strtol_error invalid
= LONGINT_OK
;
1515 uintmax_t n
= parse_integer (val
, &invalid
);
1516 uintmax_t n_min
= 0;
1517 uintmax_t n_max
= UINTMAX_MAX
;
1519 if (operand_is (name
, "ibs"))
1522 n_max
= MAX_BLOCKSIZE (INPUT_BLOCK_SLOP
);
1523 input_blocksize
= n
;
1525 else if (operand_is (name
, "obs"))
1528 n_max
= MAX_BLOCKSIZE (OUTPUT_BLOCK_SLOP
);
1529 output_blocksize
= n
;
1531 else if (operand_is (name
, "bs"))
1534 n_max
= MAX_BLOCKSIZE (INPUT_BLOCK_SLOP
);
1537 else if (operand_is (name
, "cbs"))
1541 conversion_blocksize
= n
;
1543 else if (operand_is (name
, "skip"))
1545 else if (operand_is (name
, "seek"))
1547 else if (operand_is (name
, "count"))
1551 error (0, 0, _("unrecognized operand %s"),
1553 usage (EXIT_FAILURE
);
1557 invalid
= LONGINT_INVALID
;
1559 invalid
= LONGINT_OVERFLOW
;
1561 if (invalid
!= LONGINT_OK
)
1562 die (EXIT_FAILURE
, invalid
== LONGINT_OVERFLOW
? EOVERFLOW
: 0,
1563 "%s: %s", _("invalid number"), quote (val
));
1568 input_blocksize
= output_blocksize
= blocksize
;
1571 /* POSIX says dd aggregates partial reads into
1572 output_blocksize if bs= is not specified. */
1573 conversions_mask
|= C_TWOBUFS
;
1576 if (input_blocksize
== 0)
1577 input_blocksize
= DEFAULT_BLOCKSIZE
;
1578 if (output_blocksize
== 0)
1579 output_blocksize
= DEFAULT_BLOCKSIZE
;
1580 if (conversion_blocksize
== 0)
1581 conversions_mask
&= ~(C_BLOCK
| C_UNBLOCK
);
1583 if (input_flags
& (O_DSYNC
| O_SYNC
))
1584 input_flags
|= O_RSYNC
;
1586 if (output_flags
& O_FULLBLOCK
)
1588 error (0, 0, "%s: %s", _("invalid output flag"), quote ("fullblock"));
1589 usage (EXIT_FAILURE
);
1592 if (input_flags
& O_SEEK_BYTES
)
1594 error (0, 0, "%s: %s", _("invalid input flag"), quote ("seek_bytes"));
1595 usage (EXIT_FAILURE
);
1598 if (output_flags
& (O_COUNT_BYTES
| O_SKIP_BYTES
))
1600 error (0, 0, "%s: %s", _("invalid output flag"),
1601 quote (output_flags
& O_COUNT_BYTES
1602 ? "count_bytes" : "skip_bytes"));
1603 usage (EXIT_FAILURE
);
1606 if (input_flags
& O_SKIP_BYTES
&& skip
!= 0)
1608 skip_records
= skip
/ input_blocksize
;
1609 skip_bytes
= skip
% input_blocksize
;
1612 skip_records
= skip
;
1614 if (input_flags
& O_COUNT_BYTES
&& count
!= (uintmax_t) -1)
1616 max_records
= count
/ input_blocksize
;
1617 max_bytes
= count
% input_blocksize
;
1619 else if (count
!= (uintmax_t) -1)
1620 max_records
= count
;
1622 if (output_flags
& O_SEEK_BYTES
&& seek
!= 0)
1624 seek_records
= seek
/ output_blocksize
;
1625 seek_bytes
= seek
% output_blocksize
;
1628 seek_records
= seek
;
1630 /* Warn about partial reads if bs=SIZE is given and iflag=fullblock
1631 is not, and if counting or skipping bytes or using direct I/O.
1632 This helps to avoid confusion with miscounts, and to avoid issues
1633 with direct I/O on GNU/Linux. */
1635 (! (conversions_mask
& C_TWOBUFS
) && ! (input_flags
& O_FULLBLOCK
)
1637 || (0 < max_records
&& max_records
< (uintmax_t) -1)
1638 || (input_flags
| output_flags
) & O_DIRECT
));
1640 iread_fnc
= ((input_flags
& O_FULLBLOCK
)
1643 input_flags
&= ~O_FULLBLOCK
;
1645 if (multiple_bits_set (conversions_mask
& (C_ASCII
| C_EBCDIC
| C_IBM
)))
1646 die (EXIT_FAILURE
, 0, _("cannot combine any two of {ascii,ebcdic,ibm}"));
1647 if (multiple_bits_set (conversions_mask
& (C_BLOCK
| C_UNBLOCK
)))
1648 die (EXIT_FAILURE
, 0, _("cannot combine block and unblock"));
1649 if (multiple_bits_set (conversions_mask
& (C_LCASE
| C_UCASE
)))
1650 die (EXIT_FAILURE
, 0, _("cannot combine lcase and ucase"));
1651 if (multiple_bits_set (conversions_mask
& (C_EXCL
| C_NOCREAT
)))
1652 die (EXIT_FAILURE
, 0, _("cannot combine excl and nocreat"));
1653 if (multiple_bits_set (input_flags
& (O_DIRECT
| O_NOCACHE
))
1654 || multiple_bits_set (output_flags
& (O_DIRECT
| O_NOCACHE
)))
1655 die (EXIT_FAILURE
, 0, _("cannot combine direct and nocache"));
1657 if (input_flags
& O_NOCACHE
)
1660 i_nocache_eof
= (max_records
== 0 && max_bytes
== 0);
1661 input_flags
&= ~O_NOCACHE
;
1663 if (output_flags
& O_NOCACHE
)
1666 o_nocache_eof
= (max_records
== 0 && max_bytes
== 0);
1667 output_flags
&= ~O_NOCACHE
;
1671 /* Fix up translation table. */
1674 apply_translations (void)
1678 if (conversions_mask
& C_ASCII
)
1679 translate_charset (ebcdic_to_ascii
);
1681 if (conversions_mask
& C_UCASE
)
1683 for (i
= 0; i
< 256; i
++)
1684 trans_table
[i
] = toupper (trans_table
[i
]);
1685 translation_needed
= true;
1687 else if (conversions_mask
& C_LCASE
)
1689 for (i
= 0; i
< 256; i
++)
1690 trans_table
[i
] = tolower (trans_table
[i
]);
1691 translation_needed
= true;
1694 if (conversions_mask
& C_EBCDIC
)
1696 translate_charset (ascii_to_ebcdic
);
1697 newline_character
= ascii_to_ebcdic
['\n'];
1698 space_character
= ascii_to_ebcdic
[' '];
1700 else if (conversions_mask
& C_IBM
)
1702 translate_charset (ascii_to_ibm
);
1703 newline_character
= ascii_to_ibm
['\n'];
1704 space_character
= ascii_to_ibm
[' '];
1708 /* Apply the character-set translations specified by the user
1709 to the NREAD bytes in BUF. */
1712 translate_buffer (char *buf
, size_t nread
)
1716 for (i
= nread
, cp
= buf
; i
; i
--, cp
++)
1717 *cp
= trans_table
[to_uchar (*cp
)];
1720 /* If true, the last char from the previous call to 'swab_buffer'
1721 is saved in 'saved_char'. */
1722 static bool char_is_saved
= false;
1724 /* Odd char from previous call. */
1725 static char saved_char
;
1727 /* Swap NREAD bytes in BUF, plus possibly an initial char from the
1728 previous call. If NREAD is odd, save the last char for the
1729 next call. Return the new start of the BUF buffer. */
1732 swab_buffer (char *buf
, size_t *nread
)
1734 char *bufstart
= buf
;
1736 /* Is a char left from last time? */
1739 *--bufstart
= saved_char
;
1741 char_is_saved
= false;
1746 /* An odd number of chars are in the buffer. */
1747 saved_char
= bufstart
[--*nread
];
1748 char_is_saved
= true;
1751 /* Do the byte-swapping by moving every second character two
1752 positions toward the end, working from the end of the buffer
1753 toward the beginning. This way we only move half of the data. */
1755 char *cp
= bufstart
+ *nread
; /* Start one char past the last. */
1756 for (size_t i
= *nread
/ 2; i
; i
--, cp
-= 2)
1762 /* Add OFFSET to the input offset, setting the overflow flag if
1766 advance_input_offset (uintmax_t offset
)
1768 input_offset
+= offset
;
1769 if (input_offset
< offset
)
1770 input_offset_overflow
= true;
1773 /* This is a wrapper for lseek. It detects and warns about a kernel
1774 bug that makes lseek a no-op for tape devices, even though the kernel
1775 lseek return value suggests that the function succeeded.
1777 The parameters are the same as those of the lseek function, but
1778 with the addition of FILENAME, the name of the file associated with
1779 descriptor FDESC. The file name is used solely in the warning that's
1780 printed when the bug is detected. Return the same value that lseek
1781 would have returned, but when the lseek bug is detected, return -1
1782 to indicate that lseek failed.
1784 The offending behavior has been confirmed with an Exabyte SCSI tape
1785 drive accessed via /dev/nst0 on both Linux 2.2.17 and 2.4.16 kernels. */
1787 #if defined __linux__ && HAVE_SYS_MTIO_H
1789 # include <sys/mtio.h>
1791 # define MT_SAME_POSITION(P, Q) \
1792 ((P).mt_resid == (Q).mt_resid \
1793 && (P).mt_fileno == (Q).mt_fileno \
1794 && (P).mt_blkno == (Q).mt_blkno)
1797 skip_via_lseek (char const *filename
, int fdesc
, off_t offset
, int whence
)
1801 bool got_original_tape_position
= (ioctl (fdesc
, MTIOCGET
, &s1
) == 0);
1802 /* known bad device type */
1803 /* && s.mt_type == MT_ISSCSI2 */
1805 off_t new_position
= lseek (fdesc
, offset
, whence
);
1806 if (0 <= new_position
1807 && got_original_tape_position
1808 && ioctl (fdesc
, MTIOCGET
, &s2
) == 0
1809 && MT_SAME_POSITION (s1
, s2
))
1811 if (status_level
!= STATUS_NONE
)
1812 error (0, 0, _("warning: working around lseek kernel bug for file "
1813 "(%s)\n of mt_type=0x%0lx -- "
1814 "see <sys/mtio.h> for the list of types"),
1815 filename
, s2
.mt_type
+ 0Lu);
1820 return new_position
;
1823 # define skip_via_lseek(Filename, Fd, Offset, Whence) lseek (Fd, Offset, Whence)
1826 /* Throw away RECORDS blocks of BLOCKSIZE bytes plus BYTES bytes on
1827 file descriptor FDESC, which is open with read permission for FILE.
1828 Store up to BLOCKSIZE bytes of the data at a time in IBUF or OBUF, if
1829 necessary. RECORDS or BYTES must be nonzero. If FDESC is
1830 STDIN_FILENO, advance the input offset. Return the number of
1831 records remaining, i.e., that were not skipped because EOF was
1832 reached. If FDESC is STDOUT_FILENO, on return, BYTES is the
1833 remaining bytes in addition to the remaining records. */
1836 skip (int fdesc
, char const *file
, uintmax_t records
, size_t blocksize
,
1839 uintmax_t offset
= records
* blocksize
+ *bytes
;
1841 /* Try lseek and if an error indicates it was an inappropriate operation --
1842 or if the file offset is not representable as an off_t --
1843 fall back on using read. */
1846 if (records
<= OFF_T_MAX
/ blocksize
1847 && 0 <= skip_via_lseek (file
, fdesc
, offset
, SEEK_CUR
))
1849 if (fdesc
== STDIN_FILENO
)
1852 if (ifstat (STDIN_FILENO
, &st
) != 0)
1853 die (EXIT_FAILURE
, errno
, _("cannot fstat %s"), quoteaf (file
));
1854 if (usable_st_size (&st
) && st
.st_size
< input_offset
+ offset
)
1856 /* When skipping past EOF, return the number of _full_ blocks
1857 * that are not skipped, and set offset to EOF, so the caller
1858 * can determine the requested skip was not satisfied. */
1859 records
= ( offset
- st
.st_size
) / blocksize
;
1860 offset
= st
.st_size
- input_offset
;
1864 advance_input_offset (offset
);
1875 int lseek_errno
= errno
;
1877 /* The seek request may have failed above if it was too big
1878 (> device size, > max file size, etc.)
1879 Or it may not have been done at all (> OFF_T_MAX).
1880 Therefore try to seek to the end of the file,
1881 to avoid redundant reading. */
1882 if ((skip_via_lseek (file
, fdesc
, 0, SEEK_END
)) >= 0)
1884 /* File is seekable, and we're at the end of it, and
1885 size <= OFF_T_MAX. So there's no point using read to advance. */
1889 /* The original seek was not attempted as offset > OFF_T_MAX.
1890 We should error for write as can't get to the desired
1891 location, even if OFF_T_MAX < max file size.
1892 For read we're not going to read any data anyway,
1893 so we should error for consistency.
1894 It would be nice to not error for /dev/{zero,null}
1895 for any offset, but that's not a significant issue. */
1896 lseek_errno
= EOVERFLOW
;
1899 if (fdesc
== STDIN_FILENO
)
1900 error (0, lseek_errno
, _("%s: cannot skip"), quotef (file
));
1902 error (0, lseek_errno
, _("%s: cannot seek"), quotef (file
));
1903 /* If the file has a specific size and we've asked
1904 to skip/seek beyond the max allowable, then quit. */
1905 quit (EXIT_FAILURE
);
1907 /* else file_size && offset > OFF_T_MAX or file ! seekable */
1910 if (fdesc
== STDIN_FILENO
)
1923 ssize_t nread
= iread_fnc (fdesc
, buf
, records
? blocksize
: *bytes
);
1926 if (fdesc
== STDIN_FILENO
)
1928 error (0, errno
, _("error reading %s"), quoteaf (file
));
1929 if (conversions_mask
& C_NOERROR
)
1933 error (0, lseek_errno
, _("%s: cannot seek"), quotef (file
));
1934 quit (EXIT_FAILURE
);
1936 else if (nread
== 0)
1938 else if (fdesc
== STDIN_FILENO
)
1939 advance_input_offset (nread
);
1946 while (records
|| *bytes
);
1952 /* Advance the input by NBYTES if possible, after a read error.
1953 The input file offset may or may not have advanced after the failed
1954 read; adjust it to point just after the bad record regardless.
1955 Return true if successful, or if the input is already known to not
1959 advance_input_after_read_error (size_t nbytes
)
1961 if (! input_seekable
)
1963 if (input_seek_errno
== ESPIPE
)
1965 errno
= input_seek_errno
;
1970 advance_input_offset (nbytes
);
1971 input_offset_overflow
|= (OFF_T_MAX
< input_offset
);
1972 if (input_offset_overflow
)
1974 error (0, 0, _("offset overflow while reading file %s"),
1975 quoteaf (input_file
));
1978 offset
= lseek (STDIN_FILENO
, 0, SEEK_CUR
);
1982 if (offset
== input_offset
)
1984 diff
= input_offset
- offset
;
1985 if (! (0 <= diff
&& diff
<= nbytes
) && status_level
!= STATUS_NONE
)
1986 error (0, 0, _("warning: invalid file offset after failed read"));
1987 if (0 <= skip_via_lseek (input_file
, STDIN_FILENO
, diff
, SEEK_CUR
))
1990 error (0, 0, _("cannot work around kernel bug after all"));
1994 error (0, errno
, _("%s: cannot seek"), quotef (input_file
));
1998 /* Copy NREAD bytes of BUF, with no conversions. */
2001 copy_simple (char const *buf
, size_t nread
)
2003 const char *start
= buf
; /* First uncopied char in BUF. */
2007 size_t nfree
= MIN (nread
, output_blocksize
- oc
);
2009 memcpy (obuf
+ oc
, start
, nfree
);
2011 nread
-= nfree
; /* Update the number of bytes left to copy. */
2014 if (oc
>= output_blocksize
)
2020 /* Copy NREAD bytes of BUF, doing conv=block
2021 (pad newline-terminated records to 'conversion_blocksize',
2022 replacing the newline with trailing spaces). */
2025 copy_with_block (char const *buf
, size_t nread
)
2027 for (size_t i
= nread
; i
; i
--, buf
++)
2029 if (*buf
== newline_character
)
2031 if (col
< conversion_blocksize
)
2034 for (j
= col
; j
< conversion_blocksize
; j
++)
2035 output_char (space_character
);
2041 if (col
== conversion_blocksize
)
2043 else if (col
< conversion_blocksize
)
2050 /* Copy NREAD bytes of BUF, doing conv=unblock
2051 (replace trailing spaces in 'conversion_blocksize'-sized records
2055 copy_with_unblock (char const *buf
, size_t nread
)
2057 static size_t pending_spaces
= 0;
2059 for (size_t i
= 0; i
< nread
; i
++)
2063 if (col
++ >= conversion_blocksize
)
2065 col
= pending_spaces
= 0; /* Wipe out any pending spaces. */
2066 i
--; /* Push the char back; get it later. */
2067 output_char (newline_character
);
2069 else if (c
== space_character
)
2073 /* 'c' is the character after a run of spaces that were not
2074 at the end of the conversion buffer. Output them. */
2075 while (pending_spaces
)
2077 output_char (space_character
);
2085 /* Set the file descriptor flags for FD that correspond to the nonzero bits
2086 in ADD_FLAGS. The file's name is NAME. */
2089 set_fd_flags (int fd
, int add_flags
, char const *name
)
2091 /* Ignore file creation flags that are no-ops on file descriptors. */
2092 add_flags
&= ~ (O_NOCTTY
| O_NOFOLLOW
);
2096 int old_flags
= fcntl (fd
, F_GETFL
);
2097 int new_flags
= old_flags
| add_flags
;
2101 else if (old_flags
!= new_flags
)
2103 if (new_flags
& (O_DIRECTORY
| O_NOLINKS
))
2105 /* NEW_FLAGS contains at least one file creation flag that
2106 requires some checking of the open file descriptor. */
2108 if (ifstat (fd
, &st
) != 0)
2110 else if ((new_flags
& O_DIRECTORY
) && ! S_ISDIR (st
.st_mode
))
2115 else if ((new_flags
& O_NOLINKS
) && 1 < st
.st_nlink
)
2120 new_flags
&= ~ (O_DIRECTORY
| O_NOLINKS
);
2123 if (ok
&& old_flags
!= new_flags
2124 && fcntl (fd
, F_SETFL
, new_flags
) == -1)
2129 die (EXIT_FAILURE
, errno
, _("setting flags for %s"), quoteaf (name
));
2133 /* The main loop. */
2138 char *bufstart
; /* Input buffer. */
2139 ssize_t nread
; /* Bytes read in the current block. */
2141 /* If nonzero, then the previously read block was partial and
2142 PARTREAD was its size. */
2143 size_t partread
= 0;
2145 int exit_status
= EXIT_SUCCESS
;
2146 size_t n_bytes_read
;
2148 /* Leave at least one extra byte at the beginning and end of 'ibuf'
2149 for conv=swab, but keep the buffer address even. But some peculiar
2150 device drivers work only with word-aligned buffers, so leave an
2153 /* Some devices require alignment on a sector or page boundary
2154 (e.g. character disk devices). Align the input buffer to a
2155 page boundary to cover all bases. Note that due to the swab
2156 algorithm, we must have at least one byte in the page before
2157 the input buffer; thus we allocate 2 pages of slop in the
2158 real buffer. 8k above the blocksize shouldn't bother anyone.
2160 The page alignment is necessary on any Linux kernel that supports
2161 either the SGI raw I/O patch or Steven Tweedies raw I/O patch.
2162 It is necessary when accessing raw (i.e., character special) disk
2163 devices on Unixware or other SVR4-derived system. */
2165 if (skip_records
!= 0 || skip_bytes
!= 0)
2167 uintmax_t us_bytes
= input_offset
+ (skip_records
* input_blocksize
)
2169 uintmax_t us_blocks
= skip (STDIN_FILENO
, input_file
,
2170 skip_records
, input_blocksize
, &skip_bytes
);
2171 us_bytes
-= input_offset
;
2173 /* POSIX doesn't say what to do when dd detects it has been
2174 asked to skip past EOF, so I assume it's non-fatal.
2175 There are 3 reasons why there might be unskipped blocks/bytes:
2176 1. file is too small
2177 2. pipe has not enough data
2179 if ((us_blocks
|| (!input_offset_overflow
&& us_bytes
))
2180 && status_level
!= STATUS_NONE
)
2183 _("%s: cannot skip to specified offset"), quotef (input_file
));
2187 if (seek_records
!= 0 || seek_bytes
!= 0)
2189 size_t bytes
= seek_bytes
;
2190 uintmax_t write_records
= skip (STDOUT_FILENO
, output_file
,
2191 seek_records
, output_blocksize
, &bytes
);
2193 if (write_records
!= 0 || bytes
!= 0)
2195 memset (obuf
, 0, write_records
? output_blocksize
: bytes
);
2199 size_t size
= write_records
? output_blocksize
: bytes
;
2200 if (iwrite (STDOUT_FILENO
, obuf
, size
) != size
)
2202 error (0, errno
, _("writing to %s"), quoteaf (output_file
));
2203 quit (EXIT_FAILURE
);
2206 if (write_records
!= 0)
2211 while (write_records
|| bytes
);
2215 if (max_records
== 0 && max_bytes
== 0)
2223 if (status_level
== STATUS_PROGRESS
)
2225 xtime_t progress_time
= gethrxtime ();
2226 if (next_time
<= progress_time
)
2228 print_xfer_stats (progress_time
);
2229 next_time
+= XTIME_PRECISION
;
2233 if (r_partial
+ r_full
>= max_records
+ !!max_bytes
)
2236 /* Zero the buffer before reading, so that if we get a read error,
2237 whatever data we are able to read is followed by zeros.
2238 This minimizes data loss. */
2239 if ((conversions_mask
& C_SYNC
) && (conversions_mask
& C_NOERROR
))
2241 (conversions_mask
& (C_BLOCK
| C_UNBLOCK
)) ? ' ' : '\0',
2244 if (r_partial
+ r_full
>= max_records
)
2245 nread
= iread_fnc (STDIN_FILENO
, ibuf
, max_bytes
);
2247 nread
= iread_fnc (STDIN_FILENO
, ibuf
, input_blocksize
);
2251 advance_input_offset (nread
);
2253 invalidate_cache (STDIN_FILENO
, nread
);
2255 else if (nread
== 0)
2257 i_nocache_eof
|= i_nocache
;
2258 o_nocache_eof
|= o_nocache
&& ! (conversions_mask
& C_NOTRUNC
);
2263 if (!(conversions_mask
& C_NOERROR
) || status_level
!= STATUS_NONE
)
2264 error (0, errno
, _("error reading %s"), quoteaf (input_file
));
2266 if (conversions_mask
& C_NOERROR
)
2269 size_t bad_portion
= input_blocksize
- partread
;
2271 /* We already know this data is not cached,
2272 but call this so that correct offsets are maintained. */
2273 invalidate_cache (STDIN_FILENO
, bad_portion
);
2275 /* Seek past the bad block if possible. */
2276 if (!advance_input_after_read_error (bad_portion
))
2278 exit_status
= EXIT_FAILURE
;
2280 /* Suppress duplicate diagnostics. */
2281 input_seekable
= false;
2282 input_seek_errno
= ESPIPE
;
2284 if ((conversions_mask
& C_SYNC
) && !partread
)
2285 /* Replace the missing input with null bytes and
2286 proceed normally. */
2293 /* Write any partial block. */
2294 exit_status
= EXIT_FAILURE
;
2299 n_bytes_read
= nread
;
2301 if (n_bytes_read
< input_blocksize
)
2304 partread
= n_bytes_read
;
2305 if (conversions_mask
& C_SYNC
)
2307 if (!(conversions_mask
& C_NOERROR
))
2308 /* If C_NOERROR, we zeroed the block before reading. */
2309 memset (ibuf
+ n_bytes_read
,
2310 (conversions_mask
& (C_BLOCK
| C_UNBLOCK
)) ? ' ' : '\0',
2311 input_blocksize
- n_bytes_read
);
2312 n_bytes_read
= input_blocksize
;
2321 if (ibuf
== obuf
) /* If not C_TWOBUFS. */
2323 size_t nwritten
= iwrite (STDOUT_FILENO
, obuf
, n_bytes_read
);
2324 w_bytes
+= nwritten
;
2325 if (nwritten
!= n_bytes_read
)
2327 error (0, errno
, _("error writing %s"), quoteaf (output_file
));
2328 return EXIT_FAILURE
;
2330 else if (n_bytes_read
== input_blocksize
)
2337 /* Do any translations on the whole buffer at once. */
2339 if (translation_needed
)
2340 translate_buffer (ibuf
, n_bytes_read
);
2342 if (conversions_mask
& C_SWAB
)
2343 bufstart
= swab_buffer (ibuf
, &n_bytes_read
);
2347 if (conversions_mask
& C_BLOCK
)
2348 copy_with_block (bufstart
, n_bytes_read
);
2349 else if (conversions_mask
& C_UNBLOCK
)
2350 copy_with_unblock (bufstart
, n_bytes_read
);
2352 copy_simple (bufstart
, n_bytes_read
);
2355 /* If we have a char left as a result of conv=swab, output it. */
2358 if (conversions_mask
& C_BLOCK
)
2359 copy_with_block (&saved_char
, 1);
2360 else if (conversions_mask
& C_UNBLOCK
)
2361 copy_with_unblock (&saved_char
, 1);
2363 output_char (saved_char
);
2366 if ((conversions_mask
& C_BLOCK
) && col
> 0)
2368 /* If the final input line didn't end with a '\n', pad
2369 the output block to 'conversion_blocksize' chars. */
2370 for (size_t i
= col
; i
< conversion_blocksize
; i
++)
2371 output_char (space_character
);
2374 if (col
&& (conversions_mask
& C_UNBLOCK
))
2376 /* If there was any output, add a final '\n'. */
2377 output_char (newline_character
);
2380 /* Write out the last block. */
2383 size_t nwritten
= iwrite (STDOUT_FILENO
, obuf
, oc
);
2384 w_bytes
+= nwritten
;
2389 error (0, errno
, _("error writing %s"), quoteaf (output_file
));
2390 return EXIT_FAILURE
;
2394 /* If the last write was converted to a seek, then for a regular file
2395 or shared memory object, ftruncate to extend the size. */
2396 if (final_op_was_seek
)
2398 struct stat stdout_stat
;
2399 if (ifstat (STDOUT_FILENO
, &stdout_stat
) != 0)
2401 error (0, errno
, _("cannot fstat %s"), quoteaf (output_file
));
2402 return EXIT_FAILURE
;
2404 if (S_ISREG (stdout_stat
.st_mode
) || S_TYPEISSHM (&stdout_stat
))
2406 off_t output_offset
= lseek (STDOUT_FILENO
, 0, SEEK_CUR
);
2407 if (0 <= output_offset
&& stdout_stat
.st_size
< output_offset
)
2409 if (iftruncate (STDOUT_FILENO
, output_offset
) != 0)
2412 _("failed to truncate to %" PRIdMAX
" bytes"
2413 " in output file %s"),
2414 (intmax_t) output_offset
, quoteaf (output_file
));
2415 return EXIT_FAILURE
;
2421 if ((conversions_mask
& C_FDATASYNC
) && ifdatasync (STDOUT_FILENO
) != 0)
2423 if (errno
!= ENOSYS
&& errno
!= EINVAL
)
2425 error (0, errno
, _("fdatasync failed for %s"), quoteaf (output_file
));
2426 exit_status
= EXIT_FAILURE
;
2428 conversions_mask
|= C_FSYNC
;
2431 if ((conversions_mask
& C_FSYNC
) && ifsync (STDOUT_FILENO
) != 0)
2433 error (0, errno
, _("fsync failed for %s"), quoteaf (output_file
));
2434 return EXIT_FAILURE
;
2441 main (int argc
, char **argv
)
2447 install_signal_handlers ();
2449 initialize_main (&argc
, &argv
);
2450 set_program_name (argv
[0]);
2451 setlocale (LC_ALL
, "");
2452 bindtextdomain (PACKAGE
, LOCALEDIR
);
2453 textdomain (PACKAGE
);
2455 /* Arrange to close stdout if parse_long_options exits. */
2456 atexit (maybe_close_stdout
);
2458 page_size
= getpagesize ();
2460 parse_gnu_standard_options_only (argc
, argv
, PROGRAM_NAME
, PACKAGE
, Version
,
2461 true, usage
, AUTHORS
, (char const *) NULL
);
2462 close_stdout_required
= false;
2464 /* Initialize translation table to identity translation. */
2465 for (i
= 0; i
< 256; i
++)
2468 /* Decode arguments. */
2469 scanargs (argc
, argv
);
2471 apply_translations ();
2473 if (input_file
== NULL
)
2475 input_file
= _("standard input");
2476 set_fd_flags (STDIN_FILENO
, input_flags
, input_file
);
2480 if (ifd_reopen (STDIN_FILENO
, input_file
, O_RDONLY
| input_flags
, 0) < 0)
2481 die (EXIT_FAILURE
, errno
, _("failed to open %s"),
2482 quoteaf (input_file
));
2485 offset
= lseek (STDIN_FILENO
, 0, SEEK_CUR
);
2486 input_seekable
= (0 <= offset
);
2487 input_offset
= MAX (0, offset
);
2488 input_seek_errno
= errno
;
2490 if (output_file
== NULL
)
2492 output_file
= _("standard output");
2493 set_fd_flags (STDOUT_FILENO
, output_flags
, output_file
);
2497 mode_t perms
= MODE_RW_UGO
;
2500 | (conversions_mask
& C_NOCREAT
? 0 : O_CREAT
)
2501 | (conversions_mask
& C_EXCL
? O_EXCL
: 0)
2502 | (seek_records
|| (conversions_mask
& C_NOTRUNC
) ? 0 : O_TRUNC
));
2504 /* Open the output file with *read* access only if we might
2505 need to read to satisfy a 'seek=' request. If we can't read
2506 the file, go ahead with write-only access; it might work. */
2508 || ifd_reopen (STDOUT_FILENO
, output_file
, O_RDWR
| opts
, perms
) < 0)
2509 && (ifd_reopen (STDOUT_FILENO
, output_file
, O_WRONLY
| opts
, perms
)
2511 die (EXIT_FAILURE
, errno
, _("failed to open %s"),
2512 quoteaf (output_file
));
2514 if (seek_records
!= 0 && !(conversions_mask
& C_NOTRUNC
))
2516 uintmax_t size
= seek_records
* output_blocksize
+ seek_bytes
;
2517 unsigned long int obs
= output_blocksize
;
2519 if (OFF_T_MAX
/ output_blocksize
< seek_records
)
2520 die (EXIT_FAILURE
, 0,
2521 _("offset too large: "
2522 "cannot truncate to a length of seek=%"PRIuMAX
""
2523 " (%lu-byte) blocks"),
2526 if (iftruncate (STDOUT_FILENO
, size
) != 0)
2528 /* Complain only when ftruncate fails on a regular file, a
2529 directory, or a shared memory object, as POSIX 1003.1-2004
2530 specifies ftruncate's behavior only for these file types.
2531 For example, do not complain when Linux kernel 2.4 ftruncate
2532 fails on /dev/fd0. */
2533 int ftruncate_errno
= errno
;
2534 struct stat stdout_stat
;
2535 if (ifstat (STDOUT_FILENO
, &stdout_stat
) != 0)
2536 die (EXIT_FAILURE
, errno
, _("cannot fstat %s"),
2537 quoteaf (output_file
));
2538 if (S_ISREG (stdout_stat
.st_mode
)
2539 || S_ISDIR (stdout_stat
.st_mode
)
2540 || S_TYPEISSHM (&stdout_stat
))
2541 die (EXIT_FAILURE
, ftruncate_errno
,
2542 _("failed to truncate to %"PRIuMAX
" bytes"
2543 " in output file %s"),
2544 size
, quoteaf (output_file
));
2549 start_time
= gethrxtime ();
2550 next_time
= start_time
+ XTIME_PRECISION
;
2552 exit_status
= dd_copy ();
2554 if (max_records
== 0 && max_bytes
== 0)
2556 /* Special case to invalidate cache to end of file. */
2557 if (i_nocache
&& !invalidate_cache (STDIN_FILENO
, 0))
2559 error (0, errno
, _("failed to discard cache for: %s"),
2560 quotef (input_file
));
2561 exit_status
= EXIT_FAILURE
;
2563 if (o_nocache
&& !invalidate_cache (STDOUT_FILENO
, 0))
2565 error (0, errno
, _("failed to discard cache for: %s"),
2566 quotef (output_file
));
2567 exit_status
= EXIT_FAILURE
;
2572 /* Invalidate any pending region or to EOF if appropriate. */
2573 if (i_nocache
|| i_nocache_eof
)
2574 invalidate_cache (STDIN_FILENO
, 0);
2575 if (o_nocache
|| o_nocache_eof
)
2576 invalidate_cache (STDOUT_FILENO
, 0);