GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — reactos/base/shell/cmd/move.c at master · reactos/reactos

Expand file tree

/

move.c

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
522 lines (469 loc) · 18.3 KB
/

move.c

Copy path

File metadata and controls

522 lines (469 loc) · 18.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
/*
* MOVE.C - move internal command.
*
*
* History:
*
* 14-Dec-1998 (Eric Kohl)
* Started.
*
* 18-Jan-1999 (Eric Kohl)
* Unicode safe!
* Preliminary version!!!
*
* 20-Jan-1999 (Eric Kohl)
* Redirection safe!
*
* 27-Jan-1999 (Eric Kohl)
* Added help text ("/?").
* Added more error checks.
*
* 03-Feb-1999 (Eric Kohl)
* Added "/N" option.
*
* 30-Apr-2005 (Magnus Olsen <[email protected]>)
* Remove all hardcoded strings in En.rc
*
* 24-Jun-2005 (Brandon Turner <[email protected]>)
* Fixed bug to allow MS style wildcards + code clean up
* added /y and /-y
*/
#include "precomp.h"
#ifdef INCLUDE_CMD_MOVE
enum
{
MOVE_NOTHING = 0x001, /* /N */
MOVE_OVER_YES = 0x002, /* /Y */
MOVE_OVER_NO = 0x004, /* /-Y */
};
enum
{
/* Move status flags */
MOVE_SOURCE_IS_DIR = 0x001,
MOVE_SOURCE_IS_FILE = 0x002,
MOVE_DEST_IS_DIR = 0x004,
MOVE_DEST_IS_FILE = 0x008,
MOVE_SOURCE_HAS_WILD = 0x010, /* source has wildcard */
MOVE_SRC_CURRENT_IS_DIR = 0x020, /* source is file but at the current round we found a directory */
MOVE_DEST_EXISTS = 0x040,
MOVE_PATHS_ON_DIF_VOL = 0x080 /* source and destination paths are on different volume */
};
static INT MoveOverwrite (LPTSTR fn)
{
/* ask the user if they want to override */
INT res;
ConOutResPrintf(STRING_MOVE_HELP1, fn);
res = FilePromptYNA (0);
return res;
}
void GetDirectory (LPTSTR wholepath, LPTSTR directory, BOOL CheckExisting)
{
/* returns only directory part of path with backslash */
/* TODO: make code unc aware */
/* Is there a better alternative to this? */
LPTSTR last;
if (CheckExisting && IsExistingDirectory(wholepath))
{
_tcscpy(directory, wholepath);
}
else if ((last = _tcsrchr(wholepath,_T('\\'))) != NULL)
{
_tcsncpy(directory, wholepath, last - wholepath + 1);
directory[last - wholepath + 1] = 0;
}
else
{
GetRootPath(wholepath,directory, MAX_PATH);
}
}
INT cmd_move (LPTSTR param)
{
LPTSTR *arg;
INT argc, i, nFiles;
LPTSTR pszDest;
TCHAR szDestPath[MAX_PATH];
TCHAR szFullDestPath[MAX_PATH];
TCHAR szSrcDirPath[MAX_PATH];
TCHAR szSrcPath[MAX_PATH];
TCHAR szFullSrcPath[MAX_PATH];
DWORD dwFlags = 0;
INT nOverwrite = 0;
WIN32_FIND_DATA findBuffer;
HANDLE hFile;
/* used only when source and destination directories are on different volume */
HANDLE hDestFile = NULL;
WIN32_FIND_DATA findDestBuffer;
TCHAR szMoveDest[MAX_PATH];
TCHAR szMoveSrc[MAX_PATH];
LPTSTR pszDestDirPointer;
LPTSTR pszSrcDirPointer;
INT nDirLevel = 0;
LPTSTR pszFile;
BOOL OnlyOneFile;
BOOL FoundFile;
BOOL MoveStatus;
DWORD dwMoveFlags = 0;
DWORD dwMoveStatusFlags = 0;
if (!_tcsncmp (param, _T("/?"), 2))
{
#if 0
ConOutPuts (_T("Moves files and renames files and directories.\n\n"
"To move one or more files:\n"
"MOVE [/N][/Y|/-Y][drive:][path]filename1[,...] destination\n"
"\n"
"To rename a directory:\n"
"MOVE [/N][/Y|/-Y][drive:][path]dirname1 dirname2\n"
"\n"
" [drive:][path]filename1 Specifies the location and name of the file\n"
" or files you want to move.\n"
" /N Nothing. Don everthing but move files or directories.\n"
" /Y\n"
" /-Y\n"
"..."));
#else
ConOutResPaging(TRUE,STRING_MOVE_HELP2);
#endif
return 0;
}
nErrorLevel = 0;
arg = splitspace(param, &argc);
/* read options */
for (i = 0; i < argc; i++)
{
if (!_tcsicmp(arg[i], _T("/N")))
dwFlags |= MOVE_NOTHING;
else if (!_tcsicmp(arg[i], _T("/Y")))
dwFlags |= MOVE_OVER_YES;
else if (!_tcsicmp(arg[i], _T("/-Y")))
dwFlags |= MOVE_OVER_NO;
else
break;
}
nFiles = argc - i;
if (nFiles < 1)
{
/* there must be at least one pathspec */
error_req_param_missing();
nErrorLevel = 1;
goto Quit;
}
if (nFiles > 2)
{
/* there are more than two pathspecs */
error_too_many_parameters(param);
nErrorLevel = 1;
goto Quit;
}
/* If no destination is given, default to current directory */
pszDest = (nFiles == 1) ? _T(".") : arg[i + 1];
/* check for wildcards in source and destination */
if (_tcschr(pszDest, _T('*')) != NULL || _tcschr(pszDest, _T('?')) != NULL)
{
/* '*'/'?' in dest, this doesnt happen. give folder name instead*/
error_invalid_parameter_format(pszDest);
nErrorLevel = 1;
goto Quit;
}
if (_tcschr(arg[i], _T('*')) != NULL || _tcschr(arg[i], _T('?')) != NULL)
{
dwMoveStatusFlags |= MOVE_SOURCE_HAS_WILD;
}
/* get destination */
GetFullPathName (pszDest, MAX_PATH, szDestPath, NULL);
TRACE ("Destination: %s\n", debugstr_aw(szDestPath));
/* get source folder */
GetFullPathName(arg[i], MAX_PATH, szSrcDirPath, &pszFile);
if (pszFile != NULL)
*pszFile = _T('\0');
TRACE ("Source Folder: %s\n", debugstr_aw(szSrcDirPath));
hFile = FindFirstFile (arg[i], &findBuffer);
if (hFile == INVALID_HANDLE_VALUE)
{
ErrorMessage(GetLastError(), arg[i]);
nErrorLevel = 1;
goto Quit;
}
/* check for special cases "." and ".." and if found skip them */
FoundFile = TRUE;
while(FoundFile &&
(_tcscmp(findBuffer.cFileName,_T(".")) == 0 ||
_tcscmp(findBuffer.cFileName,_T("..")) == 0))
FoundFile = FindNextFile (hFile, &findBuffer);
if (!FoundFile)
{
/* what? we don't have anything to move? */
error_file_not_found();
FindClose(hFile);
nErrorLevel = 1;
goto Quit;
}
OnlyOneFile = TRUE;
/* check if there can be found files as files have first priority */
if (findBuffer.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
dwMoveStatusFlags |= MOVE_SOURCE_IS_DIR;
else
dwMoveStatusFlags |= MOVE_SOURCE_IS_FILE;
while(OnlyOneFile && FindNextFile(hFile,&findBuffer))
{
if (!(findBuffer.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
ConOutPrintf(_T(""));
if (dwMoveStatusFlags & MOVE_SOURCE_IS_FILE) OnlyOneFile = FALSE;
else
{ /* this has been done this way so that we don't disturb other settings if they have been set before this */
dwMoveStatusFlags |= MOVE_SOURCE_IS_FILE;
dwMoveStatusFlags &= ~MOVE_SOURCE_IS_DIR;
}
}
}
FindClose(hFile);
TRACE ("Do we have only one file: %s\n", OnlyOneFile ? "TRUE" : "FALSE");
/* we have to start again to be sure we don't miss any files or folders*/
hFile = FindFirstFile (arg[i], &findBuffer);
if (hFile == INVALID_HANDLE_VALUE)
{
ErrorMessage(GetLastError(), arg[i]);
nErrorLevel = 1;
goto Quit;
}
/* check for special cases "." and ".." and if found skip them */
FoundFile = TRUE;
while(FoundFile &&
(_tcscmp(findBuffer.cFileName,_T(".")) == 0 ||
_tcscmp(findBuffer.cFileName,_T("..")) == 0))
FoundFile = FindNextFile (hFile, &findBuffer);
if (!FoundFile)
{
/* huh? somebody removed files and/or folders which were there */
error_file_not_found();
FindClose(hFile);
nErrorLevel = 1;
goto Quit;
}
/* check if source and destination paths are on different volumes */
if (szSrcDirPath[0] != szDestPath[0])
dwMoveStatusFlags |= MOVE_PATHS_ON_DIF_VOL;
/* move it */
do
{
TRACE ("Found file/directory: %s\n", debugstr_aw(findBuffer.cFileName));
nOverwrite = 1;
dwMoveFlags = 0;
dwMoveStatusFlags &= ~MOVE_DEST_IS_FILE &
~MOVE_DEST_IS_DIR &
~MOVE_SRC_CURRENT_IS_DIR &
~MOVE_DEST_EXISTS;
_tcscpy(szFullSrcPath,szSrcDirPath);
if (szFullSrcPath[_tcslen(szFullSrcPath) - 1] != _T('\\'))
_tcscat (szFullSrcPath, _T("\\"));
_tcscat(szFullSrcPath,findBuffer.cFileName);
_tcscpy(szSrcPath, szFullSrcPath);
if (IsExistingDirectory(szSrcPath))
{
/* source is directory */
if (dwMoveStatusFlags & MOVE_SOURCE_IS_FILE)
{
dwMoveStatusFlags |= MOVE_SRC_CURRENT_IS_DIR; /* source is file but at the current round we found a directory */
continue;
}
TRACE ("Source is dir: %s\n", debugstr_aw(szSrcPath));
dwMoveFlags = MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH | MOVEFILE_COPY_ALLOWED;
}
/* if source is file we don't need to do anything special */
if (IsExistingDirectory(szDestPath))
{
/* destination is existing directory */
TRACE ("Destination is directory: %s\n", debugstr_aw(szDestPath));
dwMoveStatusFlags |= MOVE_DEST_IS_DIR;
/*build the dest string(accounts for *)*/
_tcscpy (szFullDestPath, szDestPath);
/*check to see if there is an ending slash, if not add one*/
if (szFullDestPath[_tcslen(szFullDestPath) - 1] != _T('\\'))
_tcscat (szFullDestPath, _T("\\"));
_tcscat (szFullDestPath, findBuffer.cFileName);
if (IsExistingFile(szFullDestPath) || IsExistingDirectory(szFullDestPath))
dwMoveStatusFlags |= MOVE_DEST_EXISTS;
dwMoveFlags |= MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH | MOVEFILE_COPY_ALLOWED;
}
if (IsExistingFile(szDestPath))
{
/* destination is a file */
TRACE ("Destination is file: %s\n", debugstr_aw(szDestPath));
dwMoveStatusFlags |= MOVE_DEST_IS_FILE | MOVE_DEST_EXISTS;
_tcscpy (szFullDestPath, szDestPath);
dwMoveFlags |= MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH | MOVEFILE_COPY_ALLOWED;
}
TRACE ("Move Status Flags: 0x%X\n",dwMoveStatusFlags);
if (dwMoveStatusFlags & MOVE_SOURCE_IS_DIR &&
dwMoveStatusFlags & MOVE_DEST_IS_DIR &&
dwMoveStatusFlags & MOVE_SOURCE_HAS_WILD)
{
/* We are not allowed to have existing source and destination dir when there is wildcard in source */
error_syntax(NULL);
FindClose(hFile);
nErrorLevel = 1;
goto Quit;
}
if (!(dwMoveStatusFlags & (MOVE_DEST_IS_FILE | MOVE_DEST_IS_DIR)))
{
/* destination doesn't exist */
_tcscpy (szFullDestPath, szDestPath);
if (dwMoveStatusFlags & MOVE_SOURCE_IS_FILE) dwMoveStatusFlags |= MOVE_DEST_IS_FILE;
if (dwMoveStatusFlags & MOVE_SOURCE_IS_DIR) dwMoveStatusFlags |= MOVE_DEST_IS_DIR;
dwMoveFlags |= MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH | MOVEFILE_COPY_ALLOWED;
}
if (dwMoveStatusFlags & MOVE_SOURCE_IS_FILE &&
dwMoveStatusFlags & MOVE_DEST_IS_FILE &&
!OnlyOneFile)
{
/*source has many files but there is only one destination file*/
error_invalid_parameter_format(pszDest);
FindClose(hFile);
nErrorLevel = 1;
goto Quit;
}
/*checks to make sure user wanted/wants the override*/
if ((dwFlags & MOVE_OVER_NO) &&
(dwMoveStatusFlags & MOVE_DEST_EXISTS))
continue;
if (!(dwFlags & MOVE_OVER_YES) &&
(dwMoveStatusFlags & MOVE_DEST_EXISTS))
nOverwrite = MoveOverwrite (szFullDestPath);
if (nOverwrite == PROMPT_NO || nOverwrite == PROMPT_BREAK)
continue;
if (nOverwrite == PROMPT_ALL)
dwFlags |= MOVE_OVER_YES;
ConOutPrintf (_T("%s => %s "), szSrcPath, szFullDestPath);
/* are we really supposed to do something */
if (dwFlags & MOVE_NOTHING)
continue;
/*move the file*/
if (!(dwMoveStatusFlags & MOVE_SOURCE_IS_DIR &&
dwMoveStatusFlags & MOVE_PATHS_ON_DIF_VOL))
/* we aren't moving source folder to different drive */
MoveStatus = MoveFileEx (szSrcPath, szFullDestPath, dwMoveFlags);
else
{ /* we are moving source folder to different drive */
_tcscpy(szMoveDest, szFullDestPath);
_tcscpy(szMoveSrc, szSrcPath);
DeleteFile(szMoveDest);
MoveStatus = CreateDirectory(szMoveDest, NULL); /* we use default security settings */
if (MoveStatus)
{
_tcscat(szMoveDest,_T("\\"));
_tcscat(szMoveSrc,_T("\\"));
nDirLevel = 0;
pszDestDirPointer = szMoveDest + _tcslen(szMoveDest);
pszSrcDirPointer = szMoveSrc + _tcslen(szMoveSrc);
_tcscpy(pszSrcDirPointer,_T("*.*"));
hDestFile = FindFirstFile(szMoveSrc, &findDestBuffer);
if (hDestFile == INVALID_HANDLE_VALUE)
MoveStatus = FALSE;
else
{
BOOL FirstTime = TRUE;
FoundFile = TRUE;
MoveStatus = FALSE;
while(FoundFile)
{
if (FirstTime)
FirstTime = FALSE;
else
FoundFile = FindNextFile (hDestFile, &findDestBuffer);
if (!FoundFile)
{
/* Nothing to do in this folder so we stop working on it */
FindClose(hDestFile);
(pszSrcDirPointer)--;
(pszDestDirPointer)--;
_tcscpy(pszSrcDirPointer,_T(""));
_tcscpy(pszDestDirPointer,_T(""));
if (nDirLevel > 0)
{
TCHAR szTempPath[MAX_PATH];
INT_PTR nDiff;
FoundFile = TRUE; /* we need to continue our seek for files */
nDirLevel--;
RemoveDirectory(szMoveSrc);
GetDirectory(szMoveSrc,szTempPath,0);
nDiff = _tcslen(szMoveSrc) - _tcslen(szTempPath);
pszSrcDirPointer = pszSrcDirPointer - nDiff;
_tcscpy(pszSrcDirPointer,_T(""));
GetDirectory(szMoveDest,szTempPath,0);
nDiff = _tcslen(szMoveDest) - _tcslen(szTempPath);
pszDestDirPointer = pszDestDirPointer - nDiff;
_tcscpy(pszDestDirPointer,_T(""));
if (szMoveSrc[_tcslen(szMoveSrc) - 1] != _T('\\'))
_tcscat (szMoveSrc, _T("\\"));
if (szMoveDest[_tcslen(szMoveDest) - 1] != _T('\\'))
_tcscat (szMoveDest, _T("\\"));
pszDestDirPointer = szMoveDest + _tcslen(szMoveDest);
pszSrcDirPointer = szMoveSrc + _tcslen(szMoveSrc);
_tcscpy(pszSrcDirPointer,_T("*.*"));
hDestFile = FindFirstFile(szMoveSrc, &findDestBuffer);
if (hDestFile == INVALID_HANDLE_VALUE)
continue;
FirstTime = TRUE;
}
else
{
MoveStatus = TRUE; /* we moved everything so lets tell user about it */
RemoveDirectory(szMoveSrc);
}
continue;
}
/* if we find "." or ".." we'll skip them */
if (_tcscmp(findDestBuffer.cFileName,_T(".")) == 0 ||
_tcscmp(findDestBuffer.cFileName,_T("..")) == 0)
continue;
_tcscpy(pszSrcDirPointer, findDestBuffer.cFileName);
_tcscpy(pszDestDirPointer, findDestBuffer.cFileName);
if (IsExistingFile(szMoveSrc))
{
FoundFile = CopyFile(szMoveSrc, szMoveDest, FALSE);
if (!FoundFile) continue;
DeleteFile(szMoveSrc);
}
else
{
FindClose(hDestFile);
CreateDirectory(szMoveDest, NULL);
_tcscat(szMoveDest,_T("\\"));
_tcscat(szMoveSrc,_T("\\"));
nDirLevel++;
pszDestDirPointer = szMoveDest + _tcslen(szMoveDest);
pszSrcDirPointer = szMoveSrc + _tcslen(szMoveSrc);
_tcscpy(pszSrcDirPointer,_T("*.*"));
hDestFile = FindFirstFile(szMoveSrc, &findDestBuffer);
if (hDestFile == INVALID_HANDLE_VALUE)
{
FoundFile = FALSE;
continue;
}
FirstTime = TRUE;
}
}
}
}
}
if (MoveStatus)
{
ConOutResPrintf(STRING_MOVE_ERROR1);
}
else
{
ConOutResPrintf(STRING_MOVE_ERROR2);
nErrorLevel = 1;
}
}
while ((!OnlyOneFile || dwMoveStatusFlags & MOVE_SRC_CURRENT_IS_DIR ) &&
!(dwMoveStatusFlags & MOVE_SOURCE_IS_DIR) &&
FindNextFile (hFile, &findBuffer));
FindClose (hFile);
if(hDestFile && hDestFile != INVALID_HANDLE_VALUE)
FindClose(hDestFile);
Quit:
freep(arg);
return nErrorLevel;
}
#endif /* INCLUDE_CMD_MOVE */
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.