blob: 297d0ab51a056245ab91627c51d96a9a13af8d57 [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 Kraai24ac0172000-12-18 21:38:57 +00006 * Copyright (C) 2000 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
Eric Andersenc4996011999-10-20 22:08:37 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
Eric Andersencc8ed391999-10-05 16:24:54 +000022 */
23
Matt Kraai24ac0172000-12-18 21:38:57 +000024#include <sys/types.h>
Eric Andersened3ef502001-01-27 08:24:39 +000025#include <stdlib.h>
26#include <stdio.h>
27#include <unistd.h>
28#include <string.h>
Eric Andersen3cf52d11999-10-12 22:26:06 +000029#include <fcntl.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000030#include "busybox.h"
31
Eric Andersencc8ed391999-10-05 16:24:54 +000032
Matt Kraaia164c642001-02-05 17:50:03 +000033static const struct suffix_mult dd_suffixes[] = {
Matt Kraai24ac0172000-12-18 21:38:57 +000034 { "c", 1 },
35 { "w", 2 },
36 { "b", 512 },
37 { "kD", 1000 },
38 { "k", 1024 },
39 { "MD", 1000000 },
40 { "M", 1048576 },
41 { "GD", 1000000000 },
42 { "G", 1073741824 },
43 { NULL, 0 }
44};
45
46int dd_main(int argc, char **argv)
Eric Andersen3cf52d11999-10-12 22:26:06 +000047{
Eric Andersen1ca20a72001-03-21 07:34:27 +000048 int i, ifd, ofd, oflag, sync_flag = FALSE, trunc = TRUE;
Matt Kraai24ac0172000-12-18 21:38:57 +000049 size_t in_full = 0, in_part = 0, out_full = 0, out_part = 0;
50 size_t bs = 512, count = -1;
51 ssize_t n;
52 off_t seek = 0, skip = 0;
53 FILE *statusfp;
54 char *infile = NULL, *outfile = NULL, *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)
58 bs = parse_number(argv[i]+3, dd_suffixes);
59 else if (strncmp("count=", argv[i], 6) == 0)
60 count = parse_number(argv[i]+6, dd_suffixes);
61 else if (strncmp("seek=", argv[i], 5) == 0)
62 seek = parse_number(argv[i]+5, dd_suffixes);
63 else if (strncmp("skip=", argv[i], 5) == 0)
64 skip = parse_number(argv[i]+5, dd_suffixes);
65 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) {
73 trunc = FALSE;
74 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;
78 } else {
Matt Kraaidd19c692001-01-31 19:00:21 +000079 error_msg_and_die("invalid conversion `%s'", argv[i]+5);
Matt Kraai24ac0172000-12-18 21:38:57 +000080 }
81 if (buf[0] == '\0')
82 break;
83 if (buf[0] == ',')
84 buf++;
Erik Andersene49d5ec2000-02-08 19:58:47 +000085 }
Matt Kraai24ac0172000-12-18 21:38:57 +000086 } else
Eric Andersen67991cf2001-02-14 21:23:06 +000087 show_usage();
Eric Andersen3cf52d11999-10-12 22:26:06 +000088 }
Eric Andersen3cf52d11999-10-12 22:26:06 +000089
Matt Kraai24ac0172000-12-18 21:38:57 +000090 buf = xmalloc(bs);
Erik Andersene49d5ec2000-02-08 19:58:47 +000091
Matt Kraai24ac0172000-12-18 21:38:57 +000092 if (infile != NULL) {
93 if ((ifd = open(infile, O_RDONLY)) < 0)
94 perror_msg_and_die("%s", infile);
95 } else {
96 ifd = STDIN_FILENO;
97 infile = "standard input";
Erik Andersene49d5ec2000-02-08 19:58:47 +000098 }
99
Matt Kraai24ac0172000-12-18 21:38:57 +0000100 if (outfile != NULL) {
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000101 oflag = O_WRONLY | O_CREAT;
102
103 if (!seek && trunc)
104 oflag |= O_TRUNC;
105
106 if ((ofd = open(outfile, oflag, 0666)) < 0)
Matt Kraai24ac0172000-12-18 21:38:57 +0000107 perror_msg_and_die("%s", outfile);
Matt Kraaic9acf8c2001-01-17 00:21:05 +0000108
109 if (seek && trunc) {
110 if (ftruncate(ofd, seek * bs) < 0)
111 perror_msg_and_die("%s", outfile);
112 }
113
Matt Kraai24ac0172000-12-18 21:38:57 +0000114 statusfp = stdout;
115 } else {
116 ofd = STDOUT_FILENO;
117 outfile = "standard output";
118 statusfp = stderr;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000119 }
120
Matt Kraai24ac0172000-12-18 21:38:57 +0000121 if (skip) {
122 if (lseek(ifd, skip * bs, SEEK_CUR) < 0)
123 perror_msg_and_die("%s", infile);
124 }
Eric Andersenddea3682000-11-29 22:33:02 +0000125
Matt Kraai24ac0172000-12-18 21:38:57 +0000126 if (seek) {
127 if (lseek(ofd, seek * bs, SEEK_CUR) < 0)
128 perror_msg_and_die("%s", outfile);
129 }
Eric Andersenddea3682000-11-29 22:33:02 +0000130
Matt Kraai24ac0172000-12-18 21:38:57 +0000131 while (in_full + in_part != count) {
132 n = safe_read(ifd, buf, bs);
133 if (n < 0)
134 perror_msg_and_die("%s", infile);
135 if (n == 0)
Glenn L McGrath06aeb6c2000-08-25 03:50:10 +0000136 break;
Matt Kraai24ac0172000-12-18 21:38:57 +0000137 if (n == bs)
138 in_full++;
139 else
140 in_part++;
Eric Andersen1ca20a72001-03-21 07:34:27 +0000141 if (sync_flag) {
Matt Kraai24ac0172000-12-18 21:38:57 +0000142 memset(buf + n, '\0', bs - n);
143 n = bs;
Eric Andersenddea3682000-11-29 22:33:02 +0000144 }
Matt Kraai24ac0172000-12-18 21:38:57 +0000145 n = full_write(ofd, buf, n);
146 if (n < 0)
147 perror_msg_and_die("%s", outfile);
148 if (n == bs)
149 out_full++;
150 else
151 out_part++;
Glenn L McGrathf0b073f2000-09-11 00:32:13 +0000152 }
Eric Andersen3cf52d11999-10-12 22:26:06 +0000153
Eric Andersene76c3b02001-04-05 03:14:39 +0000154 fprintf(statusfp, "%ld+%ld records in\n", (long)in_full, (long)in_part);
155 fprintf(statusfp, "%ld+%ld records out\n", (long)out_full, (long)out_part);
Matt Kraai24ac0172000-12-18 21:38:57 +0000156
Matt Kraai3e856ce2000-12-01 02:55:13 +0000157 return EXIT_SUCCESS;
Eric Andersen3cf52d11999-10-12 22:26:06 +0000158}