Subversion Repositories SvarDOS

Rev

Rev 1050 | 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
 *
989 mateusz.vi 4
 * Copyright (C) 2021-2022 Mateusz Viste
421 mateuszvis 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
 
406 mateuszvis 25
/*
26
 * rename/ren
27
 */
28
 
533 mateuszvis 29
static enum cmd_result cmd_rename(struct cmd_funcparam *p) {
406 mateuszvis 30
  char *src = p->BUFFER;
501 mateuszvis 31
  char *dst = p->BUFFER + 256;
32
  char *buff1 = p->BUFFER + 512;
33
  char *buff2 = p->BUFFER + 1024;
406 mateuszvis 34
  unsigned short i, fnameoffset;
35
  struct DTA *dta = (void *)0x80; /* use default DTA in PSP */
36
 
37
  if (cmd_ishlp(p)) {
1050 mateusz.vi 38
    nls_outputnl(25,0); /* "Renames a file or a set of files" */
406 mateuszvis 39
    outputnl("");
989 mateusz.vi 40
    nls_outputnl(25,1); /* "RENAME [drive:][path]filename1 filename2" */
41
    nls_outputnl(25,2); /* "REN [drive:][path]filename1 filename2" */
406 mateuszvis 42
    outputnl("");
989 mateusz.vi 43
    nls_outputnl(25,3); /* "Note that you cannot specify a new drive or (...)" */
1050 mateusz.vi 44
    nls_outputnl(25,4); /* extra note about usage of wildcards */
533 mateuszvis 45
    return(CMD_OK);
406 mateuszvis 46
  }
47
 
48
  /* I expect exactly two arguments */
49
  if (p->argc != 2) {
989 mateusz.vi 50
    nls_outputnl(0,1); /* "Invalid syntax" */
533 mateuszvis 51
    return(CMD_FAIL);
406 mateuszvis 52
  }
53
 
54
  /* convert src to truename format */
55
  i = file_truename(p->argv[0], src);
56
  if (i != 0) {
538 mateuszvis 57
    nls_outputnl_doserr(i);
533 mateuszvis 58
    return(CMD_FAIL);
406 mateuszvis 59
  }
60
 
61
  /* copy src path to buffers and remember where the filename starts */
62
  fnameoffset = 0;
63
  for (i = 0;; i++) {
64
    buff1[i] = src[i];
65
    buff2[i] = src[i];
66
    if (buff1[i] == '\\') fnameoffset = i + 1;
67
    if (buff1[i] == 0) break;
68
  }
69
 
70
  /* now append dst filename to the buffer and validate it: cannot contain backslash, slash or ':' */
71
  for (i = 0;; i++) {
72
    switch (p->argv[1][i]) {
73
      case ':':
74
      case '\\':
75
      case '/':
990 mateusz.vi 76
        nls_outputnl(0,8); /* "Invalid destination" */
533 mateuszvis 77
        return(CMD_FAIL);
406 mateuszvis 78
    }
79
    buff1[fnameoffset + i] = p->argv[1][i];
80
    if (buff1[fnameoffset + i] == 0) break;
81
  }
82
 
83
  /* apply truename to dest to normalize wildcards into ? chars */
84
  i = file_truename(buff1, dst);
85
  if (i != 0) {
538 mateuszvis 86
    nls_outputnl_doserr(i);
533 mateuszvis 87
    return(CMD_FAIL);
406 mateuszvis 88
  }
89
 
90
  /* we're good to go, src and dst should look somehow like that now:
91
   * src   =   C:\TEMP\PATH\FILE????.TXT
92
   * dst   =   C:\TEMP\PATH\FILE????.DOC
93
   * buff1 =   C:\TEMP\PATH\
94
   * buff2 =   C:\TEMP\PATH\
95
   * fnameoffset = 13
96
   *
97
   * src is used for FindFirst/FindNext iterations, then buff1 is filled with
98
   * the source filename found by FindFirst/FindNext and buff2 is filled with
99
   * the destination file (with ?'s replaced by whatever is found at the same
100
   * location in buff1).
101
   */
102
 
1057 mateusz.vi 103
  i = findfirst(dta, src, DOS_ATTR_RO | DOS_ATTR_HID | DOS_ATTR_SYS | DOS_ATTR_ARC | DOS_ATTR_DIR);
538 mateuszvis 104
  if (i != 0) nls_outputnl_doserr(i);
406 mateuszvis 105
 
106
  while (i == 0) {
107
    /* write found fname into buff1 and dst fname into buff2 - both in FCB
108
     * format (MYFILE  EXT) so it is easy to compare them */
109
    file_fname2fcb(buff1 + fnameoffset, dta->fname);
110
    file_fname2fcb(buff2 + fnameoffset, dst + fnameoffset);
111
 
112
    /* scan buff2 fname for '?' and replace them with whatever is in buff1 */
113
    for (i = fnameoffset; buff2[i] != 0; i++) {
114
      if (buff2[i] == '?') buff2[i] = buff1[i];
115
    }
116
 
117
    /* fill buff1 with the 8+3 found file and convert the one in buff2 to 8+3 as well */
118
    file_fcb2fname(buff1 + fnameoffset, buff2 + fnameoffset);
119
    strcpy(buff2 + fnameoffset, buff1 + fnameoffset);
120
    strcpy(buff1 + fnameoffset, dta->fname);
121
 
122
    /* buff1 contains now a fully resolved source and buff2 a proper destination */
123
    #if 0  /* DEBUG ("if 1" to enable) */
124
    output(buff1);
125
    output(" -> ");
126
    outputnl(buff2);
127
    #endif
128
    /* call DOS to do the actual job */
129
    i = 0;
130
    _asm {
131
      push ax
132
      push di
133
      push dx
134
      push es
135
 
136
      mov ah, 0x56  /* rename file: DS:DX=ASCIIZ of src  ES:DI=ASCIIZ of dst */
137
      push ds
138
      pop es
139
      mov dx, buff1
140
      mov di, buff2
141
      int 0x21      /* CF clear on success, otherwise err code in AX */
142
      jnc DONE
143
      mov [i], ax   /* copy error code to i */
144
      DONE:
145
 
146
      pop es
147
      pop dx
148
      pop di
149
      pop ax
150
    }
151
    if (i != 0) {
152
      output(buff1 + fnameoffset);
153
      output(" -> ");
154
      output(buff2 + fnameoffset);
155
      output("  ");
538 mateuszvis 156
      nls_outputnl_doserr(i);
406 mateuszvis 157
    }
158
    /* next please */
159
    i = findnext(dta);
160
  }
161
 
533 mateuszvis 162
  return(CMD_OK);
406 mateuszvis 163
}