Most .c files (AUTHORS): Revert the WRITTEN_BY/AUTHORS change
[platform/upstream/coreutils.git] / src / dd.c
1 /* dd -- convert a file while copying it.
2    Copyright (C) 85, 90, 91, 1995-2003 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 2, or (at your option)
7    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, write to the Free Software Foundation,
16    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
17
18 /* Written by Paul Rubin, David MacKenzie, and Stuart Kemp. */
19
20 #include <config.h>
21 #include <stdio.h>
22
23 #define SWAB_ALIGN_OFFSET 2
24
25 #include <sys/types.h>
26 #include <signal.h>
27 #include <getopt.h>
28
29 #include "system.h"
30 #include "error.h"
31 #include "full-write.h"
32 #include "getpagesize.h"
33 #include "inttostr.h"
34 #include "long-options.h"
35 #include "quote.h"
36 #include "safe-read.h"
37 #include "xstrtol.h"
38
39 /* The official name of this program (e.g., no `g' prefix).  */
40 #define PROGRAM_NAME "dd"
41
42 #define AUTHORS "Paul Rubin", "David MacKenzie", "Stuart Kemp"
43
44 #ifndef SIGINFO
45 # define SIGINFO SIGUSR1
46 #endif
47
48 #ifndef S_TYPEISSHM
49 # define S_TYPEISSHM(Stat_ptr) 0
50 #endif
51
52 #define ROUND_UP_OFFSET(X, M) ((M) - 1 - (((X) + (M) - 1) % (M)))
53 #define PTR_ALIGN(Ptr, M) ((Ptr) \
54                            + ROUND_UP_OFFSET ((char *)(Ptr) - (char *)0, (M)))
55
56 #define max(a, b) ((a) > (b) ? (a) : (b))
57 #define output_char(c)                          \
58   do                                            \
59     {                                           \
60       obuf[oc++] = (c);                         \
61       if (oc >= output_blocksize)               \
62         write_output ();                        \
63     }                                           \
64   while (0)
65
66 /* Default input and output blocksize. */
67 #define DEFAULT_BLOCKSIZE 512
68
69 /* Conversions bit masks. */
70 #define C_ASCII 01
71 #define C_EBCDIC 02
72 #define C_IBM 04
73 #define C_BLOCK 010
74 #define C_UNBLOCK 020
75 #define C_LCASE 040
76 #define C_UCASE 0100
77 #define C_SWAB 0200
78 #define C_NOERROR 0400
79 #define C_NOTRUNC 01000
80 #define C_SYNC 02000
81 /* Use separate input and output buffers, and combine partial input blocks. */
82 #define C_TWOBUFS 04000
83
84 /* The name this program was run with. */
85 char *program_name;
86
87 /* The name of the input file, or NULL for the standard input. */
88 static char const *input_file = NULL;
89
90 /* The name of the output file, or NULL for the standard output. */
91 static char const *output_file = NULL;
92
93 /* The number of bytes in which atomic reads are done. */
94 static size_t input_blocksize = 0;
95
96 /* The number of bytes in which atomic writes are done. */
97 static size_t output_blocksize = 0;
98
99 /* Conversion buffer size, in bytes.  0 prevents conversions. */
100 static size_t conversion_blocksize = 0;
101
102 /* Skip this many records of `input_blocksize' bytes before input. */
103 static uintmax_t skip_records = 0;
104
105 /* Skip this many records of `output_blocksize' bytes before output. */
106 static uintmax_t seek_records = 0;
107
108 /* Copy only this many records.  The default is effectively infinity.  */
109 static uintmax_t max_records = (uintmax_t) -1;
110
111 /* Bit vector of conversions to apply. */
112 static int conversions_mask = 0;
113
114 /* If nonzero, filter characters through the translation table.  */
115 static int translation_needed = 0;
116
117 /* Number of partial blocks written. */
118 static uintmax_t w_partial = 0;
119
120 /* Number of full blocks written. */
121 static uintmax_t w_full = 0;
122
123 /* Number of partial blocks read. */
124 static uintmax_t r_partial = 0;
125
126 /* Number of full blocks read. */
127 static uintmax_t r_full = 0;
128
129 /* Records truncated by conv=block. */
130 static uintmax_t r_truncate = 0;
131
132 /* Output representation of newline and space characters.
133    They change if we're converting to EBCDIC.  */
134 static char newline_character = '\n';
135 static char space_character = ' ';
136
137 /* Output buffer. */
138 static char *obuf;
139
140 /* Current index into `obuf'. */
141 static size_t oc = 0;
142
143 /* Index into current line, for `conv=block' and `conv=unblock'.  */
144 static size_t col = 0;
145
146 struct conversion
147 {
148   char *convname;
149   int conversion;
150 };
151
152 static struct conversion conversions[] =
153 {
154   {"ascii", C_ASCII | C_TWOBUFS},       /* EBCDIC to ASCII. */
155   {"ebcdic", C_EBCDIC | C_TWOBUFS},     /* ASCII to EBCDIC. */
156   {"ibm", C_IBM | C_TWOBUFS},   /* Slightly different ASCII to EBCDIC. */
157   {"block", C_BLOCK | C_TWOBUFS},       /* Variable to fixed length records. */
158   {"unblock", C_UNBLOCK | C_TWOBUFS},   /* Fixed to variable length records. */
159   {"lcase", C_LCASE | C_TWOBUFS},       /* Translate upper to lower case. */
160   {"ucase", C_UCASE | C_TWOBUFS},       /* Translate lower to upper case. */
161   {"swab", C_SWAB | C_TWOBUFS}, /* Swap bytes of input. */
162   {"noerror", C_NOERROR},       /* Ignore i/o errors. */
163   {"notrunc", C_NOTRUNC},       /* Do not truncate output file. */
164   {"sync", C_SYNC},             /* Pad input records to ibs with NULs. */
165   {NULL, 0}
166 };
167
168 /* Translation table formed by applying successive transformations. */
169 static unsigned char trans_table[256];
170
171 static char const ascii_to_ebcdic[] =
172 {
173   '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
174   '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
175   '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
176   '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
177   '\100', '\117', '\177', '\173', '\133', '\154', '\120', '\175',
178   '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
179   '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
180   '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
181   '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
182   '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
183   '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
184   '\347', '\350', '\351', '\112', '\340', '\132', '\137', '\155',
185   '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
186   '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
187   '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
188   '\247', '\250', '\251', '\300', '\152', '\320', '\241', '\007',
189   '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
190   '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
191   '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
192   '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
193   '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
194   '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
195   '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
196   '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
197   '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
198   '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',
199   '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',
200   '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
201   '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
202   '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
203   '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
204   '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
205 };
206
207 static char const ascii_to_ibm[] =
208 {
209   '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
210   '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
211   '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
212   '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
213   '\100', '\132', '\177', '\173', '\133', '\154', '\120', '\175',
214   '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
215   '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
216   '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
217   '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
218   '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
219   '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
220   '\347', '\350', '\351', '\255', '\340', '\275', '\137', '\155',
221   '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
222   '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
223   '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
224   '\247', '\250', '\251', '\300', '\117', '\320', '\241', '\007',
225   '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
226   '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
227   '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
228   '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
229   '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
230   '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
231   '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
232   '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
233   '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
234   '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',
235   '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',
236   '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
237   '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
238   '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
239   '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
240   '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
241 };
242
243 static char const ebcdic_to_ascii[] =
244 {
245   '\000', '\001', '\002', '\003', '\234', '\011', '\206', '\177',
246   '\227', '\215', '\216', '\013', '\014', '\015', '\016', '\017',
247   '\020', '\021', '\022', '\023', '\235', '\205', '\010', '\207',
248   '\030', '\031', '\222', '\217', '\034', '\035', '\036', '\037',
249   '\200', '\201', '\202', '\203', '\204', '\012', '\027', '\033',
250   '\210', '\211', '\212', '\213', '\214', '\005', '\006', '\007',
251   '\220', '\221', '\026', '\223', '\224', '\225', '\226', '\004',
252   '\230', '\231', '\232', '\233', '\024', '\025', '\236', '\032',
253   '\040', '\240', '\241', '\242', '\243', '\244', '\245', '\246',
254   '\247', '\250', '\133', '\056', '\074', '\050', '\053', '\041',
255   '\046', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
256   '\260', '\261', '\135', '\044', '\052', '\051', '\073', '\136',
257   '\055', '\057', '\262', '\263', '\264', '\265', '\266', '\267',
258   '\270', '\271', '\174', '\054', '\045', '\137', '\076', '\077',
259   '\272', '\273', '\274', '\275', '\276', '\277', '\300', '\301',
260   '\302', '\140', '\072', '\043', '\100', '\047', '\075', '\042',
261   '\303', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
262   '\150', '\151', '\304', '\305', '\306', '\307', '\310', '\311',
263   '\312', '\152', '\153', '\154', '\155', '\156', '\157', '\160',
264   '\161', '\162', '\313', '\314', '\315', '\316', '\317', '\320',
265   '\321', '\176', '\163', '\164', '\165', '\166', '\167', '\170',
266   '\171', '\172', '\322', '\323', '\324', '\325', '\326', '\327',
267   '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
268   '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
269   '\173', '\101', '\102', '\103', '\104', '\105', '\106', '\107',
270   '\110', '\111', '\350', '\351', '\352', '\353', '\354', '\355',
271   '\175', '\112', '\113', '\114', '\115', '\116', '\117', '\120',
272   '\121', '\122', '\356', '\357', '\360', '\361', '\362', '\363',
273   '\134', '\237', '\123', '\124', '\125', '\126', '\127', '\130',
274   '\131', '\132', '\364', '\365', '\366', '\367', '\370', '\371',
275   '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
276   '\070', '\071', '\372', '\373', '\374', '\375', '\376', '\377'
277 };
278
279 void
280 usage (int status)
281 {
282   if (status != 0)
283     fprintf (stderr, _("Try `%s --help' for more information.\n"),
284              program_name);
285   else
286     {
287       printf (_("Usage: %s [OPTION]...\n"), program_name);
288       fputs (_("\
289 Copy a file, converting and formatting according to the options.\n\
290 \n\
291   bs=BYTES        force ibs=BYTES and obs=BYTES\n\
292   cbs=BYTES       convert BYTES bytes at a time\n\
293   conv=KEYWORDS   convert the file as per the comma separated keyword list\n\
294   count=BLOCKS    copy only BLOCKS input blocks\n\
295   ibs=BYTES       read BYTES bytes at a time\n\
296 "), stdout);
297       fputs (_("\
298   if=FILE         read from FILE instead of stdin\n\
299   obs=BYTES       write BYTES bytes at a time\n\
300   of=FILE         write to FILE instead of stdout\n\
301   seek=BLOCKS     skip BLOCKS obs-sized blocks at start of output\n\
302   skip=BLOCKS     skip BLOCKS ibs-sized blocks at start of input\n\
303 "), stdout);
304       fputs (HELP_OPTION_DESCRIPTION, stdout);
305       fputs (VERSION_OPTION_DESCRIPTION, stdout);
306       fputs (_("\
307 \n\
308 BLOCKS and BYTES may be followed by the following multiplicative suffixes:\n\
309 xM M, c 1, w 2, b 512, kB 1000, K 1024, MB 1000*1000, M 1024*1024,\n\
310 GB 1000*1000*1000, G 1024*1024*1024, and so on for T, P, E, Z, Y.\n\
311 Each KEYWORD may be:\n\
312 \n\
313 "), stdout);
314       fputs (_("\
315   ascii     from EBCDIC to ASCII\n\
316   ebcdic    from ASCII to EBCDIC\n\
317   ibm       from ASCII to alternated EBCDIC\n\
318   block     pad newline-terminated records with spaces to cbs-size\n\
319   unblock   replace trailing spaces in cbs-size records with newline\n\
320   lcase     change upper case to lower case\n\
321 "), stdout);
322       fputs (_("\
323   notrunc   do not truncate the output file\n\
324   ucase     change lower case to upper case\n\
325   swab      swap every pair of input bytes\n\
326   noerror   continue after read errors\n\
327   sync      pad every input block with NULs to ibs-size; when used\n\
328               with block or unblock, pad with spaces rather than NULs\n\
329 "), stdout);
330       fputs (_("\
331 \n\
332 Note that sending a SIGUSR1 signal to a running `dd' process makes it\n\
333 print to standard error the number of records read and written so far,\n\
334 then to resume copying.\n\
335 \n\
336   $ dd if=/dev/zero of=/dev/null& pid=$!\n\
337   $ kill -USR1 $pid; sleep 1; kill $pid\n\
338   10899206+0 records in\n\
339   10899206+0 records out\n\
340 "), stdout);
341       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
342     }
343   exit (status);
344 }
345
346 static void
347 translate_charset (char const *new_trans)
348 {
349   int i;
350
351   for (i = 0; i < 256; i++)
352     trans_table[i] = new_trans[trans_table[i]];
353   translation_needed = 1;
354 }
355
356 /* Return the number of 1 bits in `i'. */
357
358 static int
359 bit_count (register int i)
360 {
361   register int set_bits;
362
363   for (set_bits = 0; i != 0; set_bits++)
364     i &= i - 1;
365   return set_bits;
366 }
367
368 static void
369 print_stats (void)
370 {
371   char buf[2][INT_BUFSIZE_BOUND (uintmax_t)];
372   fprintf (stderr, _("%s+%s records in\n"),
373            umaxtostr (r_full, buf[0]), umaxtostr (r_partial, buf[1]));
374   fprintf (stderr, _("%s+%s records out\n"),
375            umaxtostr (w_full, buf[0]), umaxtostr (w_partial, buf[1]));
376   if (r_truncate > 0)
377     {
378       fprintf (stderr, "%s %s\n",
379                umaxtostr (r_truncate, buf[0]),
380                (r_truncate == 1
381                 ? _("truncated record")
382                 : _("truncated records")));
383     }
384 }
385
386 static void
387 cleanup (void)
388 {
389   print_stats ();
390   if (close (STDIN_FILENO) < 0)
391     error (EXIT_FAILURE, errno,
392            _("closing input file %s"), quote (input_file));
393   if (close (STDOUT_FILENO) < 0)
394     error (EXIT_FAILURE, errno,
395            _("closing output file %s"), quote (output_file));
396 }
397
398 static inline void
399 quit (int code)
400 {
401   cleanup ();
402   exit (code);
403 }
404
405 static RETSIGTYPE
406 interrupt_handler (int sig)
407 {
408 #ifdef SA_NOCLDSTOP
409   struct sigaction sigact;
410
411   sigact.sa_handler = SIG_DFL;
412   sigemptyset (&sigact.sa_mask);
413   sigact.sa_flags = 0;
414   sigaction (sig, &sigact, NULL);
415 #else
416   signal (sig, SIG_DFL);
417 #endif
418   cleanup ();
419   raise (sig);
420 }
421
422 static RETSIGTYPE
423 siginfo_handler (int sig ATTRIBUTE_UNUSED)
424 {
425   print_stats ();
426 }
427
428 /* Encapsulate portability mess of establishing signal handlers.  */
429
430 static void
431 install_handler (int sig_num, RETSIGTYPE (*sig_handler) (int sig))
432 {
433 #ifdef SA_NOCLDSTOP
434   struct sigaction sigact;
435   sigaction (sig_num, NULL, &sigact);
436   if (sigact.sa_handler != SIG_IGN)
437     {
438       sigact.sa_handler = sig_handler;
439       sigemptyset (&sigact.sa_mask);
440       sigact.sa_flags = 0;
441       sigaction (sig_num, &sigact, NULL);
442     }
443 #else
444   if (signal (sig_num, SIG_IGN) != SIG_IGN)
445     signal (sig_num, sig_handler);
446 #endif
447 }
448
449 /* Open a file to a particular file descriptor.  This is like standard
450    `open', except it always returns DESIRED_FD if successful.  */
451 static int
452 open_fd (int desired_fd, char const *filename, int options, mode_t mode)
453 {
454   int fd;
455   close (desired_fd);
456   fd = open (filename, options, mode);
457   if (fd < 0)
458     return -1;
459
460   if (fd != desired_fd)
461     {
462       if (dup2 (fd, desired_fd) != desired_fd)
463         desired_fd = -1;
464       if (close (fd) != 0)
465         return -1;
466     }
467
468   return desired_fd;
469 }
470
471 /* Write, then empty, the output buffer `obuf'. */
472
473 static void
474 write_output (void)
475 {
476   size_t nwritten = full_write (STDOUT_FILENO, obuf, output_blocksize);
477   if (nwritten != output_blocksize)
478     {
479       error (0, errno, _("writing to %s"), quote (output_file));
480       if (nwritten != 0)
481         w_partial++;
482       quit (1);
483     }
484   else
485     w_full++;
486   oc = 0;
487 }
488
489 /* Interpret one "conv=..." option.
490    As a by product, this function replaces each `,' in STR with a NUL byte.  */
491
492 static void
493 parse_conversion (char *str)
494 {
495   char *new;
496   int i;
497
498   do
499     {
500       new = strchr (str, ',');
501       if (new != NULL)
502         *new++ = '\0';
503       for (i = 0; conversions[i].convname != NULL; i++)
504         if (STREQ (conversions[i].convname, str))
505           {
506             conversions_mask |= conversions[i].conversion;
507             break;
508           }
509       if (conversions[i].convname == NULL)
510         {
511           error (0, 0, _("invalid conversion: %s"), quote (str));
512           usage (EXIT_FAILURE);
513         }
514       str = new;
515   } while (new != NULL);
516 }
517
518 /* Return the value of STR, interpreted as a non-negative decimal integer,
519    optionally multiplied by various values.
520    Assign nonzero to *INVALID if STR does not represent a number in
521    this format. */
522
523 static uintmax_t
524 parse_integer (const char *str, int *invalid)
525 {
526   uintmax_t n;
527   char *suffix;
528   enum strtol_error e = xstrtoumax (str, &suffix, 10, &n, "bcEGkKMPTwYZ0");
529
530   if (e == LONGINT_INVALID_SUFFIX_CHAR && *suffix == 'x')
531     {
532       uintmax_t multiplier = parse_integer (suffix + 1, invalid);
533
534       if (multiplier != 0 && n * multiplier / multiplier != n)
535         {
536           *invalid = 1;
537           return 0;
538         }
539
540       n *= multiplier;
541     }
542   else if (e != LONGINT_OK)
543     {
544       *invalid = 1;
545       return 0;
546     }
547
548   return n;
549 }
550
551 static void
552 scanargs (int argc, char **argv)
553 {
554   int i;
555
556   --argc;
557   ++argv;
558
559   for (i = optind; i < argc; i++)
560     {
561       char *name, *val;
562
563       name = argv[i];
564       val = strchr (name, '=');
565       if (val == NULL)
566         {
567           error (0, 0, _("unrecognized option %s"), quote (name));
568           usage (EXIT_FAILURE);
569         }
570       *val++ = '\0';
571
572       if (STREQ (name, "if"))
573         input_file = val;
574       else if (STREQ (name, "of"))
575         output_file = val;
576       else if (STREQ (name, "conv"))
577         parse_conversion (val);
578       else
579         {
580           int invalid = 0;
581           uintmax_t n = parse_integer (val, &invalid);
582
583           if (STREQ (name, "ibs"))
584             {
585               /* Ensure that each blocksize is <= SSIZE_MAX.  */
586               invalid |= SSIZE_MAX < n;
587               input_blocksize = n;
588               invalid |= input_blocksize != n || input_blocksize == 0;
589               conversions_mask |= C_TWOBUFS;
590             }
591           else if (STREQ (name, "obs"))
592             {
593               /* Ensure that each blocksize is <= SSIZE_MAX.  */
594               invalid |= SSIZE_MAX < n;
595               output_blocksize = n;
596               invalid |= output_blocksize != n || output_blocksize == 0;
597               conversions_mask |= C_TWOBUFS;
598             }
599           else if (STREQ (name, "bs"))
600             {
601               /* Ensure that each blocksize is <= SSIZE_MAX.  */
602               invalid |= SSIZE_MAX < n;
603               output_blocksize = input_blocksize = n;
604               invalid |= output_blocksize != n || output_blocksize == 0;
605             }
606           else if (STREQ (name, "cbs"))
607             {
608               conversion_blocksize = n;
609               invalid |= (conversion_blocksize != n
610                           || conversion_blocksize == 0);
611             }
612           else if (STREQ (name, "skip"))
613             skip_records = n;
614           else if (STREQ (name, "seek"))
615             seek_records = n;
616           else if (STREQ (name, "count"))
617             max_records = n;
618           else
619             {
620               error (0, 0, _("unrecognized option %s=%s"),
621                      quote_n (0, name), quote_n (1, val));
622               usage (EXIT_FAILURE);
623             }
624
625           if (invalid)
626             error (EXIT_FAILURE, 0, _("invalid number %s"), quote (val));
627         }
628     }
629
630   /* If bs= was given, both `input_blocksize' and `output_blocksize' will
631      have been set to positive values.  If either has not been set,
632      bs= was not given, so make sure two buffers are used. */
633   if (input_blocksize == 0 || output_blocksize == 0)
634     conversions_mask |= C_TWOBUFS;
635   if (input_blocksize == 0)
636     input_blocksize = DEFAULT_BLOCKSIZE;
637   if (output_blocksize == 0)
638     output_blocksize = DEFAULT_BLOCKSIZE;
639   if (conversion_blocksize == 0)
640     conversions_mask &= ~(C_BLOCK | C_UNBLOCK);
641 }
642
643 /* Fix up translation table. */
644
645 static void
646 apply_translations (void)
647 {
648   int i;
649
650 #define MX(a) (bit_count (conversions_mask & (a)))
651   if ((MX (C_ASCII | C_EBCDIC | C_IBM) > 1)
652       || (MX (C_BLOCK | C_UNBLOCK) > 1)
653       || (MX (C_LCASE | C_UCASE) > 1)
654       || (MX (C_UNBLOCK | C_SYNC) > 1))
655     {
656       error (EXIT_FAILURE, 0, _("\
657 only one conv in {ascii,ebcdic,ibm}, {lcase,ucase}, {block,unblock}, {unblock,sync}"));
658     }
659 #undef MX
660
661   if (conversions_mask & C_ASCII)
662     translate_charset (ebcdic_to_ascii);
663
664   if (conversions_mask & C_UCASE)
665     {
666       for (i = 0; i < 256; i++)
667         if (ISLOWER (trans_table[i]))
668           trans_table[i] = TOUPPER (trans_table[i]);
669       translation_needed = 1;
670     }
671   else if (conversions_mask & C_LCASE)
672     {
673       for (i = 0; i < 256; i++)
674         if (ISUPPER (trans_table[i]))
675           trans_table[i] = TOLOWER (trans_table[i]);
676       translation_needed = 1;
677     }
678
679   if (conversions_mask & C_EBCDIC)
680     {
681       translate_charset (ascii_to_ebcdic);
682       newline_character = ascii_to_ebcdic['\n'];
683       space_character = ascii_to_ebcdic[' '];
684     }
685   else if (conversions_mask & C_IBM)
686     {
687       translate_charset (ascii_to_ibm);
688       newline_character = ascii_to_ibm['\n'];
689       space_character = ascii_to_ibm[' '];
690     }
691 }
692
693 /* Apply the character-set translations specified by the user
694    to the NREAD bytes in BUF.  */
695
696 static void
697 translate_buffer (char *buf, size_t nread)
698 {
699   char *cp;
700   size_t i;
701
702   for (i = nread, cp = buf; i; i--, cp++)
703     *cp = trans_table[(unsigned char) *cp];
704 }
705
706 /* If nonnzero, the last char from the previous call to `swab_buffer'
707    is saved in `saved_char'.  */
708 static int char_is_saved = 0;
709
710 /* Odd char from previous call.  */
711 static char saved_char;
712
713 /* Swap NREAD bytes in BUF, plus possibly an initial char from the
714    previous call.  If NREAD is odd, save the last char for the
715    next call.   Return the new start of the BUF buffer.  */
716
717 static char *
718 swab_buffer (char *buf, size_t *nread)
719 {
720   char *bufstart = buf;
721   register char *cp;
722   register int i;
723
724   /* Is a char left from last time?  */
725   if (char_is_saved)
726     {
727       *--bufstart = saved_char;
728       (*nread)++;
729       char_is_saved = 0;
730     }
731
732   if (*nread & 1)
733     {
734       /* An odd number of chars are in the buffer.  */
735       saved_char = bufstart[--*nread];
736       char_is_saved = 1;
737     }
738
739   /* Do the byte-swapping by moving every second character two
740      positions toward the end, working from the end of the buffer
741      toward the beginning.  This way we only move half of the data.  */
742
743   cp = bufstart + *nread;       /* Start one char past the last.  */
744   for (i = *nread / 2; i; i--, cp -= 2)
745     *cp = *(cp - 2);
746
747   return ++bufstart;
748 }
749
750 /* This is a wrapper for lseek.  It detects and warns about a kernel
751    bug that makes lseek a no-op for tape devices, even though the kernel
752    lseek return value suggests that the function succeeded.
753
754    The parameters are the same as those of the lseek function, but
755    with the addition of FILENAME, the name of the file associated with
756    descriptor FDESC.  The file name is used solely in the warning that's
757    printed when the bug is detected.  Return the same value that lseek
758    would have returned, but when the lseek bug is detected, return -1
759    to indicate that lseek failed.
760
761    The offending behavior has been confirmed with an Exabyte SCSI tape
762    drive accessed via /dev/nst0 on both Linux-2.2.17 and Linux-2.4.16.  */
763
764 #ifdef __linux__
765
766 # include <sys/mtio.h>
767
768 # define MT_SAME_POSITION(P, Q) \
769    ((P).mt_resid == (Q).mt_resid \
770     && (P).mt_fileno == (Q).mt_fileno \
771     && (P).mt_blkno == (Q).mt_blkno)
772
773 static off_t
774 skip_via_lseek (char const *filename, int fdesc, off_t offset, int whence)
775 {
776   struct mtget s1;
777   struct mtget s2;
778   off_t new_position;
779   int got_original_tape_position;
780
781   got_original_tape_position = (ioctl (fdesc, MTIOCGET, &s1) == 0);
782   /* known bad device type */
783   /* && s.mt_type == MT_ISSCSI2 */
784
785   new_position = lseek (fdesc, offset, whence);
786   if (0 <= new_position
787       && got_original_tape_position
788       && ioctl (fdesc, MTIOCGET, &s2) == 0
789       && MT_SAME_POSITION (s1, s2))
790     {
791       error (0, 0, _("warning: working around lseek kernel bug for file (%s)\n\
792   of mt_type=0x%0lx -- see <sys/mtio.h> for the list of types"),
793              filename, s2.mt_type);
794       new_position = -1;
795     }
796
797   return new_position;
798 }
799 #else
800 # define skip_via_lseek(Filename, Fd, Offset, Whence) lseek (Fd, Offset, Whence)
801 #endif
802
803 /* Throw away RECORDS blocks of BLOCKSIZE bytes on file descriptor FDESC,
804    which is open with read permission for FILE.  Store up to BLOCKSIZE
805    bytes of the data at a time in BUF, if necessary.  RECORDS must be
806    nonzero.  */
807
808 static void
809 skip (int fdesc, char const *file, uintmax_t records, size_t blocksize,
810       char *buf)
811 {
812   off_t offset = records * blocksize;
813
814   /* Try lseek and if an error indicates it was an inappropriate operation --
815      or if the the file offset is not representable as an off_t --
816      fall back on using read.  */
817
818   if ((uintmax_t) offset / blocksize != records
819       || skip_via_lseek (file, fdesc, offset, SEEK_CUR) < 0)
820     {
821       while (records--)
822         {
823           size_t nread = safe_read (fdesc, buf, blocksize);
824           if (nread == SAFE_READ_ERROR)
825             {
826               error (0, errno, _("reading %s"), quote (file));
827               quit (1);
828             }
829           /* POSIX doesn't say what to do when dd detects it has been
830              asked to skip past EOF, so I assume it's non-fatal.
831              FIXME: maybe give a warning.  */
832           if (nread == 0)
833             break;
834         }
835     }
836 }
837
838 /* Copy NREAD bytes of BUF, with no conversions.  */
839
840 static void
841 copy_simple (char const *buf, int nread)
842 {
843   int nfree;                    /* Number of unused bytes in `obuf'.  */
844   const char *start = buf;      /* First uncopied char in BUF.  */
845
846   do
847     {
848       nfree = output_blocksize - oc;
849       if (nfree > nread)
850         nfree = nread;
851
852       memcpy (obuf + oc, start, nfree);
853
854       nread -= nfree;           /* Update the number of bytes left to copy. */
855       start += nfree;
856       oc += nfree;
857       if (oc >= output_blocksize)
858         write_output ();
859     }
860   while (nread > 0);
861 }
862
863 /* Copy NREAD bytes of BUF, doing conv=block
864    (pad newline-terminated records to `conversion_blocksize',
865    replacing the newline with trailing spaces).  */
866
867 static void
868 copy_with_block (char const *buf, size_t nread)
869 {
870   size_t i;
871
872   for (i = nread; i; i--, buf++)
873     {
874       if (*buf == newline_character)
875         {
876           if (col < conversion_blocksize)
877             {
878               size_t j;
879               for (j = col; j < conversion_blocksize; j++)
880                 output_char (space_character);
881             }
882           col = 0;
883         }
884       else
885         {
886           if (col == conversion_blocksize)
887             r_truncate++;
888           else if (col < conversion_blocksize)
889             output_char (*buf);
890           col++;
891         }
892     }
893 }
894
895 /* Copy NREAD bytes of BUF, doing conv=unblock
896    (replace trailing spaces in `conversion_blocksize'-sized records
897    with a newline).  */
898
899 static void
900 copy_with_unblock (char const *buf, size_t nread)
901 {
902   size_t i;
903   char c;
904   static int pending_spaces = 0;
905
906   for (i = 0; i < nread; i++)
907     {
908       c = buf[i];
909
910       if (col++ >= conversion_blocksize)
911         {
912           col = pending_spaces = 0; /* Wipe out any pending spaces.  */
913           i--;                  /* Push the char back; get it later. */
914           output_char (newline_character);
915         }
916       else if (c == space_character)
917         pending_spaces++;
918       else
919         {
920           /* `c' is the character after a run of spaces that were not
921              at the end of the conversion buffer.  Output them.  */
922           while (pending_spaces)
923             {
924               output_char (space_character);
925               --pending_spaces;
926             }
927           output_char (c);
928         }
929     }
930 }
931
932 /* The main loop.  */
933
934 static int
935 dd_copy (void)
936 {
937   char *ibuf, *bufstart;        /* Input buffer. */
938   char *real_buf;               /* real buffer address before alignment */
939   char *real_obuf;
940   size_t nread;                 /* Bytes read in the current block. */
941   int exit_status = 0;
942   size_t page_size = getpagesize ();
943   size_t n_bytes_read;
944
945   /* Leave at least one extra byte at the beginning and end of `ibuf'
946      for conv=swab, but keep the buffer address even.  But some peculiar
947      device drivers work only with word-aligned buffers, so leave an
948      extra two bytes.  */
949
950   /* Some devices require alignment on a sector or page boundary
951      (e.g. character disk devices).  Align the input buffer to a
952      page boundary to cover all bases.  Note that due to the swab
953      algorithm, we must have at least one byte in the page before
954      the input buffer;  thus we allocate 2 pages of slop in the
955      real buffer.  8k above the blocksize shouldn't bother anyone.
956
957      The page alignment is necessary on any linux system that supports
958      either the SGI raw I/O patch or Steven Tweedies raw I/O patch.
959      It is necessary when accessing raw (i.e. character special) disk
960      devices on Unixware or other SVR4-derived system.  */
961
962   real_buf = xmalloc (input_blocksize
963                       + 2 * SWAB_ALIGN_OFFSET
964                       + 2 * page_size - 1);
965   ibuf = real_buf;
966   ibuf += SWAB_ALIGN_OFFSET;    /* allow space for swab */
967
968   ibuf = PTR_ALIGN (ibuf, page_size);
969
970   if (conversions_mask & C_TWOBUFS)
971     {
972       /* Page-align the output buffer, too.  */
973       real_obuf = xmalloc (output_blocksize + page_size - 1);
974       obuf = PTR_ALIGN (real_obuf, page_size);
975     }
976   else
977     {
978       real_obuf = NULL;
979       obuf = ibuf;
980     }
981
982   if (skip_records != 0)
983     skip (STDIN_FILENO, input_file, skip_records, input_blocksize, ibuf);
984
985   if (seek_records != 0)
986     {
987       /* FIXME: this loses for
988          % ./dd if=dd seek=1 |:
989          ./dd: standard output: Bad file descriptor
990          0+0 records in
991          0+0 records out
992          */
993
994       skip (STDOUT_FILENO, output_file, seek_records, output_blocksize, obuf);
995     }
996
997   if (max_records == 0)
998     quit (exit_status);
999
1000   while (1)
1001     {
1002       if (r_partial + r_full >= max_records)
1003         break;
1004
1005       /* Zero the buffer before reading, so that if we get a read error,
1006          whatever data we are able to read is followed by zeros.
1007          This minimizes data loss. */
1008       if ((conversions_mask & C_SYNC) && (conversions_mask & C_NOERROR))
1009         memset (ibuf,
1010                 (conversions_mask & (C_BLOCK | C_UNBLOCK)) ? ' ' : '\0',
1011                 input_blocksize);
1012
1013       nread = safe_read (STDIN_FILENO, ibuf, input_blocksize);
1014
1015       if (nread == 0)
1016         break;                  /* EOF.  */
1017
1018       if (nread == SAFE_READ_ERROR)
1019         {
1020           error (0, errno, _("reading %s"), quote (input_file));
1021           if (conversions_mask & C_NOERROR)
1022             {
1023               print_stats ();
1024               /* Seek past the bad block if possible. */
1025               lseek (STDIN_FILENO, (off_t) input_blocksize, SEEK_CUR);
1026               if (conversions_mask & C_SYNC)
1027                 /* Replace the missing input with null bytes and
1028                    proceed normally.  */
1029                 nread = 0;
1030               else
1031                 continue;
1032             }
1033           else
1034             {
1035               /* Write any partial block. */
1036               exit_status = 2;
1037               break;
1038             }
1039         }
1040
1041       n_bytes_read = nread;
1042
1043       if (n_bytes_read < input_blocksize)
1044         {
1045           r_partial++;
1046           if (conversions_mask & C_SYNC)
1047             {
1048               if (!(conversions_mask & C_NOERROR))
1049                 /* If C_NOERROR, we zeroed the block before reading. */
1050                 memset (ibuf + n_bytes_read,
1051                         (conversions_mask & (C_BLOCK | C_UNBLOCK)) ? ' ' : '\0',
1052                         input_blocksize - n_bytes_read);
1053               n_bytes_read = input_blocksize;
1054             }
1055         }
1056       else
1057         r_full++;
1058
1059       if (ibuf == obuf)         /* If not C_TWOBUFS. */
1060         {
1061           size_t nwritten = full_write (STDOUT_FILENO, obuf, n_bytes_read);
1062           if (nwritten != n_bytes_read)
1063             {
1064               error (0, errno, _("writing %s"), quote (output_file));
1065               quit (1);
1066             }
1067           else if (n_bytes_read == input_blocksize)
1068             w_full++;
1069           else
1070             w_partial++;
1071           continue;
1072         }
1073
1074       /* Do any translations on the whole buffer at once.  */
1075
1076       if (translation_needed)
1077         translate_buffer (ibuf, n_bytes_read);
1078
1079       if (conversions_mask & C_SWAB)
1080         bufstart = swab_buffer (ibuf, &n_bytes_read);
1081       else
1082         bufstart = ibuf;
1083
1084       if (conversions_mask & C_BLOCK)
1085         copy_with_block (bufstart, n_bytes_read);
1086       else if (conversions_mask & C_UNBLOCK)
1087         copy_with_unblock (bufstart, n_bytes_read);
1088       else
1089         copy_simple (bufstart, n_bytes_read);
1090     }
1091
1092   /* If we have a char left as a result of conv=swab, output it.  */
1093   if (char_is_saved)
1094     {
1095       if (conversions_mask & C_BLOCK)
1096         copy_with_block (&saved_char, 1);
1097       else if (conversions_mask & C_UNBLOCK)
1098         copy_with_unblock (&saved_char, 1);
1099       else
1100         output_char (saved_char);
1101     }
1102
1103   if ((conversions_mask & C_BLOCK) && col > 0)
1104     {
1105       /* If the final input line didn't end with a '\n', pad
1106          the output block to `conversion_blocksize' chars.  */
1107       size_t i;
1108       for (i = col; i < conversion_blocksize; i++)
1109         output_char (space_character);
1110     }
1111
1112   if ((conversions_mask & C_UNBLOCK) && col == conversion_blocksize)
1113     /* Add a final '\n' if there are exactly `conversion_blocksize'
1114        characters in the final record. */
1115     output_char (newline_character);
1116
1117   /* Write out the last block. */
1118   if (oc != 0)
1119     {
1120       size_t nwritten = full_write (STDOUT_FILENO, obuf, oc);
1121       if (nwritten != 0)
1122         w_partial++;
1123       if (nwritten != oc)
1124         {
1125           error (0, errno, _("writing %s"), quote (output_file));
1126           quit (1);
1127         }
1128     }
1129
1130   free (real_buf);
1131   if (real_obuf)
1132     free (real_obuf);
1133
1134   return exit_status;
1135 }
1136
1137 /* This is gross, but necessary, because of the way close_stdout
1138    works and because this program closes STDOUT_FILENO directly.  */
1139 static void (*closeout_func) (void) = close_stdout;
1140
1141 static void
1142 close_stdout_wrapper (void)
1143 {
1144   if (closeout_func)
1145     (*closeout_func) ();
1146 }
1147
1148 int
1149 main (int argc, char **argv)
1150 {
1151   int i;
1152   int exit_status;
1153
1154   initialize_main (&argc, &argv);
1155   program_name = argv[0];
1156   setlocale (LC_ALL, "");
1157   bindtextdomain (PACKAGE, LOCALEDIR);
1158   textdomain (PACKAGE);
1159
1160   /* Arrange to close stdout if parse_long_options exits.  */
1161   atexit (close_stdout_wrapper);
1162
1163   parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, VERSION,
1164                       usage, AUTHORS, NULL);
1165
1166   /* Don't close stdout on exit from here on.  */
1167   closeout_func = NULL;
1168
1169   /* Initialize translation table to identity translation. */
1170   for (i = 0; i < 256; i++)
1171     trans_table[i] = i;
1172
1173   /* Decode arguments. */
1174   scanargs (argc, argv);
1175
1176   apply_translations ();
1177
1178   if (input_file != NULL)
1179     {
1180       if (open_fd (STDIN_FILENO, input_file, O_RDONLY, 0) < 0)
1181         error (EXIT_FAILURE, errno, _("opening %s"), quote (input_file));
1182     }
1183   else
1184     input_file = _("standard input");
1185
1186   if (output_file != NULL)
1187     {
1188       mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
1189       int opts
1190         = (O_CREAT
1191            | (seek_records || (conversions_mask & C_NOTRUNC) ? 0 : O_TRUNC));
1192
1193       /* Open the output file with *read* access only if we might
1194          need to read to satisfy a `seek=' request.  If we can't read
1195          the file, go ahead with write-only access; it might work.  */
1196       if ((! seek_records
1197            || open_fd (STDOUT_FILENO, output_file, O_RDWR | opts, perms) < 0)
1198           && open_fd (STDOUT_FILENO, output_file, O_WRONLY | opts, perms) < 0)
1199         error (EXIT_FAILURE, errno, _("opening %s"), quote (output_file));
1200
1201 #if HAVE_FTRUNCATE
1202       if (seek_records != 0 && !(conversions_mask & C_NOTRUNC))
1203         {
1204           struct stat stdout_stat;
1205           off_t o = seek_records * output_blocksize;
1206           if ((uintmax_t) o / output_blocksize != seek_records)
1207             error (EXIT_FAILURE, 0, _("file offset out of range"));
1208
1209           if (fstat (STDOUT_FILENO, &stdout_stat) != 0)
1210             error (EXIT_FAILURE, errno, _("cannot fstat %s"),
1211                    quote (output_file));
1212
1213           /* Complain only when ftruncate fails on a regular file, a
1214              directory, or a shared memory object, as the 2000-08
1215              POSIX draft specifies ftruncate's behavior only for these
1216              file types.  For example, do not complain when Linux 2.4
1217              ftruncate fails on /dev/fd0.  */
1218           if (ftruncate (STDOUT_FILENO, o) != 0
1219               && (S_ISREG (stdout_stat.st_mode)
1220                   || S_ISDIR (stdout_stat.st_mode)
1221                   || S_TYPEISSHM (&stdout_stat)))
1222             {
1223               char buf[INT_BUFSIZE_BOUND (off_t)];
1224               error (EXIT_FAILURE, errno,
1225                      _("advancing past %s bytes in output file %s"),
1226                      offtostr (o, buf), quote (output_file));
1227             }
1228         }
1229 #endif
1230     }
1231   else
1232     {
1233       output_file = _("standard output");
1234     }
1235
1236   install_handler (SIGINT, interrupt_handler);
1237   install_handler (SIGQUIT, interrupt_handler);
1238   install_handler (SIGPIPE, interrupt_handler);
1239   install_handler (SIGINFO, siginfo_handler);
1240
1241   exit_status = dd_copy ();
1242
1243   quit (exit_status);
1244 }