diff options
author | Bernhard Reutner-Fischer <rep.dot.nop@gmail.com> | 2006-08-28 23:31:54 +0000 |
---|---|---|
committer | Bernhard Reutner-Fischer <rep.dot.nop@gmail.com> | 2006-08-28 23:31:54 +0000 |
commit | 73561cc75ac31e63bcab4b4ebfaf738e4ca225e6 (patch) | |
tree | 2a57564b8917a983365c765d4daa83032d9f0a17 /coreutils/dd.c | |
parent | 6ce8dae1d5a201156a83201de6c49cdcc4c8df3e (diff) | |
download | busybox-w32-73561cc75ac31e63bcab4b4ebfaf738e4ca225e6.tar.gz busybox-w32-73561cc75ac31e63bcab4b4ebfaf738e4ca225e6.tar.bz2 busybox-w32-73561cc75ac31e63bcab4b4ebfaf738e4ca225e6.zip |
- pull from busybox_scratch: r15829:15850
Various fixes, cleanups and shrinkage:
saves 952 Bytes:
text data bss dec hex filename
1087742 15853 790632 1894227 1ce753 ../busybox/busybox.old
1086790 15853 790632 1893275 1ce39b busybox
via:
# scripts/bloat-o-meter ../busybox/busybox_unstripped.old busybox_unstripped
function old new delta
ipcrm_main 756 822 +66
getval - 61 +61
maybe_set_utc - 40 +40
udhcpc_main 2896 2912 +16
md5_hash_block 428 437 +9
opt 8 16 +8
qgravechar 106 110 +4
make_bitmap 292 295 +3
inflate_unzip 2056 2059 +3
add_partition 1412 1414 +2
__parsespent 156 158 +2
qrealloc 41 42 +1
format - 1 +1
catv_main 313 314 +1
watch_main 293 292 -1
varunset 81 80 -1
part 1 - -1
check_if_skip 837 836 -1
start_stop_daemon_main 840 837 -3
create_lost_and_found 175 172 -3
supress_non_delimited_lines 4 - -4
static.l 4 - -4
static.c 5 1 -4
bsd_sum_file 237 233 -4
eval2 338 332 -6
arithmetic_common 166 158 -8
cmpfunc 22 5 -17
cksum_main 294 275 -19
cmp_main 465 439 -26
dd_main 1535 1508 -27
rmmod_main 376 333 -43
cut_file 727 644 -83
ipcs_main 3809 3721 -88
cut_main 722 614 -108
date_main 1443 1263 -180
remove_ids 222 - -222
------------------------------------------------------------------------------
(add/remove: 3/4 grow/shrink: 11/18 up/down: 217/-853) Total: -636 bytes
Diffstat (limited to 'coreutils/dd.c')
-rw-r--r-- | coreutils/dd.c | 128 |
1 files changed, 65 insertions, 63 deletions
diff --git a/coreutils/dd.c b/coreutils/dd.c index 052cd2902..a9536a584 100644 --- a/coreutils/dd.c +++ b/coreutils/dd.c | |||
@@ -9,6 +9,7 @@ | |||
9 | */ | 9 | */ |
10 | 10 | ||
11 | #include "busybox.h" | 11 | #include "busybox.h" |
12 | #include <signal.h> /* For FEATURE_DD_SIGNAL_HANDLING */ | ||
12 | 13 | ||
13 | static const struct suffix_mult dd_suffixes[] = { | 14 | static const struct suffix_mult dd_suffixes[] = { |
14 | { "c", 1 }, | 15 | { "c", 1 }, |
@@ -23,72 +24,72 @@ static const struct suffix_mult dd_suffixes[] = { | |||
23 | { NULL, 0 } | 24 | { NULL, 0 } |
24 | }; | 25 | }; |
25 | 26 | ||
26 | static size_t out_full; | 27 | static size_t out_full, out_part, in_full, in_part; |
27 | static size_t out_part; | ||
28 | static size_t in_full; | ||
29 | static size_t in_part; | ||
30 | 28 | ||
31 | static void dd_output_status(int cur_signal) | 29 | static void dd_output_status(int ATTRIBUTE_UNUSED cur_signal) |
32 | { | 30 | { |
33 | fprintf(stderr, "%ld+%ld records in\n%ld+%ld records out\n", | 31 | bb_fprintf(stderr, "%ld+%ld records in\n%ld+%ld records out\n", |
34 | (long)in_full, (long)in_part, | 32 | (long)in_full, (long)in_part, |
35 | (long)out_full, (long)out_part); | 33 | (long)out_full, (long)out_part); |
36 | } | 34 | } |
37 | 35 | ||
38 | int dd_main(int argc, char **argv) | 36 | int dd_main(int argc, char **argv) |
39 | { | 37 | { |
38 | #define sync_flag (1<<0) | ||
39 | #define noerror (1<<1) | ||
40 | #define trunc_flag (1<<2) | ||
41 | #define twobufs_flag (1<<3) | ||
42 | int flags = trunc_flag; | ||
40 | size_t count = -1, oc = 0, ibs = 512, obs = 512; | 43 | size_t count = -1, oc = 0, ibs = 512, obs = 512; |
41 | ssize_t n; | 44 | ssize_t n; |
42 | off_t seek = 0, skip = 0; | 45 | off_t seek = 0, skip = 0; |
43 | int sync_flag = FALSE, noerror = FALSE, trunc_flag = TRUE, twobufs_flag = 0, | 46 | int oflag, ifd, ofd; |
44 | oflag, ifd, ofd, i; | ||
45 | const char *infile = NULL, *outfile = NULL; | 47 | const char *infile = NULL, *outfile = NULL; |
46 | char *ibuf, *obuf; | 48 | char *ibuf, *obuf; |
47 | 49 | ||
48 | if (ENABLE_FEATURE_DD_SIGNAL_HANDLING) | 50 | if (ENABLE_FEATURE_DD_SIGNAL_HANDLING) { |
49 | { | ||
50 | struct sigaction sa; | 51 | struct sigaction sa; |
51 | 52 | ||
52 | memset(&sa, 0, sizeof(sa)); | 53 | memset(&sa, 0, sizeof(sa)); |
53 | sa.sa_handler = dd_output_status; | 54 | sa.sa_handler = dd_output_status; |
54 | sa.sa_flags = SA_RESTART; | 55 | sa.sa_flags = SA_RESTART; |
55 | sigemptyset(&sa.sa_mask); | 56 | sigemptyset(&sa.sa_mask); |
56 | sigaction(SIGUSR1, &sa, 0); | 57 | sigaction(SIGUSR1, &sa, 0); |
57 | } | 58 | } |
58 | 59 | ||
59 | for (i = 1; i < argc; i++) { | 60 | for (n = 1; n < argc; n++) { |
60 | if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("ibs=", argv[i], 4)) { | 61 | if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("ibs=", argv[n], 4)) { |
61 | ibs = bb_xparse_number(argv[i]+4, dd_suffixes); | 62 | ibs = bb_xparse_number(argv[n]+4, dd_suffixes); |
62 | twobufs_flag++; | 63 | flags |= twobufs_flag; |
63 | } else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("obs=", argv[i], 4)) { | 64 | } else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("obs=", argv[n], 4)) { |
64 | obs = bb_xparse_number(argv[i]+4, dd_suffixes); | 65 | obs = bb_xparse_number(argv[n]+4, dd_suffixes); |
65 | twobufs_flag++; | 66 | flags |= twobufs_flag; |
66 | } else if (!strncmp("bs=", argv[i], 3)) { | 67 | } else if (!strncmp("bs=", argv[n], 3)) |
67 | ibs = obs = bb_xparse_number(argv[i]+3, dd_suffixes); | 68 | ibs = obs = bb_xparse_number(argv[n]+3, dd_suffixes); |
68 | } else if (!strncmp("count=", argv[i], 6)) | 69 | else if (!strncmp("count=", argv[n], 6)) |
69 | count = bb_xparse_number(argv[i]+6, dd_suffixes); | 70 | count = bb_xparse_number(argv[n]+6, dd_suffixes); |
70 | else if (!strncmp("seek=", argv[i], 5)) | 71 | else if (!strncmp("seek=", argv[n], 5)) |
71 | seek = bb_xparse_number(argv[i]+5, dd_suffixes); | 72 | seek = bb_xparse_number(argv[n]+5, dd_suffixes); |
72 | else if (!strncmp("skip=", argv[i], 5)) | 73 | else if (!strncmp("skip=", argv[n], 5)) |
73 | skip = bb_xparse_number(argv[i]+5, dd_suffixes); | 74 | skip = bb_xparse_number(argv[n]+5, dd_suffixes); |
74 | else if (!strncmp("if=", argv[i], 3)) | 75 | else if (!strncmp("if=", argv[n], 3)) |
75 | infile = argv[i]+3; | 76 | infile = argv[n]+3; |
76 | else if (!strncmp("of=", argv[i], 3)) | 77 | else if (!strncmp("of=", argv[n], 3)) |
77 | outfile = argv[i]+3; | 78 | outfile = argv[n]+3; |
78 | else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("conv=", argv[i], 5)) { | 79 | else if (ENABLE_FEATURE_DD_IBS_OBS && !strncmp("conv=", argv[n], 5)) { |
79 | ibuf = argv[i]+5; | 80 | ibuf = argv[n]+5; |
80 | while (1) { | 81 | while (1) { |
81 | if (!strncmp("notrunc", ibuf, 7)) { | 82 | if (!strncmp("notrunc", ibuf, 7)) { |
82 | trunc_flag = FALSE; | 83 | flags ^= trunc_flag; |
83 | ibuf += 7; | 84 | ibuf += 7; |
84 | } else if (!strncmp("sync", ibuf, 4)) { | 85 | } else if (!strncmp("sync", ibuf, 4)) { |
85 | sync_flag = TRUE; | 86 | flags |= sync_flag; |
86 | ibuf += 4; | 87 | ibuf += 4; |
87 | } else if (!strncmp("noerror", ibuf, 7)) { | 88 | } else if (!strncmp("noerror", ibuf, 7)) { |
88 | noerror = TRUE; | 89 | flags |= noerror; |
89 | ibuf += 7; | 90 | ibuf += 7; |
90 | } else { | 91 | } else { |
91 | bb_error_msg_and_die(bb_msg_invalid_arg, argv[i]+5, "conv"); | 92 | bb_error_msg_and_die(bb_msg_invalid_arg, argv[n]+5, "conv"); |
92 | } | 93 | } |
93 | if (ibuf[0] == '\0') break; | 94 | if (ibuf[0] == '\0') break; |
94 | if (ibuf[0] == ',') ibuf++; | 95 | if (ibuf[0] == ',') ibuf++; |
@@ -98,12 +99,14 @@ int dd_main(int argc, char **argv) | |||
98 | } | 99 | } |
99 | ibuf = xmalloc(ibs); | 100 | ibuf = xmalloc(ibs); |
100 | 101 | ||
101 | if (twobufs_flag) obuf = xmalloc(obs); | 102 | if (flags & twobufs_flag) |
102 | else obuf = ibuf; | 103 | obuf = xmalloc(obs); |
104 | else | ||
105 | obuf = ibuf; | ||
103 | 106 | ||
104 | if (infile != NULL) { | 107 | if (infile != NULL) |
105 | ifd = xopen(infile, O_RDONLY); | 108 | ifd = xopen(infile, O_RDONLY); |
106 | } else { | 109 | else { |
107 | ifd = STDIN_FILENO; | 110 | ifd = STDIN_FILENO; |
108 | infile = bb_msg_standard_input; | 111 | infile = bb_msg_standard_input; |
109 | } | 112 | } |
@@ -111,20 +114,18 @@ int dd_main(int argc, char **argv) | |||
111 | if (outfile != NULL) { | 114 | if (outfile != NULL) { |
112 | oflag = O_WRONLY | O_CREAT; | 115 | oflag = O_WRONLY | O_CREAT; |
113 | 116 | ||
114 | if (!seek && trunc_flag) { | 117 | if (!seek && (flags & trunc_flag)) |
115 | oflag |= O_TRUNC; | 118 | oflag |= O_TRUNC; |
116 | } | ||
117 | 119 | ||
118 | ofd = xopen3(outfile, oflag, 0666); | 120 | ofd = xopen3(outfile, oflag, 0666); |
119 | 121 | ||
120 | if (seek && trunc_flag) { | 122 | if (seek && (flags & trunc_flag)) { |
121 | if (ftruncate(ofd, seek * obs) < 0) { | 123 | if (ftruncate(ofd, seek * obs) < 0) { |
122 | struct stat st; | 124 | struct stat st; |
123 | 125 | ||
124 | if (fstat (ofd, &st) < 0 || S_ISREG (st.st_mode) || | 126 | if (fstat(ofd, &st) < 0 || S_ISREG(st.st_mode) || |
125 | S_ISDIR (st.st_mode)) { | 127 | S_ISDIR(st.st_mode)) |
126 | bb_perror_msg_and_die("%s", outfile); | 128 | goto die_outfile; |
127 | } | ||
128 | } | 129 | } |
129 | } | 130 | } |
130 | } else { | 131 | } else { |
@@ -145,44 +146,42 @@ int dd_main(int argc, char **argv) | |||
145 | } | 146 | } |
146 | 147 | ||
147 | if (seek) { | 148 | if (seek) { |
148 | if (lseek(ofd, seek * obs, SEEK_CUR) < 0) { | 149 | if (lseek(ofd, seek * obs, SEEK_CUR) < 0) |
149 | bb_perror_msg_and_die("%s", outfile); | 150 | goto die_outfile; |
150 | } | ||
151 | } | 151 | } |
152 | 152 | ||
153 | while (in_full + in_part != count) { | 153 | while (in_full + in_part != count) { |
154 | if (noerror) { | 154 | if (flags & noerror) { |
155 | /* Pre-zero the buffer when doing the noerror thing */ | 155 | /* Pre-zero the buffer when doing the noerror thing */ |
156 | memset(ibuf, '\0', ibs); | 156 | memset(ibuf, '\0', ibs); |
157 | } | 157 | } |
158 | 158 | ||
159 | n = safe_read(ifd, ibuf, ibs); | 159 | n = safe_read(ifd, ibuf, ibs); |
160 | if (n == 0) { | 160 | if (n == 0) |
161 | break; | 161 | break; |
162 | } | ||
163 | if (n < 0) { | 162 | if (n < 0) { |
164 | if (noerror) { | 163 | if (flags & noerror) { |
165 | n = ibs; | 164 | n = ibs; |
166 | bb_perror_msg("%s", infile); | 165 | bb_perror_msg("%s", infile); |
167 | } else { | 166 | } else |
168 | bb_perror_msg_and_die("%s", infile); | 167 | bb_perror_msg_and_die("%s", infile); |
169 | } | ||
170 | } | 168 | } |
171 | if ((size_t)n == ibs) { | 169 | if ((size_t)n == ibs) |
172 | in_full++; | 170 | in_full++; |
173 | } else { | 171 | else { |
174 | in_part++; | 172 | in_part++; |
175 | if (sync_flag) { | 173 | if (sync_flag) { |
176 | memset(ibuf + n, '\0', ibs - n); | 174 | memset(ibuf + n, '\0', ibs - n); |
177 | n = ibs; | 175 | n = ibs; |
178 | } | 176 | } |
179 | } | 177 | } |
180 | if (twobufs_flag) { | 178 | if (flags & twobufs_flag) { |
181 | char *tmp = ibuf; | 179 | char *tmp = ibuf; |
182 | while (n) { | 180 | while (n) { |
183 | size_t d = obs - oc; | 181 | size_t d = obs - oc; |
184 | 182 | ||
185 | if (d > n) d = n; | 183 | if (d > n) |
184 | d = n; | ||
186 | memcpy(obuf + oc, tmp, d); | 185 | memcpy(obuf + oc, tmp, d); |
187 | n -= d; | 186 | n -= d; |
188 | tmp += d; | 187 | tmp += d; |
@@ -195,8 +194,10 @@ int dd_main(int argc, char **argv) | |||
195 | } | 194 | } |
196 | } else { | 195 | } else { |
197 | xwrite(ofd, ibuf, n); | 196 | xwrite(ofd, ibuf, n); |
198 | if (n == ibs) out_full++; | 197 | if (n == ibs) |
199 | else out_part++; | 198 | out_full++; |
199 | else | ||
200 | out_part++; | ||
200 | } | 201 | } |
201 | } | 202 | } |
202 | 203 | ||
@@ -209,6 +210,7 @@ int dd_main(int argc, char **argv) | |||
209 | } | 210 | } |
210 | 211 | ||
211 | if (close (ofd) < 0) { | 212 | if (close (ofd) < 0) { |
213 | die_outfile: | ||
212 | bb_perror_msg_and_die("%s", outfile); | 214 | bb_perror_msg_and_die("%s", outfile); |
213 | } | 215 | } |
214 | 216 | ||