403 |
mateuszvis |
1 |
/*
|
|
|
2 |
* copy
|
|
|
3 |
*/
|
|
|
4 |
|
|
|
5 |
/* /A - Used to copy ASCII files. Applies to the filename preceding it and to
|
|
|
6 |
* all following filenames. Files will be copied until an end-of-file mark is
|
|
|
7 |
* encountered in the file being copied. If an end-of-file mark is encountered
|
|
|
8 |
* in the file, the rest of the file is not copied. DOS will append an EOF
|
|
|
9 |
* mark at the end of the copied file.
|
|
|
10 |
*
|
|
|
11 |
* /B - Used to copy binary files. Applies to the filename preceding it and to
|
|
|
12 |
* all following filenames. Copied files will be read by size (according to
|
|
|
13 |
* the number of bytes indicated in the file`s directory listing). An EOF mark
|
|
|
14 |
* is not placed at the end of the copied file.
|
|
|
15 |
*
|
|
|
16 |
* /V - Checks after the copy to assure that a file was copied correctly. If
|
|
|
17 |
* the copy cannot be verified, the program will display an error message.
|
|
|
18 |
* Using this option will result in a slower copying process.
|
409 |
mateuszvis |
19 |
*
|
|
|
20 |
* special case: "COPY A+B+C+D" means "append B, C and D files to the A file"
|
|
|
21 |
* if A does not exist, then "append C and D to B", etc.
|
403 |
mateuszvis |
22 |
*/
|
|
|
23 |
|
|
|
24 |
struct copy_setup {
|
|
|
25 |
const char *src[64];
|
|
|
26 |
unsigned short src_count; /* how many sources are declared */
|
409 |
mateuszvis |
27 |
char dst[256];
|
|
|
28 |
unsigned short dstlen;
|
403 |
mateuszvis |
29 |
char src_asciimode[64];
|
|
|
30 |
char dst_asciimode;
|
|
|
31 |
char last_asciimode; /* /A or /B impacts the file preceding it and becomes the new default for all files that follow */
|
|
|
32 |
char verifyflag;
|
|
|
33 |
char lastitemwasplus;
|
409 |
mateuszvis |
34 |
char databuf[BUFFER_SIZE - 1024];
|
403 |
mateuszvis |
35 |
};
|
|
|
36 |
|
409 |
mateuszvis |
37 |
|
412 |
mateuszvis |
38 |
/* copies src to dst, overwriting or appending to the destination.
|
|
|
39 |
* - copy is performed in ASCII mode if asciiflag set (stop at first EOF in src
|
|
|
40 |
* and append an EOF in dst).
|
|
|
41 |
* - returns zero on success, DOS error code on error */
|
|
|
42 |
unsigned short cmd_copy_internal(const char *dst, char dstascii, const char *src, char srcascii, unsigned char appendflag, void *buff, unsigned short buffsz) {
|
|
|
43 |
unsigned short errcode = 0;
|
|
|
44 |
unsigned short srch = 0xffff, dsth = 0xffff;
|
|
|
45 |
_asm {
|
|
|
46 |
|
|
|
47 |
/* open src */
|
|
|
48 |
OPENSRC:
|
|
|
49 |
mov ax, 0x3d00 /* DOS 2+ -- open an existing file, read access mode */
|
|
|
50 |
mov dx, src /* ASCIIZ fname */
|
|
|
51 |
int 0x21 /* CF clear on success, handle in AX */
|
|
|
52 |
mov [srch], ax /* store src handle in memory */
|
|
|
53 |
|
|
|
54 |
/* check appendflag so I know if I have to try opening dst for append */
|
|
|
55 |
xor al, al
|
|
|
56 |
or al, [appendflag]
|
|
|
57 |
jz CREATEDST
|
|
|
58 |
|
|
|
59 |
/* try opening dst first if appendflag set */
|
|
|
60 |
mov ax, 0x3d01 /* DOS 2+ -- open an existing file, write access mode */
|
|
|
61 |
mov dx, dst /* ASCIIZ fname */
|
|
|
62 |
int 0x21 /* CF clear on success, handle in AX */
|
|
|
63 |
jc CREATEDST /* failed to open file (file does not exist) */
|
|
|
64 |
mov [dsth], ax /* store dst handle in memory */
|
|
|
65 |
|
|
|
66 |
/* got file open, LSEEK to end of it now so future data is appended */
|
|
|
67 |
mov bx, ax /* file handle in BX (was still in AX) */
|
|
|
68 |
mov ax, 0x4202 /* DOS 2+ -- set file pointer to end of file + CX:DX */
|
|
|
69 |
xor cx, cx /* offset zero */
|
|
|
70 |
xor dx, dx /* offset zero */
|
|
|
71 |
int 0x21 /* CF set on error */
|
|
|
72 |
jc FAIL
|
|
|
73 |
jmp COPY
|
|
|
74 |
|
|
|
75 |
/* create dst */
|
|
|
76 |
CREATEDST:
|
|
|
77 |
mov ah, 0x3c /* DOS 2+ -- create a file */
|
|
|
78 |
mov dx, dst
|
|
|
79 |
xor cx, cx /* zero out attributes */
|
|
|
80 |
int 0x21 /* handle in AX on success, CF set on error */
|
|
|
81 |
jc FAIL
|
|
|
82 |
mov [dsth], ax /* store dst handle in memory */
|
|
|
83 |
|
|
|
84 |
/* perform actual copy */
|
|
|
85 |
COPY:
|
|
|
86 |
/* read a block from src */
|
|
|
87 |
mov ah, 0x3f /* DOS 2+ -- read from file */
|
|
|
88 |
mov bx, [srch]
|
|
|
89 |
mov cx, [buffsz]
|
|
|
90 |
mov dx, [buff] /* DX points to buffer */
|
|
|
91 |
int 0x21 /* CF set on error, bytes read in AX (0=EOF) */
|
|
|
92 |
jc FAIL /* abort on error */
|
|
|
93 |
/* EOF? (ax == 0) */
|
413 |
mateuszvis |
94 |
test ax, ax
|
|
|
95 |
jz ENDOFFILE
|
412 |
mateuszvis |
96 |
/* write block of AX bytes to dst */
|
|
|
97 |
mov cx, ax /* block length */
|
|
|
98 |
mov ah, 0x40 /* DOS 2+ -- write to file (CX bytes from DS:DX) */
|
|
|
99 |
mov bx, [dsth] /* file handle */
|
|
|
100 |
/* mov dx, [buff] */ /* DX points to buffer already */
|
|
|
101 |
int 0x21 /* CF clear and AX=CX on success */
|
|
|
102 |
jc FAIL
|
|
|
103 |
cmp ax, cx /* sould be equal, otherwise failed */
|
|
|
104 |
mov ax, 0x08 /* preset to DOS error "Insufficient memory" */
|
|
|
105 |
jne FAIL
|
|
|
106 |
jmp COPY
|
|
|
107 |
|
|
|
108 |
ENDOFFILE:
|
413 |
mateuszvis |
109 |
/* if dst ascii mode -> add an EOF (ASCII mode not supported for the time being) */
|
412 |
mateuszvis |
110 |
|
|
|
111 |
jmp CLOSESRC
|
|
|
112 |
|
|
|
113 |
FAIL:
|
|
|
114 |
mov [errcode], ax
|
|
|
115 |
|
|
|
116 |
CLOSESRC:
|
|
|
117 |
/* close src and dst */
|
|
|
118 |
mov bx, [srch]
|
|
|
119 |
cmp bx, 0xffff
|
|
|
120 |
je CLOSEDST
|
|
|
121 |
mov ah, 0x3e /* DOS 2+ -- close a file handle */
|
|
|
122 |
int 0x21
|
|
|
123 |
|
|
|
124 |
CLOSEDST:
|
|
|
125 |
mov bx, [dsth]
|
|
|
126 |
cmp bx, 0xffff
|
|
|
127 |
je DONE
|
|
|
128 |
mov ah, 0x3e /* DOS 2+ -- close a file handle */
|
|
|
129 |
int 0x21
|
|
|
130 |
|
|
|
131 |
DONE:
|
|
|
132 |
}
|
|
|
133 |
return(errcode);
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
|
403 |
mateuszvis |
137 |
static int cmd_copy(struct cmd_funcparam *p) {
|
|
|
138 |
struct copy_setup *setup = (void *)(p->BUFFER);
|
|
|
139 |
unsigned short i;
|
409 |
mateuszvis |
140 |
unsigned short copiedcount_in = 0, copiedcount_out = 0; /* number of input/output copied files */
|
|
|
141 |
struct DTA *dta = (void *)0x80; /* use DTA at default location in PSP */
|
403 |
mateuszvis |
142 |
|
|
|
143 |
if (cmd_ishlp(p)) {
|
|
|
144 |
outputnl("Copies one or more files to another location.");
|
|
|
145 |
outputnl("");
|
|
|
146 |
outputnl("COPY [/A|/B] source [/A|/B] [+source [/A|/B] [+...]] [destination [/A|/B]] [/V]");
|
|
|
147 |
outputnl("");
|
|
|
148 |
outputnl("source Specifies the file or files to be copied");
|
|
|
149 |
outputnl("/A Indicates an ASCII text file");
|
|
|
150 |
outputnl("/B Indicates a binary file");
|
|
|
151 |
outputnl("destination Specifies the directory and/or filename for the new file(s)");
|
|
|
152 |
outputnl("/V Verifies that new files are written correctly");
|
|
|
153 |
outputnl("");
|
|
|
154 |
outputnl("To append files, specify a single file for destination, but multiple files");
|
|
|
155 |
outputnl("for source (using wildcards or file1+file2+file3 format).");
|
413 |
mateuszvis |
156 |
outputnl("");
|
|
|
157 |
outputnl("NOTE: /A and /B are no-ops (ignored), provided only for compatibility reasons.");
|
403 |
mateuszvis |
158 |
return(-1);
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
/* parse cmdline and fill the setup struct accordingly */
|
|
|
162 |
|
|
|
163 |
memset(setup, 0, sizeof(*setup));
|
|
|
164 |
|
|
|
165 |
for (i = 0; i < p->argc; i++) {
|
|
|
166 |
|
|
|
167 |
/* switch? */
|
|
|
168 |
if (p->argv[i][0] == '/') {
|
|
|
169 |
if ((imatch(p->argv[i], "/a")) || (imatch(p->argv[i], "/b"))) {
|
|
|
170 |
setup->last_asciimode = 'b';
|
|
|
171 |
if (imatch(p->argv[i], "/a")) setup->last_asciimode = 'a';
|
|
|
172 |
/* */
|
409 |
mateuszvis |
173 |
if (setup->dst[0] != 0) {
|
403 |
mateuszvis |
174 |
setup->dst_asciimode = setup->last_asciimode;
|
|
|
175 |
} else if (setup->src_count != 0) {
|
|
|
176 |
setup->src_asciimode[setup->src_count - 1] = setup->last_asciimode;
|
|
|
177 |
}
|
|
|
178 |
} else if (imatch(p->argv[i], "/v")) {
|
|
|
179 |
setup->verifyflag = 1;
|
|
|
180 |
} else {
|
|
|
181 |
outputnl("Invalid switch");
|
|
|
182 |
return(-1);
|
|
|
183 |
}
|
|
|
184 |
continue;
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
/* not a switch - must be either a source, a destination or a + */
|
|
|
188 |
if (p->argv[i][0] == '+') {
|
|
|
189 |
/* a plus cannot appear after destination or before first source */
|
409 |
mateuszvis |
190 |
if ((setup->dst[0] != 0) || (setup->src_count == 0)) {
|
403 |
mateuszvis |
191 |
outputnl("Invalid syntax");
|
|
|
192 |
return(-1);
|
|
|
193 |
}
|
|
|
194 |
setup->lastitemwasplus = 1;
|
|
|
195 |
/* a plus may be immediately followed by a filename - if so, emulate
|
|
|
196 |
* a new argument */
|
|
|
197 |
if (p->argv[i][1] != 0) {
|
|
|
198 |
p->argv[i] += 1;
|
|
|
199 |
i--;
|
|
|
200 |
}
|
|
|
201 |
continue;
|
|
|
202 |
}
|
|
|
203 |
|
|
|
204 |
/* src? (first non-switch or something that follows a +) */
|
|
|
205 |
if ((setup->lastitemwasplus) || (setup->src_count == 0)) {
|
|
|
206 |
setup->src[setup->src_count] = p->argv[i];
|
|
|
207 |
setup->src_asciimode[setup->src_count] = setup->last_asciimode;
|
|
|
208 |
setup->src_count++;
|
|
|
209 |
setup->lastitemwasplus = 0;
|
|
|
210 |
continue;
|
|
|
211 |
}
|
|
|
212 |
|
|
|
213 |
/* must be a dst then */
|
409 |
mateuszvis |
214 |
if (setup->dst[0] != 0) {
|
403 |
mateuszvis |
215 |
outputnl("Invalid syntax");
|
|
|
216 |
return(-1);
|
|
|
217 |
}
|
409 |
mateuszvis |
218 |
if (file_truename(p->argv[i], setup->dst) != 0) {
|
|
|
219 |
outputnl("Invalid destination");
|
|
|
220 |
return(-1);
|
|
|
221 |
}
|
403 |
mateuszvis |
222 |
setup->dst_asciimode = setup->last_asciimode;
|
409 |
mateuszvis |
223 |
/* if dst is a directory then append a backslash */
|
415 |
mateuszvis |
224 |
setup->dstlen = path_appendbkslash_if_dir(setup->dst);
|
403 |
mateuszvis |
225 |
}
|
|
|
226 |
|
|
|
227 |
/* DEBUG: output setup content ("if 1" to enable) */
|
418 |
mateuszvis |
228 |
#if 0
|
403 |
mateuszvis |
229 |
printf("src: ");
|
|
|
230 |
for (i = 0; i < setup->src_count; i++) {
|
|
|
231 |
if (i != 0) printf(", ");
|
|
|
232 |
printf("%s [%c]", setup->src[i], setup->src_asciimode[i]);
|
|
|
233 |
}
|
|
|
234 |
printf("\r\n");
|
|
|
235 |
printf("dst: %s [%c]\r\n", setup->dst, setup->dst_asciimode);
|
|
|
236 |
printf("verify: %s\r\n", (setup->verifyflag)?"ON":"OFF");
|
|
|
237 |
#endif
|
|
|
238 |
|
409 |
mateuszvis |
239 |
/* must have at least one source */
|
|
|
240 |
if (setup->src_count == 0) {
|
|
|
241 |
outputnl("Required parameter missing");
|
|
|
242 |
return(-1);
|
|
|
243 |
}
|
403 |
mateuszvis |
244 |
|
409 |
mateuszvis |
245 |
/* perform the operation based on setup directives:
|
|
|
246 |
* iterate over every source and copy it to dest */
|
|
|
247 |
|
|
|
248 |
for (i = 0; i < setup->src_count; i++) {
|
|
|
249 |
unsigned short t;
|
|
|
250 |
unsigned short databuflen;
|
|
|
251 |
unsigned short pathendoffset;
|
|
|
252 |
|
|
|
253 |
/* resolve truename of src and write it to buffer */
|
|
|
254 |
t = file_truename(setup->src[i], setup->databuf);
|
|
|
255 |
if (t != 0) {
|
|
|
256 |
output(setup->src[i]);
|
|
|
257 |
output(" - ");
|
|
|
258 |
outputnl(doserr(t));
|
|
|
259 |
continue;
|
|
|
260 |
}
|
|
|
261 |
databuflen = strlen(setup->databuf); /* remember databuf length */
|
|
|
262 |
|
|
|
263 |
/* if length zero, skip (not sure why this would be possible, though) */
|
|
|
264 |
if (databuflen == 0) continue;
|
|
|
265 |
|
|
|
266 |
/* if src does not end with a backslash AND it is a directory then append a backslash */
|
415 |
mateuszvis |
267 |
databuflen = path_appendbkslash_if_dir(setup->databuf);
|
409 |
mateuszvis |
268 |
|
|
|
269 |
/* if src ends with a '\' then append *.* */
|
|
|
270 |
if (setup->databuf[databuflen - 1] == '\\') {
|
|
|
271 |
strcat(setup->databuf, "*.*");
|
|
|
272 |
}
|
|
|
273 |
|
|
|
274 |
/* remember where the path in databuf ends */
|
|
|
275 |
for (t = 0; setup->databuf[t] != 0; t++) {
|
|
|
276 |
if (setup->databuf[t] == '\\') pathendoffset = t + 1;
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
/* */
|
|
|
280 |
if (findfirst(dta, setup->databuf, 0) != 0) {
|
|
|
281 |
continue;
|
|
|
282 |
}
|
|
|
283 |
|
|
|
284 |
do {
|
412 |
mateuszvis |
285 |
char appendflag;
|
409 |
mateuszvis |
286 |
if (dta->attr & DOS_ATTR_DIR) continue; /* skip directories */
|
|
|
287 |
|
|
|
288 |
/* compute full path/name of the file */
|
|
|
289 |
strcpy(setup->databuf + pathendoffset, dta->fname);
|
|
|
290 |
|
|
|
291 |
/* if there was no destination, then YOU are the destination now!
|
|
|
292 |
* this handles situations like COPY a.txt+b.txt+c.txt */
|
|
|
293 |
if (setup->dst[0] == NULL) {
|
|
|
294 |
strcpy(setup->dst, setup->databuf);
|
|
|
295 |
setup->dstlen = strlen(setup->dst);
|
|
|
296 |
copiedcount_in++;
|
|
|
297 |
copiedcount_out++;
|
|
|
298 |
continue;
|
|
|
299 |
}
|
|
|
300 |
|
|
|
301 |
/* is dst ending with a backslash? then append fname to it */
|
|
|
302 |
if (setup->dst[setup->dstlen - 1] == '\\') strcpy(setup->dst + setup->dstlen, dta->fname);
|
|
|
303 |
|
|
|
304 |
/* now databuf contains the full source and dst contains the full dest... COPY TIME! */
|
|
|
305 |
|
|
|
306 |
/* if dst file exists already -> overwrite it or append?
|
|
|
307 |
- if dst is a dir (dstlen-1 points at a \\) -> overwrite
|
|
|
308 |
- otherwise: if copiedcount_in==0 overwrite, else append */
|
|
|
309 |
output(setup->databuf);
|
|
|
310 |
if ((setup->dst[setup->dstlen - 1] == '\\') || (copiedcount_in == 0)) {
|
412 |
mateuszvis |
311 |
appendflag = 0;
|
409 |
mateuszvis |
312 |
output(" > ");
|
|
|
313 |
copiedcount_out++;
|
|
|
314 |
} else {
|
412 |
mateuszvis |
315 |
appendflag = 1;
|
409 |
mateuszvis |
316 |
output(" >> ");
|
|
|
317 |
}
|
|
|
318 |
outputnl(setup->dst);
|
|
|
319 |
|
412 |
mateuszvis |
320 |
t = cmd_copy_internal(setup->dst, 0, setup->databuf, 0, appendflag, setup->databuf, sizeof(setup->databuf));
|
|
|
321 |
if (t != 0) {
|
|
|
322 |
outputnl(doserr(t));
|
|
|
323 |
return(-1);
|
|
|
324 |
}
|
|
|
325 |
|
409 |
mateuszvis |
326 |
copiedcount_in++;
|
|
|
327 |
} while (findnext(dta) == 0);
|
|
|
328 |
|
|
|
329 |
}
|
|
|
330 |
|
|
|
331 |
sprintf(setup->databuf, "%u file(s) copied", copiedcount_out);
|
|
|
332 |
outputnl(setup->databuf);
|
|
|
333 |
|
403 |
mateuszvis |
334 |
return(-1);
|
|
|
335 |
}
|