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