406 |
mateuszvis |
1 |
/*
|
|
|
2 |
* rename/ren
|
|
|
3 |
*/
|
|
|
4 |
|
|
|
5 |
static int cmd_rename(struct cmd_funcparam *p) {
|
|
|
6 |
char *src = p->BUFFER;
|
|
|
7 |
char *dst = p->BUFFER + (BUFFER_SIZE / 4);
|
|
|
8 |
char *buff1 = p->BUFFER + (BUFFER_SIZE / 4 * 2);
|
|
|
9 |
char *buff2 = p->BUFFER + (BUFFER_SIZE / 4 * 3);
|
|
|
10 |
unsigned short i, fnameoffset;
|
|
|
11 |
struct DTA *dta = (void *)0x80; /* use default DTA in PSP */
|
|
|
12 |
|
|
|
13 |
if (cmd_ishlp(p)) {
|
|
|
14 |
outputnl("Renames a file or files");
|
|
|
15 |
outputnl("");
|
|
|
16 |
outputnl("RENAME [drive:][path]filename1 filename2");
|
|
|
17 |
outputnl("REN [drive:][path]filename1 filename2");
|
|
|
18 |
outputnl("");
|
|
|
19 |
outputnl("Note that you cannot specify a new drive or path for your destination file.");
|
|
|
20 |
outputnl("Use MOVE to rename a directory, or to move files from one directory to another.");
|
|
|
21 |
return(-1);
|
|
|
22 |
}
|
|
|
23 |
|
|
|
24 |
/* I expect exactly two arguments */
|
|
|
25 |
if (p->argc != 2) {
|
|
|
26 |
outputnl("Invalid syntax");
|
|
|
27 |
return(-1);
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
/* convert src to truename format */
|
|
|
31 |
i = file_truename(p->argv[0], src);
|
|
|
32 |
if (i != 0) {
|
|
|
33 |
outputnl(doserr(i));
|
|
|
34 |
return(-1);
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
/* copy src path to buffers and remember where the filename starts */
|
|
|
38 |
fnameoffset = 0;
|
|
|
39 |
for (i = 0;; i++) {
|
|
|
40 |
buff1[i] = src[i];
|
|
|
41 |
buff2[i] = src[i];
|
|
|
42 |
if (buff1[i] == '\\') fnameoffset = i + 1;
|
|
|
43 |
if (buff1[i] == 0) break;
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
/* now append dst filename to the buffer and validate it: cannot contain backslash, slash or ':' */
|
|
|
47 |
for (i = 0;; i++) {
|
|
|
48 |
switch (p->argv[1][i]) {
|
|
|
49 |
case ':':
|
|
|
50 |
case '\\':
|
|
|
51 |
case '/':
|
|
|
52 |
outputnl("Invalid destination");
|
|
|
53 |
return(-1);
|
|
|
54 |
}
|
|
|
55 |
buff1[fnameoffset + i] = p->argv[1][i];
|
|
|
56 |
if (buff1[fnameoffset + i] == 0) break;
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
/* apply truename to dest to normalize wildcards into ? chars */
|
|
|
60 |
i = file_truename(buff1, dst);
|
|
|
61 |
if (i != 0) {
|
|
|
62 |
outputnl(doserr(i));
|
|
|
63 |
return(-1);
|
|
|
64 |
}
|
|
|
65 |
|
|
|
66 |
/* we're good to go, src and dst should look somehow like that now:
|
|
|
67 |
* src = C:\TEMP\PATH\FILE????.TXT
|
|
|
68 |
* dst = C:\TEMP\PATH\FILE????.DOC
|
|
|
69 |
* buff1 = C:\TEMP\PATH\
|
|
|
70 |
* buff2 = C:\TEMP\PATH\
|
|
|
71 |
* fnameoffset = 13
|
|
|
72 |
*
|
|
|
73 |
* src is used for FindFirst/FindNext iterations, then buff1 is filled with
|
|
|
74 |
* the source filename found by FindFirst/FindNext and buff2 is filled with
|
|
|
75 |
* the destination file (with ?'s replaced by whatever is found at the same
|
|
|
76 |
* location in buff1).
|
|
|
77 |
*/
|
|
|
78 |
|
|
|
79 |
i = findfirst(dta, src, 0);
|
|
|
80 |
if (i != 0) outputnl(doserr(i));
|
|
|
81 |
|
|
|
82 |
while (i == 0) {
|
|
|
83 |
/* write found fname into buff1 and dst fname into buff2 - both in FCB
|
|
|
84 |
* format (MYFILE EXT) so it is easy to compare them */
|
|
|
85 |
file_fname2fcb(buff1 + fnameoffset, dta->fname);
|
|
|
86 |
file_fname2fcb(buff2 + fnameoffset, dst + fnameoffset);
|
|
|
87 |
|
|
|
88 |
/* scan buff2 fname for '?' and replace them with whatever is in buff1 */
|
|
|
89 |
for (i = fnameoffset; buff2[i] != 0; i++) {
|
|
|
90 |
if (buff2[i] == '?') buff2[i] = buff1[i];
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
/* fill buff1 with the 8+3 found file and convert the one in buff2 to 8+3 as well */
|
|
|
94 |
file_fcb2fname(buff1 + fnameoffset, buff2 + fnameoffset);
|
|
|
95 |
strcpy(buff2 + fnameoffset, buff1 + fnameoffset);
|
|
|
96 |
strcpy(buff1 + fnameoffset, dta->fname);
|
|
|
97 |
|
|
|
98 |
/* buff1 contains now a fully resolved source and buff2 a proper destination */
|
|
|
99 |
#if 0 /* DEBUG ("if 1" to enable) */
|
|
|
100 |
output(buff1);
|
|
|
101 |
output(" -> ");
|
|
|
102 |
outputnl(buff2);
|
|
|
103 |
#endif
|
|
|
104 |
/* call DOS to do the actual job */
|
|
|
105 |
i = 0;
|
|
|
106 |
_asm {
|
|
|
107 |
push ax
|
|
|
108 |
push di
|
|
|
109 |
push dx
|
|
|
110 |
push es
|
|
|
111 |
|
|
|
112 |
mov ah, 0x56 /* rename file: DS:DX=ASCIIZ of src ES:DI=ASCIIZ of dst */
|
|
|
113 |
push ds
|
|
|
114 |
pop es
|
|
|
115 |
mov dx, buff1
|
|
|
116 |
mov di, buff2
|
|
|
117 |
int 0x21 /* CF clear on success, otherwise err code in AX */
|
|
|
118 |
jnc DONE
|
|
|
119 |
mov [i], ax /* copy error code to i */
|
|
|
120 |
DONE:
|
|
|
121 |
|
|
|
122 |
pop es
|
|
|
123 |
pop dx
|
|
|
124 |
pop di
|
|
|
125 |
pop ax
|
|
|
126 |
}
|
|
|
127 |
if (i != 0) {
|
|
|
128 |
output(buff1 + fnameoffset);
|
|
|
129 |
output(" -> ");
|
|
|
130 |
output(buff2 + fnameoffset);
|
|
|
131 |
output(" ");
|
|
|
132 |
outputnl(doserr(i));
|
|
|
133 |
}
|
|
|
134 |
/* next please */
|
|
|
135 |
i = findnext(dta);
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
return(-1);
|
|
|
139 |
}
|