diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2002-11-28 11:05:28 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2002-11-28 11:05:28 +0000 |
commit | eaed78a91f20cddf2efa480ba71b2389b8ce6aab (patch) | |
tree | a7c3591180561262fcaf4a227b99f96819134edc | |
parent | cdf142af90b4594d4463789cae53dcb9ec29319e (diff) | |
download | busybox-w32-eaed78a91f20cddf2efa480ba71b2389b8ce6aab.tar.gz busybox-w32-eaed78a91f20cddf2efa480ba71b2389b8ce6aab.tar.bz2 busybox-w32-eaed78a91f20cddf2efa480ba71b2389b8ce6aab.zip |
Style
-rw-r--r-- | coreutils/dd.c | 67 |
1 files changed, 46 insertions, 21 deletions
diff --git a/coreutils/dd.c b/coreutils/dd.c index ad7f46c9f..8c7272b5c 100644 --- a/coreutils/dd.c +++ b/coreutils/dd.c | |||
@@ -46,12 +46,25 @@ static const struct suffix_mult dd_suffixes[] = { | |||
46 | 46 | ||
47 | int dd_main(int argc, char **argv) | 47 | int dd_main(int argc, char **argv) |
48 | { | 48 | { |
49 | int i, ifd, ofd, oflag, sync_flag = FALSE, trunc = TRUE, noerror = FALSE; | 49 | size_t out_full = 0; |
50 | size_t in_full = 0, in_part = 0, out_full = 0, out_part = 0; | 50 | size_t out_part = 0; |
51 | size_t bs = 512, count = -1; | 51 | size_t in_full = 0; |
52 | size_t in_part = 0; | ||
53 | size_t count = -1; | ||
54 | size_t bs = 512; | ||
52 | ssize_t n; | 55 | ssize_t n; |
53 | off_t seek = 0, skip = 0; | 56 | off_t seek = 0; |
54 | char *infile = NULL, *outfile = NULL, *buf; | 57 | off_t skip = 0; |
58 | int sync_flag = FALSE; | ||
59 | int noerror = FALSE; | ||
60 | int trunc = TRUE; | ||
61 | int oflag; | ||
62 | int ifd; | ||
63 | int ofd; | ||
64 | int i; | ||
65 | char *infile = NULL; | ||
66 | char *outfile = NULL; | ||
67 | char *buf; | ||
55 | 68 | ||
56 | for (i = 1; i < argc; i++) { | 69 | for (i = 1; i < argc; i++) { |
57 | if (strncmp("bs=", argv[i], 3) == 0) | 70 | if (strncmp("bs=", argv[i], 3) == 0) |
@@ -93,8 +106,9 @@ int dd_main(int argc, char **argv) | |||
93 | buf = xmalloc(bs); | 106 | buf = xmalloc(bs); |
94 | 107 | ||
95 | if (infile != NULL) { | 108 | if (infile != NULL) { |
96 | if ((ifd = open(infile, O_RDONLY)) < 0) | 109 | if ((ifd = open(infile, O_RDONLY)) < 0) { |
97 | perror_msg_and_die("%s", infile); | 110 | perror_msg_and_die("%s", infile); |
111 | } | ||
98 | } else { | 112 | } else { |
99 | ifd = STDIN_FILENO; | 113 | ifd = STDIN_FILENO; |
100 | infile = "standard input"; | 114 | infile = "standard input"; |
@@ -103,19 +117,22 @@ int dd_main(int argc, char **argv) | |||
103 | if (outfile != NULL) { | 117 | if (outfile != NULL) { |
104 | oflag = O_WRONLY | O_CREAT; | 118 | oflag = O_WRONLY | O_CREAT; |
105 | 119 | ||
106 | if (!seek && trunc) | 120 | if (!seek && trunc) { |
107 | oflag |= O_TRUNC; | 121 | oflag |= O_TRUNC; |
122 | } | ||
108 | 123 | ||
109 | if ((ofd = open(outfile, oflag, 0666)) < 0) | 124 | if ((ofd = open(outfile, oflag, 0666)) < 0) { |
110 | perror_msg_and_die("%s", outfile); | 125 | perror_msg_and_die("%s", outfile); |
126 | } | ||
111 | 127 | ||
112 | if (seek && trunc) { | 128 | if (seek && trunc) { |
113 | if (ftruncate(ofd, seek * bs) < 0) { | 129 | if (ftruncate(ofd, seek * bs) < 0) { |
114 | struct stat st; | 130 | struct stat st; |
115 | 131 | ||
116 | if (fstat (ofd, &st) < 0 || S_ISREG (st.st_mode) || | 132 | if (fstat (ofd, &st) < 0 || S_ISREG (st.st_mode) || |
117 | S_ISDIR (st.st_mode)) | 133 | S_ISDIR (st.st_mode)) { |
118 | perror_msg_and_die("%s", outfile); | 134 | perror_msg_and_die("%s", outfile); |
135 | } | ||
119 | } | 136 | } |
120 | } | 137 | } |
121 | } else { | 138 | } else { |
@@ -124,13 +141,15 @@ int dd_main(int argc, char **argv) | |||
124 | } | 141 | } |
125 | 142 | ||
126 | if (skip) { | 143 | if (skip) { |
127 | if (lseek(ifd, skip * bs, SEEK_CUR) < 0) | 144 | if (lseek(ifd, skip * bs, SEEK_CUR) < 0) { |
128 | perror_msg_and_die("%s", infile); | 145 | perror_msg_and_die("%s", infile); |
146 | } | ||
129 | } | 147 | } |
130 | 148 | ||
131 | if (seek) { | 149 | if (seek) { |
132 | if (lseek(ofd, seek * bs, SEEK_CUR) < 0) | 150 | if (lseek(ofd, seek * bs, SEEK_CUR) < 0) { |
133 | perror_msg_and_die("%s", outfile); | 151 | perror_msg_and_die("%s", outfile); |
152 | } | ||
134 | } | 153 | } |
135 | 154 | ||
136 | while (in_full + in_part != count) { | 155 | while (in_full + in_part != count) { |
@@ -147,33 +166,39 @@ int dd_main(int argc, char **argv) | |||
147 | perror_msg_and_die("%s", infile); | 166 | perror_msg_and_die("%s", infile); |
148 | } | 167 | } |
149 | } | 168 | } |
150 | if (n == 0) | 169 | if (n == 0) { |
151 | break; | 170 | break; |
152 | if (n == bs) | 171 | } |
172 | if (n == bs) { | ||
153 | in_full++; | 173 | in_full++; |
154 | else | 174 | } else { |
155 | in_part++; | 175 | in_part++; |
176 | } | ||
156 | if (sync_flag) { | 177 | if (sync_flag) { |
157 | memset(buf + n, '\0', bs - n); | 178 | memset(buf + n, '\0', bs - n); |
158 | n = bs; | 179 | n = bs; |
159 | } | 180 | } |
160 | n = full_write(ofd, buf, n); | 181 | n = full_write(ofd, buf, n); |
161 | if (n < 0) | 182 | if (n < 0) { |
162 | perror_msg_and_die("%s", outfile); | 183 | perror_msg_and_die("%s", outfile); |
163 | if (n == bs) | 184 | } |
185 | if (n == bs) { | ||
164 | out_full++; | 186 | out_full++; |
165 | else | 187 | } else { |
166 | out_part++; | 188 | out_part++; |
189 | } | ||
167 | } | 190 | } |
168 | 191 | ||
169 | if (close (ifd) < 0) | 192 | if (close (ifd) < 0) { |
170 | perror_msg_and_die("%s", infile); | 193 | perror_msg_and_die("%s", infile); |
194 | } | ||
171 | 195 | ||
172 | if (close (ofd) < 0) | 196 | if (close (ofd) < 0) { |
173 | perror_msg_and_die("%s", outfile); | 197 | perror_msg_and_die("%s", outfile); |
198 | } | ||
174 | 199 | ||
175 | fprintf(stderr, "%ld+%ld records in", (long)in_full, (long)in_part); | 200 | fprintf(stderr, "%ld+%ld records in\n", (long)in_full, (long)in_part); |
176 | fprintf(stderr, "%ld+%ld records out", (long)out_full, (long)out_part); | 201 | fprintf(stderr, "%ld+%ld records out\n", (long)out_full, (long)out_part); |
177 | 202 | ||
178 | return EXIT_SUCCESS; | 203 | return EXIT_SUCCESS; |
179 | } | 204 | } |