blob: ce8bcc6a55c938bfd6f5d1e88eb6cfee73907a18 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersencc8ed391999-10-05 16:24:54 +00002/*
Eric Andersenc4996011999-10-20 22:08:37 +00003 * Mini dd implementation for busybox
4 *
Eric Andersenc4996011999-10-20 22:08:37 +00005 *
Matt Kraai3eeab3b2001-12-07 16:27:37 +00006 * Copyright (C) 2000,2001 Matt Kraai
Eric Andersenc4996011999-10-20 22:08:37 +00007 *
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersencc8ed391999-10-05 16:24:54 +00009 */
10
Matt Kraai24ac0172000-12-18 21:38:57 +000011#include <sys/types.h>
Matt Kraaieb834782002-02-05 15:28:54 +000012#include <sys/stat.h>
Eric Andersened3ef502001-01-27 08:24:39 +000013#include <stdlib.h>
14#include <stdio.h>
15#include <unistd.h>
16#include <string.h>
Eric Andersen3cf52d11999-10-12 22:26:06 +000017#include <fcntl.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000018#include "busybox.h"
19
Eric Andersencc8ed391999-10-05 16:24:54 +000020
Matt Kraaia164c642001-02-05 17:50:03 +000021static const struct suffix_mult dd_suffixes[] = {
Matt Kraai24ac0172000-12-18 21:38:57 +000022 { "c", 1 },
23 { "w", 2 },
24 { "b", 512 },
25 { "kD", 1000 },
26 { "k", 1024 },
27 { "MD", 1000000 },
28 { "M", 1048576 },
29 { "GD", 1000000000 },
30 { "G", 1073741824 },
31 { NULL, 0 }
32};
33
34int dd_main(int argc, char **argv)
Eric Andersen3cf52d11999-10-12 22:26:06 +000035{
Glenn L McGratheaed78a2002-11-28 11:05:28 +000036 size_t out_full = 0;
37 size_t out_part = 0;
38 size_t in_full = 0;
39 size_t in_part = 0;
40 size_t count = -1;
41 size_t bs = 512;
Matt Kraai24ac0172000-12-18 21:38:57 +000042 ssize_t n;
Glenn L McGratheaed78a2002-11-28 11:05:28 +000043 off_t seek = 0;
44 off_t skip = 0;
45 int sync_flag = FALSE;
46 int noerror = FALSE;
Eric Andersenab26cc32004-01-30 22:24:32 +000047 int trunc_flag = TRUE;
Glenn L McGratheaed78a2002-11-28 11:05:28 +000048 int oflag;
49 int ifd;
50 int ofd;
51 int i;
Manuel Novoa III cad53642003-03-19 09:13:01 +000052 const char *infile = NULL;
53 const char *outfile = NULL;
Glenn L McGratheaed78a2002-11-28 11:05:28 +000054 char *buf;
Eric Andersenddea3682000-11-29 22:33:02 +000055
Matt Kraai24ac0172000-12-18 21:38:57 +000056 for (i = 1; i < argc; i++) {
57 if (strncmp("bs=", argv[i], 3) == 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000058 bs = bb_xparse_number(argv[i]+3, dd_suffixes);
Matt Kraai24ac0172000-12-18 21:38:57 +000059 else if (strncmp("count=", argv[i], 6) == 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000060 count = bb_xparse_number(argv[i]+6, dd_suffixes);
Matt Kraai24ac0172000-12-18 21:38:57 +000061 else if (strncmp("seek=", argv[i], 5) == 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000062 seek = bb_xparse_number(argv[i]+5, dd_suffixes);
Matt Kraai24ac0172000-12-18 21:38:57 +000063 else if (strncmp("skip=", argv[i], 5) == 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000064 skip = bb_xparse_number(argv[i]+5, dd_suffixes);
Matt Kraai24ac0172000-12-18 21:38:57 +000065 else if (strncmp("if=", argv[i], 3) == 0)
66 infile = argv[i]+3;
67 else if (strncmp("of=", argv[i], 3) == 0)
68 outfile = argv[i]+3;
69 else if (strncmp("conv=", argv[i], 5) == 0) {
70 buf = argv[i]+5;
71 while (1) {
72 if (strncmp("notrunc", buf, 7) == 0) {
Eric Andersenab26cc32004-01-30 22:24:32 +000073 trunc_flag = FALSE;
Matt Kraai24ac0172000-12-18 21:38:57 +000074 buf += 7;
75 } else if (strncmp("sync", buf, 4) == 0) {
Eric Andersen1ca20a72001-03-21 07:34:27 +000076 sync_flag = TRUE;
Matt Kraai24ac0172000-12-18 21:38:57 +000077 buf += 4;
Eric Andersenef38b392002-04-27 01:31:43 +000078 } else if (strncmp("noerror", buf, 7) == 0) {
79 noerror = TRUE;
80 buf += 7;
Matt Kraai24ac0172000-12-18 21:38:57 +000081 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +000082 bb_error_msg_and_die("invalid conversion `%s'", argv[i]+5);
Matt Kraai24ac0172000-12-18 21:38:57 +000083 }
84 if (buf[0] == '\0')
85 break;
86 if (buf[0] == ',')
87 buf++;
Erik Andersene49d5ec2000-02-08 19:58:47 +000088 }
Matt Kraai24ac0172000-12-18 21:38:57 +000089 } else
Manuel Novoa III cad53642003-03-19 09:13:01 +000090 bb_show_usage();
Eric Andersen3cf52d11999-10-12 22:26:06 +000091 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000092
Matt Kraai24ac0172000-12-18 21:38:57 +000093 buf = xmalloc(bs);
Erik Andersene49d5ec2000-02-08 19:58:47 +000094
Matt Kraai24ac0172000-12-18 21:38:57 +000095 if (infile != NULL) {
Eric Andersen8876fb22003-06-20 09:01:58 +000096 ifd = bb_xopen(infile, O_RDONLY);
Matt Kraai24ac0172000-12-18 21:38:57 +000097 } else {
98 ifd = STDIN_FILENO;
Manuel Novoa III cad53642003-03-19 09:13:01 +000099 infile = bb_msg_standard_input;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000100 }
101
Matt Kraai24ac0172000-12-18 21:38:57 +0000102 if (outfile != NULL) {
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000103 oflag = O_WRONLY | O_CREAT;
104
Eric Andersenab26cc32004-01-30 22:24:32 +0000105 if (!seek && trunc_flag) {
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000106 oflag |= O_TRUNC;
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000107 }
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000108
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +0000109 ofd = bb_xopen3(outfile, oflag, 0666);
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000110
Eric Andersenab26cc32004-01-30 22:24:32 +0000111 if (seek && trunc_flag) {
Matt Kraaieb834782002-02-05 15:28:54 +0000112 if (ftruncate(ofd, seek * bs) < 0) {
113 struct stat st;
114
115 if (fstat (ofd, &st) < 0 || S_ISREG (st.st_mode) ||
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000116 S_ISDIR (st.st_mode)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000117 bb_perror_msg_and_die("%s", outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000118 }
Matt Kraaieb834782002-02-05 15:28:54 +0000119 }
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000120 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000121 } else {
122 ofd = STDOUT_FILENO;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000123 outfile = bb_msg_standard_output;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000124 }
125
Matt Kraai24ac0172000-12-18 21:38:57 +0000126 if (skip) {
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000127 if (lseek(ifd, skip * bs, SEEK_CUR) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000128 bb_perror_msg_and_die("%s", infile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000129 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000130 }
Eric Andersenddea3682000-11-29 22:33:02 +0000131
Matt Kraai24ac0172000-12-18 21:38:57 +0000132 if (seek) {
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000133 if (lseek(ofd, seek * bs, SEEK_CUR) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000134 bb_perror_msg_and_die("%s", outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000135 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000136 }
Eric Andersenddea3682000-11-29 22:33:02 +0000137
Matt Kraai24ac0172000-12-18 21:38:57 +0000138 while (in_full + in_part != count) {
Eric Andersenef38b392002-04-27 01:31:43 +0000139 if (noerror) {
140 /* Pre-zero the buffer when doing the noerror thing */
141 memset(buf, '\0', bs);
142 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000143 n = safe_read(ifd, buf, bs);
Eric Andersenef38b392002-04-27 01:31:43 +0000144 if (n < 0) {
145 if (noerror) {
146 n = bs;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000147 bb_perror_msg("%s", infile);
Eric Andersenef38b392002-04-27 01:31:43 +0000148 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000149 bb_perror_msg_and_die("%s", infile);
Eric Andersenef38b392002-04-27 01:31:43 +0000150 }
151 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000152 if (n == 0) {
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000153 break;
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000154 }
"Vladimir N. Oleynik"57545c82006-01-31 12:06:57 +0000155 if ((size_t)n == bs) {
Matt Kraai24ac0172000-12-18 21:38:57 +0000156 in_full++;
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000157 } else {
Matt Kraai24ac0172000-12-18 21:38:57 +0000158 in_part++;
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000159 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000160 if (sync_flag) {
Matt Kraai24ac0172000-12-18 21:38:57 +0000161 memset(buf + n, '\0', bs - n);
162 n = bs;
Eric Andersenddea3682000-11-29 22:33:02 +0000163 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000164 n = bb_full_write(ofd, buf, n);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000165 if (n < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000166 bb_perror_msg_and_die("%s", outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000167 }
"Vladimir N. Oleynik"57545c82006-01-31 12:06:57 +0000168 if ((size_t)n == bs) {
Matt Kraai24ac0172000-12-18 21:38:57 +0000169 out_full++;
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000170 } else {
Matt Kraai24ac0172000-12-18 21:38:57 +0000171 out_part++;
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000172 }
Glenn L McGrathf0b073f2000-09-11 00:32:13 +0000173 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000174
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000175 if (close (ifd) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000176 bb_perror_msg_and_die("%s", infile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000177 }
Matt Kraai3eeab3b2001-12-07 16:27:37 +0000178
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000179 if (close (ofd) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000180 bb_perror_msg_and_die("%s", outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000181 }
Matt Kraai3eeab3b2001-12-07 16:27:37 +0000182
Manuel Novoa III cad53642003-03-19 09:13:01 +0000183 fprintf(stderr, "%ld+%ld records in\n%ld+%ld records out\n",
184 (long)in_full, (long)in_part,
185 (long)out_full, (long)out_part);
Matt Kraai24ac0172000-12-18 21:38:57 +0000186
Matt Kraai3e856ce2000-12-01 02:55:13 +0000187 return EXIT_SUCCESS;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000188}