Subversion Repositories SvarDOS

Rev

Rev 421 | Rev 502 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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