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