1/* dd -- convert a file while copying it.
2   Copyright (C) 1985, 1990-1991, 1995-2010 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17/* Written by Paul Rubin, David MacKenzie, and Stuart Kemp. */
18
19#include <config.h>
20
21#define SWAB_ALIGN_OFFSET 2
22
23#include <sys/types.h>
24#include <signal.h>
25#include <getopt.h>
26
27#include "system.h"
28#include "close-stream.h"
29#include "error.h"
30#include "fd-reopen.h"
31#include "gethrxtime.h"
32#include "human.h"
33#include "ignore-value.h"
34#include "long-options.h"
35#include "quote.h"
36#include "quotearg.h"
37#include "xstrtol.h"
38#include "xtime.h"
39
40static void process_signals (void);
41
42/* The official name of this program (e.g., no `g' prefix).  */
43#define PROGRAM_NAME "dd"
44
45#define AUTHORS \
46  proper_name ("Paul Rubin"), \
47  proper_name ("David MacKenzie"), \
48  proper_name ("Stuart Kemp")
49
50/* Use SA_NOCLDSTOP as a proxy for whether the sigaction machinery is
51   present.  SA_NODEFER and SA_RESETHAND are XSI extensions.  */
52#ifndef SA_NOCLDSTOP
53# define SA_NOCLDSTOP 0
54# define sigprocmask(How, Set, Oset) /* empty */
55# define sigset_t int
56# if ! HAVE_SIGINTERRUPT
57#  define siginterrupt(sig, flag) /* empty */
58# endif
59#endif
60#ifndef SA_NODEFER
61# define SA_NODEFER 0
62#endif
63#ifndef SA_RESETHAND
64# define SA_RESETHAND 0
65#endif
66
67#ifndef SIGINFO
68# define SIGINFO SIGUSR1
69#endif
70
71/* This may belong in GNULIB's fcntl module instead.
72   Define O_CIO to 0 if it is not supported by this OS. */
73#ifndef O_CIO
74# define O_CIO 0
75#endif
76
77#if ! HAVE_FDATASYNC
78# define fdatasync(fd) (errno = ENOSYS, -1)
79#endif
80
81#define output_char(c)				\
82  do						\
83    {						\
84      obuf[oc++] = (c);				\
85      if (oc >= output_blocksize)		\
86        write_output ();			\
87    }						\
88  while (0)
89
90/* Default input and output blocksize. */
91#define DEFAULT_BLOCKSIZE 512
92
93/* How many bytes to add to the input and output block sizes before invoking
94   malloc.  See dd_copy for details.  INPUT_BLOCK_SLOP must be no less than
95   OUTPUT_BLOCK_SLOP.  */
96#define INPUT_BLOCK_SLOP (2 * SWAB_ALIGN_OFFSET + 2 * page_size - 1)
97#define OUTPUT_BLOCK_SLOP (page_size - 1)
98
99/* Maximum blocksize for the given SLOP.
100   Keep it smaller than SIZE_MAX - SLOP, so that we can
101   allocate buffers that size.  Keep it smaller than SSIZE_MAX, for
102   the benefit of system calls like "read".  And keep it smaller than
103   OFF_T_MAX, for the benefit of the large-offset seek code.  */
104#define MAX_BLOCKSIZE(slop) MIN (SIZE_MAX - (slop), MIN (SSIZE_MAX, OFF_T_MAX))
105
106/* Conversions bit masks. */
107enum
108  {
109    C_ASCII = 01,
110
111    C_EBCDIC = 02,
112    C_IBM = 04,
113    C_BLOCK = 010,
114    C_UNBLOCK = 020,
115    C_LCASE = 040,
116    C_UCASE = 0100,
117    C_SWAB = 0200,
118    C_NOERROR = 0400,
119    C_NOTRUNC = 01000,
120    C_SYNC = 02000,
121
122    /* Use separate input and output buffers, and combine partial
123       input blocks. */
124    C_TWOBUFS = 04000,
125
126    C_NOCREAT = 010000,
127    C_EXCL = 020000,
128    C_FDATASYNC = 040000,
129    C_FSYNC = 0100000
130  };
131
132/* Status bit masks.  */
133enum
134  {
135    STATUS_NOXFER = 01
136  };
137
138/* The name of the input file, or NULL for the standard input. */
139static char const *input_file = NULL;
140
141/* The name of the output file, or NULL for the standard output. */
142static char const *output_file = NULL;
143
144/* The page size on this host.  */
145static size_t page_size;
146
147/* The number of bytes in which atomic reads are done. */
148static size_t input_blocksize = 0;
149
150/* The number of bytes in which atomic writes are done. */
151static size_t output_blocksize = 0;
152
153/* Conversion buffer size, in bytes.  0 prevents conversions. */
154static size_t conversion_blocksize = 0;
155
156/* Skip this many records of `input_blocksize' bytes before input. */
157static uintmax_t skip_records = 0;
158
159/* Skip this many records of `output_blocksize' bytes before output. */
160static uintmax_t seek_records = 0;
161
162/* Copy only this many records.  The default is effectively infinity.  */
163static uintmax_t max_records = (uintmax_t) -1;
164
165/* Bit vector of conversions to apply. */
166static int conversions_mask = 0;
167
168/* Open flags for the input and output files.  */
169static int input_flags = 0;
170static int output_flags = 0;
171
172/* Status flags for what is printed to stderr.  */
173static int status_flags = 0;
174
175/* If nonzero, filter characters through the translation table.  */
176static bool translation_needed = false;
177
178/* Number of partial blocks written. */
179static uintmax_t w_partial = 0;
180
181/* Number of full blocks written. */
182static uintmax_t w_full = 0;
183
184/* Number of partial blocks read. */
185static uintmax_t r_partial = 0;
186
187/* Number of full blocks read. */
188static uintmax_t r_full = 0;
189
190/* Number of bytes written.  */
191static uintmax_t w_bytes = 0;
192
193/* Time that dd started.  */
194static xtime_t start_time;
195
196/* True if input is seekable.  */
197static bool input_seekable;
198
199/* Error number corresponding to initial attempt to lseek input.
200   If ESPIPE, do not issue any more diagnostics about it.  */
201static int input_seek_errno;
202
203/* File offset of the input, in bytes, along with a flag recording
204   whether it overflowed.  */
205static uintmax_t input_offset;
206static bool input_offset_overflow;
207
208/* Records truncated by conv=block. */
209static uintmax_t r_truncate = 0;
210
211/* Output representation of newline and space characters.
212   They change if we're converting to EBCDIC.  */
213static char newline_character = '\n';
214static char space_character = ' ';
215
216/* Output buffer. */
217static char *obuf;
218
219/* Current index into `obuf'. */
220static size_t oc = 0;
221
222/* Index into current line, for `conv=block' and `conv=unblock'.  */
223static size_t col = 0;
224
225/* The set of signals that are caught.  */
226static sigset_t caught_signals;
227
228/* If nonzero, the value of the pending fatal signal.  */
229static sig_atomic_t volatile interrupt_signal;
230
231/* A count of the number of pending info signals that have been received.  */
232static sig_atomic_t volatile info_signal_count;
233
234/* Function used for read (to handle iflag=fullblock parameter).  */
235static ssize_t (*iread_fnc) (int fd, char *buf, size_t size);
236
237/* A longest symbol in the struct symbol_values tables below.  */
238#define LONGEST_SYMBOL "fdatasync"
239
240/* A symbol and the corresponding integer value.  */
241struct symbol_value
242{
243  char symbol[sizeof LONGEST_SYMBOL];
244  int value;
245};
246
247/* Conversion symbols, for conv="...".  */
248static struct symbol_value const conversions[] =
249{
250  {"ascii", C_ASCII | C_TWOBUFS},	/* EBCDIC to ASCII. */
251  {"ebcdic", C_EBCDIC | C_TWOBUFS},	/* ASCII to EBCDIC. */
252  {"ibm", C_IBM | C_TWOBUFS},	/* Slightly different ASCII to EBCDIC. */
253  {"block", C_BLOCK | C_TWOBUFS},	/* Variable to fixed length records. */
254  {"unblock", C_UNBLOCK | C_TWOBUFS},	/* Fixed to variable length records. */
255  {"lcase", C_LCASE | C_TWOBUFS},	/* Translate upper to lower case. */
256  {"ucase", C_UCASE | C_TWOBUFS},	/* Translate lower to upper case. */
257  {"swab", C_SWAB | C_TWOBUFS},	/* Swap bytes of input. */
258  {"noerror", C_NOERROR},	/* Ignore i/o errors. */
259  {"nocreat", C_NOCREAT},	/* Do not create output file.  */
260  {"excl", C_EXCL},		/* Fail if the output file already exists.  */
261  {"notrunc", C_NOTRUNC},	/* Do not truncate output file. */
262  {"sync", C_SYNC},		/* Pad input records to ibs with NULs. */
263  {"fdatasync", C_FDATASYNC},	/* Synchronize output data before finishing.  */
264  {"fsync", C_FSYNC},		/* Also synchronize output metadata.  */
265  {"", 0}
266};
267
268enum
269  {
270    /* Compute a value that's bitwise disjoint from the union
271       of all O_ values.  */
272    v = ~(0
273          | O_APPEND
274          | O_BINARY
275          | O_CIO
276          | O_DIRECT
277          | O_DIRECTORY
278          | O_DSYNC
279          | O_NOATIME
280          | O_NOCTTY
281          | O_NOFOLLOW
282          | O_NOLINKS
283          | O_NONBLOCK
284          | O_SYNC
285          | O_TEXT
286          ),
287    /* Use its lowest bit.  */
288    O_FULLBLOCK = v ^ (v & (v - 1))
289  };
290
291/* Ensure that we got something.  */
292verify (O_FULLBLOCK != 0);
293
294#define MULTIPLE_BITS_SET(i) (((i) & ((i) - 1)) != 0)
295
296/* Ensure that this is a single-bit value.  */
297verify ( ! MULTIPLE_BITS_SET (O_FULLBLOCK));
298
299/* Flags, for iflag="..." and oflag="...".  */
300static struct symbol_value const flags[] =
301{
302  {"append",	O_APPEND},
303  {"binary",	O_BINARY},
304  {"cio",	O_CIO},
305  {"direct",	O_DIRECT},
306  {"directory",	O_DIRECTORY},
307  {"dsync",	O_DSYNC},
308  {"noatime",	O_NOATIME},
309  {"noctty",	O_NOCTTY},
310  {"nofollow",	HAVE_WORKING_O_NOFOLLOW ? O_NOFOLLOW : 0},
311  {"nolinks",	O_NOLINKS},
312  {"nonblock",	O_NONBLOCK},
313  {"sync",	O_SYNC},
314  {"text",	O_TEXT},
315  {"fullblock", O_FULLBLOCK}, /* Accumulate full blocks from input.  */
316  {"",		0}
317};
318
319/* Status, for status="...".  */
320static struct symbol_value const statuses[] =
321{
322  {"noxfer",	STATUS_NOXFER},
323  {"",		0}
324};
325
326/* Translation table formed by applying successive transformations. */
327static unsigned char trans_table[256];
328
329static char const ascii_to_ebcdic[] =
330{
331  '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
332  '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
333  '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
334  '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
335  '\100', '\117', '\177', '\173', '\133', '\154', '\120', '\175',
336  '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
337  '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
338  '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
339  '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
340  '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
341  '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
342  '\347', '\350', '\351', '\112', '\340', '\132', '\137', '\155',
343  '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
344  '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
345  '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
346  '\247', '\250', '\251', '\300', '\152', '\320', '\241', '\007',
347  '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
348  '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
349  '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
350  '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
351  '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
352  '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
353  '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
354  '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
355  '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
356  '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',
357  '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',
358  '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
359  '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
360  '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
361  '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
362  '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
363};
364
365static char const ascii_to_ibm[] =
366{
367  '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
368  '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
369  '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
370  '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
371  '\100', '\132', '\177', '\173', '\133', '\154', '\120', '\175',
372  '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
373  '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
374  '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
375  '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
376  '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
377  '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
378  '\347', '\350', '\351', '\255', '\340', '\275', '\137', '\155',
379  '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
380  '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
381  '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
382  '\247', '\250', '\251', '\300', '\117', '\320', '\241', '\007',
383  '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
384  '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
385  '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
386  '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
387  '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
388  '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
389  '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
390  '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
391  '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
392  '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',
393  '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',
394  '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
395  '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
396  '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
397  '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
398  '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
399};
400
401static char const ebcdic_to_ascii[] =
402{
403  '\000', '\001', '\002', '\003', '\234', '\011', '\206', '\177',
404  '\227', '\215', '\216', '\013', '\014', '\015', '\016', '\017',
405  '\020', '\021', '\022', '\023', '\235', '\205', '\010', '\207',
406  '\030', '\031', '\222', '\217', '\034', '\035', '\036', '\037',
407  '\200', '\201', '\202', '\203', '\204', '\012', '\027', '\033',
408  '\210', '\211', '\212', '\213', '\214', '\005', '\006', '\007',
409  '\220', '\221', '\026', '\223', '\224', '\225', '\226', '\004',
410  '\230', '\231', '\232', '\233', '\024', '\025', '\236', '\032',
411  '\040', '\240', '\241', '\242', '\243', '\244', '\245', '\246',
412  '\247', '\250', '\133', '\056', '\074', '\050', '\053', '\041',
413  '\046', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
414  '\260', '\261', '\135', '\044', '\052', '\051', '\073', '\136',
415  '\055', '\057', '\262', '\263', '\264', '\265', '\266', '\267',
416  '\270', '\271', '\174', '\054', '\045', '\137', '\076', '\077',
417  '\272', '\273', '\274', '\275', '\276', '\277', '\300', '\301',
418  '\302', '\140', '\072', '\043', '\100', '\047', '\075', '\042',
419  '\303', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
420  '\150', '\151', '\304', '\305', '\306', '\307', '\310', '\311',
421  '\312', '\152', '\153', '\154', '\155', '\156', '\157', '\160',
422  '\161', '\162', '\313', '\314', '\315', '\316', '\317', '\320',
423  '\321', '\176', '\163', '\164', '\165', '\166', '\167', '\170',
424  '\171', '\172', '\322', '\323', '\324', '\325', '\326', '\327',
425  '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
426  '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
427  '\173', '\101', '\102', '\103', '\104', '\105', '\106', '\107',
428  '\110', '\111', '\350', '\351', '\352', '\353', '\354', '\355',
429  '\175', '\112', '\113', '\114', '\115', '\116', '\117', '\120',
430  '\121', '\122', '\356', '\357', '\360', '\361', '\362', '\363',
431  '\134', '\237', '\123', '\124', '\125', '\126', '\127', '\130',
432  '\131', '\132', '\364', '\365', '\366', '\367', '\370', '\371',
433  '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
434  '\070', '\071', '\372', '\373', '\374', '\375', '\376', '\377'
435};
436
437/* True if we need to close the standard output *stream*.  */
438static bool close_stdout_required = true;
439
440/* The only reason to close the standard output *stream* is if
441   parse_long_options fails (as it does for --help or --version).
442   In any other case, dd uses only the STDOUT_FILENO file descriptor,
443   and the "cleanup" function calls "close (STDOUT_FILENO)".
444   Closing the file descriptor and then letting the usual atexit-run
445   close_stdout function call "fclose (stdout)" would result in a
446   harmless failure of the close syscall (with errno EBADF).
447   This function serves solely to avoid the unnecessary close_stdout
448   call, once parse_long_options has succeeded.
449   Meanwhile, we guarantee that the standard error stream is flushed,
450   by inlining the last half of close_stdout as needed.  */
451static void
452maybe_close_stdout (void)
453{
454  if (close_stdout_required)
455    close_stdout ();
456  else if (close_stream (stderr) != 0)
457    _exit (EXIT_FAILURE);
458}
459
460void
461usage (int status)
462{
463  if (status != EXIT_SUCCESS)
464    fprintf (stderr, _("Try `%s --help' for more information.\n"),
465             program_name);
466  else
467    {
468      printf (_("\
469Usage: %s [OPERAND]...\n\
470  or:  %s OPTION\n\
471"),
472              program_name, program_name);
473      fputs (_("\
474Copy a file, converting and formatting according to the operands.\n\
475\n\
476  bs=BYTES        read and write BYTES bytes at a time (also see ibs=,obs=)\n\
477  cbs=BYTES       convert BYTES bytes at a time\n\
478  conv=CONVS      convert the file as per the comma separated symbol list\n\
479  count=BLOCKS    copy only BLOCKS input blocks\n\
480  ibs=BYTES       read BYTES bytes at a time (default: 512)\n\
481"), stdout);
482      fputs (_("\
483  if=FILE         read from FILE instead of stdin\n\
484  iflag=FLAGS     read as per the comma separated symbol list\n\
485  obs=BYTES       write BYTES bytes at a time (default: 512)\n\
486  of=FILE         write to FILE instead of stdout\n\
487  oflag=FLAGS     write as per the comma separated symbol list\n\
488  seek=BLOCKS     skip BLOCKS obs-sized blocks at start of output\n\
489  skip=BLOCKS     skip BLOCKS ibs-sized blocks at start of input\n\
490  status=noxfer   suppress transfer statistics\n\
491"), stdout);
492      fputs (_("\
493\n\
494BLOCKS and BYTES may be followed by the following multiplicative suffixes:\n\
495c =1, w =2, b =512, kB =1000, K =1024, MB =1000*1000, M =1024*1024, xM =M\n\
496GB =1000*1000*1000, G =1024*1024*1024, and so on for T, P, E, Z, Y.\n\
497\n\
498Each CONV symbol may be:\n\
499\n\
500"), stdout);
501      fputs (_("\
502  ascii     from EBCDIC to ASCII\n\
503  ebcdic    from ASCII to EBCDIC\n\
504  ibm       from ASCII to alternate EBCDIC\n\
505  block     pad newline-terminated records with spaces to cbs-size\n\
506  unblock   replace trailing spaces in cbs-size records with newline\n\
507  lcase     change upper case to lower case\n\
508"), stdout);
509      fputs (_("\
510  nocreat   do not create the output file\n\
511  excl      fail if the output file already exists\n\
512  notrunc   do not truncate the output file\n\
513  ucase     change lower case to upper case\n\
514  swab      swap every pair of input bytes\n\
515"), stdout);
516      fputs (_("\
517  noerror   continue after read errors\n\
518  sync      pad every input block with NULs to ibs-size; when used\n\
519            with block or unblock, pad with spaces rather than NULs\n\
520  fdatasync  physically write output file data before finishing\n\
521  fsync     likewise, but also write metadata\n\
522"), stdout);
523      fputs (_("\
524\n\
525Each FLAG symbol may be:\n\
526\n\
527  append    append mode (makes sense only for output; conv=notrunc suggested)\n\
528"), stdout);
529      if (O_CIO)
530        fputs (_("  cio       use concurrent I/O for data\n"), stdout);
531      if (O_DIRECT)
532        fputs (_("  direct    use direct I/O for data\n"), stdout);
533      if (O_DIRECTORY)
534        fputs (_("  directory  fail unless a directory\n"), stdout);
535      if (O_DSYNC)
536        fputs (_("  dsync     use synchronized I/O for data\n"), stdout);
537      if (O_SYNC)
538        fputs (_("  sync      likewise, but also for metadata\n"), stdout);
539      fputs (_("  fullblock  accumulate full blocks of input (iflag only)\n"),
540             stdout);
541      if (O_NONBLOCK)
542        fputs (_("  nonblock  use non-blocking I/O\n"), stdout);
543      if (O_NOATIME)
544        fputs (_("  noatime   do not update access time\n"), stdout);
545      if (O_NOCTTY)
546        fputs (_("  noctty    do not assign controlling terminal from file\n"),
547               stdout);
548      if (HAVE_WORKING_O_NOFOLLOW)
549        fputs (_("  nofollow  do not follow symlinks\n"), stdout);
550      if (O_NOLINKS)
551        fputs (_("  nolinks   fail if multiply-linked\n"), stdout);
552      if (O_BINARY)
553        fputs (_("  binary    use binary I/O for data\n"), stdout);
554      if (O_TEXT)
555        fputs (_("  text      use text I/O for data\n"), stdout);
556
557      {
558        char const *siginfo_name = (SIGINFO == SIGUSR1 ? "USR1" : "INFO");
559        printf (_("\
560\n\
561Sending a %s signal to a running `dd' process makes it\n\
562print I/O statistics to standard error and then resume copying.\n\
563\n\
564  $ dd if=/dev/zero of=/dev/null& pid=$!\n\
565  $ kill -%s $pid; sleep 1; kill $pid\n\
566  18335302+0 records in\n\
567  18335302+0 records out\n\
568  9387674624 bytes (9.4 GB) copied, 34.6279 seconds, 271 MB/s\n\
569\n\
570Options are:\n\
571\n\
572"),
573                siginfo_name, siginfo_name);
574      }
575
576      fputs (HELP_OPTION_DESCRIPTION, stdout);
577      fputs (VERSION_OPTION_DESCRIPTION, stdout);
578      emit_ancillary_info ();
579    }
580  exit (status);
581}
582
583static void
584translate_charset (char const *new_trans)
585{
586  int i;
587
588  for (i = 0; i < 256; i++)
589    trans_table[i] = new_trans[trans_table[i]];
590  translation_needed = true;
591}
592
593/* Return true if I has more than one bit set.  I must be nonnegative.  */
594
595static inline bool
596multiple_bits_set (int i)
597{
598  return MULTIPLE_BITS_SET (i);
599}
600
601/* Print transfer statistics.  */
602
603static void
604print_stats (void)
605{
606  xtime_t now = gethrxtime ();
607  char hbuf[LONGEST_HUMAN_READABLE + 1];
608  int human_opts =
609    (human_autoscale | human_round_to_nearest
610     | human_space_before_unit | human_SI | human_B);
611  double delta_s;
612  char const *bytes_per_second;
613
614  fprintf (stderr,
615           _("%"PRIuMAX"+%"PRIuMAX" records in\n"
616             "%"PRIuMAX"+%"PRIuMAX" records out\n"),
617           r_full, r_partial, w_full, w_partial);
618
619  if (r_truncate != 0)
620    fprintf (stderr,
621             ngettext ("%"PRIuMAX" truncated record\n",
622                       "%"PRIuMAX" truncated records\n",
623                       select_plural (r_truncate)),
624             r_truncate);
625
626  if (status_flags & STATUS_NOXFER)
627    return;
628
629  /* Use integer arithmetic to compute the transfer rate,
630     since that makes it easy to use SI abbreviations.  */
631
632  fprintf (stderr,
633           ngettext ("%"PRIuMAX" byte (%s) copied",
634                     "%"PRIuMAX" bytes (%s) copied",
635                     select_plural (w_bytes)),
636           w_bytes,
637           human_readable (w_bytes, hbuf, human_opts, 1, 1));
638
639  if (start_time < now)
640    {
641      double XTIME_PRECISIONe0 = XTIME_PRECISION;
642      uintmax_t delta_xtime = now;
643      delta_xtime -= start_time;
644      delta_s = delta_xtime / XTIME_PRECISIONe0;
645      bytes_per_second = human_readable (w_bytes, hbuf, human_opts,
646                                         XTIME_PRECISION, delta_xtime);
647    }
648  else
649    {
650      delta_s = 0;
651      bytes_per_second = _("Infinity B");
652    }
653
654  /* TRANSLATORS: The two instances of "s" in this string are the SI
655     symbol "s" (meaning second), and should not be translated.
656
657     This format used to be:
658
659     ngettext (", %g second, %s/s\n", ", %g seconds, %s/s\n", delta_s == 1)
660
661     but that was incorrect for languages like Polish.  To fix this
662     bug we now use SI symbols even though they're a bit more
663     confusing in English.  */
664  fprintf (stderr, _(", %g s, %s/s\n"), delta_s, bytes_per_second);
665}
666
667static void
668cleanup (void)
669{
670  if (close (STDIN_FILENO) < 0)
671    error (EXIT_FAILURE, errno,
672           _("closing input file %s"), quote (input_file));
673
674  /* Don't remove this call to close, even though close_stdout
675     closes standard output.  This close is necessary when cleanup
676     is called as part of a signal handler.  */
677  if (close (STDOUT_FILENO) < 0)
678    error (EXIT_FAILURE, errno,
679           _("closing output file %s"), quote (output_file));
680}
681
682static void ATTRIBUTE_NORETURN
683quit (int code)
684{
685  cleanup ();
686  print_stats ();
687  process_signals ();
688  exit (code);
689}
690
691/* An ordinary signal was received; arrange for the program to exit.  */
692
693static void
694interrupt_handler (int sig)
695{
696  if (! SA_RESETHAND)
697    signal (sig, SIG_DFL);
698  interrupt_signal = sig;
699}
700
701/* An info signal was received; arrange for the program to print status.  */
702
703static void
704siginfo_handler (int sig)
705{
706  if (! SA_NOCLDSTOP)
707    signal (sig, siginfo_handler);
708  info_signal_count++;
709}
710
711/* Install the signal handlers.  */
712
713static void
714install_signal_handlers (void)
715{
716  bool catch_siginfo = ! (SIGINFO == SIGUSR1 && getenv ("POSIXLY_CORRECT"));
717
718#if SA_NOCLDSTOP
719
720  struct sigaction act;
721  sigemptyset (&caught_signals);
722  if (catch_siginfo)
723    {
724      sigaction (SIGINFO, NULL, &act);
725      if (act.sa_handler != SIG_IGN)
726        sigaddset (&caught_signals, SIGINFO);
727    }
728  sigaction (SIGINT, NULL, &act);
729  if (act.sa_handler != SIG_IGN)
730    sigaddset (&caught_signals, SIGINT);
731  act.sa_mask = caught_signals;
732
733  if (sigismember (&caught_signals, SIGINFO))
734    {
735      act.sa_handler = siginfo_handler;
736      act.sa_flags = 0;
737      sigaction (SIGINFO, &act, NULL);
738    }
739
740  if (sigismember (&caught_signals, SIGINT))
741    {
742      /* POSIX 1003.1-2001 says SA_RESETHAND implies SA_NODEFER,
743         but this is not true on Solaris 8 at least.  It doesn't
744         hurt to use SA_NODEFER here, so leave it in.  */
745      act.sa_handler = interrupt_handler;
746      act.sa_flags = SA_NODEFER | SA_RESETHAND;
747      sigaction (SIGINT, &act, NULL);
748    }
749
750#else
751
752  if (catch_siginfo && signal (SIGINFO, SIG_IGN) != SIG_IGN)
753    {
754      signal (SIGINFO, siginfo_handler);
755      siginterrupt (SIGINFO, 1);
756    }
757  if (signal (SIGINT, SIG_IGN) != SIG_IGN)
758    {
759      signal (SIGINT, interrupt_handler);
760      siginterrupt (SIGINT, 1);
761    }
762#endif
763}
764
765/* Process any pending signals.  If signals are caught, this function
766   should be called periodically.  Ideally there should never be an
767   unbounded amount of time when signals are not being processed.  */
768
769static void
770process_signals (void)
771{
772  while (interrupt_signal || info_signal_count)
773    {
774      int interrupt;
775      int infos;
776      sigset_t oldset;
777
778      sigprocmask (SIG_BLOCK, &caught_signals, &oldset);
779
780      /* Reload interrupt_signal and info_signal_count, in case a new
781         signal was handled before sigprocmask took effect.  */
782      interrupt = interrupt_signal;
783      infos = info_signal_count;
784
785      if (infos)
786        info_signal_count = infos - 1;
787
788      sigprocmask (SIG_SETMASK, &oldset, NULL);
789
790      if (interrupt)
791        cleanup ();
792      print_stats ();
793      if (interrupt)
794        raise (interrupt);
795    }
796}
797
798/* Read from FD into the buffer BUF of size SIZE, processing any
799   signals that arrive before bytes are read.  Return the number of
800   bytes read if successful, -1 (setting errno) on failure.  */
801
802static ssize_t
803iread (int fd, char *buf, size_t size)
804{
805  for (;;)
806    {
807      ssize_t nread;
808      process_signals ();
809      nread = read (fd, buf, size);
810      if (! (nread < 0 && errno == EINTR))
811        return nread;
812    }
813}
814
815/* Wrapper around iread function to accumulate full blocks.  */
816static ssize_t
817iread_fullblock (int fd, char *buf, size_t size)
818{
819  ssize_t nread = 0;
820
821  while (0 < size)
822    {
823      ssize_t ncurr = iread (fd, buf, size);
824      if (ncurr < 0)
825        return ncurr;
826      if (ncurr == 0)
827        break;
828      nread += ncurr;
829      buf   += ncurr;
830      size  -= ncurr;
831    }
832
833  return nread;
834}
835
836/* Write to FD the buffer BUF of size SIZE, processing any signals
837   that arrive.  Return the number of bytes written, setting errno if
838   this is less than SIZE.  Keep trying if there are partial
839   writes.  */
840
841static size_t
842iwrite (int fd, char const *buf, size_t size)
843{
844  size_t total_written = 0;
845
846  if ((output_flags & O_DIRECT) && size < output_blocksize)
847    {
848      int old_flags = fcntl (STDOUT_FILENO, F_GETFL);
849      if (fcntl (STDOUT_FILENO, F_SETFL, old_flags & ~O_DIRECT) != 0)
850        error (0, errno, _("failed to turn off O_DIRECT: %s"),
851               quote (output_file));
852
853      /* Since we have just turned off O_DIRECT for the final write,
854         here we try to preserve some of its semantics.  First, use
855         posix_fadvise to tell the system not to pollute the buffer
856         cache with this data.  Don't bother to diagnose lseek or
857         posix_fadvise failure. */
858#ifdef POSIX_FADV_DONTNEED
859      off_t off = lseek (STDOUT_FILENO, 0, SEEK_CUR);
860      if (0 <= off)
861        ignore_value (posix_fadvise (STDOUT_FILENO,
862                                     off, 0, POSIX_FADV_DONTNEED));
863#endif
864
865      /* Attempt to ensure that that final block is committed
866         to disk as quickly as possible.  */
867      conversions_mask |= C_FSYNC;
868    }
869
870  while (total_written < size)
871    {
872      ssize_t nwritten;
873      process_signals ();
874      nwritten = write (fd, buf + total_written, size - total_written);
875      if (nwritten < 0)
876        {
877          if (errno != EINTR)
878            break;
879        }
880      else if (nwritten == 0)
881        {
882          /* Some buggy drivers return 0 when one tries to write beyond
883             a device's end.  (Example: Linux kernel 1.2.13 on /dev/fd0.)
884             Set errno to ENOSPC so they get a sensible diagnostic.  */
885          errno = ENOSPC;
886          break;
887        }
888      else
889        total_written += nwritten;
890    }
891
892  return total_written;
893}
894
895/* Write, then empty, the output buffer `obuf'. */
896
897static void
898write_output (void)
899{
900  size_t nwritten = iwrite (STDOUT_FILENO, obuf, output_blocksize);
901  w_bytes += nwritten;
902  if (nwritten != output_blocksize)
903    {
904      error (0, errno, _("writing to %s"), quote (output_file));
905      if (nwritten != 0)
906        w_partial++;
907      quit (EXIT_FAILURE);
908    }
909  else
910    w_full++;
911  oc = 0;
912}
913
914/* Return true if STR is of the form "PATTERN" or "PATTERNDELIM...".  */
915
916static bool
917operand_matches (char const *str, char const *pattern, char delim)
918{
919  while (*pattern)
920    if (*str++ != *pattern++)
921      return false;
922  return !*str || *str == delim;
923}
924
925/* Interpret one "conv=..." or similar operand STR according to the
926   symbols in TABLE, returning the flags specified.  If the operand
927   cannot be parsed, use ERROR_MSGID to generate a diagnostic.  */
928
929static int
930parse_symbols (char const *str, struct symbol_value const *table,
931               char const *error_msgid)
932{
933  int value = 0;
934
935  for (;;)
936    {
937      char const *strcomma = strchr (str, ',');
938      struct symbol_value const *entry;
939
940      for (entry = table;
941           ! (operand_matches (str, entry->symbol, ',') && entry->value);
942           entry++)
943        {
944          if (! entry->symbol[0])
945            {
946              size_t slen = strcomma ? strcomma - str : strlen (str);
947              error (0, 0, "%s: %s", _(error_msgid),
948                     quotearg_n_style_mem (0, locale_quoting_style, str, slen));
949              usage (EXIT_FAILURE);
950            }
951        }
952
953      value |= entry->value;
954      if (!strcomma)
955        break;
956      str = strcomma + 1;
957    }
958
959  return value;
960}
961
962/* Return the value of STR, interpreted as a non-negative decimal integer,
963   optionally multiplied by various values.
964   Set *INVALID if STR does not represent a number in this format.  */
965
966static uintmax_t
967parse_integer (const char *str, bool *invalid)
968{
969  uintmax_t n;
970  char *suffix;
971  enum strtol_error e = xstrtoumax (str, &suffix, 10, &n, "bcEGkKMPTwYZ0");
972
973  if (e == LONGINT_INVALID_SUFFIX_CHAR && *suffix == 'x')
974    {
975      uintmax_t multiplier = parse_integer (suffix + 1, invalid);
976
977      if (multiplier != 0 && n * multiplier / multiplier != n)
978        {
979          *invalid = true;
980          return 0;
981        }
982
983      n *= multiplier;
984    }
985  else if (e != LONGINT_OK)
986    {
987      *invalid = true;
988      return 0;
989    }
990
991  return n;
992}
993
994/* OPERAND is of the form "X=...".  Return true if X is NAME.  */
995
996static bool
997operand_is (char const *operand, char const *name)
998{
999  return operand_matches (operand, name, '=');
1000}
1001
1002static void
1003scanargs (int argc, char *const *argv)
1004{
1005  int i;
1006  size_t blocksize = 0;
1007
1008  for (i = optind; i < argc; i++)
1009    {
1010      char const *name = argv[i];
1011      char const *val = strchr (name, '=');
1012
1013      if (val == NULL)
1014        {
1015          error (0, 0, _("unrecognized operand %s"), quote (name));
1016          usage (EXIT_FAILURE);
1017        }
1018      val++;
1019
1020      if (operand_is (name, "if"))
1021        input_file = val;
1022      else if (operand_is (name, "of"))
1023        output_file = val;
1024      else if (operand_is (name, "conv"))
1025        conversions_mask |= parse_symbols (val, conversions,
1026                                           N_("invalid conversion"));
1027      else if (operand_is (name, "iflag"))
1028        input_flags |= parse_symbols (val, flags,
1029                                      N_("invalid input flag"));
1030      else if (operand_is (name, "oflag"))
1031        output_flags |= parse_symbols (val, flags,
1032                                       N_("invalid output flag"));
1033      else if (operand_is (name, "status"))
1034        status_flags |= parse_symbols (val, statuses,
1035                                       N_("invalid status flag"));
1036      else
1037        {
1038          bool invalid = false;
1039          uintmax_t n = parse_integer (val, &invalid);
1040
1041          if (operand_is (name, "ibs"))
1042            {
1043              invalid |= ! (0 < n && n <= MAX_BLOCKSIZE (INPUT_BLOCK_SLOP));
1044              input_blocksize = n;
1045            }
1046          else if (operand_is (name, "obs"))
1047            {
1048              invalid |= ! (0 < n && n <= MAX_BLOCKSIZE (OUTPUT_BLOCK_SLOP));
1049              output_blocksize = n;
1050            }
1051          else if (operand_is (name, "bs"))
1052            {
1053              invalid |= ! (0 < n && n <= MAX_BLOCKSIZE (INPUT_BLOCK_SLOP));
1054              blocksize = n;
1055            }
1056          else if (operand_is (name, "cbs"))
1057            {
1058              invalid |= ! (0 < n && n <= SIZE_MAX);
1059              conversion_blocksize = n;
1060            }
1061          else if (operand_is (name, "skip"))
1062            skip_records = n;
1063          else if (operand_is (name, "seek"))
1064            seek_records = n;
1065          else if (operand_is (name, "count"))
1066            max_records = n;
1067          else
1068            {
1069              error (0, 0, _("unrecognized operand %s"), quote (name));
1070              usage (EXIT_FAILURE);
1071            }
1072
1073          if (invalid)
1074            error (EXIT_FAILURE, 0, _("invalid number %s"), quote (val));
1075        }
1076    }
1077
1078  if (blocksize)
1079    input_blocksize = output_blocksize = blocksize;
1080  else
1081    {
1082      /* POSIX says dd aggregates short reads into
1083         output_blocksize if bs= is not specified.  */
1084      conversions_mask |= C_TWOBUFS;
1085    }
1086
1087  if (input_blocksize == 0)
1088    input_blocksize = DEFAULT_BLOCKSIZE;
1089  if (output_blocksize == 0)
1090    output_blocksize = DEFAULT_BLOCKSIZE;
1091  if (conversion_blocksize == 0)
1092    conversions_mask &= ~(C_BLOCK | C_UNBLOCK);
1093
1094  if (input_flags & (O_DSYNC | O_SYNC))
1095    input_flags |= O_RSYNC;
1096
1097  if (output_flags & O_FULLBLOCK)
1098    {
1099      error (0, 0, "%s: %s", _("invalid output flag"), "'fullblock'");
1100      usage (EXIT_FAILURE);
1101    }
1102  iread_fnc = ((input_flags & O_FULLBLOCK)
1103               ? iread_fullblock
1104               : iread);
1105  input_flags &= ~O_FULLBLOCK;
1106
1107  if (multiple_bits_set (conversions_mask & (C_ASCII | C_EBCDIC | C_IBM)))
1108    error (EXIT_FAILURE, 0, _("cannot combine any two of {ascii,ebcdic,ibm}"));
1109  if (multiple_bits_set (conversions_mask & (C_BLOCK | C_UNBLOCK)))
1110    error (EXIT_FAILURE, 0, _("cannot combine block and unblock"));
1111  if (multiple_bits_set (conversions_mask & (C_LCASE | C_UCASE)))
1112    error (EXIT_FAILURE, 0, _("cannot combine lcase and ucase"));
1113  if (multiple_bits_set (conversions_mask & (C_EXCL | C_NOCREAT)))
1114    error (EXIT_FAILURE, 0, _("cannot combine excl and nocreat"));
1115}
1116
1117/* Fix up translation table. */
1118
1119static void
1120apply_translations (void)
1121{
1122  int i;
1123
1124  if (conversions_mask & C_ASCII)
1125    translate_charset (ebcdic_to_ascii);
1126
1127  if (conversions_mask & C_UCASE)
1128    {
1129      for (i = 0; i < 256; i++)
1130        trans_table[i] = toupper (trans_table[i]);
1131      translation_needed = true;
1132    }
1133  else if (conversions_mask & C_LCASE)
1134    {
1135      for (i = 0; i < 256; i++)
1136        trans_table[i] = tolower (trans_table[i]);
1137      translation_needed = true;
1138    }
1139
1140  if (conversions_mask & C_EBCDIC)
1141    {
1142      translate_charset (ascii_to_ebcdic);
1143      newline_character = ascii_to_ebcdic['\n'];
1144      space_character = ascii_to_ebcdic[' '];
1145    }
1146  else if (conversions_mask & C_IBM)
1147    {
1148      translate_charset (ascii_to_ibm);
1149      newline_character = ascii_to_ibm['\n'];
1150      space_character = ascii_to_ibm[' '];
1151    }
1152}
1153
1154/* Apply the character-set translations specified by the user
1155   to the NREAD bytes in BUF.  */
1156
1157static void
1158translate_buffer (char *buf, size_t nread)
1159{
1160  char *cp;
1161  size_t i;
1162
1163  for (i = nread, cp = buf; i; i--, cp++)
1164    *cp = trans_table[to_uchar (*cp)];
1165}
1166
1167/* If true, the last char from the previous call to `swab_buffer'
1168   is saved in `saved_char'.  */
1169static bool char_is_saved = false;
1170
1171/* Odd char from previous call.  */
1172static char saved_char;
1173
1174/* Swap NREAD bytes in BUF, plus possibly an initial char from the
1175   previous call.  If NREAD is odd, save the last char for the
1176   next call.   Return the new start of the BUF buffer.  */
1177
1178static char *
1179swab_buffer (char *buf, size_t *nread)
1180{
1181  char *bufstart = buf;
1182  char *cp;
1183  size_t i;
1184
1185  /* Is a char left from last time?  */
1186  if (char_is_saved)
1187    {
1188      *--bufstart = saved_char;
1189      (*nread)++;
1190      char_is_saved = false;
1191    }
1192
1193  if (*nread & 1)
1194    {
1195      /* An odd number of chars are in the buffer.  */
1196      saved_char = bufstart[--*nread];
1197      char_is_saved = true;
1198    }
1199
1200  /* Do the byte-swapping by moving every second character two
1201     positions toward the end, working from the end of the buffer
1202     toward the beginning.  This way we only move half of the data.  */
1203
1204  cp = bufstart + *nread;	/* Start one char past the last.  */
1205  for (i = *nread / 2; i; i--, cp -= 2)
1206    *cp = *(cp - 2);
1207
1208  return ++bufstart;
1209}
1210
1211/* Add OFFSET to the input offset, setting the overflow flag if
1212   necessary.  */
1213
1214static void
1215advance_input_offset (uintmax_t offset)
1216{
1217  input_offset += offset;
1218  if (input_offset < offset)
1219    input_offset_overflow = true;
1220}
1221
1222/* This is a wrapper for lseek.  It detects and warns about a kernel
1223   bug that makes lseek a no-op for tape devices, even though the kernel
1224   lseek return value suggests that the function succeeded.
1225
1226   The parameters are the same as those of the lseek function, but
1227   with the addition of FILENAME, the name of the file associated with
1228   descriptor FDESC.  The file name is used solely in the warning that's
1229   printed when the bug is detected.  Return the same value that lseek
1230   would have returned, but when the lseek bug is detected, return -1
1231   to indicate that lseek failed.
1232
1233   The offending behavior has been confirmed with an Exabyte SCSI tape
1234   drive accessed via /dev/nst0 on both Linux 2.2.17 and 2.4.16 kernels.  */
1235
1236#ifdef __linux__
1237
1238# include <sys/mtio.h>
1239
1240# define MT_SAME_POSITION(P, Q) \
1241   ((P).mt_resid == (Q).mt_resid \
1242    && (P).mt_fileno == (Q).mt_fileno \
1243    && (P).mt_blkno == (Q).mt_blkno)
1244
1245static off_t
1246skip_via_lseek (char const *filename, int fdesc, off_t offset, int whence)
1247{
1248  struct mtget s1;
1249  struct mtget s2;
1250  bool got_original_tape_position = (ioctl (fdesc, MTIOCGET, &s1) == 0);
1251  /* known bad device type */
1252  /* && s.mt_type == MT_ISSCSI2 */
1253
1254  off_t new_position = lseek (fdesc, offset, whence);
1255  if (0 <= new_position
1256      && got_original_tape_position
1257      && ioctl (fdesc, MTIOCGET, &s2) == 0
1258      && MT_SAME_POSITION (s1, s2))
1259    {
1260      error (0, 0, _("warning: working around lseek kernel bug for file (%s)\n\
1261  of mt_type=0x%0lx -- see <sys/mtio.h> for the list of types"),
1262             filename, s2.mt_type);
1263      errno = 0;
1264      new_position = -1;
1265    }
1266
1267  return new_position;
1268}
1269#else
1270# define skip_via_lseek(Filename, Fd, Offset, Whence) lseek (Fd, Offset, Whence)
1271#endif
1272
1273/* Throw away RECORDS blocks of BLOCKSIZE bytes on file descriptor FDESC,
1274   which is open with read permission for FILE.  Store up to BLOCKSIZE
1275   bytes of the data at a time in BUF, if necessary.  RECORDS must be
1276   nonzero.  If fdesc is STDIN_FILENO, advance the input offset.
1277   Return the number of records remaining, i.e., that were not skipped
1278   because EOF was reached.  */
1279
1280static uintmax_t
1281skip (int fdesc, char const *file, uintmax_t records, size_t blocksize,
1282      char *buf)
1283{
1284  uintmax_t offset = records * blocksize;
1285
1286  /* Try lseek and if an error indicates it was an inappropriate operation --
1287     or if the file offset is not representable as an off_t --
1288     fall back on using read.  */
1289
1290  errno = 0;
1291  if (records <= OFF_T_MAX / blocksize
1292      && 0 <= skip_via_lseek (file, fdesc, offset, SEEK_CUR))
1293    {
1294      if (fdesc == STDIN_FILENO)
1295        {
1296           struct stat st;
1297           if (fstat (STDIN_FILENO, &st) != 0)
1298             error (EXIT_FAILURE, errno, _("cannot fstat %s"), quote (file));
1299           if (S_ISREG (st.st_mode) && st.st_size < (input_offset + offset))
1300             {
1301               /* When skipping past EOF, return the number of _full_ blocks
1302                * that are not skipped, and set offset to EOF, so the caller
1303                * can determine the requested skip was not satisfied.  */
1304               records = ( offset - st.st_size ) / blocksize;
1305               offset = st.st_size - input_offset;
1306             }
1307           else
1308             records = 0;
1309           advance_input_offset (offset);
1310        }
1311      else
1312        records = 0;
1313      return records;
1314    }
1315  else
1316    {
1317      int lseek_errno = errno;
1318
1319      /* The seek request may have failed above if it was too big
1320         (> device size, > max file size, etc.)
1321         Or it may not have been done at all (> OFF_T_MAX).
1322         Therefore try to seek to the end of the file,
1323         to avoid redundant reading.  */
1324      if ((skip_via_lseek (file, fdesc, 0, SEEK_END)) >= 0)
1325        {
1326          /* File is seekable, and we're at the end of it, and
1327             size <= OFF_T_MAX. So there's no point using read to advance.  */
1328
1329          if (!lseek_errno)
1330            {
1331              /* The original seek was not attempted as offset > OFF_T_MAX.
1332                 We should error for write as can't get to the desired
1333                 location, even if OFF_T_MAX < max file size.
1334                 For read we're not going to read any data anyway,
1335                 so we should error for consistency.
1336                 It would be nice to not error for /dev/{zero,null}
1337                 for any offset, but that's not a significant issue.  */
1338              lseek_errno = EOVERFLOW;
1339            }
1340
1341          if (fdesc == STDIN_FILENO)
1342            error (0, lseek_errno, _("%s: cannot skip"), quote (file));
1343          else
1344            error (0, lseek_errno, _("%s: cannot seek"), quote (file));
1345          /* If the file has a specific size and we've asked
1346             to skip/seek beyond the max allowable, then quit.  */
1347          quit (EXIT_FAILURE);
1348        }
1349      /* else file_size && offset > OFF_T_MAX or file ! seekable */
1350
1351      do
1352        {
1353          ssize_t nread = iread_fnc (fdesc, buf, blocksize);
1354          if (nread < 0)
1355            {
1356              if (fdesc == STDIN_FILENO)
1357                {
1358                  error (0, errno, _("reading %s"), quote (file));
1359                  if (conversions_mask & C_NOERROR)
1360                    {
1361                      print_stats ();
1362                      continue;
1363                    }
1364                }
1365              else
1366                error (0, lseek_errno, _("%s: cannot seek"), quote (file));
1367              quit (EXIT_FAILURE);
1368            }
1369
1370          if (nread == 0)
1371            break;
1372          if (fdesc == STDIN_FILENO)
1373            advance_input_offset (nread);
1374        }
1375      while (--records != 0);
1376
1377      return records;
1378    }
1379}
1380
1381/* Advance the input by NBYTES if possible, after a read error.
1382   The input file offset may or may not have advanced after the failed
1383   read; adjust it to point just after the bad record regardless.
1384   Return true if successful, or if the input is already known to not
1385   be seekable.  */
1386
1387static bool
1388advance_input_after_read_error (size_t nbytes)
1389{
1390  if (! input_seekable)
1391    {
1392      if (input_seek_errno == ESPIPE)
1393        return true;
1394      errno = input_seek_errno;
1395    }
1396  else
1397    {
1398      off_t offset;
1399      advance_input_offset (nbytes);
1400      input_offset_overflow |= (OFF_T_MAX < input_offset);
1401      if (input_offset_overflow)
1402        {
1403          error (0, 0, _("offset overflow while reading file %s"),
1404                 quote (input_file));
1405          return false;
1406        }
1407      offset = lseek (STDIN_FILENO, 0, SEEK_CUR);
1408      if (0 <= offset)
1409        {
1410          off_t diff;
1411          if (offset == input_offset)
1412            return true;
1413          diff = input_offset - offset;
1414          if (! (0 <= diff && diff <= nbytes))
1415            error (0, 0, _("warning: invalid file offset after failed read"));
1416          if (0 <= skip_via_lseek (input_file, STDIN_FILENO, diff, SEEK_CUR))
1417            return true;
1418          if (errno == 0)
1419            error (0, 0, _("cannot work around kernel bug after all"));
1420        }
1421    }
1422
1423  error (0, errno, _("%s: cannot seek"), quote (input_file));
1424  return false;
1425}
1426
1427/* Copy NREAD bytes of BUF, with no conversions.  */
1428
1429static void
1430copy_simple (char const *buf, size_t nread)
1431{
1432  const char *start = buf;	/* First uncopied char in BUF.  */
1433
1434  do
1435    {
1436      size_t nfree = MIN (nread, output_blocksize - oc);
1437
1438      memcpy (obuf + oc, start, nfree);
1439
1440      nread -= nfree;		/* Update the number of bytes left to copy. */
1441      start += nfree;
1442      oc += nfree;
1443      if (oc >= output_blocksize)
1444        write_output ();
1445    }
1446  while (nread != 0);
1447}
1448
1449/* Copy NREAD bytes of BUF, doing conv=block
1450   (pad newline-terminated records to `conversion_blocksize',
1451   replacing the newline with trailing spaces).  */
1452
1453static void
1454copy_with_block (char const *buf, size_t nread)
1455{
1456  size_t i;
1457
1458  for (i = nread; i; i--, buf++)
1459    {
1460      if (*buf == newline_character)
1461        {
1462          if (col < conversion_blocksize)
1463            {
1464              size_t j;
1465              for (j = col; j < conversion_blocksize; j++)
1466                output_char (space_character);
1467            }
1468          col = 0;
1469        }
1470      else
1471        {
1472          if (col == conversion_blocksize)
1473            r_truncate++;
1474          else if (col < conversion_blocksize)
1475            output_char (*buf);
1476          col++;
1477        }
1478    }
1479}
1480
1481/* Copy NREAD bytes of BUF, doing conv=unblock
1482   (replace trailing spaces in `conversion_blocksize'-sized records
1483   with a newline).  */
1484
1485static void
1486copy_with_unblock (char const *buf, size_t nread)
1487{
1488  size_t i;
1489  char c;
1490  static size_t pending_spaces = 0;
1491
1492  for (i = 0; i < nread; i++)
1493    {
1494      c = buf[i];
1495
1496      if (col++ >= conversion_blocksize)
1497        {
1498          col = pending_spaces = 0; /* Wipe out any pending spaces.  */
1499          i--;			/* Push the char back; get it later. */
1500          output_char (newline_character);
1501        }
1502      else if (c == space_character)
1503        pending_spaces++;
1504      else
1505        {
1506          /* `c' is the character after a run of spaces that were not
1507             at the end of the conversion buffer.  Output them.  */
1508          while (pending_spaces)
1509            {
1510              output_char (space_character);
1511              --pending_spaces;
1512            }
1513          output_char (c);
1514        }
1515    }
1516}
1517
1518/* Set the file descriptor flags for FD that correspond to the nonzero bits
1519   in ADD_FLAGS.  The file's name is NAME.  */
1520
1521static void
1522set_fd_flags (int fd, int add_flags, char const *name)
1523{
1524  /* Ignore file creation flags that are no-ops on file descriptors.  */
1525  add_flags &= ~ (O_NOCTTY | O_NOFOLLOW);
1526
1527  if (add_flags)
1528    {
1529      int old_flags = fcntl (fd, F_GETFL);
1530      int new_flags = old_flags | add_flags;
1531      bool ok = true;
1532      if (old_flags < 0)
1533        ok = false;
1534      else if (old_flags != new_flags)
1535        {
1536          if (new_flags & (O_DIRECTORY | O_NOLINKS))
1537            {
1538              /* NEW_FLAGS contains at least one file creation flag that
1539                 requires some checking of the open file descriptor.  */
1540              struct stat st;
1541              if (fstat (fd, &st) != 0)
1542                ok = false;
1543              else if ((new_flags & O_DIRECTORY) && ! S_ISDIR (st.st_mode))
1544                {
1545                  errno = ENOTDIR;
1546                  ok = false;
1547                }
1548              else if ((new_flags & O_NOLINKS) && 1 < st.st_nlink)
1549                {
1550                  errno = EMLINK;
1551                  ok = false;
1552                }
1553              new_flags &= ~ (O_DIRECTORY | O_NOLINKS);
1554            }
1555
1556          if (ok && old_flags != new_flags
1557              && fcntl (fd, F_SETFL, new_flags) == -1)
1558            ok = false;
1559        }
1560
1561      if (!ok)
1562        error (EXIT_FAILURE, errno, _("setting flags for %s"), quote (name));
1563    }
1564}
1565
1566/* The main loop.  */
1567
1568static int
1569dd_copy (void)
1570{
1571  char *ibuf, *bufstart;	/* Input buffer. */
1572  /* These are declared static so that even though we don't free the
1573     buffers, valgrind will recognize that there is no "real" leak.  */
1574  static char *real_buf;	/* real buffer address before alignment */
1575  static char *real_obuf;
1576  ssize_t nread;		/* Bytes read in the current block.  */
1577
1578  /* If nonzero, then the previously read block was partial and
1579     PARTREAD was its size.  */
1580  size_t partread = 0;
1581
1582  int exit_status = EXIT_SUCCESS;
1583  size_t n_bytes_read;
1584
1585  /* Leave at least one extra byte at the beginning and end of `ibuf'
1586     for conv=swab, but keep the buffer address even.  But some peculiar
1587     device drivers work only with word-aligned buffers, so leave an
1588     extra two bytes.  */
1589
1590  /* Some devices require alignment on a sector or page boundary
1591     (e.g. character disk devices).  Align the input buffer to a
1592     page boundary to cover all bases.  Note that due to the swab
1593     algorithm, we must have at least one byte in the page before
1594     the input buffer;  thus we allocate 2 pages of slop in the
1595     real buffer.  8k above the blocksize shouldn't bother anyone.
1596
1597     The page alignment is necessary on any Linux kernel that supports
1598     either the SGI raw I/O patch or Steven Tweedies raw I/O patch.
1599     It is necessary when accessing raw (i.e. character special) disk
1600     devices on Unixware or other SVR4-derived system.  */
1601
1602  real_buf = xmalloc (input_blocksize + INPUT_BLOCK_SLOP);
1603  ibuf = real_buf;
1604  ibuf += SWAB_ALIGN_OFFSET;	/* allow space for swab */
1605
1606  ibuf = ptr_align (ibuf, page_size);
1607
1608  if (conversions_mask & C_TWOBUFS)
1609    {
1610      /* Page-align the output buffer, too.  */
1611      real_obuf = xmalloc (output_blocksize + OUTPUT_BLOCK_SLOP);
1612      obuf = ptr_align (real_obuf, page_size);
1613    }
1614  else
1615    {
1616      real_obuf = NULL;
1617      obuf = ibuf;
1618    }
1619
1620  if (skip_records != 0)
1621    {
1622      uintmax_t us_bytes = input_offset + (skip_records * input_blocksize);
1623      uintmax_t us_blocks = skip (STDIN_FILENO, input_file,
1624                                  skip_records, input_blocksize, ibuf);
1625      us_bytes -= input_offset;
1626
1627      /* POSIX doesn't say what to do when dd detects it has been
1628         asked to skip past EOF, so I assume it's non-fatal.
1629         There are 3 reasons why there might be unskipped blocks/bytes:
1630             1. file is too small
1631             2. pipe has not enough data
1632             3. short reads  */
1633      if (us_blocks || (!input_offset_overflow && us_bytes))
1634        {
1635          error (0, 0,
1636                 _("%s: cannot skip to specified offset"), quote (input_file));
1637        }
1638    }
1639
1640  if (seek_records != 0)
1641    {
1642      uintmax_t write_records = skip (STDOUT_FILENO, output_file,
1643                                      seek_records, output_blocksize, obuf);
1644
1645      if (write_records != 0)
1646        {
1647          memset (obuf, 0, output_blocksize);
1648
1649          do
1650            if (iwrite (STDOUT_FILENO, obuf, output_blocksize)
1651                != output_blocksize)
1652              {
1653                error (0, errno, _("writing to %s"), quote (output_file));
1654                quit (EXIT_FAILURE);
1655              }
1656          while (--write_records != 0);
1657        }
1658    }
1659
1660  if (max_records == 0)
1661    return exit_status;
1662
1663  while (1)
1664    {
1665      if (r_partial + r_full >= max_records)
1666        break;
1667
1668      /* Zero the buffer before reading, so that if we get a read error,
1669         whatever data we are able to read is followed by zeros.
1670         This minimizes data loss. */
1671      if ((conversions_mask & C_SYNC) && (conversions_mask & C_NOERROR))
1672        memset (ibuf,
1673                (conversions_mask & (C_BLOCK | C_UNBLOCK)) ? ' ' : '\0',
1674                input_blocksize);
1675
1676      nread = iread_fnc (STDIN_FILENO, ibuf, input_blocksize);
1677
1678      if (nread == 0)
1679        break;			/* EOF.  */
1680
1681      if (nread < 0)
1682        {
1683          error (0, errno, _("reading %s"), quote (input_file));
1684          if (conversions_mask & C_NOERROR)
1685            {
1686              print_stats ();
1687              /* Seek past the bad block if possible. */
1688              if (!advance_input_after_read_error (input_blocksize - partread))
1689                {
1690                  exit_status = EXIT_FAILURE;
1691
1692                  /* Suppress duplicate diagnostics.  */
1693                  input_seekable = false;
1694                  input_seek_errno = ESPIPE;
1695                }
1696              if ((conversions_mask & C_SYNC) && !partread)
1697                /* Replace the missing input with null bytes and
1698                   proceed normally.  */
1699                nread = 0;
1700              else
1701                continue;
1702            }
1703          else
1704            {
1705              /* Write any partial block. */
1706              exit_status = EXIT_FAILURE;
1707              break;
1708            }
1709        }
1710
1711      n_bytes_read = nread;
1712      advance_input_offset (nread);
1713
1714      if (n_bytes_read < input_blocksize)
1715        {
1716          r_partial++;
1717          partread = n_bytes_read;
1718          if (conversions_mask & C_SYNC)
1719            {
1720              if (!(conversions_mask & C_NOERROR))
1721                /* If C_NOERROR, we zeroed the block before reading. */
1722                memset (ibuf + n_bytes_read,
1723                        (conversions_mask & (C_BLOCK | C_UNBLOCK)) ? ' ' : '\0',
1724                        input_blocksize - n_bytes_read);
1725              n_bytes_read = input_blocksize;
1726            }
1727        }
1728      else
1729        {
1730          r_full++;
1731          partread = 0;
1732        }
1733
1734      if (ibuf == obuf)		/* If not C_TWOBUFS. */
1735        {
1736          size_t nwritten = iwrite (STDOUT_FILENO, obuf, n_bytes_read);
1737          w_bytes += nwritten;
1738          if (nwritten != n_bytes_read)
1739            {
1740              error (0, errno, _("writing %s"), quote (output_file));
1741              return EXIT_FAILURE;
1742            }
1743          else if (n_bytes_read == input_blocksize)
1744            w_full++;
1745          else
1746            w_partial++;
1747          continue;
1748        }
1749
1750      /* Do any translations on the whole buffer at once.  */
1751
1752      if (translation_needed)
1753        translate_buffer (ibuf, n_bytes_read);
1754
1755      if (conversions_mask & C_SWAB)
1756        bufstart = swab_buffer (ibuf, &n_bytes_read);
1757      else
1758        bufstart = ibuf;
1759
1760      if (conversions_mask & C_BLOCK)
1761        copy_with_block (bufstart, n_bytes_read);
1762      else if (conversions_mask & C_UNBLOCK)
1763        copy_with_unblock (bufstart, n_bytes_read);
1764      else
1765        copy_simple (bufstart, n_bytes_read);
1766    }
1767
1768  /* If we have a char left as a result of conv=swab, output it.  */
1769  if (char_is_saved)
1770    {
1771      if (conversions_mask & C_BLOCK)
1772        copy_with_block (&saved_char, 1);
1773      else if (conversions_mask & C_UNBLOCK)
1774        copy_with_unblock (&saved_char, 1);
1775      else
1776        output_char (saved_char);
1777    }
1778
1779  if ((conversions_mask & C_BLOCK) && col > 0)
1780    {
1781      /* If the final input line didn't end with a '\n', pad
1782         the output block to `conversion_blocksize' chars.  */
1783      size_t i;
1784      for (i = col; i < conversion_blocksize; i++)
1785        output_char (space_character);
1786    }
1787
1788  if (col && (conversions_mask & C_UNBLOCK))
1789    {
1790      /* If there was any output, add a final '\n'.  */
1791      output_char (newline_character);
1792    }
1793
1794  /* Write out the last block. */
1795  if (oc != 0)
1796    {
1797      size_t nwritten = iwrite (STDOUT_FILENO, obuf, oc);
1798      w_bytes += nwritten;
1799      if (nwritten != 0)
1800        w_partial++;
1801      if (nwritten != oc)
1802        {
1803          error (0, errno, _("writing %s"), quote (output_file));
1804          return EXIT_FAILURE;
1805        }
1806    }
1807
1808  if ((conversions_mask & C_FDATASYNC) && fdatasync (STDOUT_FILENO) != 0)
1809    {
1810      if (errno != ENOSYS && errno != EINVAL)
1811        {
1812          error (0, errno, _("fdatasync failed for %s"), quote (output_file));
1813          exit_status = EXIT_FAILURE;
1814        }
1815      conversions_mask |= C_FSYNC;
1816    }
1817
1818  if (conversions_mask & C_FSYNC)
1819    while (fsync (STDOUT_FILENO) != 0)
1820      if (errno != EINTR)
1821        {
1822          error (0, errno, _("fsync failed for %s"), quote (output_file));
1823          return EXIT_FAILURE;
1824        }
1825
1826  return exit_status;
1827}
1828
1829int
1830main (int argc, char **argv)
1831{
1832  int i;
1833  int exit_status;
1834  off_t offset;
1835
1836  install_signal_handlers ();
1837
1838  initialize_main (&argc, &argv);
1839  set_program_name (argv[0]);
1840  setlocale (LC_ALL, "");
1841  bindtextdomain (PACKAGE, LOCALEDIR);
1842  textdomain (PACKAGE);
1843
1844  /* Arrange to close stdout if parse_long_options exits.  */
1845  atexit (maybe_close_stdout);
1846
1847  page_size = getpagesize ();
1848
1849  parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, Version,
1850                      usage, AUTHORS, (char const *) NULL);
1851  close_stdout_required = false;
1852
1853  if (getopt_long (argc, argv, "", NULL, NULL) != -1)
1854    usage (EXIT_FAILURE);
1855
1856  /* Initialize translation table to identity translation. */
1857  for (i = 0; i < 256; i++)
1858    trans_table[i] = i;
1859
1860  /* Decode arguments. */
1861  scanargs (argc, argv);
1862
1863  apply_translations ();
1864
1865  if (input_file == NULL)
1866    {
1867      input_file = _("standard input");
1868      set_fd_flags (STDIN_FILENO, input_flags, input_file);
1869    }
1870  else
1871    {
1872      if (fd_reopen (STDIN_FILENO, input_file, O_RDONLY | input_flags, 0) < 0)
1873        error (EXIT_FAILURE, errno, _("opening %s"), quote (input_file));
1874    }
1875
1876  offset = lseek (STDIN_FILENO, 0, SEEK_CUR);
1877  input_seekable = (0 <= offset);
1878  input_offset = MAX(0, offset);
1879  input_seek_errno = errno;
1880
1881  if (output_file == NULL)
1882    {
1883      output_file = _("standard output");
1884      set_fd_flags (STDOUT_FILENO, output_flags, output_file);
1885    }
1886  else
1887    {
1888      mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
1889      int opts
1890        = (output_flags
1891           | (conversions_mask & C_NOCREAT ? 0 : O_CREAT)
1892           | (conversions_mask & C_EXCL ? O_EXCL : 0)
1893           | (seek_records || (conversions_mask & C_NOTRUNC) ? 0 : O_TRUNC));
1894
1895      /* Open the output file with *read* access only if we might
1896         need to read to satisfy a `seek=' request.  If we can't read
1897         the file, go ahead with write-only access; it might work.  */
1898      if ((! seek_records
1899           || fd_reopen (STDOUT_FILENO, output_file, O_RDWR | opts, perms) < 0)
1900          && (fd_reopen (STDOUT_FILENO, output_file, O_WRONLY | opts, perms)
1901              < 0))
1902        error (EXIT_FAILURE, errno, _("opening %s"), quote (output_file));
1903
1904      if (seek_records != 0 && !(conversions_mask & C_NOTRUNC))
1905        {
1906          uintmax_t size = seek_records * output_blocksize;
1907          unsigned long int obs = output_blocksize;
1908
1909          if (OFF_T_MAX / output_blocksize < seek_records)
1910            error (EXIT_FAILURE, 0,
1911                   _("offset too large: "
1912                     "cannot truncate to a length of seek=%"PRIuMAX""
1913                     " (%lu-byte) blocks"),
1914                   seek_records, obs);
1915
1916          if (ftruncate (STDOUT_FILENO, size) != 0)
1917            {
1918              /* Complain only when ftruncate fails on a regular file, a
1919                 directory, or a shared memory object, as POSIX 1003.1-2004
1920                 specifies ftruncate's behavior only for these file types.
1921                 For example, do not complain when Linux kernel 2.4 ftruncate
1922                 fails on /dev/fd0.  */
1923              int ftruncate_errno = errno;
1924              struct stat stdout_stat;
1925              if (fstat (STDOUT_FILENO, &stdout_stat) != 0)
1926                error (EXIT_FAILURE, errno, _("cannot fstat %s"),
1927                       quote (output_file));
1928              if (S_ISREG (stdout_stat.st_mode)
1929                  || S_ISDIR (stdout_stat.st_mode)
1930                  || S_TYPEISSHM (&stdout_stat))
1931                error (EXIT_FAILURE, ftruncate_errno,
1932                   _("failed to truncate to %"PRIuMAX" bytes in output file %s"),
1933                       size, quote (output_file));
1934            }
1935        }
1936    }
1937
1938  start_time = gethrxtime ();
1939
1940  exit_status = dd_copy ();
1941
1942  quit (exit_status);
1943}
1944