Implementation of the control panel folder in shell namespace.
[wine] / dlls / shell32 / shlfileop.c
1 /*
2  * SHFileOperation
3  *
4  * Copyright 2000 Juergen Schmied
5  * Copyright 2002 Andriy Palamarchuk
6  * Copyright 2002 Dietrich Teickner (from Odin)
7  * Copyright 2002 Rolf Kalbermatter
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "config.h"
25 #include "wine/port.h"
26
27 #include <stdarg.h>
28 #include <string.h>
29 #include <ctype.h>
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winreg.h"
34 #include "shellapi.h"
35 #include "wingdi.h"
36 #include "winuser.h"
37 #include "shlobj.h"
38 #include "shresdef.h"
39 #define NO_SHLWAPI_STREAM
40 #include "shlwapi.h"
41 #include "shell32_main.h"
42 #include "undocshell.h"
43 #include "wine/unicode.h"
44 #include "wine/debug.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(shell);
47
48 #define IsAttribFile(x) (!(x == -1) && !(x & FILE_ATTRIBUTE_DIRECTORY))
49 #define IsAttribDir(x)  (!(x == -1) && (x & FILE_ATTRIBUTE_DIRECTORY))
50
51 #define IsDotDir(x)     ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
52
53 #define FO_MASK         0xF
54
55 CHAR aWildcardFile[] = {'*','.','*',0};
56 WCHAR wWildcardFile[] = {'*','.','*',0};
57 WCHAR wWildcardChars[] = {'*','?',0};
58 WCHAR wBackslash[] = {'\\',0};
59
60 static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec);
61 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec);
62 static DWORD SHNotifyRemoveDirectoryA(LPCSTR path);
63 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path);
64 static DWORD SHNotifyDeleteFileA(LPCSTR path);
65 static DWORD SHNotifyDeleteFileW(LPCWSTR path);
66 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest, BOOL bRenameIfExists);
67 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bRenameIfExists);
68
69 typedef struct
70 {
71         UINT caption_resource_id, text_resource_id;
72 } SHELL_ConfirmIDstruc;
73
74 static BOOL SHELL_ConfirmIDs(int nKindOfDialog, SHELL_ConfirmIDstruc *ids)
75 {
76         switch (nKindOfDialog) {
77           case ASK_DELETE_FILE:
78             ids->caption_resource_id  = IDS_DELETEITEM_CAPTION;
79             ids->text_resource_id  = IDS_DELETEITEM_TEXT;
80             return TRUE;
81           case ASK_DELETE_FOLDER:
82             ids->caption_resource_id  = IDS_DELETEFOLDER_CAPTION;
83             ids->text_resource_id  = IDS_DELETEITEM_TEXT;
84             return TRUE;
85           case ASK_DELETE_MULTIPLE_ITEM:
86             ids->caption_resource_id  = IDS_DELETEITEM_CAPTION;
87             ids->text_resource_id  = IDS_DELETEMULTIPLE_TEXT;
88             return TRUE;
89           case ASK_OVERWRITE_FILE:
90             ids->caption_resource_id  = IDS_OVERWRITEFILE_CAPTION;
91             ids->text_resource_id  = IDS_OVERWRITEFILE_TEXT;
92             return TRUE;
93           default:
94             FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog);
95         }
96         return FALSE;
97 }
98
99 BOOL SHELL_ConfirmDialog(int nKindOfDialog, LPCSTR szDir)
100 {
101         CHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
102         SHELL_ConfirmIDstruc ids;
103
104         if (!SHELL_ConfirmIDs(nKindOfDialog, &ids))
105           return FALSE;
106
107         LoadStringA(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption));
108         LoadStringA(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText));
109
110         FormatMessageA(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
111                        szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)&szDir);
112
113         return (IDOK == MessageBoxA(GetActiveWindow(), szBuffer, szCaption, MB_OKCANCEL | MB_ICONEXCLAMATION));
114 }
115
116 BOOL SHELL_ConfirmDialogW(int nKindOfDialog, LPCWSTR szDir)
117 {
118         WCHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
119         SHELL_ConfirmIDstruc ids;
120
121         if (!SHELL_ConfirmIDs(nKindOfDialog, &ids))
122           return FALSE;
123
124         LoadStringW(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption));
125         LoadStringW(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText));
126
127         FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
128                        szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)&szDir);
129
130         return (IDOK == MessageBoxW(GetActiveWindow(), szBuffer, szCaption, MB_OKCANCEL | MB_ICONEXCLAMATION));
131 }
132
133 /**************************************************************************
134  * SHELL_DeleteDirectoryA()  [internal]
135  *
136  * like rm -r
137  */
138 BOOL SHELL_DeleteDirectoryA(LPCSTR pszDir, BOOL bShowUI)
139 {
140         BOOL    ret = TRUE;
141         HANDLE  hFind;
142         WIN32_FIND_DATAA wfd;
143         char    szTemp[MAX_PATH];
144
145         /* Make sure the directory exists before eventually prompting the user */
146         PathCombineA(szTemp, pszDir, aWildcardFile);
147         hFind = FindFirstFileA(szTemp, &wfd);
148         if (hFind == INVALID_HANDLE_VALUE)
149           return FALSE;
150
151         if (!bShowUI || SHELL_ConfirmDialog(ASK_DELETE_FOLDER, pszDir))
152         {
153           do
154           {
155             LPSTR lp = wfd.cAlternateFileName;
156             if (!lp[0])
157               lp = wfd.cFileName;
158             if (IsDotDir(lp))
159               continue;
160             PathCombineA(szTemp, pszDir, lp);
161             if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
162               ret = SHELL_DeleteDirectoryA(szTemp, FALSE);
163             else
164               ret = (SHNotifyDeleteFileA(szTemp) == ERROR_SUCCESS);
165           } while (ret && FindNextFileA(hFind, &wfd));
166         }
167         FindClose(hFind);
168         if (ret)
169           ret = (SHNotifyRemoveDirectoryA(pszDir) == ERROR_SUCCESS);
170         return ret;
171 }
172
173 BOOL SHELL_DeleteDirectoryW(LPCWSTR pszDir, BOOL bShowUI)
174 {
175         BOOL    ret = TRUE;
176         HANDLE  hFind;
177         WIN32_FIND_DATAW wfd;
178         WCHAR   szTemp[MAX_PATH];
179
180         /* Make sure the directory exists before eventually prompting the user */
181         PathCombineW(szTemp, pszDir, wWildcardFile);
182         hFind = FindFirstFileW(szTemp, &wfd);
183         if (hFind == INVALID_HANDLE_VALUE)
184           return FALSE;
185
186         if (!bShowUI || SHELL_ConfirmDialogW(ASK_DELETE_FOLDER, pszDir))
187         {
188           do
189           {
190             LPWSTR lp = wfd.cAlternateFileName;
191             if (!lp[0])
192               lp = wfd.cFileName;
193             if (IsDotDir(lp))
194               continue;
195             PathCombineW(szTemp, pszDir, lp);
196             if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
197               ret = SHELL_DeleteDirectoryW(szTemp, FALSE);
198             else
199               ret = (SHNotifyDeleteFileW(szTemp) == ERROR_SUCCESS);
200           } while (ret && FindNextFileW(hFind, &wfd));
201         }
202         FindClose(hFind);
203         if (ret)
204           ret = (SHNotifyRemoveDirectoryW(pszDir) == ERROR_SUCCESS);
205         return ret;
206 }
207
208 /**************************************************************************
209  *  SHELL_DeleteFileA()      [internal]
210  */
211 BOOL SHELL_DeleteFileA(LPCSTR pszFile, BOOL bShowUI)
212 {
213         if (bShowUI && !SHELL_ConfirmDialog(ASK_DELETE_FILE, pszFile))
214           return FALSE;
215
216         return (SHNotifyDeleteFileA(pszFile) == ERROR_SUCCESS);
217 }
218
219 BOOL SHELL_DeleteFileW(LPCWSTR pszFile, BOOL bShowUI)
220 {
221         if (bShowUI && !SHELL_ConfirmDialogW(ASK_DELETE_FILE, pszFile))
222           return FALSE;
223
224         return (SHNotifyDeleteFileW(pszFile) == ERROR_SUCCESS);
225 }
226
227 /**************************************************************************
228  * Win32CreateDirectory      [SHELL32.93]
229  *
230  * Creates a directory. Also triggers a change notify if one exists.
231  *
232  * PARAMS
233  *  path       [I]   path to directory to create
234  *
235  * RETURNS
236  *  TRUE if successful, FALSE otherwise
237  *
238  * NOTES:
239  *  Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
240  *  This is Unicode on NT/2000
241  */
242 static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec)
243 {
244         WCHAR wPath[MAX_PATH];
245         TRACE("(%s, %p)\n", debugstr_a(path), sec);
246
247         MultiByteToWideChar(CP_ACP, 0, path, -1, wPath, MAX_PATH);
248         return SHNotifyCreateDirectoryW(wPath, sec);
249 }
250
251 /**********************************************************************/
252
253 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
254 {
255         TRACE("(%s, %p)\n", debugstr_w(path), sec);
256
257         if (StrPBrkW(path, wWildcardChars))
258         {
259           /* FIXME: This test is currently necessary since our CreateDirectory
260              implementation does create directories with wildcard characters
261              without objection!! Once this is fixed, this here can go away. */
262           SetLastError(ERROR_INVALID_NAME);
263 #ifdef W98_FO_FUNCTION /* W98 */
264           return ERROR_FILE_NOT_FOUND;
265 #else
266           return ERROR_INVALID_NAME;
267 #endif
268         }
269
270         if (CreateDirectoryW(path, sec))
271         {
272           SHChangeNotify(SHCNE_MKDIR, SHCNF_PATHW, path, NULL);
273           return ERROR_SUCCESS;
274         }
275         return GetLastError();
276 }
277
278 /**********************************************************************/
279
280 BOOL WINAPI Win32CreateDirectoryAW(LPCVOID path, LPSECURITY_ATTRIBUTES sec)
281 {
282         if (SHELL_OsIsUnicode())
283           return (SHNotifyCreateDirectoryW(path, sec) == ERROR_SUCCESS);
284         return (SHNotifyCreateDirectoryA(path, sec) == ERROR_SUCCESS);
285 }
286
287 /************************************************************************
288  * Win32RemoveDirectory      [SHELL32.94]
289  *
290  * Deletes a directory. Also triggers a change notify if one exists.
291  *
292  * PARAMS
293  *  path       [I]   path to directory to delete
294  *
295  * RETURNS
296  *  TRUE if successful, FALSE otherwise
297  *
298  * NOTES:
299  *  Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
300  *  This is Unicode on NT/2000
301  */
302
303 static DWORD SHNotifyRemoveDirectoryA(LPCSTR path)
304 {
305         WCHAR wPath[MAX_PATH];
306         TRACE("(%s)\n", debugstr_a(path));
307
308         MultiByteToWideChar(CP_ACP, 0, path, -1, wPath, MAX_PATH);
309         return SHNotifyRemoveDirectoryW(wPath);
310 }
311
312 /***********************************************************************/
313
314 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path)
315 {
316         BOOL ret;
317         TRACE("(%s)\n", debugstr_w(path));
318
319         ret = RemoveDirectoryW(path);
320         if (!ret)
321         {
322           /* Directory may be write protected */
323           DWORD dwAttr = GetFileAttributesW(path);
324           if (dwAttr != -1 && dwAttr & FILE_ATTRIBUTE_READONLY)
325             if (SetFileAttributesW(path, dwAttr & ~FILE_ATTRIBUTE_READONLY))
326               ret = RemoveDirectoryW(path);
327         }
328         if (ret)
329         {
330           SHChangeNotify(SHCNE_RMDIR, SHCNF_PATHW, path, NULL);
331           return ERROR_SUCCESS;
332         }
333         return GetLastError();
334 }
335
336 /***********************************************************************/
337
338 BOOL WINAPI Win32RemoveDirectoryAW(LPCVOID path)
339 {
340         if (SHELL_OsIsUnicode())
341           return (SHNotifyRemoveDirectoryW(path) == ERROR_SUCCESS);
342         return (SHNotifyRemoveDirectoryA(path) == ERROR_SUCCESS);
343 }
344
345 /************************************************************************
346  * Win32DeleteFile           [SHELL32.164]
347  *
348  * Deletes a file. Also triggers a change notify if one exists.
349  *
350  * PARAMS
351  *  path       [I]   path to file to delete
352  *
353  * RETURNS
354  *  TRUE if successful, FALSE otherwise
355  *
356  * NOTES:
357  *  Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
358  *  This is Unicode on NT/2000
359  */
360
361 static DWORD SHNotifyDeleteFileA(LPCSTR path)
362 {
363         WCHAR wPath[MAX_PATH];
364         TRACE("(%s)\n", debugstr_a(path));
365
366         MultiByteToWideChar(CP_ACP, 0, path, -1, wPath, MAX_PATH);
367         return SHNotifyDeleteFileW(wPath);
368 }
369
370 /***********************************************************************/
371
372 static DWORD SHNotifyDeleteFileW(LPCWSTR path)
373 {
374         BOOL ret;
375
376         TRACE("(%s)\n", debugstr_w(path));
377
378         ret = DeleteFileW(path);
379         if (!ret)
380         {
381           /* File may be write protected or a system file */
382           DWORD dwAttr = GetFileAttributesW(path);
383           if ((dwAttr != -1) && (dwAttr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
384             if (SetFileAttributesW(path, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
385               ret = DeleteFileW(path);
386         }
387         if (ret)
388         {
389           SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, path, NULL);
390           return ERROR_SUCCESS;
391         }
392         return GetLastError();
393 }
394
395 /***********************************************************************/
396
397 DWORD WINAPI Win32DeleteFileAW(LPCVOID path)
398 {
399         if (SHELL_OsIsUnicode())
400           return (SHNotifyDeleteFileW(path) == ERROR_SUCCESS);
401         return (SHNotifyDeleteFileA(path) == ERROR_SUCCESS);
402 }
403
404 /************************************************************************
405  * SHNotifyMoveFile          [internal]
406  *
407  * Moves a file. Also triggers a change notify if one exists.
408  *
409  * PARAMS
410  *  src        [I]   path to source file to move
411  *  dest       [I]   path to target file to move to
412  *  bRename    [I]   if TRUE, the target file will be renamed if a
413  *                   file with this name already exists
414  *
415  * RETURNS
416  *  ERORR_SUCCESS if successful
417  */
418 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest, BOOL bRename)
419 {
420         BOOL ret;
421
422         TRACE("(%s %s %s)\n", debugstr_w(src), debugstr_w(dest), bRename ? "renameIfExists" : "");
423
424         if (StrPBrkW(dest, wWildcardChars))
425         {
426           /* FIXME: This test is currently necessary since our MoveFile
427              implementation does create files with wildcard characters
428              without objection!! Once this is fixed, this here can go away. */
429           SetLastError(ERROR_INVALID_NAME);
430 #ifdef W98_FO_FUNCTION /* W98 */
431           return ERROR_FILE_NOT_FOUND;
432 #else
433           return ERROR_INVALID_NAME;
434 #endif
435         }
436
437         ret = MoveFileW(src, dest);
438         if (!ret)
439         {
440           /* Source file may be write protected or a system file */
441           DWORD dwAttr = GetFileAttributesW(src);
442           if ((dwAttr != -1) && (dwAttr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
443             if (SetFileAttributesW(src, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
444               ret = MoveFileW(src, dest);
445
446           if (!ret && bRename)
447           {
448             /* Destination file probably exists */
449             dwAttr = GetFileAttributesW(dest);
450             if (dwAttr != -1)
451             {
452               FIXME("Rename on move to existing file not implemented!\n");
453             }
454           }
455         }
456         if (ret)
457         {
458           SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_PATHW, src, dest);
459           return ERROR_SUCCESS;
460         }
461         return GetLastError();
462 }
463
464 /************************************************************************
465  * SHNotifyCopyFile          [internal]
466  *
467  * Copies a file. Also triggers a change notify if one exists.
468  *
469  * PARAMS
470  *  src        [I]   path to source file to move
471  *  dest       [I]   path to target file to move to
472  *  bRename    [I]   if TRUE, the target file will be renamed if a
473  *                   file with this name already exists
474  *
475  * RETURNS
476  *  ERROR_SUCCESS if successful
477  */
478 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bRename)
479 {
480         BOOL ret;
481
482         TRACE("(%s %s %s)\n", debugstr_w(src), debugstr_w(dest), bRename ? "renameIfExists" : "");
483
484         ret = CopyFileW(src, dest, TRUE);
485         if (!ret && bRename)
486         {
487           /* Destination file probably exists */
488           DWORD dwAttr = GetFileAttributesW(dest);
489           if (dwAttr != -1)
490           {
491             FIXME("Rename on copy to existing file not implemented!\n");
492           }
493         }
494         if (ret)
495         {
496           SHChangeNotify(SHCNE_CREATE, SHCNF_PATHW, dest, NULL);
497           return ERROR_SUCCESS;
498         }
499         return GetLastError();
500 }
501
502 /*************************************************************************
503  * SHCreateDirectory         [SHELL32.165]
504  *
505  * Create a directory at the specified location
506  *
507  * PARAMS
508  *  hWnd       [I]
509  *  path       [I]   path of directory to create
510  *
511  * RETURNS
512  *  ERRROR_SUCCESS or one of the following values:
513  *  ERROR_BAD_PATHNAME if the path is relative
514  *  ERROR_FILE_EXISTS when a file with that name exists
515  *  ERROR_ALREADY_EXISTS when the directory already exists
516  *  ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
517  *
518  * NOTES
519  *  exported by ordinal
520  *  Win9x exports ANSI
521  *  WinNT/2000 exports Unicode
522  */
523 DWORD WINAPI SHCreateDirectory(HWND hWnd, LPCVOID path)
524 {
525         if (SHELL_OsIsUnicode())
526           return SHCreateDirectoryExW(hWnd, path, NULL);
527         return SHCreateDirectoryExA(hWnd, path, NULL);
528 }
529
530 /*************************************************************************
531  * SHCreateDirectoryExA      [SHELL32.@]
532  *
533  * Create a directory at the specified location
534  *
535  * PARAMS
536  *  hWnd       [I]   
537  *  path       [I]   path of directory to create 
538  *  sec        [I]   security attributes to use or NULL
539  *
540  * RETURNS
541  *  ERRROR_SUCCESS or one of the following values:
542  *  ERROR_BAD_PATHNAME if the path is relative
543  *  ERORO_INVALID_NAME if the path contains invalid chars
544  *  ERROR_FILE_EXISTS when a file with that name exists
545  *  ERROR_ALREADY_EXISTS when the directory already exists
546  *  ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
547  */
548 DWORD WINAPI SHCreateDirectoryExA(HWND hWnd, LPCSTR path, LPSECURITY_ATTRIBUTES sec)
549 {
550         WCHAR wPath[MAX_PATH];
551         TRACE("(%p, %s, %p)\n",hWnd, debugstr_a(path), sec);
552
553         MultiByteToWideChar(CP_ACP, 0, path, -1, wPath, MAX_PATH);
554         return SHCreateDirectoryExW(hWnd, wPath, sec);
555 }
556
557 /*************************************************************************
558  * SHCreateDirectoryExW      [SHELL32.@]
559  */
560 DWORD WINAPI SHCreateDirectoryExW(HWND hWnd, LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
561 {
562         DWORD ret = ERROR_BAD_PATHNAME;
563         TRACE("(%p, %s, %p)\n",hWnd, debugstr_w(path), sec);
564
565         if (PathIsRelativeW(path))
566         {
567           SetLastError(ret);
568         }
569         else
570         {
571           ret = SHNotifyCreateDirectoryW(path, sec);
572           if (ret != ERROR_FILE_EXISTS &&
573               ret != ERROR_ALREADY_EXISTS &&
574               ret != ERROR_FILENAME_EXCED_RANGE)
575           {
576           /* handling network file names?
577             lstrcpynW(pathName, path, MAX_PATH);
578             lpStr = PathAddBackslashW(pathName);*/
579             FIXME("Semi-stub, non zero hWnd should be used somehow?\n");
580           }
581         }
582         return ret;
583 }
584
585 /*************************************************************************
586  *
587  * SHFileStrICmp HelperFunction for SHFileOperationW
588  *
589  */
590 BOOL SHFileStrICmpW(LPWSTR p1, LPWSTR p2, LPWSTR p1End, LPWSTR p2End)
591 {
592         WCHAR C1 = '\0';
593         WCHAR C2 = '\0';
594         int i_Temp = -1;
595         int i_len1 = lstrlenW(p1);
596         int i_len2 = lstrlenW(p2);
597
598         if (p1End && (&p1[i_len1] >= p1End) && ('\\' == p1End[0]))
599         {
600           C1 = p1End[0];
601           p1End[0] = '\0';
602           i_len1 = lstrlenW(p1);
603         }
604         if (p2End)
605         {
606           if ((&p2[i_len2] >= p2End) && ('\\' == p2End[0]))
607           {
608             C2 = p2End[0];
609             if (C2)
610               p2End[0] = '\0';
611           }
612         }
613         else
614         {
615           if ((i_len1 <= i_len2) && ('\\' == p2[i_len1]))
616           {
617             C2 = p2[i_len1];
618             if (C2)
619               p2[i_len1] = '\0';
620           }
621         }
622         i_len2 = lstrlenW(p2);
623         if (i_len1 == i_len2)
624           i_Temp = lstrcmpiW(p1,p2);
625         if (C1)
626           p1[i_len1] = C1;
627         if (C2)
628           p2[i_len2] = C2;
629         return !(i_Temp);
630 }
631
632 /*************************************************************************
633  *
634  * SHFileStrCpyCat HelperFunction for SHFileOperationW
635  *
636  */
637 LPWSTR SHFileStrCpyCatW(LPWSTR pTo, LPCWSTR pFrom, LPCWSTR pCatStr)
638 {
639         LPWSTR pToFile = NULL;
640         int  i_len;
641         if (pTo)
642         {
643           if (pFrom)
644             lstrcpyW(pTo, pFrom);
645           if (pCatStr)
646           {
647             i_len = lstrlenW(pTo);
648             if ((i_len) && (pTo[--i_len] != '\\'))
649               i_len++;
650             pTo[i_len] = '\\';
651             if (pCatStr[0] == '\\')
652               pCatStr++; \
653             lstrcpyW(&pTo[i_len+1], pCatStr);
654           }
655           pToFile = StrRChrW(pTo,NULL,'\\');
656           /* termination of the new string-group */
657           pTo[(lstrlenW(pTo)) + 1] = '\0';
658         }
659         return pToFile;
660 }
661
662 /**************************************************************************
663  *      SHELL_FileNamesMatch()
664  *
665  * Accepts two \0 delimited lists of the file names. Checks whether number of
666  * files in both lists is the same, and checks also if source-name exists.
667  */
668 BOOL SHELL_FileNamesMatch(LPCWSTR pszFiles1, LPCWSTR pszFiles2, BOOL bOnlySrc)
669 {
670         while ((pszFiles1[0] != '\0') &&
671                (bOnlySrc || (pszFiles2[0] != '\0')))
672         {
673           if (NULL == StrPBrkW(pszFiles1, wWildcardChars))
674           {
675             if (-1 == GetFileAttributesW(pszFiles1))
676               return FALSE;
677           }
678           pszFiles1 += lstrlenW(pszFiles1) + 1;
679           if (!bOnlySrc)
680             pszFiles2 += lstrlenW(pszFiles2) + 1;
681         }
682         return ((pszFiles1[0] == '\0') && (bOnlySrc || (pszFiles2[0] == '\0')));
683 }
684
685 /*************************************************************************
686  *
687  * SHNameTranslate HelperFunction for SHFileOperationA
688  *
689  * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
690  * is NULL, only the necessary size of the string is determined and returned,
691  * otherwise the ASCII strings are copied into it and the buffer is increased
692  * to point to the location after the final 0 termination char.
693  */
694 DWORD SHNameTranslate(LPWSTR* wString, LPCWSTR* pWToFrom, BOOL more)
695 {
696         DWORD size = 0, aSize = 0;
697         LPCSTR aString = (LPCSTR)*pWToFrom;
698
699         if (aString)
700         {
701           do
702           {
703             size = lstrlenA(aString) + 1;
704             aSize += size;
705             aString += size;
706           } while ((size != 1) && more);
707           /* The two sizes might be different in the case of multibyte chars */
708           size = MultiByteToWideChar(CP_ACP, 0, aString, aSize, *wString, 0);
709           if (*wString) /* only in the second loop */
710           {
711             MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*pWToFrom, aSize, *wString, size);
712             *pWToFrom = *wString;
713             *wString += size;
714           }
715         }
716         return size;
717 }
718 /*************************************************************************
719  * SHFileOperationA          [SHELL32.@]
720  *
721  * Function to copy, move, delete and create one or more files with optional
722  * user prompts.
723  *
724  * PARAMS
725  *  lpFileOp   [I/O] pointer to a structure containing all the necessary information
726  *
727  * NOTES
728  *  exported by name
729  */
730 int WINAPI SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp)
731 {
732         SHFILEOPSTRUCTW nFileOp = *((LPSHFILEOPSTRUCTW)lpFileOp);
733         int retCode = 0;
734         DWORD size;
735         LPWSTR ForFree = NULL, /* we change wString in SHNameTranslate and can't use it for freeing */
736                wString = NULL; /* we change this in SHNameTranslate */
737
738         TRACE("\n");
739         if (FO_DELETE == (nFileOp.wFunc & FO_MASK))
740           nFileOp.pTo = NULL; /* we need a NULL or a valid pointer for translation */
741         if (!(nFileOp.fFlags & FOF_SIMPLEPROGRESS))
742           nFileOp.lpszProgressTitle = NULL; /* we need a NULL or a valid pointer for translation */
743         while (1) /* every loop calculate size, second translate also, if we have storage for this */
744         {
745           size = SHNameTranslate(&wString, &nFileOp.lpszProgressTitle, FALSE); /* no loop */
746           size += SHNameTranslate(&wString, &nFileOp.pFrom, TRUE); /* internal loop */
747           size += SHNameTranslate(&wString, &nFileOp.pTo, TRUE); /* internal loop */
748
749           if (ForFree)
750           {
751             retCode = SHFileOperationW(&nFileOp);
752             HeapFree(GetProcessHeap(), 0, ForFree); /* we can not use wString, it was changed */
753             break;
754           }
755           else
756           {
757             wString = ForFree = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
758             if (ForFree) continue;
759             retCode = ERROR_OUTOFMEMORY;
760             nFileOp.fAnyOperationsAborted = TRUE;
761             SetLastError(retCode);
762             return retCode;
763           }
764         }
765
766         lpFileOp->hNameMappings = nFileOp.hNameMappings;
767         lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
768         return retCode;
769 }
770
771 static const char * debug_shfileops_flags( DWORD fFlags )
772 {
773     return wine_dbg_sprintf( "%s%s%s%s%s%s%s%s%s%s%s%s%s",
774         fFlags & FOF_MULTIDESTFILES ? "FOF_MULTIDESTFILES " : "",
775         fFlags & FOF_CONFIRMMOUSE ? "FOF_CONFIRMMOUSE " : "",
776         fFlags & FOF_SILENT ? "FOF_SILENT " : "",
777         fFlags & FOF_RENAMEONCOLLISION ? "FOF_RENAMEONCOLLISION " : "",
778         fFlags & FOF_NOCONFIRMATION ? "FOF_NOCONFIRMATION " : "",
779         fFlags & FOF_WANTMAPPINGHANDLE ? "FOF_WANTMAPPINGHANDLE " : "",
780         fFlags & FOF_ALLOWUNDO ? "FOF_ALLOWUNDO " : "",
781         fFlags & FOF_FILESONLY ? "FOF_FILESONLY " : "",
782         fFlags & FOF_SIMPLEPROGRESS ? "FOF_SIMPLEPROGRESS " : "",
783         fFlags & FOF_NOCONFIRMMKDIR ? "FOF_NOCONFIRMMKDIR " : "",
784         fFlags & FOF_NOERRORUI ? "FOF_NOERRORUI " : "",
785         fFlags & FOF_NOCOPYSECURITYATTRIBS ? "FOF_NOCOPYSECURITYATTRIBS" : "",
786         fFlags & 0xf000 ? "MORE-UNKNOWN-Flags" : "");
787 }
788
789 static const char * debug_shfileops_action( DWORD op )
790 {
791     LPCSTR cFO_Name [] = {"FO_????","FO_MOVE","FO_COPY","FO_DELETE","FO_RENAME"};
792     return wine_dbg_sprintf("%s", cFO_Name[ op ]);
793 }
794
795 /*************************************************************************
796  * SHFileOperationW          [SHELL32.@]
797  *
798  * See SHFileOperationA
799  */
800 int WINAPI SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp)
801 {
802         SHFILEOPSTRUCTW nFileOp = *(lpFileOp);
803
804         LPCWSTR pNextFrom = nFileOp.pFrom;
805         LPCWSTR pNextTo = nFileOp.pTo;
806         LPCWSTR pFrom = pNextFrom;
807         LPCWSTR pTo = NULL;
808         HANDLE hFind = INVALID_HANDLE_VALUE;
809         WIN32_FIND_DATAW wfd;
810         LPWSTR pTempFrom = NULL;
811         LPWSTR pTempTo = NULL;
812         LPWSTR pFromFile;
813         LPWSTR pToFile = NULL;
814         LPWSTR lpFileName;
815         int retCode = 0;
816         DWORD ToAttr;
817         DWORD ToPathAttr;
818         DWORD FromPathAttr;
819         FILEOP_FLAGS OFl = ((FILEOP_FLAGS)lpFileOp->fFlags & 0xfff);
820
821         BOOL b_Multi = (nFileOp.fFlags & FOF_MULTIDESTFILES);
822
823         BOOL b_MultiTo = (FO_DELETE != (lpFileOp->wFunc & FO_MASK));
824         BOOL b_MultiPaired = (!b_MultiTo);
825         BOOL b_MultiFrom = FALSE;
826         BOOL not_overwrite;
827         BOOL ask_overwrite;
828         BOOL b_SameRoot;
829         BOOL b_SameTailName;
830         BOOL b_ToInvalidTail = FALSE;
831         BOOL b_ToValid; /* for W98-Bug for FO_MOVE with source and target in same rootdrive */
832         BOOL b_Mask;
833         BOOL b_ToTailSlash = FALSE;
834
835         long FuncSwitch = (nFileOp.wFunc & FO_MASK);
836         long level= nFileOp.wFunc>>4;
837
838         /*  default no error */
839         nFileOp.fAnyOperationsAborted = FALSE;
840
841         if ((FuncSwitch < FO_MOVE) || (FuncSwitch > FO_RENAME))
842           goto shfileop_normal; /* no valid FunctionCode */
843
844         if (level == 0)
845             TRACE("%s: flags (0x%04x) : %s\n",
846                 debug_shfileops_action(FuncSwitch), nFileOp.fFlags,
847                 debug_shfileops_flags(nFileOp.fFlags) );
848
849         /* establish when pTo is interpreted as the name of the destination file
850          * or the directory where the Fromfile should be copied to.
851          * This depends on:
852          * (1) pTo points to the name of an existing directory;
853          * (2) the flag FOF_MULTIDESTFILES is present;
854          * (3) whether pFrom point to multiple filenames.
855          *
856          * Some experiments:
857          *
858          * destisdir               1 1 1 1 0 0 0 0
859          * FOF_MULTIDESTFILES      1 1 0 0 1 1 0 0
860          * multiple from filenames 1 0 1 0 1 0 1 0
861          *                         ---------------
862          * copy files to dir       1 0 1 1 0 0 1 0
863          * create dir              0 0 0 0 0 0 1 0
864          */
865 /*
866  * FOF_MULTIDESTFILES, FOF_NOCONFIRMATION, FOF_FILESONLY  are implemented
867  * FOF_CONFIRMMOUSE, FOF_SILENT, FOF_NOCONFIRMMKDIR,
868  *       FOF_SIMPLEPROGRESS, FOF_NOCOPYSECURITYATTRIBS    are not implemented and ignored
869  * FOF_RENAMEONCOLLISION                                  are implemented partially and breaks if file exist
870  * FOF_ALLOWUNDO, FOF_WANTMAPPINGHANDLE                   are not implemented and breaks
871  * if any other flag set, an error occurs
872  */
873         TRACE("%s level=%ld nFileOp.fFlags=0x%x\n", 
874                 debug_shfileops_action(FuncSwitch), level, lpFileOp->fFlags);
875
876 /*    OFl &= (-1 - (FOF_MULTIDESTFILES | FOF_FILESONLY)); */
877 /*    OFl ^= (FOF_SILENT | FOF_NOCONFIRMATION | FOF_SIMPLEPROGRESS | FOF_NOCONFIRMMKDIR); */
878         OFl &= (~(FOF_MULTIDESTFILES | FOF_NOCONFIRMATION | FOF_FILESONLY));  /* implemented */
879         OFl ^= (FOF_SILENT | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_NOCOPYSECURITYATTRIBS); /* ignored, if one */
880         OFl &= (~FOF_SIMPLEPROGRESS);                      /* ignored, only with FOF_SILENT */
881         if (OFl)
882         {
883             if (OFl & (~(FOF_CONFIRMMOUSE | FOF_SILENT | FOF_RENAMEONCOLLISION |
884                          FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_NOCOPYSECURITYATTRIBS)))
885             {
886                 TRACE("%s level=%ld lpFileOp->fFlags=0x%x not implemented, Aborted=TRUE, stub\n",
887                       debug_shfileops_action(FuncSwitch), level, OFl);
888                 retCode = 0x403; /* 1027, we need an extension to shlfileop */
889                 goto shfileop_error;
890             }
891             else
892             {
893                 TRACE("%s level=%ld lpFileOp->fFlags=0x%x not fully implemented, stub\n", 
894                       debug_shfileops_action(FuncSwitch), level, OFl);
895             } 
896         } 
897
898         if ((pNextFrom) && (!(b_MultiTo) || (pNextTo)))
899         {
900             nFileOp.pFrom = pTempFrom = HeapAlloc(GetProcessHeap(), 0, ((1 + 2 * (b_MultiTo)) * MAX_PATH + 6) * sizeof(WCHAR));
901             if (!pTempFrom)
902             {
903                 retCode = ERROR_OUTOFMEMORY;
904                 SetLastError(retCode);
905                 goto shfileop_error;
906             }
907             if (b_MultiTo)
908                 pTempTo = &pTempFrom[MAX_PATH + 4];
909             nFileOp.pTo = pTempTo;
910             ask_overwrite = (!(nFileOp.fFlags & FOF_NOCONFIRMATION) && !(nFileOp.fFlags & FOF_RENAMEONCOLLISION));
911             not_overwrite = (!(nFileOp.fFlags & FOF_NOCONFIRMATION) ||  (nFileOp.fFlags & FOF_RENAMEONCOLLISION));
912         }
913         else
914         {
915             retCode = 0x402;      /* 1026 */
916             goto shfileop_error;
917         }
918         /* need break at error before change sourcepointer */
919         while(!nFileOp.fAnyOperationsAborted && (pNextFrom[0]))
920         {
921             nFileOp.wFunc =  ((level + 1) << 4) + FuncSwitch;
922             nFileOp.fFlags = lpFileOp->fFlags;
923
924             if (b_MultiTo)
925             {
926                 pTo = pNextTo;
927                 pNextTo = &pNextTo[lstrlenW(pTo)+1];
928                 b_MultiTo = (b_Multi && pNextTo[0]);
929             }
930
931             pFrom = pNextFrom;
932             pNextFrom = &pNextFrom[lstrlenW(pNextFrom)+1];
933             if (!b_MultiFrom && !b_MultiTo)
934                 b_MultiFrom = (pNextFrom[0]);
935
936             pFromFile = SHFileStrCpyCatW(pTempFrom, pFrom, NULL);
937
938             if (pTo)
939             {
940                 pToFile = SHFileStrCpyCatW(pTempTo, pTo, NULL);
941             }
942             if (!b_MultiPaired)
943             {
944                 b_MultiPaired =
945                     SHELL_FileNamesMatch(lpFileOp->pFrom, lpFileOp->pTo, (!b_Multi || b_MultiFrom));
946             }
947             if (!(b_MultiPaired) || !(pFromFile) || !(pFromFile[1]) || ((pTo) && !(pToFile)))
948             {
949                 retCode = 0x402;      /* 1026 */
950                 goto shfileop_error;
951             }
952             if (pTo)
953             {
954                 b_ToTailSlash = (!pToFile[1]);
955                 if (b_ToTailSlash)
956                 {
957                     pToFile[0] = '\0';
958                     if (StrChrW(pTempTo,'\\'))
959                     {
960                         pToFile = SHFileStrCpyCatW(pTempTo, NULL, NULL);
961                     }
962                 }
963                 b_ToInvalidTail = (NULL != StrPBrkW(&pToFile[1], wWildcardChars));
964             }
965
966             /* for all */
967             b_Mask = (NULL != StrPBrkW(&pFromFile[1], wWildcardChars));
968             if (FO_RENAME == FuncSwitch)
969             {
970                 /* temporary only for FO_RENAME */
971                 if (b_MultiTo || b_MultiFrom || (b_Mask && !b_ToInvalidTail))
972                 {
973 #ifndef W98_FO_FUNCTION
974                     retCode = ERROR_GEN_FAILURE;  /* W2K ERROR_GEN_FAILURE, W98 returns no error */
975 #endif
976                     goto shfileop_error;
977                 }
978             }
979
980             hFind = FindFirstFileW(pFrom, &wfd);
981             if (INVALID_HANDLE_VALUE == hFind)
982             {
983                 if ((FO_DELETE == FuncSwitch) && (b_Mask))
984                 {
985                     pFromFile[0] = '\0';
986                     FromPathAttr = GetFileAttributesW(pTempFrom);
987                     pFromFile[0] = '\\';
988                     if (IsAttribDir(FromPathAttr))
989                     {
990                         /* FO_DELETE with mask and without found is valid */
991                         goto shfileop_normal;
992                     }
993                 }
994                 /* root (without mask) is also not allowed as source, tested in W98 */
995                 retCode = 0x402;   /* 1026 */
996                 goto shfileop_error;
997             }
998
999 /* for all */
1000 #define HIGH_ADR (LPWSTR)0xffffffff
1001
1002 /* ???      b_Mask = (!SHFileStrICmpA(&pFromFile[1], &wfd.cFileName[0], HIGH_ADR, HIGH_ADR)); */
1003             if (!pTo) /* FO_DELETE */
1004             {
1005                 do
1006                 {
1007                     lpFileName = wfd.cAlternateFileName;
1008                     if (!lpFileName[0])
1009                         lpFileName = wfd.cFileName;
1010                     if (IsDotDir(lpFileName) ||
1011                         ((b_Mask) && IsAttribDir(wfd.dwFileAttributes) && (nFileOp.fFlags & FOF_FILESONLY)))
1012                         continue;
1013                     SHFileStrCpyCatW(&pFromFile[1], lpFileName, NULL);
1014                     /* TODO: Check the SHELL_DeleteFileOrDirectoryW() function in shell32.dll */
1015                     if (IsAttribFile(wfd.dwFileAttributes))
1016                     {
1017                         nFileOp.fAnyOperationsAborted = (SHNotifyDeleteFileW(pTempFrom) != ERROR_SUCCESS);
1018                         retCode = 0x78; /* value unknown */
1019                     }
1020                     else
1021                     {
1022                         nFileOp.fAnyOperationsAborted = (!SHELL_DeleteDirectoryW(pTempFrom, (!(nFileOp.fFlags & FOF_NOCONFIRMATION))));
1023                         retCode = 0x79; /* value unknown */
1024                     }
1025                 } while (!nFileOp.fAnyOperationsAborted && FindNextFileW(hFind, &wfd));
1026                 FindClose(hFind);
1027                 hFind = INVALID_HANDLE_VALUE;
1028                 if (nFileOp.fAnyOperationsAborted)
1029                 {
1030                     goto shfileop_error;
1031                 }
1032                 continue;
1033             } /* FO_DELETE ends, pTo must be always valid from here */
1034
1035             b_SameRoot = (toupperW(pTempFrom[0]) == toupperW(pTempTo[0]));
1036             b_SameTailName = SHFileStrICmpW(pToFile, pFromFile, NULL, NULL);
1037
1038             ToPathAttr = ToAttr = GetFileAttributesW(pTempTo);
1039             if (!b_Mask && (ToAttr == -1) && (pToFile))
1040             {
1041                 pToFile[0] = '\0';
1042                 ToPathAttr = GetFileAttributesW(pTempTo);
1043                 pToFile[0] = '\\';
1044             }
1045
1046             if (FO_RENAME == FuncSwitch)
1047             {
1048                 if (!b_SameRoot || b_Mask /* FO_RENAME works not with Mask */
1049                     || !SHFileStrICmpW(pTempFrom, pTempTo, pFromFile, NULL)
1050                     || (SHFileStrICmpW(pTempFrom, pTempTo, pFromFile, HIGH_ADR) && !b_ToTailSlash))
1051                 {
1052                     retCode = 0x73;
1053                     goto shfileop_error;
1054                 }
1055                 if (b_ToInvalidTail)
1056                 {
1057                     retCode=0x2;
1058                     goto shfileop_error;
1059                 }
1060                 if (-1 == ToPathAttr)
1061                 {
1062                     retCode = 0x75;
1063                     goto shfileop_error;
1064                 }
1065                 if (IsAttribDir(wfd.dwFileAttributes) && IsAttribDir(ToAttr))
1066                 {
1067                     retCode = (b_ToTailSlash) ? 0xb7 : 0x7b;
1068                     goto shfileop_error;
1069                 }
1070                 /* we use SHNotifyMoveFile() instead MoveFileW */
1071                 if (SHNotifyMoveFileW(pTempFrom, pTempTo, nFileOp.fFlags & FOF_RENAMEONCOLLISION) != ERROR_SUCCESS)
1072                 {
1073                     /* we need still the value for the returncode, we use the mostly assumed */
1074                     retCode = 0xb7;
1075                     goto shfileop_error;
1076                 }
1077                 goto shfileop_normal;
1078             }
1079
1080             /* W98 Bug with FO_MOVE different to FO_COPY, better the same as FO_COPY */
1081             b_ToValid = ((b_SameTailName &&  b_SameRoot && (FO_COPY == FuncSwitch)) ||
1082                          (b_SameTailName && !b_SameRoot) || (b_ToInvalidTail));
1083
1084             /* handle mask in source */
1085             if (b_Mask)
1086             {
1087                 if (!IsAttribDir(ToAttr))
1088                 {
1089                     retCode = (b_ToInvalidTail &&/* b_SameTailName &&*/ (FO_MOVE == FuncSwitch)) \
1090                         ? 0x2 : 0x75;
1091                     goto shfileop_error;
1092                 }
1093                 pToFile = SHFileStrCpyCatW(pTempTo, NULL, wBackslash);
1094                 nFileOp.fFlags = (nFileOp.fFlags | FOF_MULTIDESTFILES);
1095                 do
1096                 {
1097                     lpFileName = wfd.cAlternateFileName;
1098                     if (!lpFileName[0])
1099                         lpFileName = wfd.cFileName;
1100                     if (IsDotDir(lpFileName) ||
1101                         (IsAttribDir(wfd.dwFileAttributes) && (nFileOp.fFlags & FOF_FILESONLY)))
1102                         continue; /* next name in pTempFrom(dir) */
1103                     SHFileStrCpyCatW(&pToFile[1], lpFileName, NULL);
1104                     SHFileStrCpyCatW(&pFromFile[1], lpFileName, NULL);
1105                     retCode = SHFileOperationW (&nFileOp);
1106                 } while(!nFileOp.fAnyOperationsAborted && FindNextFileW(hFind, &wfd));
1107             }
1108             FindClose(hFind);
1109             hFind = INVALID_HANDLE_VALUE;
1110             /* FO_COPY/FO_MOVE with mask, FO_DELETE and FO_RENAME are solved */
1111             if (b_Mask)
1112                 continue;
1113
1114             /* only FO_COPY/FO_MOVE without mask, all others are (must be) solved */
1115             if (IsAttribDir(wfd.dwFileAttributes) && (ToAttr == -1))
1116             {
1117                 if (pToFile)
1118                 {
1119                     pToFile[0] = '\0';
1120                     ToPathAttr = GetFileAttributesW(pTempTo);
1121                     if ((ToPathAttr == -1) && b_ToValid)
1122                     {
1123                         /* create dir must be here, sample target D:\y\ *.* create with RC=10003 */
1124                         if (SHCreateDirectoryExW(NULL, pTempTo, NULL))
1125                         {
1126                             retCode = 0x73;/* value unknown */
1127                             goto shfileop_error;
1128                         }
1129                         ToPathAttr = GetFileAttributesW(pTempTo);
1130                     }
1131                     pToFile[0] = '\\';
1132                     if (b_ToInvalidTail)
1133                     {
1134                         retCode = 0x10003;
1135                         goto shfileop_error;
1136                     }
1137                 }
1138             }
1139
1140             /* trailing BackSlash is ever removed and pToFile points to BackSlash before */
1141             if (!b_MultiTo && (b_MultiFrom || (!(b_Multi) && IsAttribDir(ToAttr))))
1142             {
1143                 if ((FO_MOVE == FuncSwitch) && IsAttribDir(ToAttr) && IsAttribDir(wfd.dwFileAttributes))
1144                 {
1145                     if (b_Multi)
1146                     {
1147                         retCode = 0x73; /* !b_Multi = 0x8 ?? */
1148                         goto shfileop_error;
1149                     }
1150                 }
1151                 pToFile = SHFileStrCpyCatW(pTempTo, NULL, wfd.cFileName);
1152                 ToAttr = GetFileAttributesW(pTempTo);
1153             }
1154
1155             if (IsAttribDir(ToAttr))
1156             {
1157                 if (IsAttribFile(wfd.dwFileAttributes))
1158                 {
1159                     retCode = (FO_COPY == FuncSwitch) ? 0x75 : 0xb7;
1160                     goto shfileop_error;
1161                 }
1162             }
1163             else
1164             {
1165                 pToFile[0] = '\0';
1166                 ToPathAttr = GetFileAttributesW(pTempTo);
1167                 pToFile[0] = '\\';
1168                 if (IsAttribFile(ToPathAttr))
1169                 {
1170                     /* error, is this tested ? */
1171                     retCode = 0x777402;
1172                     goto shfileop_error;
1173                 }
1174             }
1175
1176             /* singlesource + no mask */
1177             if (-1 == (ToAttr & ToPathAttr))
1178             {
1179                 /* Target-dir does not exist, and cannot be created */
1180                 retCode=0x75;
1181                 goto shfileop_error;
1182             }
1183
1184             switch(FuncSwitch)
1185             {
1186             case FO_MOVE:
1187                 pToFile = NULL;
1188                 if ((ToAttr == -1) && SHFileStrICmpW(pTempFrom, pTempTo, pFromFile, NULL))
1189                 {
1190                     nFileOp.wFunc =  ((level+1)<<4) + FO_RENAME;
1191                 }
1192                 else
1193                 {
1194                     if (b_SameRoot && IsAttribDir(ToAttr) && IsAttribDir(wfd.dwFileAttributes))
1195                     {
1196                         /* we need pToFile for FO_DELETE after FO_MOVE contence */
1197                         pToFile = SHFileStrCpyCatW(pTempFrom, NULL, wWildcardFile);
1198                     }
1199                     else
1200                     {
1201                         nFileOp.wFunc =  ((level+1)<<4) + FO_COPY;
1202                     }
1203                 }
1204                 retCode = SHFileOperationW(&nFileOp);
1205                 if (pToFile)
1206                     ((DWORD*)pToFile)[0] = '\0';
1207                 if (!nFileOp.fAnyOperationsAborted && (FO_RENAME != (nFileOp.wFunc & 0xf)))
1208                 {
1209                     nFileOp.wFunc =  ((level+1)<<4) + FO_DELETE;
1210                     retCode = SHFileOperationW(&nFileOp);
1211                 }
1212                 continue;
1213             case FO_COPY:
1214                 if (SHFileStrICmpW(pTempFrom, pTempTo, NULL, NULL))
1215                 { /* target is the same as source ? */
1216                     /* we still need the value for the returncode, we assume 0x71 */
1217                     retCode = 0x71;
1218                     goto shfileop_error;
1219                 }
1220                 if (IsAttribDir((ToAttr & wfd.dwFileAttributes)))
1221                 {
1222                     if (IsAttribDir(ToAttr) || !SHCreateDirectoryExW(NULL,pTempTo, NULL))
1223                     {
1224 /* ???            nFileOp.fFlags = (nFileOp.fFlags | FOF_MULTIDESTFILES); */
1225                         SHFileStrCpyCatW(pTempFrom, NULL, wWildcardFile);
1226                         retCode = SHFileOperationW(&nFileOp);
1227                     }
1228                     else
1229                     {
1230                         retCode = 0x750;/* value unknown */
1231                         goto shfileop_error;
1232                     }
1233                 }
1234                 else
1235                 {
1236                     if (!(ask_overwrite && SHELL_ConfirmDialogW(ASK_OVERWRITE_FILE, pTempTo))
1237                         && (not_overwrite))
1238                     {
1239                         /* we still need the value for the returncode, we use the mostly assumed */
1240                         retCode = 0x73;
1241                         goto shfileop_error;
1242                     }
1243                     if (SHNotifyCopyFileW(pTempFrom, pTempTo, nFileOp.fFlags & FOF_RENAMEONCOLLISION) != ERROR_SUCCESS)
1244                     {
1245                         retCode = 0x77; /* value unknown */
1246                         goto shfileop_error;
1247                     }
1248                 }
1249             }
1250         }
1251
1252 shfileop_normal:
1253         if (!(nFileOp.fAnyOperationsAborted))
1254           retCode = 0;
1255 shfileop_error:
1256         if (hFind != INVALID_HANDLE_VALUE)
1257           FindClose(hFind);
1258         hFind = INVALID_HANDLE_VALUE;
1259         if (pTempFrom)
1260           HeapFree(GetProcessHeap(), 0, pTempFrom);
1261         if (retCode)
1262         {
1263           nFileOp.fAnyOperationsAborted = TRUE;
1264         }
1265         TRACE("%s level=%ld AnyOpsAborted=%s ret=0x%x, with %s %s%s\n",
1266               debug_shfileops_action(FuncSwitch), level,
1267               nFileOp.fAnyOperationsAborted ? "TRUE":"FALSE",
1268               retCode, debugstr_w(pFrom), pTo ? "-> ":"", debugstr_w(pTo));
1269
1270         lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
1271         return retCode;
1272 }
1273
1274 /*************************************************************************
1275  * SHFileOperation        [SHELL32.@]
1276  *
1277  */
1278 DWORD WINAPI SHFileOperationAW(LPVOID lpFileOp)
1279 {
1280         if (SHELL_OsIsUnicode())
1281           return SHFileOperationW(lpFileOp);
1282         return SHFileOperationA(lpFileOp);
1283 }
1284
1285 /*************************************************************************
1286  * SheGetDirW [SHELL32.281]
1287  *
1288  */
1289 HRESULT WINAPI SheGetDirW(LPWSTR u, LPWSTR v)
1290 {       FIXME("%p %p stub\n",u,v);
1291         return 0;
1292 }
1293
1294 /*************************************************************************
1295  * SheChangeDirW [SHELL32.274]
1296  *
1297  */
1298 HRESULT WINAPI SheChangeDirW(LPWSTR u)
1299 {       FIXME("(%s),stub\n",debugstr_w(u));
1300         return 0;
1301 }
1302
1303 /*************************************************************************
1304  * IsNetDrive                   [SHELL32.66]
1305  */
1306 BOOL WINAPI IsNetDrive(DWORD drive)
1307 {
1308         char root[4];
1309         strcpy(root, "A:\\");
1310         root[0] += (char)drive;
1311         return (GetDriveTypeA(root) == DRIVE_REMOTE);
1312 }