forked from FDOS/freecom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ren.c
224 lines (199 loc) · 5.49 KB
/
ren.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/* $Id$
* REN.C - rename command
*
* 27-Jul-1998 (John P Price <[email protected]>)
* - added config.h include
* 15-Mar-2003 (Wolf Bergenheim <[email protected]>)
* - added support for * and ?
*
*/
#include "../config.h"
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
/*#define DEBUG*/
#include <dfn.h>
#include <suppl.h>
#include <supplio.h>
#include "../include/lfnfuncs.h"
#include "../include/cmdline.h"
#include "../include/command.h"
#include "../include/misc.h"
#include "../err_fcts.h"
void myfnsplit( const char *path,
char *buf,
char *drv,
char **dir,
char **name,
char **ext )
{
const char* end;
const char* p;
const char* s;
if( path[ 0 ] && path[ 1 ] == ':' ) {
if( drv ) {
*drv++ = *path++;
*drv++ = *path++;
*drv = '\0';
}
} else if( drv ) *drv = '\0';
for( end = path; *end && *end != ':'; ) end++;
for( p = end; p > path && *--p != '\\'/* && *p != '/'*/; )
if( *p == '.' ) {
end = p;
break;
}
if ( ext ) {
*ext = buf;
for( s = end; ( *buf++ = *s++ ) != '\0'; );
}
for( p = end; p > path; )
if( *--p == '\\'/* || *p == '/'*/) {
/*
* '/' can't happen as a path seperator in FreeCOM because it's treated
* as a switch character no matter where it's found
*/
p++;
break;
}
if( name ) {
*name = buf;
for( s = p; s < end; ) *buf++ = *s++;
*buf++ = '\0';
}
if ( dir ) {
*dir = buf;
for( s = path; s < p; ) *buf++ = *s++;
*buf = '\0';
}
}
void myfnmerge( char *path, const char *drive, const char *dir,
const char *fname, const char *ext )
{
if( *drive ) {
strcpy( path, drive );
#if 0 /* Unused */
if( !drive[ 1 ] )strcat( path, ":" );
#endif
} else ( *path ) = 0;
if( *dir ) {
strcat( path, dir );
if( *( dir + strlen( dir ) - 1 ) != '\\' )
strcat( path, "\\" );
}
if( *fname ) {
strcat( path, fname );
if( ext && *ext ) {
#if 0 /* Unused */
if( *ext != '.' ) strcat( path, "." );
#endif
strcat( path, ext );
}
}
}
int cmd_rename(char *param)
{
char **argv;
int argc, opts, ec = E_None;
struct dos_ffblk ff;
int appState;
if((argv = scanCmdline(param, 0, 0, &argc, &opts)) == 0)
return 1;
appState = appendDisable();
if(argc < 2) {
error_req_param_missing();
ec = E_Useage;
}
else if(argc > 2) {
error_too_many_parameters(param);
ec = E_Useage;
} else if(dos_findfirst(argv[0], &ff
, FA_NORMAL|FA_DIREC|FA_ARCH|FA_SYSTEM|FA_RDONLY|FA_HIDDEN) != 0) {
error_sfile_not_found(argv[0]);
/* ec == E_None */
} else {
#if 0 /*
* There is some dynamic memory bug in relation to dfnmerge
* Let's spare ourselves the whole mucking about in dynamic memory
* thing, and we can use myfnmerge/myfnsplit to make everything work.
* Unless of course someone wants to fix the bug in dfnmerge/split
* So... the old code is still here if anyone wants to give it a shot
* AND, if anyone tries to blame the _REGISTER usage in lfnfuncs.c as
* the culprit, it already uses intr instead :-)
*/
char *s_drv = NULL, *s_dir = NULL, *s_fil = NULL, *s_ext = NULL;
char *d_drv = NULL, *d_dir = NULL, *d_fil = NULL, *d_ext = NULL;
char *newname = 0, *sn = 0, *dn = 0;
char *oldFname;
#define p oldFname
if(!dfnsplit(argv[0], &s_drv, &s_dir, &s_fil, &s_ext)
|| !dfnsplit(argv[1], &d_drv, &d_dir, &d_fil, &d_ext)
|| (sn = dfnmerge(0, s_drv, s_dir, "", NULL)) == 0
|| (p = realloc(sn, strlen(sn) + sizeof(ff.ff_name) + 1)) == 0) {
/* build dir to use */
error_out_of_memory();
ec = E_NoMem;
goto errRet;
}
sn = p;
p = strchr(sn, '\0');
#undef p
#else
char s_drv[ MAXDRIVE ], d_drv[ MAXDRIVE ];
char *s_dir, *d_dir, *d_fil, *d_ext;
char s_buf[ MAXPATH ], d_buf[ MAXPATH ],
*sn, dn[ MAXPATH ], newname[MAXPATH];
myfnsplit( argv[ 0 ], s_buf, s_drv, &s_dir, NULL, NULL );
myfnsplit( argv[ 1 ], d_buf, d_drv, &d_dir, &d_fil, &d_ext );
#endif
/* if drive or path in second arg, return with syntax error */
if((d_drv[ 0 ]) || (d_dir[ 0 ])) {
error_syntax(argv[1]);
ec = E_Useage;
goto errRet;
}
#if 0
if((dn = dfnmerge(0, s_drv, s_dir, d_fil, d_ext)) == 0) {
/* build dir to use */
error_out_of_memory();
ec = E_NoMem;
goto errRet;
}
#else
myfnmerge( dn, s_drv, s_dir, d_fil, d_ext );
/* d_buf no longer used after this */
sn = d_buf;
#endif
do {
fillFnam(newname, dn, ff.ff_name);
#if 0
if(newname[0] == 0) {
error_out_of_memory();
ec = E_NoMem;
break;
}
#endif
myfnmerge( sn, s_drv, s_dir, ff.ff_name, NULL );
dprintf(("rename(%s, %s)\n", sn, newname) );
if(rename(sn, newname) != 0) {
myperror("rename");
ec = E_Other;
break;
}
/*free(newname);*/
newname[0] = 0;
} while(dos_findnext(&ff) == 0);
errRet:
dos_findclose(&ff);
#if 0 /* free(newname) isn't even necessary because it is free'd above */
free(s_drv); free(s_dir); free(s_fil); free(s_ext);
free(d_drv); free(d_dir); free(d_fil); free(d_ext);
free(newname); free(sn); free(dn);
#endif
}
appendRestore(appState);
freep(argv);
return ec;
}