Subversion Repositories SvarDOS

Rev

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