Subversion Repositories SvarDOS

Rev

Rev 403 | Rev 412 | 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
 
403 mateuszvis 56
static int cmd_copy(struct cmd_funcparam *p) {
57
  struct copy_setup *setup = (void *)(p->BUFFER);
58
  unsigned short i;
409 mateuszvis 59
  unsigned short copiedcount_in = 0, copiedcount_out = 0; /* number of input/output copied files */
60
  struct DTA *dta = (void *)0x80; /* use DTA at default location in PSP */
403 mateuszvis 61
 
62
  if (cmd_ishlp(p)) {
63
    outputnl("Copies one or more files to another location.");
64
    outputnl("");
65
    outputnl("COPY [/A|/B] source [/A|/B] [+source [/A|/B] [+...]] [destination [/A|/B]] [/V]");
66
    outputnl("");
67
    outputnl("source       Specifies the file or files to be copied");
68
    outputnl("/A           Indicates an ASCII text file");
69
    outputnl("/B           Indicates a binary file");
70
    outputnl("destination  Specifies the directory and/or filename for the new file(s)");
71
    outputnl("/V           Verifies that new files are written correctly");
72
    outputnl("");
73
    outputnl("To append files, specify a single file for destination, but multiple files");
74
    outputnl("for source (using wildcards or file1+file2+file3 format).");
75
    return(-1);
76
  }
77
 
78
  /* parse cmdline and fill the setup struct accordingly */
79
 
80
  memset(setup, 0, sizeof(*setup));
81
 
82
  for (i = 0; i < p->argc; i++) {
83
 
84
    /* switch? */
85
    if (p->argv[i][0] == '/') {
86
      if ((imatch(p->argv[i], "/a")) || (imatch(p->argv[i], "/b"))) {
87
        setup->last_asciimode = 'b';
88
        if (imatch(p->argv[i], "/a")) setup->last_asciimode = 'a';
89
        /* */
409 mateuszvis 90
        if (setup->dst[0] != 0) {
403 mateuszvis 91
          setup->dst_asciimode = setup->last_asciimode;
92
        } else if (setup->src_count != 0) {
93
          setup->src_asciimode[setup->src_count - 1] = setup->last_asciimode;
94
        }
95
      } else if (imatch(p->argv[i], "/v")) {
96
        setup->verifyflag = 1;
97
      } else {
98
        outputnl("Invalid switch");
99
        return(-1);
100
      }
101
      continue;
102
    }
103
 
104
    /* not a switch - must be either a source, a destination or a + */
105
    if (p->argv[i][0] == '+') {
106
      /* a plus cannot appear after destination or before first source */
409 mateuszvis 107
      if ((setup->dst[0] != 0) || (setup->src_count == 0)) {
403 mateuszvis 108
        outputnl("Invalid syntax");
109
        return(-1);
110
      }
111
      setup->lastitemwasplus = 1;
112
      /* a plus may be immediately followed by a filename - if so, emulate
113
       * a new argument */
114
      if (p->argv[i][1] != 0) {
115
        p->argv[i] += 1;
116
        i--;
117
      }
118
      continue;
119
    }
120
 
121
    /* src? (first non-switch or something that follows a +) */
122
    if ((setup->lastitemwasplus) || (setup->src_count == 0)) {
123
      setup->src[setup->src_count] = p->argv[i];
124
      setup->src_asciimode[setup->src_count] = setup->last_asciimode;
125
      setup->src_count++;
126
      setup->lastitemwasplus = 0;
127
      continue;
128
    }
129
 
130
    /* must be a dst then */
409 mateuszvis 131
    if (setup->dst[0] != 0) {
403 mateuszvis 132
      outputnl("Invalid syntax");
133
      return(-1);
134
    }
409 mateuszvis 135
    if (file_truename(p->argv[i], setup->dst) != 0) {
136
      outputnl("Invalid destination");
137
      return(-1);
138
    }
403 mateuszvis 139
    setup->dst_asciimode = setup->last_asciimode;
409 mateuszvis 140
    /* if dst is a directory then append a backslash */
141
    setup->dstlen = cmd_copy_addbkslash_if_dir(setup->dst);
403 mateuszvis 142
  }
143
 
144
  /* DEBUG: output setup content ("if 1" to enable) */
145
  #if 1
146
  printf("src: ");
147
  for (i = 0; i < setup->src_count; i++) {
148
    if (i != 0) printf(", ");
149
    printf("%s [%c]", setup->src[i], setup->src_asciimode[i]);
150
  }
151
  printf("\r\n");
152
  printf("dst: %s [%c]\r\n", setup->dst, setup->dst_asciimode);
153
  printf("verify: %s\r\n", (setup->verifyflag)?"ON":"OFF");
154
  #endif
155
 
409 mateuszvis 156
  /* must have at least one source */
157
  if (setup->src_count == 0) {
158
    outputnl("Required parameter missing");
159
    return(-1);
160
  }
403 mateuszvis 161
 
409 mateuszvis 162
  /* perform the operation based on setup directives:
163
   * iterate over every source and copy it to dest */
164
 
165
  for (i = 0; i < setup->src_count; i++) {
166
    unsigned short t;
167
    unsigned short databuflen;
168
    unsigned short pathendoffset;
169
 
170
    /* resolve truename of src and write it to buffer */
171
    t = file_truename(setup->src[i], setup->databuf);
172
    if (t != 0) {
173
      output(setup->src[i]);
174
      output(" - ");
175
      outputnl(doserr(t));
176
      continue;
177
    }
178
    databuflen = strlen(setup->databuf); /* remember databuf length */
179
 
180
    /* if length zero, skip (not sure why this would be possible, though) */
181
    if (databuflen == 0) continue;
182
 
183
    /* if src does not end with a backslash AND it is a directory then append a backslash */
184
    databuflen = cmd_copy_addbkslash_if_dir(setup->databuf);
185
 
186
    /* if src ends with a '\' then append *.* */
187
    if (setup->databuf[databuflen - 1] == '\\') {
188
      strcat(setup->databuf, "*.*");
189
    }
190
 
191
    /* remember where the path in databuf ends */
192
    for (t = 0; setup->databuf[t] != 0; t++) {
193
      if (setup->databuf[t] == '\\') pathendoffset = t + 1;
194
    }
195
 
196
    /* */
197
    if (findfirst(dta, setup->databuf, 0) != 0) {
198
      continue;
199
    }
200
 
201
    do {
202
      if (dta->attr & DOS_ATTR_DIR) continue; /* skip directories */
203
 
204
      /* compute full path/name of the file */
205
      strcpy(setup->databuf + pathendoffset, dta->fname);
206
 
207
      /* if there was no destination, then YOU are the destination now!
208
       * this handles situations like COPY a.txt+b.txt+c.txt */
209
      if (setup->dst[0] == NULL) {
210
        strcpy(setup->dst, setup->databuf);
211
        setup->dstlen = strlen(setup->dst);
212
        copiedcount_in++;
213
        copiedcount_out++;
214
        continue;
215
      }
216
 
217
      /* is dst ending with a backslash? then append fname to it */
218
      if (setup->dst[setup->dstlen - 1] == '\\') strcpy(setup->dst + setup->dstlen, dta->fname);
219
 
220
      /* now databuf contains the full source and dst contains the full dest... COPY TIME! */
221
 
222
      /* if dst file exists already -> overwrite it or append?
223
          - if dst is a dir (dstlen-1 points at a \\) -> overwrite
224
          - otherwise: if copiedcount_in==0 overwrite, else append */
225
      output(setup->databuf);
226
      if ((setup->dst[setup->dstlen - 1] == '\\') || (copiedcount_in == 0)) {
227
        output(" > ");
228
        copiedcount_out++;
229
      } else {
230
        output(" >> ");
231
      }
232
      outputnl(setup->dst);
233
 
234
      copiedcount_in++;
235
    } while (findnext(dta) == 0);
236
 
237
  }
238
 
239
  sprintf(setup->databuf, "%u file(s) copied", copiedcount_out);
240
  outputnl(setup->databuf);
241
 
403 mateuszvis 242
  return(-1);
243
}