1 /* dd -- convert a file while copying it.
2 Copyright (C) 85, 90, 91, 1995-2002 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)
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. */
23 #define SWAB_ALIGN_OFFSET 2
26 # include <inttypes.h>
28 #include <sys/types.h>
35 #include "full-write.h"
36 #include "getpagesize.h"
38 #include "long-options.h"
40 #include "safe-read.h"
43 /* The official name of this program (e.g., no `g' prefix). */
44 #define PROGRAM_NAME "dd"
46 #define AUTHORS N_ ("Paul Rubin, David MacKenzie, and Stuart Kemp")
49 # define SIGINFO SIGUSR1
53 # define S_TYPEISSHM(Stat_ptr) 0
56 #define ROUND_UP_OFFSET(X, M) ((M) - 1 - (((X) + (M) - 1) % (M)))
57 #define PTR_ALIGN(Ptr, M) ((Ptr) \
58 + ROUND_UP_OFFSET ((char *)(Ptr) - (char *)0, (M)))
60 #define max(a, b) ((a) > (b) ? (a) : (b))
61 #define output_char(c) \
65 if (oc >= output_blocksize) \
70 /* Default input and output blocksize. */
71 #define DEFAULT_BLOCKSIZE 512
73 /* Conversions bit masks. */
82 #define C_NOERROR 0400
83 #define C_NOTRUNC 01000
85 /* Use separate input and output buffers, and combine partial input blocks. */
86 #define C_TWOBUFS 04000
88 /* The name this program was run with. */
91 /* The name of the input file, or NULL for the standard input. */
92 static char const *input_file
= NULL
;
94 /* The name of the output file, or NULL for the standard output. */
95 static char const *output_file
= NULL
;
97 /* The number of bytes in which atomic reads are done. */
98 static size_t input_blocksize
= 0;
100 /* The number of bytes in which atomic writes are done. */
101 static size_t output_blocksize
= 0;
103 /* Conversion buffer size, in bytes. 0 prevents conversions. */
104 static size_t conversion_blocksize
= 0;
106 /* Skip this many records of `input_blocksize' bytes before input. */
107 static uintmax_t skip_records
= 0;
109 /* Skip this many records of `output_blocksize' bytes before output. */
110 static uintmax_t seek_records
= 0;
112 /* Copy only this many records. The default is effectively infinity. */
113 static uintmax_t max_records
= (uintmax_t) -1;
115 /* Bit vector of conversions to apply. */
116 static int conversions_mask
= 0;
118 /* If nonzero, filter characters through the translation table. */
119 static int translation_needed
= 0;
121 /* Number of partial blocks written. */
122 static uintmax_t w_partial
= 0;
124 /* Number of full blocks written. */
125 static uintmax_t w_full
= 0;
127 /* Number of partial blocks read. */
128 static uintmax_t r_partial
= 0;
130 /* Number of full blocks read. */
131 static uintmax_t r_full
= 0;
133 /* Records truncated by conv=block. */
134 static uintmax_t r_truncate
= 0;
136 /* Output representation of newline and space characters.
137 They change if we're converting to EBCDIC. */
138 static char newline_character
= '\n';
139 static char space_character
= ' ';
144 /* Current index into `obuf'. */
145 static size_t oc
= 0;
147 /* Index into current line, for `conv=block' and `conv=unblock'. */
148 static size_t col
= 0;
156 static struct conversion conversions
[] =
158 {"ascii", C_ASCII
| C_TWOBUFS
}, /* EBCDIC to ASCII. */
159 {"ebcdic", C_EBCDIC
| C_TWOBUFS
}, /* ASCII to EBCDIC. */
160 {"ibm", C_IBM
| C_TWOBUFS
}, /* Slightly different ASCII to EBCDIC. */
161 {"block", C_BLOCK
| C_TWOBUFS
}, /* Variable to fixed length records. */
162 {"unblock", C_UNBLOCK
| C_TWOBUFS
}, /* Fixed to variable length records. */
163 {"lcase", C_LCASE
| C_TWOBUFS
}, /* Translate upper to lower case. */
164 {"ucase", C_UCASE
| C_TWOBUFS
}, /* Translate lower to upper case. */
165 {"swab", C_SWAB
| C_TWOBUFS
}, /* Swap bytes of input. */
166 {"noerror", C_NOERROR
}, /* Ignore i/o errors. */
167 {"notrunc", C_NOTRUNC
}, /* Do not truncate output file. */
168 {"sync", C_SYNC
}, /* Pad input records to ibs with NULs. */
172 /* Translation table formed by applying successive transformations. */
173 static unsigned char trans_table
[256];
175 static char const ascii_to_ebcdic
[] =
177 '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
178 '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
179 '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
180 '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
181 '\100', '\117', '\177', '\173', '\133', '\154', '\120', '\175',
182 '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
183 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
184 '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
185 '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
186 '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
187 '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
188 '\347', '\350', '\351', '\112', '\340', '\132', '\137', '\155',
189 '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
190 '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
191 '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
192 '\247', '\250', '\251', '\300', '\152', '\320', '\241', '\007',
193 '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
194 '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
195 '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
196 '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
197 '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
198 '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
199 '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
200 '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
201 '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
202 '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',
203 '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',
204 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
205 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
206 '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
207 '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
208 '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
211 static char const ascii_to_ibm
[] =
213 '\000', '\001', '\002', '\003', '\067', '\055', '\056', '\057',
214 '\026', '\005', '\045', '\013', '\014', '\015', '\016', '\017',
215 '\020', '\021', '\022', '\023', '\074', '\075', '\062', '\046',
216 '\030', '\031', '\077', '\047', '\034', '\035', '\036', '\037',
217 '\100', '\132', '\177', '\173', '\133', '\154', '\120', '\175',
218 '\115', '\135', '\134', '\116', '\153', '\140', '\113', '\141',
219 '\360', '\361', '\362', '\363', '\364', '\365', '\366', '\367',
220 '\370', '\371', '\172', '\136', '\114', '\176', '\156', '\157',
221 '\174', '\301', '\302', '\303', '\304', '\305', '\306', '\307',
222 '\310', '\311', '\321', '\322', '\323', '\324', '\325', '\326',
223 '\327', '\330', '\331', '\342', '\343', '\344', '\345', '\346',
224 '\347', '\350', '\351', '\255', '\340', '\275', '\137', '\155',
225 '\171', '\201', '\202', '\203', '\204', '\205', '\206', '\207',
226 '\210', '\211', '\221', '\222', '\223', '\224', '\225', '\226',
227 '\227', '\230', '\231', '\242', '\243', '\244', '\245', '\246',
228 '\247', '\250', '\251', '\300', '\117', '\320', '\241', '\007',
229 '\040', '\041', '\042', '\043', '\044', '\025', '\006', '\027',
230 '\050', '\051', '\052', '\053', '\054', '\011', '\012', '\033',
231 '\060', '\061', '\032', '\063', '\064', '\065', '\066', '\010',
232 '\070', '\071', '\072', '\073', '\004', '\024', '\076', '\341',
233 '\101', '\102', '\103', '\104', '\105', '\106', '\107', '\110',
234 '\111', '\121', '\122', '\123', '\124', '\125', '\126', '\127',
235 '\130', '\131', '\142', '\143', '\144', '\145', '\146', '\147',
236 '\150', '\151', '\160', '\161', '\162', '\163', '\164', '\165',
237 '\166', '\167', '\170', '\200', '\212', '\213', '\214', '\215',
238 '\216', '\217', '\220', '\232', '\233', '\234', '\235', '\236',
239 '\237', '\240', '\252', '\253', '\254', '\255', '\256', '\257',
240 '\260', '\261', '\262', '\263', '\264', '\265', '\266', '\267',
241 '\270', '\271', '\272', '\273', '\274', '\275', '\276', '\277',
242 '\312', '\313', '\314', '\315', '\316', '\317', '\332', '\333',
243 '\334', '\335', '\336', '\337', '\352', '\353', '\354', '\355',
244 '\356', '\357', '\372', '\373', '\374', '\375', '\376', '\377'
247 static char const ebcdic_to_ascii
[] =
249 '\000', '\001', '\002', '\003', '\234', '\011', '\206', '\177',
250 '\227', '\215', '\216', '\013', '\014', '\015', '\016', '\017',
251 '\020', '\021', '\022', '\023', '\235', '\205', '\010', '\207',
252 '\030', '\031', '\222', '\217', '\034', '\035', '\036', '\037',
253 '\200', '\201', '\202', '\203', '\204', '\012', '\027', '\033',
254 '\210', '\211', '\212', '\213', '\214', '\005', '\006', '\007',
255 '\220', '\221', '\026', '\223', '\224', '\225', '\226', '\004',
256 '\230', '\231', '\232', '\233', '\024', '\025', '\236', '\032',
257 '\040', '\240', '\241', '\242', '\243', '\244', '\245', '\246',
258 '\247', '\250', '\133', '\056', '\074', '\050', '\053', '\041',
259 '\046', '\251', '\252', '\253', '\254', '\255', '\256', '\257',
260 '\260', '\261', '\135', '\044', '\052', '\051', '\073', '\136',
261 '\055', '\057', '\262', '\263', '\264', '\265', '\266', '\267',
262 '\270', '\271', '\174', '\054', '\045', '\137', '\076', '\077',
263 '\272', '\273', '\274', '\275', '\276', '\277', '\300', '\301',
264 '\302', '\140', '\072', '\043', '\100', '\047', '\075', '\042',
265 '\303', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
266 '\150', '\151', '\304', '\305', '\306', '\307', '\310', '\311',
267 '\312', '\152', '\153', '\154', '\155', '\156', '\157', '\160',
268 '\161', '\162', '\313', '\314', '\315', '\316', '\317', '\320',
269 '\321', '\176', '\163', '\164', '\165', '\166', '\167', '\170',
270 '\171', '\172', '\322', '\323', '\324', '\325', '\326', '\327',
271 '\330', '\331', '\332', '\333', '\334', '\335', '\336', '\337',
272 '\340', '\341', '\342', '\343', '\344', '\345', '\346', '\347',
273 '\173', '\101', '\102', '\103', '\104', '\105', '\106', '\107',
274 '\110', '\111', '\350', '\351', '\352', '\353', '\354', '\355',
275 '\175', '\112', '\113', '\114', '\115', '\116', '\117', '\120',
276 '\121', '\122', '\356', '\357', '\360', '\361', '\362', '\363',
277 '\134', '\237', '\123', '\124', '\125', '\126', '\127', '\130',
278 '\131', '\132', '\364', '\365', '\366', '\367', '\370', '\371',
279 '\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
280 '\070', '\071', '\372', '\373', '\374', '\375', '\376', '\377'
287 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
291 printf (_("Usage: %s [OPTION]...\n"), program_name
);
293 Copy a file, converting and formatting according to the options.\n\
295 bs=BYTES force ibs=BYTES and obs=BYTES\n\
296 cbs=BYTES convert BYTES bytes at a time\n\
297 conv=KEYWORDS convert the file as per the comma separated keyword list\n\
298 count=BLOCKS copy only BLOCKS input blocks\n\
299 ibs=BYTES read BYTES bytes at a time\n\
302 if=FILE read from FILE instead of stdin\n\
303 obs=BYTES write BYTES bytes at a time\n\
304 of=FILE write to FILE instead of stdout\n\
305 seek=BLOCKS skip BLOCKS obs-sized blocks at start of output\n\
306 skip=BLOCKS skip BLOCKS ibs-sized blocks at start of input\n\
308 fputs (HELP_OPTION_DESCRIPTION
, stdout
);
309 fputs (VERSION_OPTION_DESCRIPTION
, stdout
);
312 BLOCKS and BYTES may be followed by the following multiplicative suffixes:\n\
313 xM M, c 1, w 2, b 512, kB 1000, K 1024, MB 1,000,000, M 1,048,576,\n\
314 GB 1,000,000,000, G 1,073,741,824, and so on for T, P, E, Z, Y.\n\
315 Each KEYWORD may be:\n\
319 ascii from EBCDIC to ASCII\n\
320 ebcdic from ASCII to EBCDIC\n\
321 ibm from ASCII to alternated EBCDIC\n\
322 block pad newline-terminated records with spaces to cbs-size\n\
323 unblock replace trailing spaces in cbs-size records with newline\n\
324 lcase change upper case to lower case\n\
327 notrunc do not truncate the output file\n\
328 ucase change lower case to upper case\n\
329 swab swap every pair of input bytes\n\
330 noerror continue after read errors\n\
331 sync pad every input block with NULs to ibs-size; when used\n\
332 with block or unblock, pad with spaces rather than NULs\n\
334 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
340 translate_charset (char const *new_trans
)
344 for (i
= 0; i
< 256; i
++)
345 trans_table
[i
] = new_trans
[trans_table
[i
]];
346 translation_needed
= 1;
349 /* Return the number of 1 bits in `i'. */
352 bit_count (register int i
)
354 register int set_bits
;
356 for (set_bits
= 0; i
!= 0; set_bits
++)
364 char buf
[2][LONGEST_HUMAN_READABLE
+ 1];
365 fprintf (stderr
, _("%s+%s records in\n"),
366 human_readable (r_full
, buf
[0], 1, 1),
367 human_readable (r_partial
, buf
[1], 1, 1));
368 fprintf (stderr
, _("%s+%s records out\n"),
369 human_readable (w_full
, buf
[0], 1, 1),
370 human_readable (w_partial
, buf
[1], 1, 1));
373 fprintf (stderr
, "%s %s\n",
374 human_readable (r_truncate
, buf
[0], 1, 1),
376 ? _("truncated record")
377 : _("truncated records")));
385 if (close (STDIN_FILENO
) < 0)
386 error (1, errno
, _("closing input file %s"), quote (input_file
));
387 if (close (STDOUT_FILENO
) < 0)
388 error (1, errno
, _("closing output file %s"), quote (output_file
));
399 interrupt_handler (int sig
)
402 struct sigaction sigact
;
404 sigact
.sa_handler
= SIG_DFL
;
405 sigemptyset (&sigact
.sa_mask
);
407 sigaction (sig
, &sigact
, NULL
);
409 signal (sig
, SIG_DFL
);
412 kill (getpid (), sig
);
416 siginfo_handler (int sig ATTRIBUTE_UNUSED
)
421 /* Encapsulate portability mess of establishing signal handlers. */
424 install_handler (int sig_num
, RETSIGTYPE (*sig_handler
) (int sig
))
427 struct sigaction sigact
;
428 sigaction (sig_num
, NULL
, &sigact
);
429 if (sigact
.sa_handler
!= SIG_IGN
)
431 sigact
.sa_handler
= sig_handler
;
432 sigemptyset (&sigact
.sa_mask
);
434 sigaction (sig_num
, &sigact
, NULL
);
437 if (signal (sig_num
, SIG_IGN
) != SIG_IGN
)
438 signal (sig_num
, sig_handler
);
442 /* Open a file to a particular file descriptor. This is like standard
443 `open', except it always returns DESIRED_FD if successful. */
445 open_fd (int desired_fd
, char const *filename
, int options
, mode_t mode
)
449 fd
= open (filename
, options
, mode
);
453 if (fd
!= desired_fd
)
455 if (dup2 (fd
, desired_fd
) != desired_fd
)
464 /* Write, then empty, the output buffer `obuf'. */
469 size_t nwritten
= full_write (STDOUT_FILENO
, obuf
, output_blocksize
);
470 if (nwritten
!= output_blocksize
)
472 error (0, errno
, _("writing to %s"), quote (output_file
));
482 /* Interpret one "conv=..." option.
483 As a by product, this function replaces each `,' in STR with a NUL byte. */
486 parse_conversion (char *str
)
493 new = strchr (str
, ',');
496 for (i
= 0; conversions
[i
].convname
!= NULL
; i
++)
497 if (STREQ (conversions
[i
].convname
, str
))
499 conversions_mask
|= conversions
[i
].conversion
;
502 if (conversions
[i
].convname
== NULL
)
504 error (0, 0, _("invalid conversion: %s"), quote (str
));
508 } while (new != NULL
);
511 /* Return the value of STR, interpreted as a non-negative decimal integer,
512 optionally multiplied by various values.
513 Assign nonzero to *INVALID if STR does not represent a number in
517 parse_integer (const char *str
, int *invalid
)
521 enum strtol_error e
= xstrtoumax (str
, &suffix
, 10, &n
, "bcEGkKMPTwYZ0");
523 if (e
== LONGINT_INVALID_SUFFIX_CHAR
&& *suffix
== 'x')
525 uintmax_t multiplier
= parse_integer (suffix
+ 1, invalid
);
527 if (multiplier
!= 0 && n
* multiplier
/ multiplier
!= n
)
535 else if (e
!= LONGINT_OK
)
545 scanargs (int argc
, char **argv
)
552 for (i
= optind
; i
< argc
; i
++)
557 val
= strchr (name
, '=');
560 error (0, 0, _("unrecognized option %s"), quote (name
));
565 if (STREQ (name
, "if"))
567 else if (STREQ (name
, "of"))
569 else if (STREQ (name
, "conv"))
570 parse_conversion (val
);
574 uintmax_t n
= parse_integer (val
, &invalid
);
576 if (STREQ (name
, "ibs"))
579 invalid
|= input_blocksize
!= n
|| input_blocksize
== 0;
580 conversions_mask
|= C_TWOBUFS
;
582 else if (STREQ (name
, "obs"))
584 output_blocksize
= n
;
585 invalid
|= output_blocksize
!= n
|| output_blocksize
== 0;
586 conversions_mask
|= C_TWOBUFS
;
588 else if (STREQ (name
, "bs"))
590 output_blocksize
= input_blocksize
= n
;
591 invalid
|= output_blocksize
!= n
|| output_blocksize
== 0;
593 else if (STREQ (name
, "cbs"))
595 conversion_blocksize
= n
;
596 invalid
|= (conversion_blocksize
!= n
597 || conversion_blocksize
== 0);
599 else if (STREQ (name
, "skip"))
601 else if (STREQ (name
, "seek"))
603 else if (STREQ (name
, "count"))
607 error (0, 0, _("unrecognized option %s=%s"),
608 quote_n (0, name
), quote_n (1, val
));
613 error (1, 0, _("invalid number %s"), quote (val
));
617 /* If bs= was given, both `input_blocksize' and `output_blocksize' will
618 have been set to positive values. If either has not been set,
619 bs= was not given, so make sure two buffers are used. */
620 if (input_blocksize
== 0 || output_blocksize
== 0)
621 conversions_mask
|= C_TWOBUFS
;
622 if (input_blocksize
== 0)
623 input_blocksize
= DEFAULT_BLOCKSIZE
;
624 if (output_blocksize
== 0)
625 output_blocksize
= DEFAULT_BLOCKSIZE
;
626 if (conversion_blocksize
== 0)
627 conversions_mask
&= ~(C_BLOCK
| C_UNBLOCK
);
630 /* Fix up translation table. */
633 apply_translations (void)
637 #define MX(a) (bit_count (conversions_mask & (a)))
638 if ((MX (C_ASCII
| C_EBCDIC
| C_IBM
) > 1)
639 || (MX (C_BLOCK
| C_UNBLOCK
) > 1)
640 || (MX (C_LCASE
| C_UCASE
) > 1)
641 || (MX (C_UNBLOCK
| C_SYNC
) > 1))
644 only one conv in {ascii,ebcdic,ibm}, {lcase,ucase}, {block,unblock}, {unblock,sync}"));
648 if (conversions_mask
& C_ASCII
)
649 translate_charset (ebcdic_to_ascii
);
651 if (conversions_mask
& C_UCASE
)
653 for (i
= 0; i
< 256; i
++)
654 if (ISLOWER (trans_table
[i
]))
655 trans_table
[i
] = TOUPPER (trans_table
[i
]);
656 translation_needed
= 1;
658 else if (conversions_mask
& C_LCASE
)
660 for (i
= 0; i
< 256; i
++)
661 if (ISUPPER (trans_table
[i
]))
662 trans_table
[i
] = TOLOWER (trans_table
[i
]);
663 translation_needed
= 1;
666 if (conversions_mask
& C_EBCDIC
)
668 translate_charset (ascii_to_ebcdic
);
669 newline_character
= ascii_to_ebcdic
['\n'];
670 space_character
= ascii_to_ebcdic
[' '];
672 else if (conversions_mask
& C_IBM
)
674 translate_charset (ascii_to_ibm
);
675 newline_character
= ascii_to_ibm
['\n'];
676 space_character
= ascii_to_ibm
[' '];
680 /* Apply the character-set translations specified by the user
681 to the NREAD bytes in BUF. */
684 translate_buffer (char *buf
, size_t nread
)
689 for (i
= nread
, cp
= buf
; i
; i
--, cp
++)
690 *cp
= trans_table
[(unsigned char) *cp
];
693 /* If nonnzero, the last char from the previous call to `swab_buffer'
694 is saved in `saved_char'. */
695 static int char_is_saved
= 0;
697 /* Odd char from previous call. */
698 static char saved_char
;
700 /* Swap NREAD bytes in BUF, plus possibly an initial char from the
701 previous call. If NREAD is odd, save the last char for the
702 next call. Return the new start of the BUF buffer. */
705 swab_buffer (char *buf
, size_t *nread
)
707 char *bufstart
= buf
;
711 /* Is a char left from last time? */
714 *--bufstart
= saved_char
;
721 /* An odd number of chars are in the buffer. */
722 saved_char
= bufstart
[--*nread
];
726 /* Do the byte-swapping by moving every second character two
727 positions toward the end, working from the end of the buffer
728 toward the beginning. This way we only move half of the data. */
730 cp
= bufstart
+ *nread
; /* Start one char past the last. */
731 for (i
= *nread
/ 2; i
; i
--, cp
-= 2)
737 /* This is a wrapper for lseek. It detects and warns about a kernel
738 bug that makes lseek a no-op for tape devices, even though the kernel
739 lseek return value suggests that the function succeeded.
741 The parameters are the same as those of the lseek function, but
742 with the addition of FILENAME, the name of the file associated with
743 descriptor FDESC. The file name is used solely in the warning that's
744 printed when the bug is detected. Return the same value that lseek
745 would have returned, but when the lseek bug is detected, return -1
746 to indicate that lseek failed.
748 The offending behavior has been confirmed with an Exabyte SCSI tape
749 drive accessed via /dev/nst0 on both Linux-2.2.17 and Linux-2.4.16. */
753 # include <sys/mtio.h>
755 # define MT_SAME_POSITION(P, Q) \
756 ((P).mt_resid == (Q).mt_resid \
757 && (P).mt_fileno == (Q).mt_fileno \
758 && (P).mt_blkno == (Q).mt_blkno)
761 skip_via_lseek (char const *filename
, int fdesc
, off_t offset
, int whence
)
766 int got_original_tape_position
;
768 got_original_tape_position
= (ioctl (fdesc
, MTIOCGET
, &s1
) == 0);
769 /* known bad device type */
770 /* && s.mt_type == MT_ISSCSI2 */
772 new_position
= lseek (fdesc
, offset
, whence
);
773 if (0 <= new_position
774 && got_original_tape_position
775 && ioctl (fdesc
, MTIOCGET
, &s2
) == 0
776 && MT_SAME_POSITION (s1
, s2
))
778 error (0, 0, _("warning: working around lseek kernel bug for file (%s)\n\
779 of mt_type=0x%0lx -- see <sys/mtio.h> for the list of types"),
780 filename
, s2
.mt_type
);
787 # define skip_via_lseek(Filename, Fd, Offset, Whence) lseek (Fd, Offset, Whence)
790 /* Throw away RECORDS blocks of BLOCKSIZE bytes on file descriptor FDESC,
791 which is open with read permission for FILE. Store up to BLOCKSIZE
792 bytes of the data at a time in BUF, if necessary. RECORDS must be
796 skip (int fdesc
, char const *file
, uintmax_t records
, size_t blocksize
,
799 off_t offset
= records
* blocksize
;
801 /* Try lseek and if an error indicates it was an inappropriate
802 operation, fall back on using read. */
804 if (offset
/ blocksize
!= records
805 || skip_via_lseek (file
, fdesc
, offset
, SEEK_CUR
) < 0)
809 ssize_t nread
= safe_read (fdesc
, buf
, blocksize
);
812 error (0, errno
, _("reading %s"), quote (file
));
815 /* POSIX doesn't say what to do when dd detects it has been
816 asked to skip past EOF, so I assume it's non-fatal.
817 FIXME: maybe give a warning. */
824 /* Copy NREAD bytes of BUF, with no conversions. */
827 copy_simple (char const *buf
, int nread
)
829 int nfree
; /* Number of unused bytes in `obuf'. */
830 const char *start
= buf
; /* First uncopied char in BUF. */
834 nfree
= output_blocksize
- oc
;
838 memcpy (obuf
+ oc
, start
, nfree
);
840 nread
-= nfree
; /* Update the number of bytes left to copy. */
843 if (oc
>= output_blocksize
)
849 /* Copy NREAD bytes of BUF, doing conv=block
850 (pad newline-terminated records to `conversion_blocksize',
851 replacing the newline with trailing spaces). */
854 copy_with_block (char const *buf
, size_t nread
)
858 for (i
= nread
; i
; i
--, buf
++)
860 if (*buf
== newline_character
)
862 if (col
< conversion_blocksize
)
865 for (j
= col
; j
< conversion_blocksize
; j
++)
866 output_char (space_character
);
872 if (col
== conversion_blocksize
)
874 else if (col
< conversion_blocksize
)
881 /* Copy NREAD bytes of BUF, doing conv=unblock
882 (replace trailing spaces in `conversion_blocksize'-sized records
886 copy_with_unblock (char const *buf
, size_t nread
)
890 static int pending_spaces
= 0;
892 for (i
= 0; i
< nread
; i
++)
896 if (col
++ >= conversion_blocksize
)
898 col
= pending_spaces
= 0; /* Wipe out any pending spaces. */
899 i
--; /* Push the char back; get it later. */
900 output_char (newline_character
);
902 else if (c
== space_character
)
906 /* `c' is the character after a run of spaces that were not
907 at the end of the conversion buffer. Output them. */
908 while (pending_spaces
)
910 output_char (space_character
);
923 char *ibuf
, *bufstart
; /* Input buffer. */
924 char *real_buf
; /* real buffer address before alignment */
926 ssize_t nread
; /* Bytes read in the current block. */
928 size_t page_size
= getpagesize ();
931 /* Leave at least one extra byte at the beginning and end of `ibuf'
932 for conv=swab, but keep the buffer address even. But some peculiar
933 device drivers work only with word-aligned buffers, so leave an
936 /* Some devices require alignment on a sector or page boundary
937 (e.g. character disk devices). Align the input buffer to a
938 page boundary to cover all bases. Note that due to the swab
939 algorithm, we must have at least one byte in the page before
940 the input buffer; thus we allocate 2 pages of slop in the
941 real buffer. 8k above the blocksize shouldn't bother anyone.
943 The page alignment is necessary on any linux system that supports
944 either the SGI raw I/O patch or Steven Tweedies raw I/O patch.
945 It is necessary when accessing raw (i.e. character special) disk
946 devices on Unixware or other SVR4-derived system. */
948 real_buf
= xmalloc (input_blocksize
949 + 2 * SWAB_ALIGN_OFFSET
950 + 2 * page_size
- 1);
952 ibuf
+= SWAB_ALIGN_OFFSET
; /* allow space for swab */
954 ibuf
= PTR_ALIGN (ibuf
, page_size
);
956 if (conversions_mask
& C_TWOBUFS
)
958 /* Page-align the output buffer, too. */
959 real_obuf
= xmalloc (output_blocksize
+ page_size
- 1);
960 obuf
= PTR_ALIGN (real_obuf
, page_size
);
968 if (skip_records
!= 0)
969 skip (STDIN_FILENO
, input_file
, skip_records
, input_blocksize
, ibuf
);
971 if (seek_records
!= 0)
973 /* FIXME: this loses for
974 % ./dd if=dd seek=1 |:
975 ./dd: standard output: Bad file descriptor
980 skip (STDOUT_FILENO
, output_file
, seek_records
, output_blocksize
, obuf
);
983 if (max_records
== 0)
988 if (r_partial
+ r_full
>= max_records
)
991 /* Zero the buffer before reading, so that if we get a read error,
992 whatever data we are able to read is followed by zeros.
993 This minimizes data loss. */
994 if ((conversions_mask
& C_SYNC
) && (conversions_mask
& C_NOERROR
))
996 (conversions_mask
& (C_BLOCK
| C_UNBLOCK
)) ? ' ' : '\0',
999 nread
= safe_read (STDIN_FILENO
, ibuf
, input_blocksize
);
1006 error (0, errno
, _("reading %s"), quote (input_file
));
1007 if (conversions_mask
& C_NOERROR
)
1010 /* Seek past the bad block if possible. */
1011 lseek (STDIN_FILENO
, (off_t
) input_blocksize
, SEEK_CUR
);
1012 if (conversions_mask
& C_SYNC
)
1013 /* Replace the missing input with null bytes and
1014 proceed normally. */
1021 /* Write any partial block. */
1027 n_bytes_read
= nread
;
1029 if (n_bytes_read
< input_blocksize
)
1032 if (conversions_mask
& C_SYNC
)
1034 if (!(conversions_mask
& C_NOERROR
))
1035 /* If C_NOERROR, we zeroed the block before reading. */
1036 memset (ibuf
+ n_bytes_read
,
1037 (conversions_mask
& (C_BLOCK
| C_UNBLOCK
)) ? ' ' : '\0',
1038 input_blocksize
- n_bytes_read
);
1039 n_bytes_read
= input_blocksize
;
1045 if (ibuf
== obuf
) /* If not C_TWOBUFS. */
1047 size_t nwritten
= full_write (STDOUT_FILENO
, obuf
, n_bytes_read
);
1048 if (nwritten
!= n_bytes_read
)
1050 error (0, errno
, _("writing %s"), quote (output_file
));
1053 else if (n_bytes_read
== input_blocksize
)
1060 /* Do any translations on the whole buffer at once. */
1062 if (translation_needed
)
1063 translate_buffer (ibuf
, n_bytes_read
);
1065 if (conversions_mask
& C_SWAB
)
1066 bufstart
= swab_buffer (ibuf
, &n_bytes_read
);
1070 if (conversions_mask
& C_BLOCK
)
1071 copy_with_block (bufstart
, n_bytes_read
);
1072 else if (conversions_mask
& C_UNBLOCK
)
1073 copy_with_unblock (bufstart
, n_bytes_read
);
1075 copy_simple (bufstart
, n_bytes_read
);
1078 /* If we have a char left as a result of conv=swab, output it. */
1081 if (conversions_mask
& C_BLOCK
)
1082 copy_with_block (&saved_char
, 1);
1083 else if (conversions_mask
& C_UNBLOCK
)
1084 copy_with_unblock (&saved_char
, 1);
1086 output_char (saved_char
);
1089 if ((conversions_mask
& C_BLOCK
) && col
> 0)
1091 /* If the final input line didn't end with a '\n', pad
1092 the output block to `conversion_blocksize' chars. */
1094 for (i
= col
; i
< conversion_blocksize
; i
++)
1095 output_char (space_character
);
1098 if ((conversions_mask
& C_UNBLOCK
) && col
== conversion_blocksize
)
1099 /* Add a final '\n' if there are exactly `conversion_blocksize'
1100 characters in the final record. */
1101 output_char (newline_character
);
1103 /* Write out the last block. */
1106 size_t nwritten
= full_write (STDOUT_FILENO
, obuf
, oc
);
1111 error (0, errno
, _("writing %s"), quote (output_file
));
1123 /* This is gross, but necessary, because of the way close_stdout
1124 works and because this program closes STDOUT_FILENO directly. */
1125 static void (*closeout_func
) (void) = close_stdout
;
1128 close_stdout_wrapper (void)
1131 (*closeout_func
) ();
1135 main (int argc
, char **argv
)
1140 program_name
= argv
[0];
1141 setlocale (LC_ALL
, "");
1142 bindtextdomain (PACKAGE
, LOCALEDIR
);
1143 textdomain (PACKAGE
);
1145 /* Arrange to close stdout if parse_long_options exits. */
1146 atexit (close_stdout_wrapper
);
1148 parse_long_options (argc
, argv
, PROGRAM_NAME
, PACKAGE
, VERSION
,
1151 /* Don't close stdout on exit from here on. */
1152 closeout_func
= NULL
;
1154 /* Initialize translation table to identity translation. */
1155 for (i
= 0; i
< 256; i
++)
1158 /* Decode arguments. */
1159 scanargs (argc
, argv
);
1161 apply_translations ();
1163 if (input_file
!= NULL
)
1165 if (open_fd (STDIN_FILENO
, input_file
, O_RDONLY
, 0) < 0)
1166 error (1, errno
, _("opening %s"), quote (input_file
));
1169 input_file
= _("standard input");
1171 if (output_file
!= NULL
)
1173 mode_t perms
= S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IWGRP
| S_IROTH
| S_IWOTH
;
1176 | (seek_records
|| (conversions_mask
& C_NOTRUNC
) ? 0 : O_TRUNC
));
1178 /* Open the output file with *read* access only if we might
1179 need to read to satisfy a `seek=' request. If we can't read
1180 the file, go ahead with write-only access; it might work. */
1182 || open_fd (STDOUT_FILENO
, output_file
, O_RDWR
| opts
, perms
) < 0)
1183 && open_fd (STDOUT_FILENO
, output_file
, O_WRONLY
| opts
, perms
) < 0)
1184 error (1, errno
, _("opening %s"), quote (output_file
));
1187 if (seek_records
!= 0 && !(conversions_mask
& C_NOTRUNC
))
1189 struct stat stdout_stat
;
1190 off_t o
= seek_records
* output_blocksize
;
1191 if (o
/ output_blocksize
!= seek_records
)
1192 error (1, 0, _("file offset out of range"));
1194 if (fstat (STDOUT_FILENO
, &stdout_stat
) != 0)
1195 error (1, errno
, _("cannot fstat %s"), quote (output_file
));
1197 /* Complain only when ftruncate fails on a regular file, a
1198 directory, or a shared memory object, as the 2000-08
1199 POSIX draft specifies ftruncate's behavior only for these
1200 file types. For example, do not complain when Linux 2.4
1201 ftruncate fails on /dev/fd0. */
1202 if (ftruncate (STDOUT_FILENO
, o
) != 0
1203 && (S_ISREG (stdout_stat
.st_mode
)
1204 || S_ISDIR (stdout_stat
.st_mode
)
1205 || S_TYPEISSHM (&stdout_stat
)))
1207 char buf
[LONGEST_HUMAN_READABLE
+ 1];
1208 error (1, errno
, _("advancing past %s bytes in output file %s"),
1209 human_readable (o
, buf
, 1, 1),
1210 quote (output_file
));
1217 output_file
= _("standard output");
1220 install_handler (SIGINT
, interrupt_handler
);
1221 install_handler (SIGQUIT
, interrupt_handler
);
1222 install_handler (SIGPIPE
, interrupt_handler
);
1223 install_handler (SIGINFO
, siginfo_handler
);
1225 exit_status
= dd_copy ();