Change parameter in SHGetSpecialFolderPathA/W to int as documented in
[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 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
252 {
253         TRACE("(%s, %p)\n", debugstr_w(path), sec);
254
255         if (StrPBrkW(path, wWildcardChars))
256         {
257           /* FIXME: This test is currently necessary since our CreateDirectory
258              implementation does create directories with wildcard characters
259              without objection!! Once this is fixed, this here can go away. */
260           SetLastError(ERROR_INVALID_NAME);
261           return ERROR_INVALID_NAME;
262         }
263
264         if (CreateDirectoryW(path, sec))
265         {
266           SHChangeNotify(SHCNE_MKDIR, SHCNF_PATHW, path, NULL);
267           return ERROR_SUCCESS;
268         }
269         return GetLastError();
270 }
271
272 BOOL WINAPI Win32CreateDirectoryAW(LPCVOID path, LPSECURITY_ATTRIBUTES sec)
273 {
274         if (SHELL_OsIsUnicode())
275           return (SHNotifyCreateDirectoryW(path, sec) == ERROR_SUCCESS);
276         return (SHNotifyCreateDirectoryA(path, sec) == ERROR_SUCCESS);
277 }
278
279 /************************************************************************
280  * Win32RemoveDirectory      [SHELL32.94]
281  *
282  * Deletes a directory. Also triggers a change notify if one exists.
283  *
284  * PARAMS
285  *  path       [I]   path to directory to delete
286  *
287  * RETURNS
288  *  TRUE if successful, FALSE otherwise
289  *
290  * NOTES:
291  *  Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
292  *  This is Unicode on NT/2000
293  */
294
295 static DWORD SHNotifyRemoveDirectoryA(LPCSTR path)
296 {
297         WCHAR wPath[MAX_PATH];
298         TRACE("(%s)\n", debugstr_a(path));
299
300         MultiByteToWideChar(CP_ACP, 0, path, -1, wPath, MAX_PATH);
301         return SHNotifyRemoveDirectoryW(wPath);
302 }
303
304 /***********************************************************************/
305
306 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path)
307 {
308         BOOL ret;
309         TRACE("(%s)\n", debugstr_w(path));
310
311         ret = RemoveDirectoryW(path);
312         if (!ret)
313         {
314           /* Directory may be write protected */
315           DWORD dwAttr = GetFileAttributesW(path);
316           if (dwAttr != -1 && dwAttr & FILE_ATTRIBUTE_READONLY)
317             if (SetFileAttributesW(path, dwAttr & ~FILE_ATTRIBUTE_READONLY))
318               ret = RemoveDirectoryW(path);
319         }
320         if (ret)
321         {
322           SHChangeNotify(SHCNE_RMDIR, SHCNF_PATHW, path, NULL);
323           return ERROR_SUCCESS;
324         }
325         return GetLastError();
326 }
327
328 /***********************************************************************/
329
330 BOOL WINAPI Win32RemoveDirectoryAW(LPCVOID path)
331 {
332         if (SHELL_OsIsUnicode())
333           return (SHNotifyRemoveDirectoryW(path) == ERROR_SUCCESS);
334         return (SHNotifyRemoveDirectoryA(path) == ERROR_SUCCESS);
335 }
336
337 /************************************************************************
338  * Win32DeleteFile           [SHELL32.164]
339  *
340  * Deletes a file. Also triggers a change notify if one exists.
341  *
342  * PARAMS
343  *  path       [I]   path to file to delete
344  *
345  * RETURNS
346  *  TRUE if successful, FALSE otherwise
347  *
348  * NOTES:
349  *  Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
350  *  This is Unicode on NT/2000
351  */
352
353 static DWORD SHNotifyDeleteFileA(LPCSTR path)
354 {
355         WCHAR wPath[MAX_PATH];
356         TRACE("(%s)\n", debugstr_a(path));
357
358         MultiByteToWideChar(CP_ACP, 0, path, -1, wPath, MAX_PATH);
359         return SHNotifyDeleteFileW(wPath);
360 }
361
362 /***********************************************************************/
363
364 static DWORD SHNotifyDeleteFileW(LPCWSTR path)
365 {
366         BOOL ret;
367
368         TRACE("(%s)\n", debugstr_w(path));
369
370         ret = DeleteFileW(path);
371         if (!ret)
372         {
373           /* File may be write protected or a system file */
374           DWORD dwAttr = GetFileAttributesW(path);
375           if ((dwAttr != -1) && (dwAttr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
376             if (SetFileAttributesW(path, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
377               ret = DeleteFileW(path);
378         }
379         if (ret)
380         {
381           SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, path, NULL);
382           return ERROR_SUCCESS;
383         }
384         return GetLastError();
385 }
386
387 /***********************************************************************/
388
389 DWORD WINAPI Win32DeleteFileAW(LPCVOID path)
390 {
391         if (SHELL_OsIsUnicode())
392           return (SHNotifyDeleteFileW(path) == ERROR_SUCCESS);
393         return (SHNotifyDeleteFileA(path) == ERROR_SUCCESS);
394 }
395
396 /************************************************************************
397  * SHNotifyMoveFile          [internal]
398  *
399  * Moves a file. Also triggers a change notify if one exists.
400  *
401  * PARAMS
402  *  src        [I]   path to source file to move
403  *  dest       [I]   path to target file to move to
404  *  bRename    [I]   if TRUE, the target file will be renamed if a
405  *                   file with this name already exists
406  *
407  * RETURNS
408  *  ERORR_SUCCESS if successful
409  */
410 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest, BOOL bRename)
411 {
412         BOOL ret;
413
414         TRACE("(%s %s %s)\n", debugstr_w(src), debugstr_w(dest), bRename ? "renameIfExists" : "");
415
416         ret = MoveFileW(src, dest);
417         if (!ret)
418         {
419           /* Source file may be write protected or a system file */
420           DWORD dwAttr = GetFileAttributesW(src);
421           if ((dwAttr != -1) && (dwAttr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
422             if (SetFileAttributesW(src, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
423               ret = MoveFileW(src, dest);
424
425           if (!ret && bRename)
426           {
427             /* Destination file probably exists */
428             dwAttr = GetFileAttributesW(dest);
429             if (dwAttr != -1)
430             {
431               FIXME("Rename on move to existing file not implemented!");
432             }
433           }
434         }
435         if (ret)
436         {
437           SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_PATHW, src, dest);
438           return ERROR_SUCCESS;
439         }
440         return GetLastError();
441 }
442
443 /************************************************************************
444  * SHNotifyCopyFile          [internal]
445  *
446  * Copies a file. Also triggers a change notify if one exists.
447  *
448  * PARAMS
449  *  src        [I]   path to source file to move
450  *  dest       [I]   path to target file to move to
451  *  bRename    [I]   if TRUE, the target file will be renamed if a
452  *                   file with this name already exists
453  *
454  * RETURNS
455  *  ERROR_SUCCESS if successful
456  */
457 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bRename)
458 {
459         BOOL ret;
460
461         TRACE("(%s %s %s)\n", debugstr_w(src), debugstr_w(dest), bRename ? "renameIfExists" : "");
462
463         ret = CopyFileW(src, dest, TRUE);
464         if (!ret && bRename)
465         {
466           /* Destination file probably exists */
467           DWORD dwAttr = GetFileAttributesW(dest);
468           if (dwAttr != -1)
469           {
470             FIXME("Rename on copy to existing file not implemented!");
471           }
472         }
473         if (ret)
474         {
475           SHChangeNotify(SHCNE_CREATE, SHCNF_PATHW, dest, NULL);
476           return ERROR_SUCCESS;
477         }
478         return GetLastError();
479 }
480
481 /*************************************************************************
482  * SHCreateDirectory         [SHELL32.165]
483  *
484  * Create a directory at the specified location
485  *
486  * PARAMS
487  *  hWnd       [I]
488  *  path       [I]   path of directory to create
489  *
490  * RETURNS
491  *  ERRROR_SUCCESS or one of the following values:
492  *  ERROR_BAD_PATHNAME if the path is relative
493  *  ERROR_FILE_EXISTS when a file with that name exists
494  *  ERROR_ALREADY_EXISTS when the directory already exists
495  *  ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
496  *
497  * NOTES
498  *  exported by ordinal
499  *  Win9x exports ANSI
500  *  WinNT/2000 exports Unicode
501  */
502 DWORD WINAPI SHCreateDirectory(HWND hWnd, LPCVOID path)
503 {
504         if (SHELL_OsIsUnicode())
505           return SHCreateDirectoryExW(hWnd, path, NULL);
506         return SHCreateDirectoryExA(hWnd, path, NULL);
507 }
508
509 /*************************************************************************
510  * SHCreateDirectoryExA      [SHELL32.@]
511  *
512  * Create a directory at the specified location
513  *
514  * PARAMS
515  *  hWnd       [I]   
516  *  path       [I]   path of directory to create 
517  *  sec        [I]   security attributes to use or NULL
518  *
519  * RETURNS
520  *  ERRROR_SUCCESS or one of the following values:
521  *  ERROR_BAD_PATHNAME if the path is relative
522  *  ERORO_INVALID_NAME if the path contains invalid chars
523  *  ERROR_FILE_EXISTS when a file with that name exists
524  *  ERROR_ALREADY_EXISTS when the directory already exists
525  *  ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
526  */
527 DWORD WINAPI SHCreateDirectoryExA(HWND hWnd, LPCSTR path, LPSECURITY_ATTRIBUTES sec)
528 {
529         WCHAR wPath[MAX_PATH];
530         TRACE("(%p, %s, %p)\n",hWnd, debugstr_a(path), sec);
531
532         MultiByteToWideChar(CP_ACP, 0, path, -1, wPath, MAX_PATH);
533         return SHCreateDirectoryExW(hWnd, wPath, sec);
534 }
535
536 /*************************************************************************
537  * SHCreateDirectoryExW      [SHELL32.@]
538  */
539 DWORD WINAPI SHCreateDirectoryExW(HWND hWnd, LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
540 {
541         DWORD ret = ERROR_BAD_PATHNAME;
542         TRACE("(%p, %s, %p)\n",hWnd, debugstr_w(path), sec);
543
544         if (PathIsRelativeW(path))
545         {
546           SetLastError(ret);
547         }
548         else
549         {
550           ret = SHNotifyCreateDirectoryW(path, sec);
551           if (ret != ERROR_FILE_EXISTS &&
552               ret != ERROR_ALREADY_EXISTS &&
553               ret != ERROR_FILENAME_EXCED_RANGE)
554           {
555           /* handling network file names?
556             lstrcpynW(pathName, path, MAX_PATH);
557             lpStr = PathAddBackslashW(pathName);*/
558             FIXME("Semi-stub, non zero hWnd should be used somehow?");
559           }
560         }
561         return ret;
562 }
563
564 /*************************************************************************
565  *
566  * SHFileStrICmp HelperFunction for SHFileOperationW
567  *
568  */
569 BOOL SHFileStrICmpW(LPWSTR p1, LPWSTR p2, LPWSTR p1End, LPWSTR p2End)
570 {
571         WCHAR C1 = '\0';
572         WCHAR C2 = '\0';
573         int i_Temp = -1;
574         int i_len1 = lstrlenW(p1);
575         int i_len2 = lstrlenW(p2);
576
577         if (p1End && (&p1[i_len1] >= p1End) && ('\\' == p1End[0]))
578         {
579           C1 = p1End[0];
580           p1End[0] = '\0';
581           i_len1 = lstrlenW(p1);
582         }
583         if (p2End)
584         {
585           if ((&p2[i_len2] >= p2End) && ('\\' == p2End[0]))
586           {
587             C2 = p2End[0];
588             if (C2)
589               p2End[0] = '\0';
590           }
591         }
592         else
593         {
594           if ((i_len1 <= i_len2) && ('\\' == p2[i_len1]))
595           {
596             C2 = p2[i_len1];
597             if (C2)
598               p2[i_len1] = '\0';
599           }
600         }
601         i_len2 = lstrlenW(p2);
602         if (i_len1 == i_len2)
603           i_Temp = lstrcmpiW(p1,p2);
604         if (C1)
605           p1[i_len1] = C1;
606         if (C2)
607           p2[i_len2] = C2;
608         return !(i_Temp);
609 }
610
611 /*************************************************************************
612  *
613  * SHFileStrCpyCat HelperFunction for SHFileOperationW
614  *
615  */
616 LPWSTR SHFileStrCpyCatW(LPWSTR pTo, LPCWSTR pFrom, LPCWSTR pCatStr)
617 {
618         LPWSTR pToFile = NULL;
619         int  i_len;
620         if (pTo)
621         {
622           if (pFrom)
623             lstrcpyW(pTo, pFrom);
624           if (pCatStr)
625           {
626             i_len = lstrlenW(pTo);
627             if ((i_len) && (pTo[--i_len] != '\\'))
628               i_len++;
629             pTo[i_len] = '\\';
630             if (pCatStr[0] == '\\')
631               pCatStr++; \
632             lstrcpyW(&pTo[i_len+1], pCatStr);
633           }
634           pToFile = StrRChrW(pTo,NULL,'\\');
635           /* termination of the new string-group */
636           pTo[(lstrlenW(pTo)) + 1] = '\0';
637         }
638         return pToFile;
639 }
640
641 /**************************************************************************
642  *      SHELL_FileNamesMatch()
643  *
644  * Accepts two \0 delimited lists of the file names. Checks whether number of
645  * files in both lists is the same, and checks also if source-name exists.
646  */
647 BOOL SHELL_FileNamesMatch(LPCWSTR pszFiles1, LPCWSTR pszFiles2, BOOL bOnlySrc)
648 {
649         while ((pszFiles1[0] != '\0') &&
650                (bOnlySrc || (pszFiles2[0] != '\0')))
651         {
652           if (NULL == StrPBrkW(pszFiles1, wWildcardChars))
653           {
654             if (-1 == GetFileAttributesW(pszFiles1))
655               return FALSE;
656           }
657           pszFiles1 += lstrlenW(pszFiles1) + 1;
658           if (!bOnlySrc)
659             pszFiles2 += lstrlenW(pszFiles2) + 1;
660         }
661         return ((pszFiles1[0] == '\0') && (bOnlySrc || (pszFiles2[0] == '\0')));
662 }
663
664 /*************************************************************************
665  *
666  * SHNameTranslate HelperFunction for SHFileOperationA
667  *
668  * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
669  * is NULL, only the necessary size of the string is determined and returned,
670  * otherwise the ASCII strings are copied into it and the buffer is increased
671  * to point to the location after the final 0 termination char.
672  */
673 DWORD SHNameTranslate(LPWSTR* wString, LPCWSTR* pWToFrom, BOOL more)
674 {
675         DWORD size = 0, aSize = 0;
676         LPCSTR aString = (LPCSTR)*pWToFrom;
677
678         if (aString)
679         {
680           do
681           {
682             size = lstrlenA(aString) + 1;
683             aSize += size;
684             aString += size;
685           } while ((size != 1) && more);
686           /* The two sizes might be different in the case of multibyte chars */
687           size = MultiByteToWideChar(CP_ACP, 0, aString, aSize, *wString, 0);
688           if (*wString) /* only in the second loop */
689           {
690             MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*pWToFrom, aSize, *wString, size);
691             *pWToFrom = *wString;
692             *wString += size;
693           }
694         }
695         return size;
696 }
697 /*************************************************************************
698  * SHFileOperationA          [SHELL32.@]
699  *
700  * Function to copy, move, delete and create one or more files with optional
701  * user prompts.
702  *
703  * PARAMS
704  *  lpFileOp   [I/O] pointer to a structure containing all the necessary information
705  *
706  * NOTES
707  *  exported by name
708  */
709 DWORD WINAPI SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp)
710 {
711         SHFILEOPSTRUCTW nFileOp = *((LPSHFILEOPSTRUCTW)lpFileOp);
712         DWORD retCode = 0, size;
713         LPWSTR ForFree = NULL, /* we change wString in SHNameTranslate and can't use it for freeing */
714                wString = NULL; /* we change this in SHNameTranslate */
715
716         TRACE("\n");
717         if (FO_DELETE == (nFileOp.wFunc & FO_MASK))
718           nFileOp.pTo = NULL; /* we need a NULL or a valid pointer for translation */
719         if (!(nFileOp.fFlags & FOF_SIMPLEPROGRESS))
720           nFileOp.lpszProgressTitle = NULL; /* we need a NULL or a valid pointer for translation */
721         while (1) /* every loop calculate size, second translate also, if we have storage for this */
722         {
723           size = SHNameTranslate(&wString, &nFileOp.lpszProgressTitle, FALSE); /* no loop */
724           size += SHNameTranslate(&wString, &nFileOp.pFrom, TRUE); /* internal loop */
725           size += SHNameTranslate(&wString, &nFileOp.pTo, TRUE); /* internal loop */
726
727           if (ForFree)
728           {
729             retCode = SHFileOperationW(&nFileOp);
730             HeapFree(GetProcessHeap(), 0, ForFree); /* we can not use wString, it was changed */
731             break;
732           }
733           else
734           {
735             wString = ForFree = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
736             if (ForFree) continue;
737             retCode = ERROR_OUTOFMEMORY;
738             nFileOp.fAnyOperationsAborted = TRUE;
739             SetLastError(retCode);
740             return retCode;
741           }
742         }
743
744         lpFileOp->hNameMappings = nFileOp.hNameMappings;
745         lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
746         return retCode;
747 }
748
749 static const char * debug_shfileops_flags( DWORD fFlags )
750 {
751     return wine_dbg_sprintf( "%s%s%s%s%s%s%s%s%s%s%s%s%s",
752         fFlags & FOF_MULTIDESTFILES ? "FOF_MULTIDESTFILES " : "",
753         fFlags & FOF_CONFIRMMOUSE ? "FOF_CONFIRMMOUSE " : "",
754         fFlags & FOF_SILENT ? "FOF_SILENT " : "",
755         fFlags & FOF_RENAMEONCOLLISION ? "FOF_RENAMEONCOLLISION " : "",
756         fFlags & FOF_NOCONFIRMATION ? "FOF_NOCONFIRMATION " : "",
757         fFlags & FOF_WANTMAPPINGHANDLE ? "FOF_WANTMAPPINGHANDLE " : "",
758         fFlags & FOF_ALLOWUNDO ? "FOF_ALLOWUNDO " : "",
759         fFlags & FOF_FILESONLY ? "FOF_FILESONLY " : "",
760         fFlags & FOF_SIMPLEPROGRESS ? "FOF_SIMPLEPROGRESS " : "",
761         fFlags & FOF_NOCONFIRMMKDIR ? "FOF_NOCONFIRMMKDIR " : "",
762         fFlags & FOF_NOERRORUI ? "FOF_NOERRORUI " : "",
763         fFlags & FOF_NOCOPYSECURITYATTRIBS ? "FOF_NOCOPYSECURITYATTRIBS" : "",
764         fFlags & 0xf000 ? "MORE-UNKNOWN-Flags" : "");
765 }
766
767 static const char * debug_shfileops_action( DWORD op )
768 {
769     LPCSTR cFO_Name [] = {"FO_????","FO_MOVE","FO_COPY","FO_DELETE","FO_RENAME"};
770     return wine_dbg_sprintf("%s", cFO_Name[ op ]);
771 }
772
773 /*************************************************************************
774  * SHFileOperationW          [SHELL32.@]
775  *
776  * See SHFileOperationA
777  */
778 DWORD WINAPI SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp)
779 {
780         SHFILEOPSTRUCTW nFileOp = *(lpFileOp);
781
782         LPCWSTR pNextFrom = nFileOp.pFrom;
783         LPCWSTR pNextTo = nFileOp.pTo;
784         LPCWSTR pFrom = pNextFrom;
785         LPCWSTR pTo = NULL;
786         HANDLE hFind = INVALID_HANDLE_VALUE;
787         WIN32_FIND_DATAW wfd;
788         LPWSTR pTempFrom = NULL;
789         LPWSTR pTempTo = NULL;
790         LPWSTR pFromFile;
791         LPWSTR pToFile = NULL;
792         LPWSTR lpFileName;
793         long retCode = 0;
794         DWORD ToAttr;
795         DWORD ToPathAttr;
796         DWORD FromPathAttr;
797         FILEOP_FLAGS OFl = ((FILEOP_FLAGS)lpFileOp->fFlags & 0xfff);
798
799         BOOL b_Multi = (nFileOp.fFlags & FOF_MULTIDESTFILES);
800
801         BOOL b_MultiTo = (FO_DELETE != (lpFileOp->wFunc & FO_MASK));
802         BOOL b_MultiPaired = (!b_MultiTo);
803         BOOL b_MultiFrom = FALSE;
804         BOOL not_overwrite;
805         BOOL ask_overwrite;
806         BOOL b_SameRoot;
807         BOOL b_SameTailName;
808         BOOL b_ToInvalidTail = FALSE;
809         BOOL b_ToValid; /* for W98-Bug for FO_MOVE with source and target in same rootdrive */
810         BOOL b_Mask;
811         BOOL b_ToTailSlash = FALSE;
812
813         long FuncSwitch = (nFileOp.wFunc & FO_MASK);
814         long level= nFileOp.wFunc>>4;
815
816         /*  default no error */
817         nFileOp.fAnyOperationsAborted = FALSE;
818
819         if ((FuncSwitch < FO_MOVE) || (FuncSwitch > FO_RENAME))
820           goto shfileop_normal; /* no valid FunctionCode */
821
822         if (level == 0)
823             TRACE("%s: flags (0x%04x) : %s\n",
824                 debug_shfileops_action(FuncSwitch), nFileOp.fFlags,
825                 debug_shfileops_flags(nFileOp.fFlags) );
826
827         /* establish when pTo is interpreted as the name of the destination file
828          * or the directory where the Fromfile should be copied to.
829          * This depends on:
830          * (1) pTo points to the name of an existing directory;
831          * (2) the flag FOF_MULTIDESTFILES is present;
832          * (3) whether pFrom point to multiple filenames.
833          *
834          * Some experiments:
835          *
836          * destisdir               1 1 1 1 0 0 0 0
837          * FOF_MULTIDESTFILES      1 1 0 0 1 1 0 0
838          * multiple from filenames 1 0 1 0 1 0 1 0
839          *                         ---------------
840          * copy files to dir       1 0 1 1 0 0 1 0
841          * create dir              0 0 0 0 0 0 1 0
842          */
843 /*
844  * FOF_MULTIDESTFILES, FOF_NOCONFIRMATION, FOF_FILESONLY  are implemented
845  * FOF_CONFIRMMOUSE, FOF_SILENT, FOF_NOCONFIRMMKDIR,
846  *       FOF_SIMPLEPROGRESS, FOF_NOCOPYSECURITYATTRIBS    are not implemented and ignored
847  * FOF_RENAMEONCOLLISION                                  are implemented partially and breaks if file exist
848  * FOF_ALLOWUNDO, FOF_WANTMAPPINGHANDLE                   are not implemented and breaks
849  * if any other flag set, an error occurs
850  */
851         TRACE("%s level=%ld nFileOp.fFlags=0x%x\n", 
852                 debug_shfileops_action(FuncSwitch), level, lpFileOp->fFlags);
853
854 /*    OFl &= (-1 - (FOF_MULTIDESTFILES | FOF_FILESONLY)); */
855 /*    OFl ^= (FOF_SILENT | FOF_NOCONFIRMATION | FOF_SIMPLEPROGRESS | FOF_NOCONFIRMMKDIR); */
856         OFl &= (~(FOF_MULTIDESTFILES | FOF_NOCONFIRMATION | FOF_FILESONLY));  /* implemented */
857         OFl ^= (FOF_SILENT | FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_NOCOPYSECURITYATTRIBS); /* ignored, if one */
858         OFl &= (~FOF_SIMPLEPROGRESS);                      /* ignored, only with FOF_SILENT */
859         if (OFl)
860         {
861             if (OFl & (~(FOF_CONFIRMMOUSE | FOF_SILENT | FOF_RENAMEONCOLLISION |
862                          FOF_NOCONFIRMMKDIR | FOF_NOERRORUI | FOF_NOCOPYSECURITYATTRIBS)))
863             {
864                 TRACE("%s level=%ld lpFileOp->fFlags=0x%x not implemented, Aborted=TRUE, stub\n",
865                       debug_shfileops_action(FuncSwitch), level, OFl);
866                 retCode = 0x403; /* 1027, we need an extension to shlfileop */
867                 goto shfileop_error;
868             }
869             else
870             {
871                 TRACE("%s level=%ld lpFileOp->fFlags=0x%x not fully implemented, stub\n", 
872                       debug_shfileops_action(FuncSwitch), level, OFl);
873             } 
874         } 
875
876         if ((pNextFrom) && (!(b_MultiTo) || (pNextTo)))
877         {
878             nFileOp.pFrom = pTempFrom = HeapAlloc(GetProcessHeap(), 0, ((1 + 2 * (b_MultiTo)) * MAX_PATH + 6) * sizeof(WCHAR));
879             if (!pTempFrom)
880             {
881                 retCode = ERROR_OUTOFMEMORY;
882                 SetLastError(retCode);
883                 goto shfileop_error;
884             }
885             if (b_MultiTo)
886                 pTempTo = &pTempFrom[MAX_PATH + 4];
887             nFileOp.pTo = pTempTo;
888             ask_overwrite = (!(nFileOp.fFlags & FOF_NOCONFIRMATION) && !(nFileOp.fFlags & FOF_RENAMEONCOLLISION));
889             not_overwrite = (!(nFileOp.fFlags & FOF_NOCONFIRMATION) ||  (nFileOp.fFlags & FOF_RENAMEONCOLLISION));
890         }
891         else
892         {
893             retCode = 0x402;      /* 1026 */
894             goto shfileop_error;
895         }
896         /* need break at error before change sourcepointer */
897         while(!nFileOp.fAnyOperationsAborted && (pNextFrom[0]))
898         {
899             nFileOp.wFunc =  ((level + 1) << 4) + FuncSwitch;
900             nFileOp.fFlags = lpFileOp->fFlags;
901
902             if (b_MultiTo)
903             {
904                 pTo = pNextTo;
905                 pNextTo = &pNextTo[lstrlenW(pTo)+1];
906                 b_MultiTo = (b_Multi && pNextTo[0]);
907             }
908
909             pFrom = pNextFrom;
910             pNextFrom = &pNextFrom[lstrlenW(pNextFrom)+1];
911             if (!b_MultiFrom && !b_MultiTo)
912                 b_MultiFrom = (pNextFrom[0]);
913
914             pFromFile = SHFileStrCpyCatW(pTempFrom, pFrom, NULL);
915
916             if (pTo)
917             {
918                 pToFile = SHFileStrCpyCatW(pTempTo, pTo, NULL);
919             }
920             if (!b_MultiPaired)
921             {
922                 b_MultiPaired =
923                     SHELL_FileNamesMatch(lpFileOp->pFrom, lpFileOp->pTo, (!b_Multi || b_MultiFrom));
924             }
925             if (!(b_MultiPaired) || !(pFromFile) || !(pFromFile[1]) || ((pTo) && !(pToFile)))
926             {
927                 retCode = 0x402;      /* 1026 */
928                 goto shfileop_error;
929             }
930             if (pTo)
931             {
932                 b_ToTailSlash = (!pToFile[1]);
933                 if (b_ToTailSlash)
934                 {
935                     pToFile[0] = '\0';
936                     if (StrChrW(pTempTo,'\\'))
937                     {
938                         pToFile = SHFileStrCpyCatW(pTempTo, NULL, NULL);
939                     }
940                 }
941                 b_ToInvalidTail = (NULL != StrPBrkW(&pToFile[1], wWildcardChars));
942             }
943
944             /* for all */
945             b_Mask = (NULL != StrPBrkW(&pFromFile[1], wWildcardChars));
946             if (FO_RENAME == FuncSwitch)
947             {
948                 /* temporary only for FO_RENAME */
949 /* ???        b_Mask = (NULL != strrbrk(pFrom,"*?")); */
950                 if (b_MultiTo || b_MultiFrom || (b_Mask && !b_ToInvalidTail))
951                 {
952                     /* no work, only RC=0 */
953 /* ???          nFileOp.fAnyOperationsAborted = TRUE; */
954 /*#define W98_FO_RENEME */
955 #ifdef W98_FO_RENEME
956                     goto shfileop_normal;
957 #endif
958                     retCode = 0x1;      /* 1 value unknown, W98 returns no error */
959                     goto shfileop_error;
960                 }
961             }
962
963             hFind = FindFirstFileW(pFrom, &wfd);
964             if (INVALID_HANDLE_VALUE == hFind)
965             {
966                 if ((FO_DELETE == FuncSwitch) && (b_Mask))
967                 {
968                     pFromFile[0] = '\0';
969                     FromPathAttr = GetFileAttributesW(pTempFrom);
970                     pFromFile[0] = '\\';
971                     if (IsAttribDir(FromPathAttr))
972                     {
973                         /* FO_DELETE with mask and without found is valid */
974                         goto shfileop_normal;
975                     }
976                 }
977                 /* root (without mask) is also not allowed as source, tested in W98 */
978                 retCode = 0x402;   /* 1026 */
979                 goto shfileop_error;
980             }
981
982 /* for all */
983 #define HIGH_ADR (LPWSTR)0xffffffff
984
985 /* ???      b_Mask = (!SHFileStrICmpA(&pFromFile[1], &wfd.cFileName[0], HIGH_ADR, HIGH_ADR)); */
986             if (!pTo) /* FO_DELETE */
987             {
988                 do
989                 {
990                     lpFileName = wfd.cAlternateFileName;
991                     if (!lpFileName[0])
992                         lpFileName = wfd.cFileName;
993                     if (IsDotDir(lpFileName) ||
994                         ((b_Mask) && IsAttribDir(wfd.dwFileAttributes) && (nFileOp.fFlags & FOF_FILESONLY)))
995                         continue;
996                     SHFileStrCpyCatW(&pFromFile[1], lpFileName, NULL);
997                     /* TODO: Check the SHELL_DeleteFileOrDirectoryW() function in shell32.dll */
998                     if (IsAttribFile(wfd.dwFileAttributes))
999                     {
1000                         nFileOp.fAnyOperationsAborted = (SHNotifyDeleteFileW(pTempFrom) != ERROR_SUCCESS);
1001                         retCode = 0x78; /* value unknown */
1002                     }
1003                     else
1004                     {
1005                         nFileOp.fAnyOperationsAborted = (!SHELL_DeleteDirectoryW(pTempFrom, (!(nFileOp.fFlags & FOF_NOCONFIRMATION))));
1006                         retCode = 0x79; /* value unknown */
1007                     }
1008                 } while (!nFileOp.fAnyOperationsAborted && FindNextFileW(hFind, &wfd));
1009                 FindClose(hFind);
1010                 hFind = INVALID_HANDLE_VALUE;
1011                 if (nFileOp.fAnyOperationsAborted)
1012                 {
1013                     goto shfileop_error;
1014                 }
1015                 continue;
1016             } /* FO_DELETE ends, pTo must be always valid from here */
1017
1018             b_SameRoot = (toupperW(pTempFrom[0]) == toupperW(pTempTo[0]));
1019             b_SameTailName = SHFileStrICmpW(pToFile, pFromFile, NULL, NULL);
1020
1021             ToPathAttr = ToAttr = GetFileAttributesW(pTempTo);
1022             if (!b_Mask && (ToAttr == -1) && (pToFile))
1023             {
1024                 pToFile[0] = '\0';
1025                 ToPathAttr = GetFileAttributesW(pTempTo);
1026                 pToFile[0] = '\\';
1027             }
1028
1029             if (FO_RENAME == FuncSwitch)
1030             {
1031                 if (!b_SameRoot || b_Mask /* FO_RENAME works not with Mask */
1032                     || !SHFileStrICmpW(pTempFrom, pTempTo, pFromFile, NULL)
1033                     || (SHFileStrICmpW(pTempFrom, pTempTo, pFromFile, HIGH_ADR) && !b_ToTailSlash))
1034                 {
1035                     retCode = 0x73;
1036                     goto shfileop_error;
1037                 }
1038                 if (b_ToInvalidTail)
1039                 {
1040                     retCode=0x2;
1041                     goto shfileop_error;
1042                 }
1043                 if (-1 == ToPathAttr)
1044                 {
1045                     retCode = 0x75;
1046                     goto shfileop_error;
1047                 }
1048                 if (IsAttribDir(wfd.dwFileAttributes) && IsAttribDir(ToAttr))
1049                 {
1050                     retCode = (b_ToTailSlash) ? 0xb7 : 0x7b;
1051                     goto shfileop_error;
1052                 }
1053                 /* we use SHNotifyMoveFile() instead MoveFileW */
1054                 if (SHNotifyMoveFileW(pTempFrom, pTempTo, nFileOp.fFlags & FOF_RENAMEONCOLLISION) != ERROR_SUCCESS)
1055                 {
1056                     /* we need still the value for the returncode, we use the mostly assumed */
1057                     retCode = 0xb7;
1058                     goto shfileop_error;
1059                 }
1060                 goto shfileop_normal;
1061             }
1062
1063             /* W98 Bug with FO_MOVE different to FO_COPY, better the same as FO_COPY */
1064             b_ToValid = ((b_SameTailName &&  b_SameRoot && (FO_COPY == FuncSwitch)) ||
1065                          (b_SameTailName && !b_SameRoot) || (b_ToInvalidTail));
1066
1067             /* handle mask in source */
1068             if (b_Mask)
1069             {
1070                 if (!IsAttribDir(ToAttr))
1071                 {
1072                     retCode = (b_ToInvalidTail &&/* b_SameTailName &&*/ (FO_MOVE == FuncSwitch)) \
1073                         ? 0x2 : 0x75;
1074                     goto shfileop_error;
1075                 }
1076                 pToFile = SHFileStrCpyCatW(pTempTo, NULL, wBackslash);
1077                 nFileOp.fFlags = (nFileOp.fFlags | FOF_MULTIDESTFILES);
1078                 do
1079                 {
1080                     lpFileName = wfd.cAlternateFileName;
1081                     if (!lpFileName[0])
1082                         lpFileName = wfd.cFileName;
1083                     if (IsDotDir(lpFileName) ||
1084                         (IsAttribDir(wfd.dwFileAttributes) && (nFileOp.fFlags & FOF_FILESONLY)))
1085                         continue; /* next name in pTempFrom(dir) */
1086                     SHFileStrCpyCatW(&pToFile[1], lpFileName, NULL);
1087                     SHFileStrCpyCatW(&pFromFile[1], lpFileName, NULL);
1088                     retCode = SHFileOperationW (&nFileOp);
1089                 } while(!nFileOp.fAnyOperationsAborted && FindNextFileW(hFind, &wfd));
1090             }
1091             FindClose(hFind);
1092             hFind = INVALID_HANDLE_VALUE;
1093             /* FO_COPY/FO_MOVE with mask, FO_DELETE and FO_RENAME are solved */
1094             if (b_Mask)
1095                 continue;
1096
1097             /* only FO_COPY/FO_MOVE without mask, all others are (must be) solved */
1098             if (IsAttribDir(wfd.dwFileAttributes) && (ToAttr == -1))
1099             {
1100                 if (pToFile)
1101                 {
1102                     pToFile[0] = '\0';
1103                     ToPathAttr = GetFileAttributesW(pTempTo);
1104                     if ((ToPathAttr == -1) && b_ToValid)
1105                     {
1106                         /* create dir must be here, sample target D:\y\ *.* create with RC=10003 */
1107                         if (SHCreateDirectoryExW(NULL, pTempTo, NULL))
1108                         {
1109                             retCode = 0x73;/* value unknown */
1110                             goto shfileop_error;
1111                         }
1112                         ToPathAttr = GetFileAttributesW(pTempTo);
1113                     }
1114                     pToFile[0] = '\\';
1115                     if (b_ToInvalidTail)
1116                     {
1117                         retCode = 0x10003;
1118                         goto shfileop_error;
1119                     }
1120                 }
1121             }
1122
1123             /* trailing BackSlash is ever removed and pToFile points to BackSlash before */
1124             if (!b_MultiTo && (b_MultiFrom || (!(b_Multi) && IsAttribDir(ToAttr))))
1125             {
1126                 if ((FO_MOVE == FuncSwitch) && IsAttribDir(ToAttr) && IsAttribDir(wfd.dwFileAttributes))
1127                 {
1128                     if (b_Multi)
1129                     {
1130                         retCode = 0x73; /* !b_Multi = 0x8 ?? */
1131                         goto shfileop_error;
1132                     }
1133                 }
1134                 pToFile = SHFileStrCpyCatW(pTempTo, NULL, wfd.cFileName);
1135                 ToAttr = GetFileAttributesW(pTempTo);
1136             }
1137
1138             if (IsAttribDir(ToAttr))
1139             {
1140                 if (IsAttribFile(wfd.dwFileAttributes))
1141                 {
1142                     retCode = (FO_COPY == FuncSwitch) ? 0x75 : 0xb7;
1143                     goto shfileop_error;
1144                 }
1145             }
1146             else
1147             {
1148                 pToFile[0] = '\0';
1149                 ToPathAttr = GetFileAttributesW(pTempTo);
1150                 pToFile[0] = '\\';
1151                 if (IsAttribFile(ToPathAttr))
1152                 {
1153                     /* error, is this tested ? */
1154                     retCode = 0x777402;
1155                     goto shfileop_error;
1156                 }
1157             }
1158
1159             /* singlesource + no mask */
1160             if (-1 == (ToAttr & ToPathAttr))
1161             {
1162                 /* Target-dir does not exist, and cannot be created */
1163                 retCode=0x75;
1164                 goto shfileop_error;
1165             }
1166
1167             switch(FuncSwitch)
1168             {
1169             case FO_MOVE:
1170                 pToFile = NULL;
1171                 if ((ToAttr == -1) && SHFileStrICmpW(pTempFrom, pTempTo, pFromFile, NULL))
1172                 {
1173                     nFileOp.wFunc =  ((level+1)<<4) + FO_RENAME;
1174                 }
1175                 else
1176                 {
1177                     if (b_SameRoot && IsAttribDir(ToAttr) && IsAttribDir(wfd.dwFileAttributes))
1178                     {
1179                         /* we need pToFile for FO_DELETE after FO_MOVE contence */
1180                         pToFile = SHFileStrCpyCatW(pTempFrom, NULL, wWildcardFile);
1181                     }
1182                     else
1183                     {
1184                         nFileOp.wFunc =  ((level+1)<<4) + FO_COPY;
1185                     }
1186                 }
1187                 retCode = SHFileOperationW(&nFileOp);
1188                 if (pToFile)
1189                     ((DWORD*)pToFile)[0] = '\0';
1190                 if (!nFileOp.fAnyOperationsAborted && (FO_RENAME != (nFileOp.wFunc & 0xf)))
1191                 {
1192                     nFileOp.wFunc =  ((level+1)<<4) + FO_DELETE;
1193                     retCode = SHFileOperationW(&nFileOp);
1194                 }
1195                 continue;
1196             case FO_COPY:
1197                 if (SHFileStrICmpW(pTempFrom, pTempTo, NULL, NULL))
1198                 { /* target is the same as source ? */
1199                     /* we still need the value for the returncode, we assume 0x71 */
1200                     retCode = 0x71;
1201                     goto shfileop_error;
1202                 }
1203                 if (IsAttribDir((ToAttr & wfd.dwFileAttributes)))
1204                 {
1205                     if (IsAttribDir(ToAttr) || !SHCreateDirectoryExW(NULL,pTempTo, NULL))
1206                     {
1207 /* ???            nFileOp.fFlags = (nFileOp.fFlags | FOF_MULTIDESTFILES); */
1208                         SHFileStrCpyCatW(pTempFrom, NULL, wWildcardFile);
1209                         retCode = SHFileOperationW(&nFileOp);
1210                     }
1211                     else
1212                     {
1213                         retCode = 0x750;/* value unknown */
1214                         goto shfileop_error;
1215                     }
1216                 }
1217                 else
1218                 {
1219                     if (!(ask_overwrite && SHELL_ConfirmDialogW(ASK_OVERWRITE_FILE, pTempTo))
1220                         && (not_overwrite))
1221                     {
1222                         /* we still need the value for the returncode, we use the mostly assumed */
1223                         retCode = 0x73;
1224                         goto shfileop_error;
1225                     }
1226                     if (SHNotifyCopyFileW(pTempFrom, pTempTo, nFileOp.fFlags & FOF_RENAMEONCOLLISION) != ERROR_SUCCESS)
1227                     {
1228                         retCode = 0x77; /* value unknown */
1229                         goto shfileop_error;
1230                     }
1231                 }
1232             }
1233         }
1234
1235 shfileop_normal:
1236         if (!(nFileOp.fAnyOperationsAborted))
1237           retCode = 0;
1238 shfileop_error:
1239         if (hFind != INVALID_HANDLE_VALUE)
1240           FindClose(hFind);
1241         hFind = INVALID_HANDLE_VALUE;
1242         if (pTempFrom)
1243           HeapFree(GetProcessHeap(), 0, pTempFrom);
1244         if (retCode)
1245         {
1246           nFileOp.fAnyOperationsAborted = TRUE;
1247         }
1248         TRACE("%s level=%ld AnyOpsAborted=%s ret=0x%lx, with %s %s%s\n",
1249               debug_shfileops_action(FuncSwitch), level,
1250               nFileOp.fAnyOperationsAborted ? "TRUE":"FALSE",
1251               retCode, debugstr_w(pFrom), pTo ? "-> ":"", debugstr_w(pTo));
1252
1253         lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
1254         return retCode;
1255 }
1256
1257 /*************************************************************************
1258  * SHFileOperation        [SHELL32.@]
1259  *
1260  */
1261 DWORD WINAPI SHFileOperationAW(LPVOID lpFileOp)
1262 {
1263         if (SHELL_OsIsUnicode())
1264           return SHFileOperationW(lpFileOp);
1265         return SHFileOperationA(lpFileOp);
1266 }
1267
1268 /*************************************************************************
1269  * SheGetDirW [SHELL32.281]
1270  *
1271  */
1272 HRESULT WINAPI SheGetDirW(LPWSTR u, LPWSTR v)
1273 {       FIXME("%p %p stub\n",u,v);
1274         return 0;
1275 }
1276
1277 /*************************************************************************
1278  * SheChangeDirW [SHELL32.274]
1279  *
1280  */
1281 HRESULT WINAPI SheChangeDirW(LPWSTR u)
1282 {       FIXME("(%s),stub\n",debugstr_w(u));
1283         return 0;
1284 }
1285
1286 /*************************************************************************
1287  * IsNetDrive                   [SHELL32.66]
1288  */
1289 BOOL WINAPI IsNetDrive(DWORD drive)
1290 {
1291         char root[4];
1292         strcpy(root, "A:\\");
1293         root[0] += (char)drive;
1294         return (GetDriveTypeA(root) == DRIVE_REMOTE);
1295 }