Woops, forgot they printout applet name as well....
[oweals/busybox.git] / coreutils / dd.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini dd implementation for busybox
4  *
5  *
6  * Copyright (C) 2000,2001  Matt Kraai
7  *
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  *
22  */
23
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <unistd.h>
29 #include <string.h>
30 #include <fcntl.h>
31 #include "busybox.h"
32
33
34 static const struct suffix_mult dd_suffixes[] = {
35         { "c", 1 },
36         { "w", 2 },
37         { "b", 512 },
38         { "kD", 1000 },
39         { "k", 1024 },
40         { "MD", 1000000 },
41         { "M", 1048576 },
42         { "GD", 1000000000 },
43         { "G", 1073741824 },
44         { NULL, 0 }
45 };
46
47 int dd_main(int argc, char **argv)
48 {
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;
52         ssize_t n;
53         off_t seek = 0, skip = 0;
54         char *infile = NULL, *outfile = NULL, *buf;
55
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)
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) {
76                                         sync_flag = TRUE;
77                                         buf += 4;
78                                 } else if (strncmp("noerror", buf, 7) == 0) {
79                                         noerror = TRUE;
80                                         buf += 7;
81                                 } else {
82                                         error_msg_and_die("invalid conversion `%s'", argv[i]+5);
83                                 }
84                                 if (buf[0] == '\0')
85                                         break;
86                                 if (buf[0] == ',')
87                                         buf++;
88                         }
89                 } else
90                         show_usage();
91         }
92
93         buf = xmalloc(bs);
94
95         if (infile != NULL) {
96                 if ((ifd = open(infile, O_RDONLY)) < 0)
97                         perror_msg_and_die("%s", infile);
98         } else {
99                 ifd = STDIN_FILENO;
100                 infile = "standard input";
101         }
102
103         if (outfile != NULL) {
104                 oflag = O_WRONLY | O_CREAT;
105
106                 if (!seek && trunc)
107                         oflag |= O_TRUNC;
108
109                 if ((ofd = open(outfile, oflag, 0666)) < 0)
110                         perror_msg_and_die("%s", outfile);
111
112                 if (seek && trunc) {
113                         if (ftruncate(ofd, seek * bs) < 0) {
114                                 struct stat st;
115
116                                 if (fstat (ofd, &st) < 0 || S_ISREG (st.st_mode) ||
117                                                 S_ISDIR (st.st_mode))
118                                         perror_msg_and_die("%s", outfile);
119                         }
120                 }
121         } else {
122                 ofd = STDOUT_FILENO;
123                 outfile = "standard output";
124         }
125
126         if (skip) {
127                 if (lseek(ifd, skip * bs, SEEK_CUR) < 0)
128                         perror_msg_and_die("%s", infile);
129         }
130
131         if (seek) {
132                 if (lseek(ofd, seek * bs, SEEK_CUR) < 0)
133                         perror_msg_and_die("%s", outfile);
134         }
135
136         while (in_full + in_part != count) {
137                 if (noerror) {
138                         /* Pre-zero the buffer when doing the noerror thing */
139                         memset(buf, '\0', bs);
140                 }
141                 n = safe_read(ifd, buf, bs);
142                 if (n < 0) {
143                         if (noerror) {
144                                 n = bs;
145                                 perror_msg("%s", infile);
146                         } else {
147                                 perror_msg_and_die("%s", infile);
148                         }
149                 }
150                 if (n == 0)
151                         break;
152                 if (n == bs)
153                         in_full++;
154                 else
155                         in_part++;
156                 if (sync_flag) {
157                         memset(buf + n, '\0', bs - n);
158                         n = bs;
159                 }
160                 n = full_write(ofd, buf, n);
161                 if (n < 0)
162                         perror_msg_and_die("%s", outfile);
163                 if (n == bs)
164                         out_full++;
165                 else
166                         out_part++;
167         }
168
169         if (close (ifd) < 0)
170                 perror_msg_and_die("%s", infile);
171
172         if (close (ofd) < 0)
173                 perror_msg_and_die("%s", outfile);
174
175         fprintf(stderr, "%ld+%ld records in", (long)in_full, (long)in_part);
176         fprintf(stderr, "%ld+%ld records out", (long)out_full, (long)out_part);
177
178         return EXIT_SUCCESS;
179 }