blob: 378e212dedcd3d863e22f1fb21ca35fc1fcfa0a0 [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>
Rob Landleyc5598172006-05-02 22:44:04 +000018#include <signal.h> // For FEATURE_DD_SIGNAL_HANDLING
Eric Andersencbe31da2001-02-20 06:14:08 +000019#include "busybox.h"
20
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
Rob Landleyc5598172006-05-02 22:44:04 +000034static size_t out_full;
35static size_t out_part;
36static size_t in_full;
37static size_t in_part;
38
39static void dd_output_status(int cur_signal)
40{
41 fprintf(stderr, "%ld+%ld records in\n%ld+%ld records out\n",
42 (long)in_full, (long)in_part,
43 (long)out_full, (long)out_part);
44}
45
Matt Kraai24ac0172000-12-18 21:38:57 +000046int dd_main(int argc, char **argv)
Eric Andersen3cf52d11999-10-12 22:26:06 +000047{
Glenn L McGratheaed78a2002-11-28 11:05:28 +000048 size_t count = -1;
49 size_t bs = 512;
Matt Kraai24ac0172000-12-18 21:38:57 +000050 ssize_t n;
Glenn L McGratheaed78a2002-11-28 11:05:28 +000051 off_t seek = 0;
52 off_t skip = 0;
53 int sync_flag = FALSE;
54 int noerror = FALSE;
Eric Andersenab26cc32004-01-30 22:24:32 +000055 int trunc_flag = TRUE;
Glenn L McGratheaed78a2002-11-28 11:05:28 +000056 int oflag;
57 int ifd;
58 int ofd;
59 int i;
Manuel Novoa III cad53642003-03-19 09:13:01 +000060 const char *infile = NULL;
61 const char *outfile = NULL;
Glenn L McGratheaed78a2002-11-28 11:05:28 +000062 char *buf;
Eric Andersenddea3682000-11-29 22:33:02 +000063
Rob Landleyc5598172006-05-02 22:44:04 +000064 if (ENABLE_FEATURE_DD_SIGNAL_HANDLING)
65 {
66 struct sigaction sa;
67
68 memset(&sa, 0, sizeof(sa));
69 sa.sa_handler = dd_output_status;
70 sa.sa_flags = SA_RESTART;
71 sigemptyset(&sa.sa_mask);
72 sigaction(SIGUSR1, &sa, 0);
73 }
74
Matt Kraai24ac0172000-12-18 21:38:57 +000075 for (i = 1; i < argc; i++) {
76 if (strncmp("bs=", argv[i], 3) == 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000077 bs = bb_xparse_number(argv[i]+3, dd_suffixes);
Matt Kraai24ac0172000-12-18 21:38:57 +000078 else if (strncmp("count=", argv[i], 6) == 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000079 count = bb_xparse_number(argv[i]+6, dd_suffixes);
Matt Kraai24ac0172000-12-18 21:38:57 +000080 else if (strncmp("seek=", argv[i], 5) == 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000081 seek = bb_xparse_number(argv[i]+5, dd_suffixes);
Matt Kraai24ac0172000-12-18 21:38:57 +000082 else if (strncmp("skip=", argv[i], 5) == 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000083 skip = bb_xparse_number(argv[i]+5, dd_suffixes);
Matt Kraai24ac0172000-12-18 21:38:57 +000084 else if (strncmp("if=", argv[i], 3) == 0)
85 infile = argv[i]+3;
86 else if (strncmp("of=", argv[i], 3) == 0)
87 outfile = argv[i]+3;
88 else if (strncmp("conv=", argv[i], 5) == 0) {
89 buf = argv[i]+5;
90 while (1) {
91 if (strncmp("notrunc", buf, 7) == 0) {
Eric Andersenab26cc32004-01-30 22:24:32 +000092 trunc_flag = FALSE;
Matt Kraai24ac0172000-12-18 21:38:57 +000093 buf += 7;
94 } else if (strncmp("sync", buf, 4) == 0) {
Eric Andersen1ca20a72001-03-21 07:34:27 +000095 sync_flag = TRUE;
Matt Kraai24ac0172000-12-18 21:38:57 +000096 buf += 4;
Eric Andersenef38b392002-04-27 01:31:43 +000097 } else if (strncmp("noerror", buf, 7) == 0) {
98 noerror = TRUE;
99 buf += 7;
Matt Kraai24ac0172000-12-18 21:38:57 +0000100 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101 bb_error_msg_and_die("invalid conversion `%s'", argv[i]+5);
Matt Kraai24ac0172000-12-18 21:38:57 +0000102 }
103 if (buf[0] == '\0')
104 break;
105 if (buf[0] == ',')
106 buf++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000107 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000108 } else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000109 bb_show_usage();
Eric Andersen3cf52d11999-10-12 22:26:06 +0000110 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000111
Matt Kraai24ac0172000-12-18 21:38:57 +0000112 buf = xmalloc(bs);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000113
Matt Kraai24ac0172000-12-18 21:38:57 +0000114 if (infile != NULL) {
Eric Andersen8876fb22003-06-20 09:01:58 +0000115 ifd = bb_xopen(infile, O_RDONLY);
Matt Kraai24ac0172000-12-18 21:38:57 +0000116 } else {
117 ifd = STDIN_FILENO;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000118 infile = bb_msg_standard_input;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000119 }
120
Matt Kraai24ac0172000-12-18 21:38:57 +0000121 if (outfile != NULL) {
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000122 oflag = O_WRONLY | O_CREAT;
123
Eric Andersenab26cc32004-01-30 22:24:32 +0000124 if (!seek && trunc_flag) {
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000125 oflag |= O_TRUNC;
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000126 }
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000127
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +0000128 ofd = bb_xopen3(outfile, oflag, 0666);
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000129
Eric Andersenab26cc32004-01-30 22:24:32 +0000130 if (seek && trunc_flag) {
Matt Kraaieb834782002-02-05 15:28:54 +0000131 if (ftruncate(ofd, seek * bs) < 0) {
132 struct stat st;
133
134 if (fstat (ofd, &st) < 0 || S_ISREG (st.st_mode) ||
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000135 S_ISDIR (st.st_mode)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000136 bb_perror_msg_and_die("%s", outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000137 }
Matt Kraaieb834782002-02-05 15:28:54 +0000138 }
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000139 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000140 } else {
141 ofd = STDOUT_FILENO;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000142 outfile = bb_msg_standard_output;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000143 }
144
Matt Kraai24ac0172000-12-18 21:38:57 +0000145 if (skip) {
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000146 if (lseek(ifd, skip * bs, SEEK_CUR) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000147 bb_perror_msg_and_die("%s", infile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000148 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000149 }
Eric Andersenddea3682000-11-29 22:33:02 +0000150
Matt Kraai24ac0172000-12-18 21:38:57 +0000151 if (seek) {
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000152 if (lseek(ofd, seek * bs, SEEK_CUR) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000153 bb_perror_msg_and_die("%s", outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000154 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000155 }
Eric Andersenddea3682000-11-29 22:33:02 +0000156
Matt Kraai24ac0172000-12-18 21:38:57 +0000157 while (in_full + in_part != count) {
Eric Andersenef38b392002-04-27 01:31:43 +0000158 if (noerror) {
159 /* Pre-zero the buffer when doing the noerror thing */
160 memset(buf, '\0', bs);
161 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000162 n = safe_read(ifd, buf, bs);
Eric Andersenef38b392002-04-27 01:31:43 +0000163 if (n < 0) {
164 if (noerror) {
165 n = bs;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000166 bb_perror_msg("%s", infile);
Eric Andersenef38b392002-04-27 01:31:43 +0000167 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000168 bb_perror_msg_and_die("%s", infile);
Eric Andersenef38b392002-04-27 01:31:43 +0000169 }
170 }
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000171 if (n == 0) {
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000172 break;
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000173 }
"Vladimir N. Oleynik"57545c82006-01-31 12:06:57 +0000174 if ((size_t)n == bs) {
Matt Kraai24ac0172000-12-18 21:38:57 +0000175 in_full++;
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000176 } else {
Matt Kraai24ac0172000-12-18 21:38:57 +0000177 in_part++;
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000178 }
Eric Andersen1ca20a72001-03-21 07:34:27 +0000179 if (sync_flag) {
Matt Kraai24ac0172000-12-18 21:38:57 +0000180 memset(buf + n, '\0', bs - n);
181 n = bs;
Eric Andersenddea3682000-11-29 22:33:02 +0000182 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000183 n = bb_full_write(ofd, buf, n);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000184 if (n < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000185 bb_perror_msg_and_die("%s", outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000186 }
"Vladimir N. Oleynik"57545c82006-01-31 12:06:57 +0000187 if ((size_t)n == bs) {
Matt Kraai24ac0172000-12-18 21:38:57 +0000188 out_full++;
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000189 } else {
Matt Kraai24ac0172000-12-18 21:38:57 +0000190 out_part++;
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000191 }
Glenn L McGrathf0b073f2000-09-11 00:32:13 +0000192 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000193
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000194 if (close (ifd) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000195 bb_perror_msg_and_die("%s", infile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000196 }
Matt Kraai3eeab3b2001-12-07 16:27:37 +0000197
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000198 if (close (ofd) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000199 bb_perror_msg_and_die("%s", outfile);
Glenn L McGratheaed78a2002-11-28 11:05:28 +0000200 }
Matt Kraai3eeab3b2001-12-07 16:27:37 +0000201
Rob Landleyc5598172006-05-02 22:44:04 +0000202 dd_output_status(0);
Matt Kraai24ac0172000-12-18 21:38:57 +0000203
Matt Kraai3e856ce2000-12-01 02:55:13 +0000204 return EXIT_SUCCESS;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000205}