Subversion Repositories SvarDOS

Rev

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

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