*** empty log message ***
[coreutils.git] / src / dd.c
blob2c334a134996096df6c14a576c930644cf2d2f65
1 /* dd -- convert a file while copying it.
2 Copyright (C) 85, 90, 91, 1995-2001 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Paul Rubin, David MacKenzie, and Stuart Kemp. */
20 #include <config.h>
21 #include <stdio.h>
23 #define SWAB_ALIGN_OFFSET 2
25 #if HAVE_INTTYPES_H
26 # include <inttypes.h>
27 #endif
28 #include <sys/types.h>
29 #include <signal.h>
30 #include <getopt.h>
32 #include "system.h"
33 #include "closeout.h"
34 #include "error.h"
35 #include "getpagesize.h"
36 #include "human.h"
37 #include "long-options.h"
38 #include "quote.h"
39 #include "safe-read.h"
40 #include "xstrtol.h"
42 /* The official name of this program (e.g., no `g' prefix). */
43 #define PROGRAM_NAME "dd"
45 #define AUTHORS "Paul Rubin, David MacKenzie, and Stuart Kemp"
47 #ifndef SIGINFO
48 # define SIGINFO SIGUSR1
49 #endif
51 #ifndef S_TYPEISSHM
52 # define S_TYPEISSHM(Mode) 0
53 #endif
55 #define ROUND_UP_OFFSET(X, M) ((M) - 1 - (((X) + (M) - 1) % (M)))
56 #define PTR_ALIGN(Ptr, M) ((Ptr) \
57 + ROUND_UP_OFFSET ((char *)(Ptr) - (char *)0, (M)))
59 #define max(a, b) ((a) > (b) ? (a) : (b))
60 #define output_char(c) \
61 do { \
62 obuf[oc++] = (c); if (oc >= output_blocksize) write_output (); \
63 } while (0)
65 /* Default input and output blocksize. */
66 #define DEFAULT_BLOCKSIZE 512
68 /* Conversions bit masks. */
69 #define C_ASCII 01
70 #define C_EBCDIC 02
71 #define C_IBM 04
72 #define C_BLOCK 010
73 #define C_UNBLOCK 020
74 #define C_LCASE 040
75 #define C_UCASE 0100
76 #define C_SWAB 0200
77 #define C_NOERROR 0400
78 #define C_NOTRUNC 01000
79 #define C_SYNC 02000
80 /* Use separate input and output buffers, and combine partial input blocks. */
81 #define C_TWOBUFS 04000
83 int full_write ();
85 /* The name this program was run with. */
86 char *program_name;
88 /* The name of the input file, or NULL for the standard input. */
89 static char *input_file = NULL;
91 /* The name of the output file, or NULL for the standard output. */
92 static char *output_file = NULL;
94 /* The number of bytes in which atomic reads are done. */
95 static size_t input_blocksize = 0;
97 /* The number of bytes in which atomic writes are done. */
98 static size_t output_blocksize = 0;
100 /* Conversion buffer size, in bytes. 0 prevents conversions. */
101 static size_t conversion_blocksize = 0;
103 /* Skip this many records of `input_blocksize' bytes before input. */
104 static uintmax_t skip_records = 0;
106 /* Skip this many records of `output_blocksize' bytes before output. */
107 static uintmax_t seek_records = 0;
109 /* Copy only this many records. The default is effectively infinity. */
110 static uintmax_t max_records = (uintmax_t) -1;
112 /* Bit vector of conversions to apply. */
113 static int conversions_mask = 0;
115 /* If nonzero, filter characters through the translation table. */
116 static int translation_needed = 0;
118 /* Number of partial blocks written. */
119 static uintmax_t w_partial = 0;
121 /* Number of full blocks written. */
122 static uintmax_t w_full = 0;
124 /* Number of partial blocks read. */
125 static uintmax_t r_partial = 0;
127 /* Number of full blocks read. */
128 static uintmax_t r_full = 0;
130 /* Records truncated by conv=block. */
131 static uintmax_t r_truncate = 0;
133 /* Output representation of newline and space characters.
134 They change if we're converting to EBCDIC. */
135 static unsigned char newline_character = '\n';
136 static unsigned char space_character = ' ';
138 /* Output buffer. */
139 static unsigned char *obuf;
141 /* Current index into `obuf'. */
142 static size_t oc = 0;
144 /* Index into current line, for `conv=block' and `conv=unblock'. */
145 static size_t col = 0;
147 struct conversion
149 char *convname;
150 int conversion;
153 static struct conversion conversions[] =
155 {"ascii", C_ASCII | C_TWOBUFS}, /* EBCDIC to ASCII. */
156 {"ebcdic", C_EBCDIC | C_TWOBUFS}, /* ASCII to EBCDIC. */
157 {"ibm", C_IBM | C_TWOBUFS}, /* Slightly different ASCII to EBCDIC. */
158 {"block", C_BLOCK | C_TWOBUFS}, /* Variable to fixed length records. */
159 {"unblock", C_UNBLOCK | C_TWOBUFS}, /* Fixed to variable length records. */
160 {"lcase", C_LCASE | C_TWOBUFS}, /* Translate upper to lower case. */
161 {"ucase", C_UCASE | C_TWOBUFS}, /* Translate lower to upper case. */
162 {"swab", C_SWAB | C_TWOBUFS}, /* Swap bytes of input. */
163 {"noerror", C_NOERROR}, /* Ignore i/o errors. */
164 {"notrunc", C_NOTRUNC}, /* Do not truncate output file. */
165 {"sync", C_SYNC}, /* Pad input records to ibs with NULs. */
166 {NULL, 0}
169 /* Translation table formed by applying successive transformations. */
170 static unsigned char trans_table[256];
172 static unsigned char const ascii_to_ebcdic[] =
174 0, 01, 02, 03, 067, 055, 056, 057,
175 026, 05, 045, 013, 014, 015, 016, 017,
176 020, 021, 022, 023, 074, 075, 062, 046,
177 030, 031, 077, 047, 034, 035, 036, 037,
178 0100, 0117, 0177, 0173, 0133, 0154, 0120, 0175,
179 0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
180 0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
181 0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
182 0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
183 0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
184 0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
185 0347, 0350, 0351, 0112, 0340, 0132, 0137, 0155,
186 0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
187 0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
188 0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
189 0247, 0250, 0251, 0300, 0152, 0320, 0241, 07,
190 040, 041, 042, 043, 044, 025, 06, 027,
191 050, 051, 052, 053, 054, 011, 012, 033,
192 060, 061, 032, 063, 064, 065, 066, 010,
193 070, 071, 072, 073, 04, 024, 076, 0341,
194 0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
195 0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
196 0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
197 0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
198 0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
199 0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236,
200 0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257,
201 0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
202 0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
203 0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
204 0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
205 0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377
208 static unsigned char const ascii_to_ibm[] =
210 0, 01, 02, 03, 067, 055, 056, 057,
211 026, 05, 045, 013, 014, 015, 016, 017,
212 020, 021, 022, 023, 074, 075, 062, 046,
213 030, 031, 077, 047, 034, 035, 036, 037,
214 0100, 0132, 0177, 0173, 0133, 0154, 0120, 0175,
215 0115, 0135, 0134, 0116, 0153, 0140, 0113, 0141,
216 0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367,
217 0370, 0371, 0172, 0136, 0114, 0176, 0156, 0157,
218 0174, 0301, 0302, 0303, 0304, 0305, 0306, 0307,
219 0310, 0311, 0321, 0322, 0323, 0324, 0325, 0326,
220 0327, 0330, 0331, 0342, 0343, 0344, 0345, 0346,
221 0347, 0350, 0351, 0255, 0340, 0275, 0137, 0155,
222 0171, 0201, 0202, 0203, 0204, 0205, 0206, 0207,
223 0210, 0211, 0221, 0222, 0223, 0224, 0225, 0226,
224 0227, 0230, 0231, 0242, 0243, 0244, 0245, 0246,
225 0247, 0250, 0251, 0300, 0117, 0320, 0241, 07,
226 040, 041, 042, 043, 044, 025, 06, 027,
227 050, 051, 052, 053, 054, 011, 012, 033,
228 060, 061, 032, 063, 064, 065, 066, 010,
229 070, 071, 072, 073, 04, 024, 076, 0341,
230 0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110,
231 0111, 0121, 0122, 0123, 0124, 0125, 0126, 0127,
232 0130, 0131, 0142, 0143, 0144, 0145, 0146, 0147,
233 0150, 0151, 0160, 0161, 0162, 0163, 0164, 0165,
234 0166, 0167, 0170, 0200, 0212, 0213, 0214, 0215,
235 0216, 0217, 0220, 0232, 0233, 0234, 0235, 0236,
236 0237, 0240, 0252, 0253, 0254, 0255, 0256, 0257,
237 0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267,
238 0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277,
239 0312, 0313, 0314, 0315, 0316, 0317, 0332, 0333,
240 0334, 0335, 0336, 0337, 0352, 0353, 0354, 0355,
241 0356, 0357, 0372, 0373, 0374, 0375, 0376, 0377
244 static unsigned char const ebcdic_to_ascii[] =
246 0, 01, 02, 03, 0234, 011, 0206, 0177,
247 0227, 0215, 0216, 013, 014, 015, 016, 017,
248 020, 021, 022, 023, 0235, 0205, 010, 0207,
249 030, 031, 0222, 0217, 034, 035, 036, 037,
250 0200, 0201, 0202, 0203, 0204, 012, 027, 033,
251 0210, 0211, 0212, 0213, 0214, 05, 06, 07,
252 0220, 0221, 026, 0223, 0224, 0225, 0226, 04,
253 0230, 0231, 0232, 0233, 024, 025, 0236, 032,
254 040, 0240, 0241, 0242, 0243, 0244, 0245, 0246,
255 0247, 0250, 0133, 056, 074, 050, 053, 041,
256 046, 0251, 0252, 0253, 0254, 0255, 0256, 0257,
257 0260, 0261, 0135, 044, 052, 051, 073, 0136,
258 055, 057, 0262, 0263, 0264, 0265, 0266, 0267,
259 0270, 0271, 0174, 054, 045, 0137, 076, 077,
260 0272, 0273, 0274, 0275, 0276, 0277, 0300, 0301,
261 0302, 0140, 072, 043, 0100, 047, 075, 042,
262 0303, 0141, 0142, 0143, 0144, 0145, 0146, 0147,
263 0150, 0151, 0304, 0305, 0306, 0307, 0310, 0311,
264 0312, 0152, 0153, 0154, 0155, 0156, 0157, 0160,
265 0161, 0162, 0313, 0314, 0315, 0316, 0317, 0320,
266 0321, 0176, 0163, 0164, 0165, 0166, 0167, 0170,
267 0171, 0172, 0322, 0323, 0324, 0325, 0326, 0327,
268 0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337,
269 0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347,
270 0173, 0101, 0102, 0103, 0104, 0105, 0106, 0107,
271 0110, 0111, 0350, 0351, 0352, 0353, 0354, 0355,
272 0175, 0112, 0113, 0114, 0115, 0116, 0117, 0120,
273 0121, 0122, 0356, 0357, 0360, 0361, 0362, 0363,
274 0134, 0237, 0123, 0124, 0125, 0126, 0127, 0130,
275 0131, 0132, 0364, 0365, 0366, 0367, 0370, 0371,
276 060, 061, 062, 063, 064, 065, 066, 067,
277 070, 071, 0372, 0373, 0374, 0375, 0376, 0377
280 void
281 usage (int status)
283 if (status != 0)
284 fprintf (stderr, _("Try `%s --help' for more information.\n"),
285 program_name);
286 else
288 printf (_("Usage: %s [OPTION]...\n"), program_name);
289 printf (_("\
290 Copy a file, converting and formatting according to the options.\n\
292 bs=BYTES force ibs=BYTES and obs=BYTES\n\
293 cbs=BYTES convert BYTES bytes at a time\n\
294 conv=KEYWORDS convert the file as per the comma separated keyword list\n\
295 count=BLOCKS copy only BLOCKS input blocks\n\
296 ibs=BYTES read BYTES bytes at a time\n\
297 if=FILE read from FILE instead of stdin\n\
298 obs=BYTES write BYTES bytes at a time\n\
299 of=FILE write to FILE instead of stdout\n\
300 seek=BLOCKS skip BLOCKS obs-sized blocks at start of output\n\
301 skip=BLOCKS skip BLOCKS ibs-sized blocks at start of input\n\
302 --help display this help and exit\n\
303 --version output version information and exit\n\
305 BLOCKS and BYTES may be followed by the following multiplicative suffixes:\n\
306 xM M, c 1, w 2, b 512, kD 1000, k 1024, MD 1,000,000, M 1,048,576,\n\
307 GD 1,000,000,000, G 1,073,741,824, and so on for T, P, E, Z, Y.\n\
308 Each KEYWORD may be:\n\
310 ascii from EBCDIC to ASCII\n\
311 ebcdic from ASCII to EBCDIC\n\
312 ibm from ASCII to alternated EBCDIC\n\
313 block pad newline-terminated records with spaces to cbs-size\n\
314 unblock replace trailing spaces in cbs-size records with newline\n\
315 lcase change upper case to lower case\n\
316 notrunc do not truncate the output file\n\
317 ucase change lower case to upper case\n\
318 swab swap every pair of input bytes\n\
319 noerror continue after read errors\n\
320 sync pad every input block with NULs to ibs-size\n\
321 "));
322 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
324 exit (status);
327 static void
328 translate_charset (const unsigned char *new_trans)
330 unsigned int i;
332 for (i = 0; i < 256; i++)
333 trans_table[i] = new_trans[trans_table[i]];
334 translation_needed = 1;
337 /* Return the number of 1 bits in `i'. */
339 static int
340 bit_count (register unsigned int i)
342 register int set_bits;
344 for (set_bits = 0; i != 0; set_bits++)
345 i &= i - 1;
346 return set_bits;
349 static void
350 print_stats (void)
352 char buf[2][LONGEST_HUMAN_READABLE + 1];
353 fprintf (stderr, _("%s+%s records in\n"),
354 human_readable (r_full, buf[0], 1, 1),
355 human_readable (r_partial, buf[1], 1, 1));
356 fprintf (stderr, _("%s+%s records out\n"),
357 human_readable (w_full, buf[0], 1, 1),
358 human_readable (w_partial, buf[1], 1, 1));
359 if (r_truncate > 0)
361 fprintf (stderr, "%s %s\n",
362 human_readable (r_truncate, buf[0], 1, 1),
363 (r_truncate == 1
364 ? _("truncated record")
365 : _("truncated records")));
369 static void
370 cleanup (void)
372 print_stats ();
373 if (close (STDIN_FILENO) < 0)
374 error (1, errno, _("closing input file %s"), quote (input_file));
375 if (close (STDOUT_FILENO) < 0)
376 error (1, errno, _("closing output file %s"), quote (output_file));
379 static inline void
380 quit (int code)
382 cleanup ();
383 exit (code);
386 static RETSIGTYPE
387 interrupt_handler (int sig)
389 #ifdef SA_NOCLDSTOP
390 struct sigaction sigact;
392 sigact.sa_handler = SIG_DFL;
393 sigemptyset (&sigact.sa_mask);
394 sigact.sa_flags = 0;
395 sigaction (sig, &sigact, NULL);
396 #else
397 signal (sig, SIG_DFL);
398 #endif
399 cleanup ();
400 kill (getpid (), sig);
403 static RETSIGTYPE
404 siginfo_handler (int sig ATTRIBUTE_UNUSED)
406 print_stats ();
409 /* Encapsulate portability mess of establishing signal handlers. */
411 static void
412 install_handler (int sig_num, RETSIGTYPE (*sig_handler) (int sig))
414 #ifdef SA_NOCLDSTOP
415 struct sigaction sigact;
416 sigaction (sig_num, NULL, &sigact);
417 if (sigact.sa_handler != SIG_IGN)
419 sigact.sa_handler = sig_handler;
420 sigemptyset (&sigact.sa_mask);
421 sigact.sa_flags = 0;
422 sigaction (sig_num, &sigact, NULL);
424 #else
425 if (signal (sig_num, SIG_IGN) != SIG_IGN)
426 signal (sig_num, sig_handler);
427 #endif
430 /* Open a file to a particular file descriptor. This is like standard
431 `open', except it always returns DESIRED_FD if successful. */
432 static int
433 open_fd (int desired_fd, char const *filename, int options, mode_t mode)
435 int fd;
436 close (desired_fd);
437 fd = open (filename, options, mode);
438 if (fd < 0)
439 return -1;
441 if (fd != desired_fd)
443 if (dup2 (fd, desired_fd) != desired_fd)
444 desired_fd = -1;
445 if (close (fd) != 0)
446 return -1;
449 return desired_fd;
452 /* Write, then empty, the output buffer `obuf'. */
454 static void
455 write_output (void)
457 int nwritten = full_write (STDOUT_FILENO, obuf, output_blocksize);
458 if (nwritten != output_blocksize)
460 error (0, errno, _("writing to %s"), quote (output_file));
461 if (nwritten > 0)
462 w_partial++;
463 quit (1);
465 else
466 w_full++;
467 oc = 0;
470 /* Interpret one "conv=..." option.
471 As a by product, this function replaces each `,' in STR with a NUL byte. */
473 static void
474 parse_conversion (char *str)
476 char *new;
477 unsigned int i;
481 new = strchr (str, ',');
482 if (new != NULL)
483 *new++ = '\0';
484 for (i = 0; conversions[i].convname != NULL; i++)
485 if (STREQ (conversions[i].convname, str))
487 conversions_mask |= conversions[i].conversion;
488 break;
490 if (conversions[i].convname == NULL)
492 error (0, 0, _("invalid conversion: %s"), quote (str));
493 usage (1);
495 str = new;
496 } while (new != NULL);
499 /* Return the value of STR, interpreted as a non-negative decimal integer,
500 optionally multiplied by various values.
501 Assign nonzero to *INVALID if STR does not represent a number in
502 this format. */
504 static uintmax_t
505 parse_integer (const char *str, int *invalid)
507 uintmax_t n;
508 char *suffix;
509 enum strtol_error e = xstrtoumax (str, &suffix, 10, &n, "bcEGkMPTwYZ0");
511 if (e == LONGINT_INVALID_SUFFIX_CHAR && *suffix == 'x')
513 uintmax_t multiplier = parse_integer (suffix + 1, invalid);
515 if (multiplier != 0 && n * multiplier / multiplier != n)
517 *invalid = 1;
518 return 0;
521 n *= multiplier;
523 else if (e != LONGINT_OK)
525 *invalid = 1;
526 return 0;
529 return n;
532 static void
533 scanargs (int argc, char **argv)
535 int i;
537 --argc;
538 ++argv;
540 for (i = optind; i < argc; i++)
542 char *name, *val;
544 name = argv[i];
545 val = strchr (name, '=');
546 if (val == NULL)
548 error (0, 0, _("unrecognized option %s"), quote (name));
549 usage (1);
551 *val++ = '\0';
553 if (STREQ (name, "if"))
554 input_file = val;
555 else if (STREQ (name, "of"))
556 output_file = val;
557 else if (STREQ (name, "conv"))
558 parse_conversion (val);
559 else
561 int invalid = 0;
562 uintmax_t n = parse_integer (val, &invalid);
564 if (STREQ (name, "ibs"))
566 input_blocksize = n;
567 invalid |= input_blocksize != n || input_blocksize == 0;
568 conversions_mask |= C_TWOBUFS;
570 else if (STREQ (name, "obs"))
572 output_blocksize = n;
573 invalid |= output_blocksize != n || output_blocksize == 0;
574 conversions_mask |= C_TWOBUFS;
576 else if (STREQ (name, "bs"))
578 output_blocksize = input_blocksize = n;
579 invalid |= output_blocksize != n || output_blocksize == 0;
581 else if (STREQ (name, "cbs"))
583 conversion_blocksize = n;
584 invalid |= (conversion_blocksize != n
585 || conversion_blocksize == 0);
587 else if (STREQ (name, "skip"))
588 skip_records = n;
589 else if (STREQ (name, "seek"))
590 seek_records = n;
591 else if (STREQ (name, "count"))
592 max_records = n;
593 else
595 error (0, 0, _("unrecognized option %s=%s"),
596 quote_n (0, name), quote_n (1, val));
597 usage (1);
600 if (invalid)
601 error (1, 0, _("invalid number %s"), quote (val));
605 /* If bs= was given, both `input_blocksize' and `output_blocksize' will
606 have been set to positive values. If either has not been set,
607 bs= was not given, so make sure two buffers are used. */
608 if (input_blocksize == 0 || output_blocksize == 0)
609 conversions_mask |= C_TWOBUFS;
610 if (input_blocksize == 0)
611 input_blocksize = DEFAULT_BLOCKSIZE;
612 if (output_blocksize == 0)
613 output_blocksize = DEFAULT_BLOCKSIZE;
614 if (conversion_blocksize == 0)
615 conversions_mask &= ~(C_BLOCK | C_UNBLOCK);
618 /* Fix up translation table. */
620 static void
621 apply_translations (void)
623 unsigned int i;
625 #define MX(a) (bit_count (conversions_mask & (a)))
626 if ((MX (C_ASCII | C_EBCDIC | C_IBM) > 1)
627 || (MX (C_BLOCK | C_UNBLOCK) > 1)
628 || (MX (C_LCASE | C_UCASE) > 1)
629 || (MX (C_UNBLOCK | C_SYNC) > 1))
631 error (1, 0, _("\
632 only one conv in {ascii,ebcdic,ibm}, {lcase,ucase}, {block,unblock}, {unblock,sync}"));
634 #undef MX
636 if (conversions_mask & C_ASCII)
637 translate_charset (ebcdic_to_ascii);
639 if (conversions_mask & C_UCASE)
641 for (i = 0; i < 256; i++)
642 if (ISLOWER (trans_table[i]))
643 trans_table[i] = TOUPPER (trans_table[i]);
644 translation_needed = 1;
646 else if (conversions_mask & C_LCASE)
648 for (i = 0; i < 256; i++)
649 if (ISUPPER (trans_table[i]))
650 trans_table[i] = TOLOWER (trans_table[i]);
651 translation_needed = 1;
654 if (conversions_mask & C_EBCDIC)
656 translate_charset (ascii_to_ebcdic);
657 newline_character = ascii_to_ebcdic['\n'];
658 space_character = ascii_to_ebcdic[' '];
660 else if (conversions_mask & C_IBM)
662 translate_charset (ascii_to_ibm);
663 newline_character = ascii_to_ibm['\n'];
664 space_character = ascii_to_ibm[' '];
668 /* Apply the character-set translations specified by the user
669 to the NREAD bytes in BUF. */
671 static void
672 translate_buffer (unsigned char *buf, size_t nread)
674 unsigned char *cp;
675 size_t i;
677 for (i = nread, cp = buf; i; i--, cp++)
678 *cp = trans_table[*cp];
681 /* If nonnzero, the last char from the previous call to `swab_buffer'
682 is saved in `saved_char'. */
683 static int char_is_saved = 0;
685 /* Odd char from previous call. */
686 static unsigned char saved_char;
688 /* Swap NREAD bytes in BUF, plus possibly an initial char from the
689 previous call. If NREAD is odd, save the last char for the
690 next call. Return the new start of the BUF buffer. */
692 static unsigned char *
693 swab_buffer (unsigned char *buf, size_t *nread)
695 unsigned char *bufstart = buf;
696 register unsigned char *cp;
697 register int i;
699 /* Is a char left from last time? */
700 if (char_is_saved)
702 *--bufstart = saved_char;
703 (*nread)++;
704 char_is_saved = 0;
707 if (*nread & 1)
709 /* An odd number of chars are in the buffer. */
710 saved_char = bufstart[--*nread];
711 char_is_saved = 1;
714 /* Do the byte-swapping by moving every second character two
715 positions toward the end, working from the end of the buffer
716 toward the beginning. This way we only move half of the data. */
718 cp = bufstart + *nread; /* Start one char past the last. */
719 for (i = *nread / 2; i; i--, cp -= 2)
720 *cp = *(cp - 2);
722 return ++bufstart;
725 /* Return nonzero iff the file referenced by FDESC is of a type for
726 which lseek's return value is known to be invalid on some systems.
727 Otherwise, return zero.
728 For example, return nonzero if FDESC references a character device
729 (on any system) because the lseek on many Linux systems incorrectly
730 returns an offset implying it succeeds for tape devices, even though
731 the function fails to perform the requested operation. In that case,
732 lseek should return nonzero and set errno. */
734 static int
735 buggy_lseek_support (int fdesc)
737 /* We have to resort to this because on some systems, lseek doesn't work
738 on some special files but doesn't return an error, either.
739 In particular, the Linux tape drivers are a problem.
740 For example, when I did the following using dd-4.0y or earlier on a
741 Linux-2.2.17 system with an Exabyte SCSI tape drive:
743 dev=/dev/nst0
744 reset='mt -f $dev rewind; mt -f $dev fsf 1'
745 eval $reset; dd if=$dev bs=32k of=out1
746 eval $reset; dd if=$dev bs=32k of=out2 skip=1
748 the resulting files, out1 and out2, would compare equal. */
750 struct stat stats;
752 return (fstat (fdesc, &stats) == 0
753 && (S_ISCHR (stats.st_mode)));
756 /* Throw away RECORDS blocks of BLOCKSIZE bytes on file descriptor FDESC,
757 which is open with read permission for FILE. Store up to BLOCKSIZE
758 bytes of the data at a time in BUF, if necessary. RECORDS must be
759 nonzero. */
761 static void
762 skip (int fdesc, char *file, uintmax_t records, size_t blocksize,
763 unsigned char *buf)
765 off_t offset = records * blocksize;
767 /* Try lseek and if an error indicates it was an inappropriate
768 operation, fall back on using read. Some broken versions of
769 lseek may return zero, so count that as an error too as a valid
770 zero return is not possible here. */
772 if (offset / blocksize != records
773 || buggy_lseek_support (fdesc)
774 || lseek (fdesc, offset, SEEK_CUR) <= 0)
776 while (records--)
778 ssize_t nread = safe_read (fdesc, buf, blocksize);
779 if (nread < 0)
781 error (0, errno, _("reading %s"), quote (file));
782 quit (1);
784 /* POSIX doesn't say what to do when dd detects it has been
785 asked to skip past EOF, so I assume it's non-fatal.
786 FIXME: maybe give a warning. */
787 if (nread == 0)
788 break;
793 /* Copy NREAD bytes of BUF, with no conversions. */
795 static void
796 copy_simple (unsigned char const *buf, int nread)
798 int nfree; /* Number of unused bytes in `obuf'. */
799 const unsigned char *start = buf; /* First uncopied char in BUF. */
803 nfree = output_blocksize - oc;
804 if (nfree > nread)
805 nfree = nread;
807 memcpy ((char *) (obuf + oc), (char *) start, nfree);
809 nread -= nfree; /* Update the number of bytes left to copy. */
810 start += nfree;
811 oc += nfree;
812 if (oc >= output_blocksize)
813 write_output ();
815 while (nread > 0);
818 /* Copy NREAD bytes of BUF, doing conv=block
819 (pad newline-terminated records to `conversion_blocksize',
820 replacing the newline with trailing spaces). */
822 static void
823 copy_with_block (unsigned char const *buf, size_t nread)
825 size_t i;
827 for (i = nread; i; i--, buf++)
829 if (*buf == newline_character)
831 if (col < conversion_blocksize)
833 size_t j;
834 for (j = col; j < conversion_blocksize; j++)
835 output_char (space_character);
837 col = 0;
839 else
841 if (col == conversion_blocksize)
842 r_truncate++;
843 else if (col < conversion_blocksize)
844 output_char (*buf);
845 col++;
850 /* Copy NREAD bytes of BUF, doing conv=unblock
851 (replace trailing spaces in `conversion_blocksize'-sized records
852 with a newline). */
854 static void
855 copy_with_unblock (unsigned char const *buf, size_t nread)
857 size_t i;
858 unsigned char c;
859 static int pending_spaces = 0;
861 for (i = 0; i < nread; i++)
863 c = buf[i];
865 if (col++ >= conversion_blocksize)
867 col = pending_spaces = 0; /* Wipe out any pending spaces. */
868 i--; /* Push the char back; get it later. */
869 output_char (newline_character);
871 else if (c == space_character)
872 pending_spaces++;
873 else
875 /* `c' is the character after a run of spaces that were not
876 at the end of the conversion buffer. Output them. */
877 while (pending_spaces)
879 output_char (space_character);
880 --pending_spaces;
882 output_char (c);
887 /* The main loop. */
889 static int
890 dd_copy (void)
892 unsigned char *ibuf, *bufstart; /* Input buffer. */
893 unsigned char *real_buf; /* real buffer address before alignment */
894 unsigned char *real_obuf;
895 ssize_t nread; /* Bytes read in the current block. */
896 int exit_status = 0;
897 size_t page_size = getpagesize ();
898 size_t n_bytes_read;
900 /* Leave at least one extra byte at the beginning and end of `ibuf'
901 for conv=swab, but keep the buffer address even. But some peculiar
902 device drivers work only with word-aligned buffers, so leave an
903 extra two bytes. */
905 /* Some devices require alignment on a sector or page boundary
906 (e.g. character disk devices). Align the input buffer to a
907 page boundary to cover all bases. Note that due to the swab
908 algorithm, we must have at least one byte in the page before
909 the input buffer; thus we allocate 2 pages of slop in the
910 real buffer. 8k above the blocksize shouldn't bother anyone.
912 The page alignment is necessary on any linux system that supports
913 either the SGI raw I/O patch or Steven Tweedies raw I/O patch.
914 It is necessary when accessing raw (i.e. character special) disk
915 devices on Unixware or other SVR4-derived system. */
917 real_buf = (unsigned char *) xmalloc (input_blocksize
918 + 2 * SWAB_ALIGN_OFFSET
919 + 2 * page_size - 1);
920 ibuf = real_buf;
921 ibuf += SWAB_ALIGN_OFFSET; /* allow space for swab */
923 ibuf = PTR_ALIGN (ibuf, page_size);
925 if (conversions_mask & C_TWOBUFS)
927 /* Page-align the output buffer, too. */
928 real_obuf = (unsigned char *) xmalloc (output_blocksize + page_size - 1);
929 obuf = PTR_ALIGN (real_obuf, page_size);
931 else
933 real_obuf = NULL;
934 obuf = ibuf;
937 if (skip_records != 0)
938 skip (STDIN_FILENO, input_file, skip_records, input_blocksize, ibuf);
940 if (seek_records != 0)
942 /* FIXME: this loses for
943 % ./dd if=dd seek=1 |:
944 ./dd: standard output: Bad file number
945 0+0 records in
946 0+0 records out
949 skip (STDOUT_FILENO, output_file, seek_records, output_blocksize, obuf);
952 if (max_records == 0)
953 quit (exit_status);
955 while (1)
957 if (r_partial + r_full >= max_records)
958 break;
960 /* Zero the buffer before reading, so that if we get a read error,
961 whatever data we are able to read is followed by zeros.
962 This minimizes data loss. */
963 if ((conversions_mask & C_SYNC) && (conversions_mask & C_NOERROR))
964 memset ((char *) ibuf, 0, input_blocksize);
966 nread = safe_read (STDIN_FILENO, ibuf, input_blocksize);
968 if (nread == 0)
969 break; /* EOF. */
971 if (nread < 0)
973 error (0, errno, _("reading %s"), quote (input_file));
974 if (conversions_mask & C_NOERROR)
976 print_stats ();
977 /* Seek past the bad block if possible. */
978 lseek (STDIN_FILENO, (off_t) input_blocksize, SEEK_CUR);
979 if (conversions_mask & C_SYNC)
980 /* Replace the missing input with null bytes and
981 proceed normally. */
982 nread = 0;
983 else
984 continue;
986 else
988 /* Write any partial block. */
989 exit_status = 2;
990 break;
994 n_bytes_read = nread;
996 if (n_bytes_read < input_blocksize)
998 r_partial++;
999 if (conversions_mask & C_SYNC)
1001 if (!(conversions_mask & C_NOERROR))
1002 /* If C_NOERROR, we zeroed the block before reading. */
1003 memset ((char *) (ibuf + n_bytes_read), 0,
1004 input_blocksize - n_bytes_read);
1005 n_bytes_read = input_blocksize;
1008 else
1009 r_full++;
1011 if (ibuf == obuf) /* If not C_TWOBUFS. */
1013 int nwritten = full_write (STDOUT_FILENO, obuf, n_bytes_read);
1014 if (nwritten < 0)
1016 error (0, errno, _("writing %s"), quote (output_file));
1017 quit (1);
1019 else if (n_bytes_read == input_blocksize)
1020 w_full++;
1021 else
1022 w_partial++;
1023 continue;
1026 /* Do any translations on the whole buffer at once. */
1028 if (translation_needed)
1029 translate_buffer (ibuf, n_bytes_read);
1031 if (conversions_mask & C_SWAB)
1032 bufstart = swab_buffer (ibuf, &n_bytes_read);
1033 else
1034 bufstart = ibuf;
1036 if (conversions_mask & C_BLOCK)
1037 copy_with_block (bufstart, n_bytes_read);
1038 else if (conversions_mask & C_UNBLOCK)
1039 copy_with_unblock (bufstart, n_bytes_read);
1040 else
1041 copy_simple (bufstart, n_bytes_read);
1044 /* If we have a char left as a result of conv=swab, output it. */
1045 if (char_is_saved)
1047 if (conversions_mask & C_BLOCK)
1048 copy_with_block (&saved_char, 1);
1049 else if (conversions_mask & C_UNBLOCK)
1050 copy_with_unblock (&saved_char, 1);
1051 else
1052 output_char (saved_char);
1055 if ((conversions_mask & C_BLOCK) && col > 0)
1057 /* If the final input line didn't end with a '\n', pad
1058 the output block to `conversion_blocksize' chars. */
1059 unsigned int i;
1060 for (i = col; i < conversion_blocksize; i++)
1061 output_char (space_character);
1064 if ((conversions_mask & C_UNBLOCK) && col == conversion_blocksize)
1065 /* Add a final '\n' if there are exactly `conversion_blocksize'
1066 characters in the final record. */
1067 output_char (newline_character);
1069 /* Write out the last block. */
1070 if (oc != 0)
1072 int nwritten = full_write (STDOUT_FILENO, obuf, oc);
1073 if (nwritten > 0)
1074 w_partial++;
1075 if (nwritten < 0)
1077 error (0, errno, _("writing %s"), quote (output_file));
1078 quit (1);
1082 free (real_buf);
1083 if (real_obuf)
1084 free (real_obuf);
1086 return exit_status;
1089 /* This is gross, but necessary, because of the way close_stdout
1090 works and because this program closes STDOUT_FILENO directly. */
1091 static void (*closeout_func) (void) = close_stdout;
1093 static void
1094 close_stdout_wrapper (void)
1096 if (closeout_func)
1097 (*closeout_func) ();
1101 main (int argc, char **argv)
1103 int i;
1104 int exit_status;
1106 program_name = argv[0];
1107 setlocale (LC_ALL, "");
1108 bindtextdomain (PACKAGE, LOCALEDIR);
1109 textdomain (PACKAGE);
1111 /* Arrange to close stdout if parse_long_options exits. */
1112 atexit (close_stdout_wrapper);
1114 parse_long_options (argc, argv, PROGRAM_NAME, PACKAGE, VERSION,
1115 AUTHORS, usage);
1117 /* Don't close stdout on exit from here on. */
1118 closeout_func = NULL;
1120 /* Initialize translation table to identity translation. */
1121 for (i = 0; i < 256; i++)
1122 trans_table[i] = i;
1124 /* Decode arguments. */
1125 scanargs (argc, argv);
1127 apply_translations ();
1129 if (input_file != NULL)
1131 if (open_fd (STDIN_FILENO, input_file, O_RDONLY, 0) < 0)
1132 error (1, errno, _("opening %s"), quote (input_file));
1134 else
1135 input_file = _("standard input");
1137 if (output_file != NULL)
1139 mode_t perms = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
1140 int opts
1141 = (O_CREAT
1142 | (seek_records || (conversions_mask & C_NOTRUNC) ? 0 : O_TRUNC));
1144 /* Open the output file with *read* access only if we might
1145 need to read to satisfy a `seek=' request. If we can't read
1146 the file, go ahead with write-only access; it might work. */
1147 if ((! seek_records
1148 || open_fd (STDOUT_FILENO, output_file, O_RDWR | opts, perms) < 0)
1149 && open_fd (STDOUT_FILENO, output_file, O_WRONLY | opts, perms) < 0)
1150 error (1, errno, _("opening %s"), quote (output_file));
1152 #if HAVE_FTRUNCATE
1153 if (seek_records != 0 && !(conversions_mask & C_NOTRUNC))
1155 struct stat stdout_stat;
1156 off_t o = seek_records * output_blocksize;
1157 if (o / output_blocksize != seek_records)
1158 error (1, 0, _("file offset out of range"));
1160 if (fstat (STDOUT_FILENO, &stdout_stat) != 0)
1161 error (1, errno, _("cannot fstat %s"), quote (output_file));
1163 /* Complain only when ftruncate fails on a regular file, a
1164 directory, or a shared memory object, as the 2000-08
1165 POSIX draft specifies ftruncate's behavior only for these
1166 file types. For example, do not complain when Linux 2.4
1167 ftruncate fails on /dev/fd0. */
1168 if (ftruncate (STDOUT_FILENO, o) != 0
1169 && (S_ISREG (stdout_stat.st_mode)
1170 || S_ISDIR (stdout_stat.st_mode)
1171 || S_TYPEISSHM (stdout_stat.st_mode)))
1173 char buf[LONGEST_HUMAN_READABLE + 1];
1174 error (1, errno, _("advancing past %s bytes in output file %s"),
1175 human_readable (o, buf, 1, 1),
1176 quote (output_file));
1179 #endif
1181 else
1183 output_file = _("standard output");
1186 install_handler (SIGINT, interrupt_handler);
1187 install_handler (SIGQUIT, interrupt_handler);
1188 install_handler (SIGPIPE, interrupt_handler);
1189 install_handler (SIGINFO, siginfo_handler);
1191 exit_status = dd_copy ();
1193 quit (exit_status);