1 /* vi: set sw=4 ts=4: */
3 * Mini dd implementation for busybox
6 * Copyright (C) 2000,2001 Matt Kraai
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.
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.
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
24 #include <sys/types.h>
34 static const struct suffix_mult dd_suffixes[] = {
47 int dd_main(int argc, char **argv)
49 int i, ifd, ofd, oflag, sync_flag = FALSE, trunc = TRUE, noerror = FALSE;
50 size_t in_full = 0, in_part = 0, out_full = 0, out_part = 0;
51 size_t bs = 512, count = -1;
53 off_t seek = 0, skip = 0;
54 char *infile = NULL, *outfile = NULL, *buf;
56 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)
67 else if (strncmp("of=", argv[i], 3) == 0)
69 else if (strncmp("conv=", argv[i], 5) == 0) {
72 if (strncmp("notrunc", buf, 7) == 0) {
75 } else if (strncmp("sync", buf, 4) == 0) {
78 } else if (strncmp("noerror", buf, 7) == 0) {
82 error_msg_and_die("invalid conversion `%s'", argv[i]+5);
96 if ((ifd = open(infile, O_RDONLY)) < 0)
97 perror_msg_and_die("%s", infile);
100 infile = "standard input";
103 if (outfile != NULL) {
104 oflag = O_WRONLY | O_CREAT;
109 if ((ofd = open(outfile, oflag, 0666)) < 0)
110 perror_msg_and_die("%s", outfile);
113 if (ftruncate(ofd, seek * bs) < 0) {
116 if (fstat (ofd, &st) < 0 || S_ISREG (st.st_mode) ||
117 S_ISDIR (st.st_mode))
118 perror_msg_and_die("%s", outfile);
123 outfile = "standard output";
127 if (lseek(ifd, skip * bs, SEEK_CUR) < 0)
128 perror_msg_and_die("%s", infile);
132 if (lseek(ofd, seek * bs, SEEK_CUR) < 0)
133 perror_msg_and_die("%s", outfile);
136 while (in_full + in_part != count) {
138 /* Pre-zero the buffer when doing the noerror thing */
139 memset(buf, '\0', bs);
141 n = safe_read(ifd, buf, bs);
145 perror_msg("%s", infile);
147 perror_msg_and_die("%s", infile);
157 memset(buf + n, '\0', bs - n);
160 n = full_write(ofd, buf, n);
162 perror_msg_and_die("%s", outfile);
170 perror_msg_and_die("%s", infile);
173 perror_msg_and_die("%s", outfile);
175 fprintf(stderr, "%ld+%ld records in\n", (long)in_full, (long)in_part);
176 fprintf(stderr, "%ld+%ld records out\n", (long)out_full, (long)out_part);