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