aboutsummaryrefslogtreecommitdiff
path: root/dd.c
diff options
context:
space:
mode:
authorerik <erik@69ca8d6d-28ef-0310-b511-8ec308f3f277>2000-02-08 19:58:47 +0000
committererik <erik@69ca8d6d-28ef-0310-b511-8ec308f3f277>2000-02-08 19:58:47 +0000
commita18125005d67c38a8ad7fb454571bb996664ad96 (patch)
treec90bda10731ad9333ce3b404f993354c9fc104b8 /dd.c
parent8ef3b44285a8ce5b83bed9cf32ce5f40b30ba72f (diff)
downloadbusybox-w32-a18125005d67c38a8ad7fb454571bb996664ad96.tar.gz
busybox-w32-a18125005d67c38a8ad7fb454571bb996664ad96.tar.bz2
busybox-w32-a18125005d67c38a8ad7fb454571bb996664ad96.zip
Some formatting updates (ran the code through indent)
-Erik git-svn-id: svn://busybox.net/trunk/busybox@357 69ca8d6d-28ef-0310-b511-8ec308f3f277
Diffstat (limited to 'dd.c')
-rw-r--r--dd.c284
1 files changed, 140 insertions, 144 deletions
diff --git a/dd.c b/dd.c
index 3e1024a60..0d5b3e8ab 100644
--- a/dd.c
+++ b/dd.c
@@ -1,3 +1,4 @@
1/* vi: set sw=4 ts=4: */
1/* 2/*
2 * Mini dd implementation for busybox 3 * Mini dd implementation for busybox
3 * 4 *
@@ -40,164 +41,159 @@ typedef unsigned long long int uintmax_t;
40#endif 41#endif
41 42
42static const char dd_usage[] = 43static const char dd_usage[] =
43"dd [if=name] [of=name] [bs=n] [count=n] [skip=n] [seek=n]\n\n" 44 "dd [if=name] [of=name] [bs=n] [count=n] [skip=n] [seek=n]\n\n"
44"Copy a file, converting and formatting according to options\n\n" 45 "Copy a file, converting and formatting according to options\n\n"
45"\tif=FILE\tread from FILE instead of stdin\n" 46 "\tif=FILE\tread from FILE instead of stdin\n"
46"\tof=FILE\twrite to FILE instead of stdout\n" 47 "\tof=FILE\twrite to FILE instead of stdout\n"
47"\tbs=n\tread and write n bytes at a time\n" 48 "\tbs=n\tread and write n bytes at a time\n"
48"\tcount=n\tcopy only n input blocks\n" 49 "\tcount=n\tcopy only n input blocks\n"
49"\tskip=n\tskip n input blocks\n" 50 "\tskip=n\tskip n input blocks\n"
50"\tseek=n\tskip n output blocks\n" 51 "\tseek=n\tskip n output blocks\n"
51"\n"
52"Numbers may be suffixed by w (x2), k (x1024), b (x512), or M (x1024^2)\n";
53 52
53 "\n"
54 "Numbers may be suffixed by w (x2), k (x1024), b (x512), or M (x1024^2)\n";
54 55
55 56
56extern int dd_main (int argc, char **argv) 57
58extern int dd_main(int argc, char **argv)
57{ 59{
58 const char *inFile = NULL; 60 const char *inFile = NULL;
59 const char *outFile = NULL; 61 const char *outFile = NULL;
60 char *cp; 62 char *cp;
61 int inFd; 63 int inFd;
62 int outFd; 64 int outFd;
63 int inCc = 0; 65 int inCc = 0;
64 int outCc; 66 int outCc;
65 long blockSize = 512; 67 long blockSize = 512;
66 uintmax_t skipBlocks = 0; 68 uintmax_t skipBlocks = 0;
67 uintmax_t seekBlocks = 0; 69 uintmax_t seekBlocks = 0;
68 uintmax_t count = (uintmax_t)-1; 70 uintmax_t count = (uintmax_t) - 1;
69 uintmax_t intotal; 71 uintmax_t intotal;
70 uintmax_t outTotal; 72 uintmax_t outTotal;
71 unsigned char *buf; 73 unsigned char *buf;
72 74
73 argc--; 75 argc--;
74 argv++; 76 argv++;
75
76 /* Parse any options */
77 while (argc) {
78 if (inFile == NULL && (strncmp(*argv, "if", 2) == 0))
79 inFile=((strchr(*argv, '='))+1);
80 else if (outFile == NULL && (strncmp(*argv, "of", 2) == 0))
81 outFile=((strchr(*argv, '='))+1);
82 else if (strncmp("count", *argv, 5) == 0) {
83 count = getNum ((strchr(*argv, '='))+1);
84 if (count <= 0) {
85 fprintf (stderr, "Bad count value %s\n", *argv);
86 goto usage;
87 }
88 }
89 else if (strncmp(*argv, "bs", 2) == 0) {
90 blockSize = getNum ((strchr(*argv, '='))+1);
91 if (blockSize <= 0) {
92 fprintf (stderr, "Bad block size value %s\n", *argv);
93 goto usage;
94 }
95 }
96 else if (strncmp(*argv, "skip", 4) == 0) {
97 skipBlocks = getNum ((strchr(*argv, '='))+1);
98 if (skipBlocks <= 0) {
99 fprintf (stderr, "Bad skip value %s\n", *argv);
100 goto usage;
101 }
102 77
78 /* Parse any options */
79 while (argc) {
80 if (inFile == NULL && (strncmp(*argv, "if", 2) == 0))
81 inFile = ((strchr(*argv, '=')) + 1);
82 else if (outFile == NULL && (strncmp(*argv, "of", 2) == 0))
83 outFile = ((strchr(*argv, '=')) + 1);
84 else if (strncmp("count", *argv, 5) == 0) {
85 count = getNum((strchr(*argv, '=')) + 1);
86 if (count <= 0) {
87 fprintf(stderr, "Bad count value %s\n", *argv);
88 goto usage;
89 }
90 } else if (strncmp(*argv, "bs", 2) == 0) {
91 blockSize = getNum((strchr(*argv, '=')) + 1);
92 if (blockSize <= 0) {
93 fprintf(stderr, "Bad block size value %s\n", *argv);
94 goto usage;
95 }
96 } else if (strncmp(*argv, "skip", 4) == 0) {
97 skipBlocks = getNum((strchr(*argv, '=')) + 1);
98 if (skipBlocks <= 0) {
99 fprintf(stderr, "Bad skip value %s\n", *argv);
100 goto usage;
101 }
102
103 } else if (strncmp(*argv, "seek", 4) == 0) {
104 seekBlocks = getNum((strchr(*argv, '=')) + 1);
105 if (seekBlocks <= 0) {
106 fprintf(stderr, "Bad seek value %s\n", *argv);
107 goto usage;
108 }
109
110 } else {
111 goto usage;
112 }
113 argc--;
114 argv++;
103 } 115 }
104 else if (strncmp(*argv, "seek", 4) == 0) {
105 seekBlocks = getNum ((strchr(*argv, '='))+1);
106 if (seekBlocks <= 0) {
107 fprintf (stderr, "Bad seek value %s\n", *argv);
108 goto usage;
109 }
110 116
117 buf = malloc(blockSize);
118 if (buf == NULL) {
119 fprintf(stderr, "Cannot allocate buffer\n");
120 exit(FALSE);
111 } 121 }
112 else { 122
113 goto usage; 123 intotal = 0;
124 outTotal = 0;
125
126 if (inFile == NULL)
127 inFd = fileno(stdin);
128 else
129 inFd = open(inFile, 0);
130
131 if (inFd < 0) {
132 perror(inFile);
133 free(buf);
134 exit(FALSE);
114 } 135 }
115 argc--; 136
116 argv++; 137 if (outFile == NULL)
117 } 138 outFd = fileno(stdout);
118 139 else
119 buf = malloc (blockSize); 140 outFd = open(outFile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
120 if (buf == NULL) { 141
121 fprintf (stderr, "Cannot allocate buffer\n"); 142 if (outFd < 0) {
122 exit( FALSE); 143 perror(outFile);
123 } 144 close(inFd);
124 145 free(buf);
125 intotal = 0; 146 exit(FALSE);
126 outTotal = 0;
127
128 if (inFile == NULL)
129 inFd = fileno(stdin);
130 else
131 inFd = open (inFile, 0);
132
133 if (inFd < 0) {
134 perror (inFile);
135 free (buf);
136 exit( FALSE);
137 }
138
139 if (outFile == NULL)
140 outFd = fileno(stdout);
141 else
142 outFd = open(outFile, O_WRONLY | O_CREAT | O_TRUNC, 0666);
143
144 if (outFd < 0) {
145 perror (outFile);
146 close (inFd);
147 free (buf);
148 exit( FALSE);
149 }
150
151 lseek(inFd, skipBlocks*blockSize, SEEK_SET);
152 lseek(outFd, seekBlocks*blockSize, SEEK_SET);
153 //
154 //TODO: Convert to using fullRead & fullWrite
155 // from utility.c
156 // -Erik
157 while (outTotal < count * blockSize) {
158 inCc = read (inFd, buf, blockSize);
159 if (inCc < 0) {
160 perror (inFile);
161 goto cleanup;
162 } else if (inCc == 0) {
163 goto cleanup;
164 } 147 }
165 intotal += inCc; 148
166 cp = buf; 149 lseek(inFd, skipBlocks * blockSize, SEEK_SET);
167 150 lseek(outFd, seekBlocks * blockSize, SEEK_SET);
168 while (intotal > outTotal) { 151 //
169 if (outTotal + inCc > count * blockSize) 152 //TODO: Convert to using fullRead & fullWrite
170 inCc = count * blockSize - outTotal; 153 // from utility.c
171 outCc = write (outFd, cp, inCc); 154 // -Erik
172 if (outCc < 0) { 155 while (outTotal < count * blockSize) {
173 perror (outFile); 156 inCc = read(inFd, buf, blockSize);
174 goto cleanup; 157 if (inCc < 0) {
175 } else if (outCc == 0) { 158 perror(inFile);
176 goto cleanup; 159 goto cleanup;
177 } 160 } else if (inCc == 0) {
178 161 goto cleanup;
179 inCc -= outCc; 162 }
180 cp += outCc; 163 intotal += inCc;
181 outTotal += outCc; 164 cp = buf;
165
166 while (intotal > outTotal) {
167 if (outTotal + inCc > count * blockSize)
168 inCc = count * blockSize - outTotal;
169 outCc = write(outFd, cp, inCc);
170 if (outCc < 0) {
171 perror(outFile);
172 goto cleanup;
173 } else if (outCc == 0) {
174 goto cleanup;
175 }
176
177 inCc -= outCc;
178 cp += outCc;
179 outTotal += outCc;
180 }
182 } 181 }
183 }
184 182
185 if (inCc < 0) 183 if (inCc < 0)
186 perror (inFile); 184 perror(inFile);
187 185
188 cleanup: 186 cleanup:
189 close (inFd); 187 close(inFd);
190 close (outFd); 188 close(outFd);
191 free (buf); 189 free(buf);
192 190
193 printf ("%ld+%d records in\n", (long)(intotal / blockSize), 191 printf("%ld+%d records in\n", (long) (intotal / blockSize),
194 (intotal % blockSize) != 0); 192 (intotal % blockSize) != 0);
195 printf ("%ld+%d records out\n", (long)(outTotal / blockSize), 193 printf("%ld+%d records out\n", (long) (outTotal / blockSize),
196 (outTotal % blockSize) != 0); 194 (outTotal % blockSize) != 0);
197 exit( TRUE); 195 exit(TRUE);
198 usage: 196 usage:
199 197
200 usage( dd_usage); 198 usage(dd_usage);
201} 199}
202
203