Subversion Repositories SvarDOS

Rev

Rev 409 | Go to most recent revision | Details | 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.
19
 */
20
 
21
struct copy_setup {
22
  const char *src[64];
23
  unsigned short src_count; /* how many sources are declared */
24
  const char *dst;
25
  char src_asciimode[64];
26
  char dst_asciimode;
27
  char last_asciimode; /* /A or /B impacts the file preceding it and becomes the new default for all files that follow */
28
  char verifyflag;
29
  char lastitemwasplus;
30
  char databuf[BUFFER_SIZE - 512];
31
};
32
 
33
static int cmd_copy(struct cmd_funcparam *p) {
34
  struct copy_setup *setup = (void *)(p->BUFFER);
35
  unsigned short i;
36
 
37
  if (cmd_ishlp(p)) {
38
    outputnl("Copies one or more files to another location.");
39
    outputnl("");
40
    outputnl("COPY [/A|/B] source [/A|/B] [+source [/A|/B] [+...]] [destination [/A|/B]] [/V]");
41
    outputnl("");
42
    outputnl("source       Specifies the file or files to be copied");
43
    outputnl("/A           Indicates an ASCII text file");
44
    outputnl("/B           Indicates a binary file");
45
    outputnl("destination  Specifies the directory and/or filename for the new file(s)");
46
    outputnl("/V           Verifies that new files are written correctly");
47
    outputnl("");
48
    outputnl("To append files, specify a single file for destination, but multiple files");
49
    outputnl("for source (using wildcards or file1+file2+file3 format).");
50
    return(-1);
51
  }
52
 
53
  /* parse cmdline and fill the setup struct accordingly */
54
 
55
  memset(setup, 0, sizeof(*setup));
56
 
57
  for (i = 0; i < p->argc; i++) {
58
 
59
    /* switch? */
60
    if (p->argv[i][0] == '/') {
61
      if ((imatch(p->argv[i], "/a")) || (imatch(p->argv[i], "/b"))) {
62
        setup->last_asciimode = 'b';
63
        if (imatch(p->argv[i], "/a")) setup->last_asciimode = 'a';
64
        /* */
65
        if (setup->dst != NULL) {
66
          setup->dst_asciimode = setup->last_asciimode;
67
        } else if (setup->src_count != 0) {
68
          setup->src_asciimode[setup->src_count - 1] = setup->last_asciimode;
69
        }
70
      } else if (imatch(p->argv[i], "/v")) {
71
        setup->verifyflag = 1;
72
      } else {
73
        outputnl("Invalid switch");
74
        return(-1);
75
      }
76
      continue;
77
    }
78
 
79
    /* not a switch - must be either a source, a destination or a + */
80
    if (p->argv[i][0] == '+') {
81
      /* a plus cannot appear after destination or before first source */
82
      if ((setup->dst != NULL) || (setup->src_count == 0)) {
83
        outputnl("Invalid syntax");
84
        return(-1);
85
      }
86
      setup->lastitemwasplus = 1;
87
      /* a plus may be immediately followed by a filename - if so, emulate
88
       * a new argument */
89
      if (p->argv[i][1] != 0) {
90
        p->argv[i] += 1;
91
        i--;
92
      }
93
      continue;
94
    }
95
 
96
    /* src? (first non-switch or something that follows a +) */
97
    if ((setup->lastitemwasplus) || (setup->src_count == 0)) {
98
      setup->src[setup->src_count] = p->argv[i];
99
      setup->src_asciimode[setup->src_count] = setup->last_asciimode;
100
      setup->src_count++;
101
      setup->lastitemwasplus = 0;
102
      continue;
103
    }
104
 
105
    /* must be a dst then */
106
    if (setup->dst != NULL) {
107
      outputnl("Invalid syntax");
108
      return(-1);
109
    }
110
    setup->dst = p->argv[i];
111
    setup->dst_asciimode = setup->last_asciimode;
112
  }
113
 
114
  /* DEBUG: output setup content ("if 1" to enable) */
115
  #if 1
116
  printf("src: ");
117
  for (i = 0; i < setup->src_count; i++) {
118
    if (i != 0) printf(", ");
119
    printf("%s [%c]", setup->src[i], setup->src_asciimode[i]);
120
  }
121
  printf("\r\n");
122
  printf("dst: %s [%c]\r\n", setup->dst, setup->dst_asciimode);
123
  printf("verify: %s\r\n", (setup->verifyflag)?"ON":"OFF");
124
  #endif
125
 
126
  /* TODO perform the operation based on setup directives */
127
 
128
  return(-1);
129
}