Subversion Repositories SvarDOS

Rev

Rev 409 | Rev 413 | 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) */
112
    cmp ax, 0
113
    je ENDOFFILE
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
    /* if dst ascii mode -> add an EOF */
127
    ENDOFFILE:
128
    /* TODO */
129
 
130
    jmp CLOSESRC
131
 
132
    FAIL:
133
    mov [errcode], ax
134
 
135
    CLOSESRC:
136
    /* close src and dst */
137
    mov bx, [srch]
138
    cmp bx, 0xffff
139
    je CLOSEDST
140
    mov ah, 0x3e   /* DOS 2+ -- close a file handle */
141
    int 0x21
142
 
143
    CLOSEDST:
144
    mov bx, [dsth]
145
    cmp bx, 0xffff
146
    je DONE
147
    mov ah, 0x3e   /* DOS 2+ -- close a file handle */
148
    int 0x21
149
 
150
    DONE:
151
  }
152
  return(errcode);
153
}
154
 
155
 
403 mateuszvis 156
static int cmd_copy(struct cmd_funcparam *p) {
157
  struct copy_setup *setup = (void *)(p->BUFFER);
158
  unsigned short i;
409 mateuszvis 159
  unsigned short copiedcount_in = 0, copiedcount_out = 0; /* number of input/output copied files */
160
  struct DTA *dta = (void *)0x80; /* use DTA at default location in PSP */
403 mateuszvis 161
 
162
  if (cmd_ishlp(p)) {
163
    outputnl("Copies one or more files to another location.");
164
    outputnl("");
165
    outputnl("COPY [/A|/B] source [/A|/B] [+source [/A|/B] [+...]] [destination [/A|/B]] [/V]");
166
    outputnl("");
167
    outputnl("source       Specifies the file or files to be copied");
168
    outputnl("/A           Indicates an ASCII text file");
169
    outputnl("/B           Indicates a binary file");
170
    outputnl("destination  Specifies the directory and/or filename for the new file(s)");
171
    outputnl("/V           Verifies that new files are written correctly");
172
    outputnl("");
173
    outputnl("To append files, specify a single file for destination, but multiple files");
174
    outputnl("for source (using wildcards or file1+file2+file3 format).");
175
    return(-1);
176
  }
177
 
178
  /* parse cmdline and fill the setup struct accordingly */
179
 
180
  memset(setup, 0, sizeof(*setup));
181
 
182
  for (i = 0; i < p->argc; i++) {
183
 
184
    /* switch? */
185
    if (p->argv[i][0] == '/') {
186
      if ((imatch(p->argv[i], "/a")) || (imatch(p->argv[i], "/b"))) {
187
        setup->last_asciimode = 'b';
188
        if (imatch(p->argv[i], "/a")) setup->last_asciimode = 'a';
189
        /* */
409 mateuszvis 190
        if (setup->dst[0] != 0) {
403 mateuszvis 191
          setup->dst_asciimode = setup->last_asciimode;
192
        } else if (setup->src_count != 0) {
193
          setup->src_asciimode[setup->src_count - 1] = setup->last_asciimode;
194
        }
195
      } else if (imatch(p->argv[i], "/v")) {
196
        setup->verifyflag = 1;
197
      } else {
198
        outputnl("Invalid switch");
199
        return(-1);
200
      }
201
      continue;
202
    }
203
 
204
    /* not a switch - must be either a source, a destination or a + */
205
    if (p->argv[i][0] == '+') {
206
      /* a plus cannot appear after destination or before first source */
409 mateuszvis 207
      if ((setup->dst[0] != 0) || (setup->src_count == 0)) {
403 mateuszvis 208
        outputnl("Invalid syntax");
209
        return(-1);
210
      }
211
      setup->lastitemwasplus = 1;
212
      /* a plus may be immediately followed by a filename - if so, emulate
213
       * a new argument */
214
      if (p->argv[i][1] != 0) {
215
        p->argv[i] += 1;
216
        i--;
217
      }
218
      continue;
219
    }
220
 
221
    /* src? (first non-switch or something that follows a +) */
222
    if ((setup->lastitemwasplus) || (setup->src_count == 0)) {
223
      setup->src[setup->src_count] = p->argv[i];
224
      setup->src_asciimode[setup->src_count] = setup->last_asciimode;
225
      setup->src_count++;
226
      setup->lastitemwasplus = 0;
227
      continue;
228
    }
229
 
230
    /* must be a dst then */
409 mateuszvis 231
    if (setup->dst[0] != 0) {
403 mateuszvis 232
      outputnl("Invalid syntax");
233
      return(-1);
234
    }
409 mateuszvis 235
    if (file_truename(p->argv[i], setup->dst) != 0) {
236
      outputnl("Invalid destination");
237
      return(-1);
238
    }
403 mateuszvis 239
    setup->dst_asciimode = setup->last_asciimode;
409 mateuszvis 240
    /* if dst is a directory then append a backslash */
241
    setup->dstlen = cmd_copy_addbkslash_if_dir(setup->dst);
403 mateuszvis 242
  }
243
 
244
  /* DEBUG: output setup content ("if 1" to enable) */
245
  #if 1
246
  printf("src: ");
247
  for (i = 0; i < setup->src_count; i++) {
248
    if (i != 0) printf(", ");
249
    printf("%s [%c]", setup->src[i], setup->src_asciimode[i]);
250
  }
251
  printf("\r\n");
252
  printf("dst: %s [%c]\r\n", setup->dst, setup->dst_asciimode);
253
  printf("verify: %s\r\n", (setup->verifyflag)?"ON":"OFF");
254
  #endif
255
 
409 mateuszvis 256
  /* must have at least one source */
257
  if (setup->src_count == 0) {
258
    outputnl("Required parameter missing");
259
    return(-1);
260
  }
403 mateuszvis 261
 
409 mateuszvis 262
  /* perform the operation based on setup directives:
263
   * iterate over every source and copy it to dest */
264
 
265
  for (i = 0; i < setup->src_count; i++) {
266
    unsigned short t;
267
    unsigned short databuflen;
268
    unsigned short pathendoffset;
269
 
270
    /* resolve truename of src and write it to buffer */
271
    t = file_truename(setup->src[i], setup->databuf);
272
    if (t != 0) {
273
      output(setup->src[i]);
274
      output(" - ");
275
      outputnl(doserr(t));
276
      continue;
277
    }
278
    databuflen = strlen(setup->databuf); /* remember databuf length */
279
 
280
    /* if length zero, skip (not sure why this would be possible, though) */
281
    if (databuflen == 0) continue;
282
 
283
    /* if src does not end with a backslash AND it is a directory then append a backslash */
284
    databuflen = cmd_copy_addbkslash_if_dir(setup->databuf);
285
 
286
    /* if src ends with a '\' then append *.* */
287
    if (setup->databuf[databuflen - 1] == '\\') {
288
      strcat(setup->databuf, "*.*");
289
    }
290
 
291
    /* remember where the path in databuf ends */
292
    for (t = 0; setup->databuf[t] != 0; t++) {
293
      if (setup->databuf[t] == '\\') pathendoffset = t + 1;
294
    }
295
 
296
    /* */
297
    if (findfirst(dta, setup->databuf, 0) != 0) {
298
      continue;
299
    }
300
 
301
    do {
412 mateuszvis 302
      char appendflag;
409 mateuszvis 303
      if (dta->attr & DOS_ATTR_DIR) continue; /* skip directories */
304
 
305
      /* compute full path/name of the file */
306
      strcpy(setup->databuf + pathendoffset, dta->fname);
307
 
308
      /* if there was no destination, then YOU are the destination now!
309
       * this handles situations like COPY a.txt+b.txt+c.txt */
310
      if (setup->dst[0] == NULL) {
311
        strcpy(setup->dst, setup->databuf);
312
        setup->dstlen = strlen(setup->dst);
313
        copiedcount_in++;
314
        copiedcount_out++;
315
        continue;
316
      }
317
 
318
      /* is dst ending with a backslash? then append fname to it */
319
      if (setup->dst[setup->dstlen - 1] == '\\') strcpy(setup->dst + setup->dstlen, dta->fname);
320
 
321
      /* now databuf contains the full source and dst contains the full dest... COPY TIME! */
322
 
323
      /* if dst file exists already -> overwrite it or append?
324
          - if dst is a dir (dstlen-1 points at a \\) -> overwrite
325
          - otherwise: if copiedcount_in==0 overwrite, else append */
326
      output(setup->databuf);
327
      if ((setup->dst[setup->dstlen - 1] == '\\') || (copiedcount_in == 0)) {
412 mateuszvis 328
        appendflag = 0;
409 mateuszvis 329
        output(" > ");
330
        copiedcount_out++;
331
      } else {
412 mateuszvis 332
        appendflag = 1;
409 mateuszvis 333
        output(" >> ");
334
      }
335
      outputnl(setup->dst);
336
 
412 mateuszvis 337
      t = cmd_copy_internal(setup->dst, 0, setup->databuf, 0, appendflag, setup->databuf, sizeof(setup->databuf));
338
      if (t != 0) {
339
        outputnl(doserr(t));
340
        return(-1);
341
      }
342
 
409 mateuszvis 343
      copiedcount_in++;
344
    } while (findnext(dta) == 0);
345
 
346
  }
347
 
348
  sprintf(setup->databuf, "%u file(s) copied", copiedcount_out);
349
  outputnl(setup->databuf);
350
 
403 mateuszvis 351
  return(-1);
352
}