usp10: Fix dead code in ScriptItemize.
[wine] / dlls / shell32 / shlfileop.c
1 /*
2  * SHFileOperation
3  *
4  * Copyright 2000 Juergen Schmied
5  * Copyright 2002 Andriy Palamarchuk
6  * Copyright 2004 Dietrich Teickner (from Odin)
7  * Copyright 2004 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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/debug.h"
44 #include "xdg.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(shell);
47
48 #define IsAttrib(x, y)  ((INVALID_FILE_ATTRIBUTES != (x)) && ((x) & (y)))
49 #define IsAttribFile(x) (!((x) & FILE_ATTRIBUTE_DIRECTORY))
50 #define IsAttribDir(x)  IsAttrib(x, FILE_ATTRIBUTE_DIRECTORY)
51 #define IsDotDir(x)     ((x[0] == '.') && ((x[1] == 0) || ((x[1] == '.') && (x[2] == 0))))
52
53 #define FO_MASK         0xF
54
55 static const WCHAR wWildcardFile[] = {'*',0};
56 static const WCHAR wWildcardChars[] = {'*','?',0};
57
58 static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec);
59 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec);
60 static DWORD SHNotifyRemoveDirectoryA(LPCSTR path);
61 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path);
62 static DWORD SHNotifyDeleteFileA(LPCSTR path);
63 static DWORD SHNotifyDeleteFileW(LPCWSTR path);
64 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest);
65 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists);
66 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly);
67
68 typedef struct
69 {
70         HINSTANCE hIconInstance;
71         UINT icon_resource_id;
72         UINT caption_resource_id, text_resource_id;
73 } SHELL_ConfirmIDstruc;
74
75 static BOOL SHELL_ConfirmIDs(int nKindOfDialog, SHELL_ConfirmIDstruc *ids)
76 {
77         ids->hIconInstance = shell32_hInstance;
78         switch (nKindOfDialog) {
79           case ASK_DELETE_FILE:
80             ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
81             ids->caption_resource_id  = IDS_DELETEITEM_CAPTION;
82             ids->text_resource_id  = IDS_DELETEITEM_TEXT;
83             return TRUE;
84           case ASK_DELETE_FOLDER:
85             ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
86             ids->caption_resource_id  = IDS_DELETEFOLDER_CAPTION;
87             ids->text_resource_id  = IDS_DELETEITEM_TEXT;
88             return TRUE;
89           case ASK_DELETE_MULTIPLE_ITEM:
90             ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
91             ids->caption_resource_id  = IDS_DELETEITEM_CAPTION;
92             ids->text_resource_id  = IDS_DELETEMULTIPLE_TEXT;
93             return TRUE;
94           case ASK_TRASH_FILE:
95             ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
96             ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
97             ids->text_resource_id = IDS_TRASHITEM_TEXT;
98             return TRUE;
99           case ASK_TRASH_FOLDER:
100             ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
101             ids->caption_resource_id = IDS_DELETEFOLDER_CAPTION;
102             ids->text_resource_id = IDS_TRASHFOLDER_TEXT;
103             return TRUE;
104           case ASK_TRASH_MULTIPLE_ITEM:
105             ids->icon_resource_id = IDI_SHELL_TRASH_FILE;
106             ids->caption_resource_id = IDS_DELETEITEM_CAPTION;
107             ids->text_resource_id = IDS_TRASHMULTIPLE_TEXT;
108             return TRUE;
109           case ASK_CANT_TRASH_ITEM:
110             ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
111             ids->caption_resource_id  = IDS_DELETEITEM_CAPTION;
112             ids->text_resource_id  = IDS_CANTTRASH_TEXT;
113             return TRUE;
114           case ASK_DELETE_SELECTED:
115             ids->icon_resource_id = IDI_SHELL_CONFIRM_DELETE;
116             ids->caption_resource_id  = IDS_DELETEITEM_CAPTION;
117             ids->text_resource_id  = IDS_DELETESELECTED_TEXT;
118             return TRUE;
119           case ASK_OVERWRITE_FILE:
120             ids->hIconInstance = NULL;
121             ids->icon_resource_id = IDI_WARNING;
122             ids->caption_resource_id  = IDS_OVERWRITEFILE_CAPTION;
123             ids->text_resource_id  = IDS_OVERWRITEFILE_TEXT;
124             return TRUE;
125           default:
126             FIXME(" Unhandled nKindOfDialog %d stub\n", nKindOfDialog);
127         }
128         return FALSE;
129 }
130
131 BOOL SHELL_ConfirmDialogW(HWND hWnd, int nKindOfDialog, LPCWSTR szDir)
132 {
133         WCHAR szCaption[255], szText[255], szBuffer[MAX_PATH + 256];
134         SHELL_ConfirmIDstruc ids;
135         MSGBOXPARAMSW params;
136         DWORD_PTR args[1];
137
138         if (!SHELL_ConfirmIDs(nKindOfDialog, &ids))
139           return FALSE;
140
141         LoadStringW(shell32_hInstance, ids.caption_resource_id, szCaption, sizeof(szCaption)/sizeof(WCHAR));
142         LoadStringW(shell32_hInstance, ids.text_resource_id, szText, sizeof(szText)/sizeof(WCHAR));
143
144         args[0] = (DWORD_PTR)szDir;
145         FormatMessageW(FORMAT_MESSAGE_FROM_STRING|FORMAT_MESSAGE_ARGUMENT_ARRAY,
146                        szText, 0, 0, szBuffer, sizeof(szBuffer), (va_list*)args);
147
148         ZeroMemory(&params, sizeof(params));
149         params.cbSize = sizeof(MSGBOXPARAMSW);
150         params.hwndOwner = hWnd;
151         params.hInstance = ids.hIconInstance;
152         params.lpszText = szBuffer;
153         params.lpszCaption = szCaption;
154         params.lpszIcon = (LPWSTR)MAKEINTRESOURCE(ids.icon_resource_id);
155         params.dwStyle = MB_YESNO | MB_USERICON;
156
157         return (IDYES == MessageBoxIndirectW(&params));
158 }
159
160 static DWORD SHELL32_AnsiToUnicodeBuf(LPCSTR aPath, LPWSTR *wPath, DWORD minChars)
161 {
162         DWORD len = MultiByteToWideChar(CP_ACP, 0, aPath, -1, NULL, 0);
163
164         if (len < minChars)
165           len = minChars;
166
167         *wPath = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
168         if (*wPath)
169         {
170           MultiByteToWideChar(CP_ACP, 0, aPath, -1, *wPath, len);
171           return NO_ERROR;
172         }
173         return E_OUTOFMEMORY;
174 }
175
176 static void SHELL32_FreeUnicodeBuf(LPWSTR wPath)
177 {
178         HeapFree(GetProcessHeap(), 0, wPath);
179 }
180
181 HRESULT WINAPI SHIsFileAvailableOffline(LPCWSTR path, LPDWORD status)
182 {
183     FIXME("(%s, %p) stub\n", debugstr_w(path), status);
184     return E_FAIL;
185 }
186
187 /**************************************************************************
188  * SHELL_DeleteDirectory()  [internal]
189  *
190  * Asks for confirmation when bShowUI is true and deletes the directory and
191  * all its subdirectories and files if necessary.
192  */
193 BOOL SHELL_DeleteDirectoryW(HWND hwnd, LPCWSTR pszDir, BOOL bShowUI)
194 {
195         BOOL    ret = TRUE;
196         HANDLE  hFind;
197         WIN32_FIND_DATAW wfd;
198         WCHAR   szTemp[MAX_PATH];
199
200         /* Make sure the directory exists before eventually prompting the user */
201         PathCombineW(szTemp, pszDir, wWildcardFile);
202         hFind = FindFirstFileW(szTemp, &wfd);
203         if (hFind == INVALID_HANDLE_VALUE)
204           return FALSE;
205
206         if (!bShowUI || (ret = SHELL_ConfirmDialogW(hwnd, ASK_DELETE_FOLDER, pszDir)))
207         {
208           do
209           {
210             LPWSTR lp = wfd.cAlternateFileName;
211             if (!lp[0])
212               lp = wfd.cFileName;
213             if (IsDotDir(lp))
214               continue;
215             PathCombineW(szTemp, pszDir, lp);
216             if (FILE_ATTRIBUTE_DIRECTORY & wfd.dwFileAttributes)
217               ret = SHELL_DeleteDirectoryW(hwnd, szTemp, FALSE);
218             else
219               ret = (SHNotifyDeleteFileW(szTemp) == ERROR_SUCCESS);
220           } while (ret && FindNextFileW(hFind, &wfd));
221         }
222         FindClose(hFind);
223         if (ret)
224           ret = (SHNotifyRemoveDirectoryW(pszDir) == ERROR_SUCCESS);
225         return ret;
226 }
227
228 /**************************************************************************
229  * Win32CreateDirectory      [SHELL32.93]
230  *
231  * Creates a directory. Also triggers a change notify if one exists.
232  *
233  * PARAMS
234  *  path       [I]   path to directory to create
235  *
236  * RETURNS
237  *  TRUE if successful, FALSE otherwise
238  *
239  * NOTES
240  *  Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
241  *  This is Unicode on NT/2000
242  */
243 static DWORD SHNotifyCreateDirectoryA(LPCSTR path, LPSECURITY_ATTRIBUTES sec)
244 {
245         LPWSTR wPath;
246         DWORD retCode;
247
248         TRACE("(%s, %p)\n", debugstr_a(path), sec);
249
250         retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
251         if (!retCode)
252         {
253           retCode = SHNotifyCreateDirectoryW(wPath, sec);
254           SHELL32_FreeUnicodeBuf(wPath);
255         }
256         return retCode;
257 }
258
259 /**********************************************************************/
260
261 static DWORD SHNotifyCreateDirectoryW(LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
262 {
263         TRACE("(%s, %p)\n", debugstr_w(path), sec);
264
265         if (CreateDirectoryW(path, sec))
266         {
267           SHChangeNotify(SHCNE_MKDIR, SHCNF_PATHW, path, NULL);
268           return ERROR_SUCCESS;
269         }
270         return GetLastError();
271 }
272
273 /**********************************************************************/
274
275 BOOL WINAPI Win32CreateDirectoryAW(LPCVOID path, LPSECURITY_ATTRIBUTES sec)
276 {
277         if (SHELL_OsIsUnicode())
278           return (SHNotifyCreateDirectoryW(path, sec) == ERROR_SUCCESS);
279         return (SHNotifyCreateDirectoryA(path, sec) == ERROR_SUCCESS);
280 }
281
282 /************************************************************************
283  * Win32RemoveDirectory      [SHELL32.94]
284  *
285  * Deletes a directory. Also triggers a change notify if one exists.
286  *
287  * PARAMS
288  *  path       [I]   path to directory to delete
289  *
290  * RETURNS
291  *  TRUE if successful, FALSE otherwise
292  *
293  * NOTES
294  *  Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
295  *  This is Unicode on NT/2000
296  */
297 static DWORD SHNotifyRemoveDirectoryA(LPCSTR path)
298 {
299         LPWSTR wPath;
300         DWORD retCode;
301
302         TRACE("(%s)\n", debugstr_a(path));
303
304         retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
305         if (!retCode)
306         {
307           retCode = SHNotifyRemoveDirectoryW(wPath);
308           SHELL32_FreeUnicodeBuf(wPath);
309         }
310         return retCode;
311 }
312
313 /***********************************************************************/
314
315 static DWORD SHNotifyRemoveDirectoryW(LPCWSTR path)
316 {
317         BOOL ret;
318         TRACE("(%s)\n", debugstr_w(path));
319
320         ret = RemoveDirectoryW(path);
321         if (!ret)
322         {
323           /* Directory may be write protected */
324           DWORD dwAttr = GetFileAttributesW(path);
325           if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY))
326             if (SetFileAttributesW(path, dwAttr & ~FILE_ATTRIBUTE_READONLY))
327               ret = RemoveDirectoryW(path);
328         }
329         if (ret)
330         {
331           SHChangeNotify(SHCNE_RMDIR, SHCNF_PATHW, path, NULL);
332           return ERROR_SUCCESS;
333         }
334         return GetLastError();
335 }
336
337 /***********************************************************************/
338
339 BOOL WINAPI Win32RemoveDirectoryAW(LPCVOID path)
340 {
341         if (SHELL_OsIsUnicode())
342           return (SHNotifyRemoveDirectoryW(path) == ERROR_SUCCESS);
343         return (SHNotifyRemoveDirectoryA(path) == ERROR_SUCCESS);
344 }
345
346 /************************************************************************
347  * Win32DeleteFile           [SHELL32.164]
348  *
349  * Deletes a file. Also triggers a change notify if one exists.
350  *
351  * PARAMS
352  *  path       [I]   path to file to delete
353  *
354  * RETURNS
355  *  TRUE if successful, FALSE otherwise
356  *
357  * NOTES
358  *  Verified on Win98 / IE 5 (SHELL32 4.72, March 1999 build) to be ANSI.
359  *  This is Unicode on NT/2000
360  */
361 static DWORD SHNotifyDeleteFileA(LPCSTR path)
362 {
363         LPWSTR wPath;
364         DWORD retCode;
365
366         TRACE("(%s)\n", debugstr_a(path));
367
368         retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
369         if (!retCode)
370         {
371           retCode = SHNotifyDeleteFileW(wPath);
372           SHELL32_FreeUnicodeBuf(wPath);
373         }
374         return retCode;
375 }
376
377 /***********************************************************************/
378
379 static DWORD SHNotifyDeleteFileW(LPCWSTR path)
380 {
381         BOOL ret;
382
383         TRACE("(%s)\n", debugstr_w(path));
384
385         ret = DeleteFileW(path);
386         if (!ret)
387         {
388           /* File may be write protected or a system file */
389           DWORD dwAttr = GetFileAttributesW(path);
390           if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
391             if (SetFileAttributesW(path, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
392               ret = DeleteFileW(path);
393         }
394         if (ret)
395         {
396           SHChangeNotify(SHCNE_DELETE, SHCNF_PATHW, path, NULL);
397           return ERROR_SUCCESS;
398         }
399         return GetLastError();
400 }
401
402 /***********************************************************************/
403
404 DWORD WINAPI Win32DeleteFileAW(LPCVOID path)
405 {
406         if (SHELL_OsIsUnicode())
407           return (SHNotifyDeleteFileW(path) == ERROR_SUCCESS);
408         return (SHNotifyDeleteFileA(path) == ERROR_SUCCESS);
409 }
410
411 /************************************************************************
412  * SHNotifyMoveFile          [internal]
413  *
414  * Moves a file. Also triggers a change notify if one exists.
415  *
416  * PARAMS
417  *  src        [I]   path to source file to move
418  *  dest       [I]   path to target file to move to
419  *
420  * RETURNS
421  *  ERORR_SUCCESS if successful
422  */
423 static DWORD SHNotifyMoveFileW(LPCWSTR src, LPCWSTR dest)
424 {
425         BOOL ret;
426
427         TRACE("(%s %s)\n", debugstr_w(src), debugstr_w(dest));
428
429         ret = MoveFileExW(src, dest, MOVEFILE_REPLACE_EXISTING);
430
431         /* MOVEFILE_REPLACE_EXISTING fails with dirs, so try MoveFile */
432         if (!ret)
433             ret = MoveFileW(src, dest);
434
435         if (!ret)
436         {
437           DWORD dwAttr;
438
439           dwAttr = SHFindAttrW(dest, FALSE);
440           if (INVALID_FILE_ATTRIBUTES == dwAttr)
441           {
442             /* Source file may be write protected or a system file */
443             dwAttr = GetFileAttributesW(src);
444             if (IsAttrib(dwAttr, FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM))
445               if (SetFileAttributesW(src, dwAttr & ~(FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_SYSTEM)))
446                 ret = MoveFileW(src, dest);
447           }
448         }
449         if (ret)
450         {
451           SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_PATHW, src, dest);
452           return ERROR_SUCCESS;
453         }
454         return GetLastError();
455 }
456
457 /************************************************************************
458  * SHNotifyCopyFile          [internal]
459  *
460  * Copies a file. Also triggers a change notify if one exists.
461  *
462  * PARAMS
463  *  src           [I]   path to source file to move
464  *  dest          [I]   path to target file to move to
465  *  bFailIfExists [I]   if TRUE, the target file will not be overwritten if
466  *                      a file with this name already exists
467  *
468  * RETURNS
469  *  ERROR_SUCCESS if successful
470  */
471 static DWORD SHNotifyCopyFileW(LPCWSTR src, LPCWSTR dest, BOOL bFailIfExists)
472 {
473         BOOL ret;
474
475         TRACE("(%s %s %s)\n", debugstr_w(src), debugstr_w(dest), bFailIfExists ? "failIfExists" : "");
476
477         ret = CopyFileW(src, dest, bFailIfExists);
478         if (ret)
479         {
480           SHChangeNotify(SHCNE_CREATE, SHCNF_PATHW, dest, NULL);
481           return ERROR_SUCCESS;
482         }
483
484         return GetLastError();
485 }
486
487 /*************************************************************************
488  * SHCreateDirectory         [SHELL32.165]
489  *
490  * This function creates a file system folder whose fully qualified path is
491  * given by path. If one or more of the intermediate folders do not exist,
492  * they will be created as well.
493  *
494  * PARAMS
495  *  hWnd       [I]
496  *  path       [I]   path of directory to create
497  *
498  * RETURNS
499  *  ERRROR_SUCCESS or one of the following values:
500  *  ERROR_BAD_PATHNAME if the path is relative
501  *  ERROR_FILE_EXISTS when a file with that name exists
502  *  ERROR_PATH_NOT_FOUND can't find the path, probably invalid
503  *  ERROR_INVLID_NAME if the path contains invalid chars
504  *  ERROR_ALREADY_EXISTS when the directory already exists
505  *  ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
506  *
507  * NOTES
508  *  exported by ordinal
509  *  Win9x exports ANSI
510  *  WinNT/2000 exports Unicode
511  */
512 DWORD WINAPI SHCreateDirectory(HWND hWnd, LPCVOID path)
513 {
514         if (SHELL_OsIsUnicode())
515           return SHCreateDirectoryExW(hWnd, path, NULL);
516         return SHCreateDirectoryExA(hWnd, path, NULL);
517 }
518
519 /*************************************************************************
520  * SHCreateDirectoryExA      [SHELL32.@]
521  *
522  * This function creates a file system folder whose fully qualified path is
523  * given by path. If one or more of the intermediate folders do not exist,
524  * they will be created as well.
525  *
526  * PARAMS
527  *  hWnd       [I]
528  *  path       [I]   path of directory to create
529  *  sec        [I]   security attributes to use or NULL
530  *
531  * RETURNS
532  *  ERRROR_SUCCESS or one of the following values:
533  *  ERROR_BAD_PATHNAME or ERROR_PATH_NOT_FOUND if the path is relative
534  *  ERROR_INVLID_NAME if the path contains invalid chars
535  *  ERROR_FILE_EXISTS when a file with that name exists
536  *  ERROR_ALREADY_EXISTS when the directory already exists
537  *  ERROR_FILENAME_EXCED_RANGE if the filename was to long to process
538  *
539  *  FIXME: Not implemented yet;
540  *  SHCreateDirectoryEx also verifies that the files in the directory will be visible
541  *  if the path is a network path to deal with network drivers which might have a limited
542  *  but unknown maximum path length. If not:
543  *
544  *  If hWnd is set to a valid window handle, a message box is displayed warning
545  *  the user that the files may not be accessible. If the user chooses not to
546  *  proceed, the function returns ERROR_CANCELLED.
547  *
548  *  If hWnd is set to NULL, no user interface is displayed and the function
549  *  returns ERROR_CANCELLED.
550  */
551 int WINAPI SHCreateDirectoryExA(HWND hWnd, LPCSTR path, LPSECURITY_ATTRIBUTES sec)
552 {
553         LPWSTR wPath;
554         DWORD retCode;
555
556         TRACE("(%s, %p)\n", debugstr_a(path), sec);
557
558         retCode = SHELL32_AnsiToUnicodeBuf(path, &wPath, 0);
559         if (!retCode)
560         {
561           retCode = SHCreateDirectoryExW(hWnd, wPath, sec);
562           SHELL32_FreeUnicodeBuf(wPath);
563         }
564         return retCode;
565 }
566
567 /*************************************************************************
568  * SHCreateDirectoryExW      [SHELL32.@]
569  *
570  * See SHCreateDirectoryExA.
571  */
572 int WINAPI SHCreateDirectoryExW(HWND hWnd, LPCWSTR path, LPSECURITY_ATTRIBUTES sec)
573 {
574         int ret = ERROR_BAD_PATHNAME;
575         TRACE("(%p, %s, %p)\n", hWnd, debugstr_w(path), sec);
576
577         if (PathIsRelativeW(path))
578         {
579           SetLastError(ret);
580         }
581         else
582         {
583           ret = SHNotifyCreateDirectoryW(path, sec);
584           /* Refuse to work on certain error codes before trying to create directories recursively */
585           if (ret != ERROR_SUCCESS &&
586               ret != ERROR_FILE_EXISTS &&
587               ret != ERROR_ALREADY_EXISTS &&
588               ret != ERROR_FILENAME_EXCED_RANGE)
589           {
590             WCHAR *pEnd, *pSlash, szTemp[MAX_PATH + 1];  /* extra for PathAddBackslash() */
591
592             lstrcpynW(szTemp, path, MAX_PATH);
593             pEnd = PathAddBackslashW(szTemp);
594             pSlash = szTemp + 3;
595
596             while (*pSlash)
597             {
598               while (*pSlash && *pSlash != '\\')
599                 pSlash = CharNextW(pSlash);
600
601               if (*pSlash)
602               {
603                 *pSlash = 0;    /* terminate path at separator */
604
605                 ret = SHNotifyCreateDirectoryW(szTemp, pSlash + 1 == pEnd ? sec : NULL);
606               }
607               *pSlash++ = '\\'; /* put the separator back */
608             }
609           }
610
611           if (ret && hWnd && (ERROR_CANCELLED != ret))
612           {
613             /* We failed and should show a dialog box */
614             FIXME("Show system error message, creating path %s, failed with error %d\n", debugstr_w(path), ret);
615             ret = ERROR_CANCELLED; /* Error has been already presented to user (not really yet!) */
616           }
617         }
618         return ret;
619 }
620
621 /*************************************************************************
622  * SHFindAttrW      [internal]
623  *
624  * Get the Attributes for a file or directory. The difference to GetAttributes()
625  * is that this function will also work for paths containing wildcard characters
626  * in its filename.
627
628  * PARAMS
629  *  path       [I]   path of directory or file to check
630  *  fileOnly   [I]   TRUE if only files should be found
631  *
632  * RETURNS
633  *  INVALID_FILE_ATTRIBUTES if the path does not exist, the actual attributes of
634  *  the first file or directory found otherwise
635  */
636 static DWORD SHFindAttrW(LPCWSTR pName, BOOL fileOnly)
637 {
638         WIN32_FIND_DATAW wfd;
639         BOOL b_FileMask = fileOnly && (NULL != StrPBrkW(pName, wWildcardChars));
640         DWORD dwAttr = INVALID_FILE_ATTRIBUTES;
641         HANDLE hFind = FindFirstFileW(pName, &wfd);
642
643         TRACE("%s %d\n", debugstr_w(pName), fileOnly);
644         if (INVALID_HANDLE_VALUE != hFind)
645         {
646           do
647           {
648             if (b_FileMask && IsAttribDir(wfd.dwFileAttributes))
649                continue;
650             dwAttr = wfd.dwFileAttributes;
651             break;
652           }
653           while (FindNextFileW(hFind, &wfd));
654           FindClose(hFind);
655         }
656         return dwAttr;
657 }
658
659 /*************************************************************************
660  *
661  * SHNameTranslate HelperFunction for SHFileOperationA
662  *
663  * Translates a list of 0 terminated ASCII strings into Unicode. If *wString
664  * is NULL, only the necessary size of the string is determined and returned,
665  * otherwise the ASCII strings are copied into it and the buffer is increased
666  * to point to the location after the final 0 termination char.
667  */
668 DWORD SHNameTranslate(LPWSTR* wString, LPCWSTR* pWToFrom, BOOL more)
669 {
670         DWORD size = 0, aSize = 0;
671         LPCSTR aString = (LPCSTR)*pWToFrom;
672
673         if (aString)
674         {
675           do
676           {
677             size = lstrlenA(aString) + 1;
678             aSize += size;
679             aString += size;
680           } while ((size != 1) && more);
681           /* The two sizes might be different in the case of multibyte chars */
682           size = MultiByteToWideChar(CP_ACP, 0, aString, aSize, *wString, 0);
683           if (*wString) /* only in the second loop */
684           {
685             MultiByteToWideChar(CP_ACP, 0, (LPCSTR)*pWToFrom, aSize, *wString, size);
686             *pWToFrom = *wString;
687             *wString += size;
688           }
689         }
690         return size;
691 }
692 /*************************************************************************
693  * SHFileOperationA          [SHELL32.@]
694  *
695  * Function to copy, move, delete and create one or more files with optional
696  * user prompts.
697  *
698  * PARAMS
699  *  lpFileOp   [I/O] pointer to a structure containing all the necessary information
700  *
701  * RETURNS
702  *  Success: ERROR_SUCCESS.
703  *  Failure: ERROR_CANCELLED.
704  *
705  * NOTES
706  *  exported by name
707  */
708 int WINAPI SHFileOperationA(LPSHFILEOPSTRUCTA lpFileOp)
709 {
710         SHFILEOPSTRUCTW nFileOp = *((LPSHFILEOPSTRUCTW)lpFileOp);
711         int retCode = 0;
712         DWORD size;
713         LPWSTR ForFree = NULL, /* we change wString in SHNameTranslate and can't use it for freeing */
714                wString = NULL; /* we change this in SHNameTranslate */
715
716         TRACE("\n");
717         if (FO_DELETE == (nFileOp.wFunc & FO_MASK))
718           nFileOp.pTo = NULL; /* we need a NULL or a valid pointer for translation */
719         if (!(nFileOp.fFlags & FOF_SIMPLEPROGRESS))
720           nFileOp.lpszProgressTitle = NULL; /* we need a NULL or a valid pointer for translation */
721         while (1) /* every loop calculate size, second translate also, if we have storage for this */
722         {
723           size = SHNameTranslate(&wString, &nFileOp.lpszProgressTitle, FALSE); /* no loop */
724           size += SHNameTranslate(&wString, &nFileOp.pFrom, TRUE); /* internal loop */
725           size += SHNameTranslate(&wString, &nFileOp.pTo, TRUE); /* internal loop */
726
727           if (ForFree)
728           {
729             retCode = SHFileOperationW(&nFileOp);
730             HeapFree(GetProcessHeap(), 0, ForFree); /* we cannot use wString, it was changed */
731             break;
732           }
733           else
734           {
735             wString = ForFree = HeapAlloc(GetProcessHeap(), 0, size * sizeof(WCHAR));
736             if (ForFree) continue;
737             retCode = ERROR_OUTOFMEMORY;
738             nFileOp.fAnyOperationsAborted = TRUE;
739             SetLastError(retCode);
740             return retCode;
741           }
742         }
743
744         lpFileOp->hNameMappings = nFileOp.hNameMappings;
745         lpFileOp->fAnyOperationsAborted = nFileOp.fAnyOperationsAborted;
746         return retCode;
747 }
748
749 #define ERROR_SHELL_INTERNAL_FILE_NOT_FOUND 1026
750
751 typedef struct
752 {
753     DWORD attributes;
754     LPWSTR szDirectory;
755     LPWSTR szFilename;
756     LPWSTR szFullPath;
757     BOOL bFromWildcard;
758     BOOL bFromRelative;
759     BOOL bExists;
760 } FILE_ENTRY;
761
762 typedef struct
763 {
764     FILE_ENTRY *feFiles;
765     DWORD num_alloc;
766     DWORD dwNumFiles;
767     BOOL bAnyFromWildcard;
768     BOOL bAnyDirectories;
769     BOOL bAnyDontExist;
770 } FILE_LIST;
771
772
773 static inline void grow_list(FILE_LIST *list)
774 {
775     FILE_ENTRY *new = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, list->feFiles,
776                                   list->num_alloc * 2 * sizeof(*new) );
777     list->feFiles = new;
778     list->num_alloc *= 2;
779 }
780
781 /* adds a file to the FILE_ENTRY struct
782  */
783 static void add_file_to_entry(FILE_ENTRY *feFile, LPWSTR szFile)
784 {
785     DWORD dwLen = lstrlenW(szFile) + 1;
786     LPWSTR ptr;
787
788     feFile->szFullPath = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
789     lstrcpyW(feFile->szFullPath, szFile);
790
791     ptr = StrRChrW(szFile, NULL, '\\');
792     if (ptr)
793     {
794         dwLen = ptr - szFile + 1;
795         feFile->szDirectory = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
796         lstrcpynW(feFile->szDirectory, szFile, dwLen);
797
798         dwLen = lstrlenW(feFile->szFullPath) - dwLen + 1;
799         feFile->szFilename = HeapAlloc(GetProcessHeap(), 0, dwLen * sizeof(WCHAR));
800         lstrcpyW(feFile->szFilename, ptr + 1); /* skip over backslash */
801     }
802     feFile->bFromWildcard = FALSE;
803 }
804
805 static LPWSTR wildcard_to_file(LPWSTR szWildCard, LPWSTR szFileName)
806 {
807     LPWSTR szFullPath, ptr;
808     DWORD dwDirLen, dwFullLen;
809
810     ptr = StrRChrW(szWildCard, NULL, '\\');
811     dwDirLen = ptr - szWildCard + 1;
812
813     dwFullLen = dwDirLen + lstrlenW(szFileName) + 1;
814     szFullPath = HeapAlloc(GetProcessHeap(), 0, dwFullLen * sizeof(WCHAR));
815
816     lstrcpynW(szFullPath, szWildCard, dwDirLen + 1);
817     lstrcatW(szFullPath, szFileName);
818
819     return szFullPath;
820 }
821
822 static void parse_wildcard_files(FILE_LIST *flList, LPWSTR szFile, LPDWORD pdwListIndex)
823 {
824     WIN32_FIND_DATAW wfd;
825     HANDLE hFile = FindFirstFileW(szFile, &wfd);
826     FILE_ENTRY *file;
827     LPWSTR szFullPath;
828     BOOL res;
829
830     for (res = TRUE; res; res = FindNextFileW(hFile, &wfd))
831     {
832         if (IsDotDir(wfd.cFileName)) continue;
833         if (*pdwListIndex >= flList->num_alloc) grow_list( flList );
834         szFullPath = wildcard_to_file(szFile, wfd.cFileName);
835         file = &flList->feFiles[(*pdwListIndex)++];
836         add_file_to_entry(file, szFullPath);
837         file->bFromWildcard = TRUE;
838         file->attributes = wfd.dwFileAttributes;
839         if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
840         HeapFree(GetProcessHeap(), 0, szFullPath);
841     }
842
843     FindClose(hFile);
844 }
845
846 /* takes the null-separated file list and fills out the FILE_LIST */
847 static HRESULT parse_file_list(FILE_LIST *flList, LPCWSTR szFiles)
848 {
849     LPCWSTR ptr = szFiles;
850     WCHAR szCurFile[MAX_PATH];
851     DWORD i = 0, dwDirLen;
852
853     if (!szFiles)
854         return ERROR_INVALID_PARAMETER;
855
856     flList->bAnyFromWildcard = FALSE;
857     flList->bAnyDirectories = FALSE;
858     flList->bAnyDontExist = FALSE;
859     flList->num_alloc = 32;
860     flList->dwNumFiles = 0;
861
862     /* empty list */
863     if (!szFiles[0])
864         return ERROR_ACCESS_DENIED;
865         
866     flList->feFiles = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
867                                 flList->num_alloc * sizeof(FILE_ENTRY));
868
869     while (*ptr)
870     {
871         if (i >= flList->num_alloc) grow_list( flList );
872
873         /* change relative to absolute path */
874         if (PathIsRelativeW(ptr))
875         {
876             dwDirLen = GetCurrentDirectoryW(MAX_PATH, szCurFile) + 1;
877             PathCombineW(szCurFile, szCurFile, ptr);
878             flList->feFiles[i].bFromRelative = TRUE;
879         }
880         else
881         {
882             lstrcpyW(szCurFile, ptr);
883             flList->feFiles[i].bFromRelative = FALSE;
884         }
885
886         /* parse wildcard files if they are in the filename */
887         if (StrPBrkW(szCurFile, wWildcardChars))
888         {
889             parse_wildcard_files(flList, szCurFile, &i);
890             flList->bAnyFromWildcard = TRUE;
891             i--;
892         }
893         else
894         {
895             FILE_ENTRY *file = &flList->feFiles[i];
896             add_file_to_entry(file, szCurFile);
897             file->attributes = GetFileAttributesW( file->szFullPath );
898             file->bExists = (file->attributes != INVALID_FILE_ATTRIBUTES);
899             if (!file->bExists) flList->bAnyDontExist = TRUE;
900             if (IsAttribDir(file->attributes)) flList->bAnyDirectories = TRUE;
901         }
902
903         /* advance to the next string */
904         ptr += lstrlenW(ptr) + 1;
905         i++;
906     }
907     flList->dwNumFiles = i;
908
909     return S_OK;
910 }
911
912 /* free the FILE_LIST */
913 static void destroy_file_list(FILE_LIST *flList)
914 {
915     DWORD i;
916
917     if (!flList || !flList->feFiles)
918         return;
919
920     for (i = 0; i < flList->dwNumFiles; i++)
921     {
922         HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szDirectory);
923         HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFilename);
924         HeapFree(GetProcessHeap(), 0, flList->feFiles[i].szFullPath);
925     }
926
927     HeapFree(GetProcessHeap(), 0, flList->feFiles);
928 }
929
930 static void copy_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, LPWSTR szDestPath)
931 {
932     WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
933     SHFILEOPSTRUCTW fileOp;
934
935     static const WCHAR wildCardFiles[] = {'*','.','*',0};
936
937     if (IsDotDir(feFrom->szFilename))
938         return;
939
940     if (PathFileExistsW(szDestPath))
941         PathCombineW(szTo, szDestPath, feFrom->szFilename);
942     else
943         lstrcpyW(szTo, szDestPath);
944
945     szTo[lstrlenW(szTo) + 1] = '\0';
946     SHNotifyCreateDirectoryW(szTo, NULL);
947
948     PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
949     szFrom[lstrlenW(szFrom) + 1] = '\0';
950
951     memcpy(&fileOp, lpFileOp, sizeof(fileOp));
952     fileOp.pFrom = szFrom;
953     fileOp.pTo = szTo;
954     fileOp.fFlags &= ~FOF_MULTIDESTFILES; /* we know we're copying to one dir */
955
956     SHFileOperationW(&fileOp);
957 }
958
959 /* copy a file or directory to another directory */
960 static void copy_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, FILE_ENTRY *feTo)
961 {
962     if (!PathFileExistsW(feTo->szFullPath))
963         SHNotifyCreateDirectoryW(feTo->szFullPath, NULL);
964
965     if (IsAttribFile(feFrom->attributes))
966     {
967         WCHAR szDestPath[MAX_PATH];
968
969         PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
970         SHNotifyCopyFileW(feFrom->szFullPath, szDestPath, FALSE);
971     }
972     else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
973         copy_dir_to_dir(lpFileOp, feFrom, feTo->szFullPath);
974 }
975
976 static void create_dest_dirs(LPWSTR szDestDir)
977 {
978     WCHAR dir[MAX_PATH];
979     LPWSTR ptr = StrChrW(szDestDir, '\\');
980
981     /* make sure all directories up to last one are created */
982     while (ptr && (ptr = StrChrW(ptr + 1, '\\')))
983     {
984         lstrcpynW(dir, szDestDir, ptr - szDestDir + 1);
985
986         if (!PathFileExistsW(dir))
987             SHNotifyCreateDirectoryW(dir, NULL);
988     }
989
990     /* create last directory */
991     if (!PathFileExistsW(szDestDir))
992         SHNotifyCreateDirectoryW(szDestDir, NULL);
993 }
994
995 /* the FO_COPY operation */
996 static HRESULT copy_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
997 {
998     DWORD i;
999     FILE_ENTRY *entryToCopy;
1000     FILE_ENTRY *fileDest = &flTo->feFiles[0];
1001     BOOL bCancelIfAnyDirectories = FALSE;
1002
1003     if (flFrom->bAnyDontExist)
1004         return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1005
1006     if (lpFileOp->fFlags & FOF_MULTIDESTFILES && flFrom->bAnyFromWildcard)
1007         return ERROR_CANCELLED;
1008
1009     if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) && flTo->dwNumFiles != 1)
1010         return ERROR_CANCELLED;
1011
1012     if (lpFileOp->fFlags & FOF_MULTIDESTFILES && flFrom->dwNumFiles != 1 &&
1013         flFrom->dwNumFiles != flTo->dwNumFiles)
1014     {
1015         return ERROR_CANCELLED;
1016     }
1017
1018     if (flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1 &&
1019         !PathFileExistsW(flTo->feFiles[0].szFullPath) &&
1020         IsAttribFile(fileDest->attributes))
1021     {
1022         bCancelIfAnyDirectories = TRUE;
1023     }
1024
1025     if (flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1 && fileDest->bFromRelative &&
1026         !PathFileExistsW(fileDest->szFullPath))
1027     {
1028         lpFileOp->fAnyOperationsAborted = TRUE;
1029         return ERROR_CANCELLED;
1030     }
1031
1032     if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) && flFrom->dwNumFiles != 1 &&
1033         PathFileExistsW(fileDest->szFullPath) &&
1034         IsAttribFile(fileDest->attributes))
1035     {
1036         return ERROR_CANCELLED;
1037     }
1038
1039     for (i = 0; i < flFrom->dwNumFiles; i++)
1040     {
1041         entryToCopy = &flFrom->feFiles[i];
1042
1043         if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1044             fileDest = &flTo->feFiles[i];
1045
1046         if (IsAttribDir(entryToCopy->attributes) &&
1047             !lstrcmpiW(entryToCopy->szFullPath, fileDest->szDirectory))
1048         {
1049             return ERROR_SUCCESS;
1050         }
1051
1052         if (IsAttribDir(entryToCopy->attributes) && bCancelIfAnyDirectories)
1053             return ERROR_CANCELLED;
1054
1055         create_dest_dirs(fileDest->szDirectory);
1056
1057         if (!lstrcmpiW(entryToCopy->szFullPath, fileDest->szFullPath))
1058         {
1059             if (IsAttribFile(entryToCopy->attributes))
1060                 return ERROR_NO_MORE_SEARCH_HANDLES;
1061             else
1062                 return ERROR_SUCCESS;
1063         }
1064
1065         if ((flFrom->dwNumFiles > 1 && flTo->dwNumFiles == 1) ||
1066             (flFrom->dwNumFiles == 1 && IsAttribDir(fileDest->attributes)))
1067         {
1068             copy_to_dir(lpFileOp, entryToCopy, fileDest);
1069         }
1070         else if (IsAttribDir(entryToCopy->attributes))
1071         {
1072             copy_dir_to_dir(lpFileOp, entryToCopy, fileDest->szFullPath);
1073         }
1074         else
1075         {
1076             if (SHNotifyCopyFileW(entryToCopy->szFullPath, fileDest->szFullPath, TRUE))
1077             {
1078                 lpFileOp->fAnyOperationsAborted = TRUE;
1079                 return ERROR_CANCELLED;
1080             }
1081         }
1082     }
1083
1084     return ERROR_SUCCESS;
1085 }
1086
1087 static BOOL confirm_delete_list(HWND hWnd, DWORD fFlags, BOOL fTrash, FILE_LIST *flFrom)
1088 {
1089     if (flFrom->dwNumFiles > 1)
1090     {
1091         WCHAR tmp[8];
1092         const WCHAR format[] = {'%','d',0};
1093
1094         wnsprintfW(tmp, sizeof(tmp)/sizeof(tmp[0]), format, flFrom->dwNumFiles);
1095         return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_MULTIPLE_ITEM:ASK_DELETE_MULTIPLE_ITEM), tmp);
1096     }
1097     else
1098     {
1099         FILE_ENTRY *fileEntry = &flFrom->feFiles[0];
1100
1101         if (IsAttribFile(fileEntry->attributes))
1102             return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FILE:ASK_DELETE_FILE), fileEntry->szFullPath);
1103         else if (!(fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1104             return SHELL_ConfirmDialogW(hWnd, (fTrash?ASK_TRASH_FOLDER:ASK_DELETE_FOLDER), fileEntry->szFullPath);
1105     }
1106     return TRUE;
1107 }
1108
1109 /* the FO_DELETE operation */
1110 static HRESULT delete_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom)
1111 {
1112     FILE_ENTRY *fileEntry;
1113     DWORD i;
1114     BOOL bPathExists;
1115     BOOL bTrash;
1116
1117     if (!flFrom->dwNumFiles)
1118         return ERROR_SUCCESS;
1119
1120     /* Windows also checks only the first item */
1121     bTrash = (lpFileOp->fFlags & FOF_ALLOWUNDO)
1122         && TRASH_CanTrashFile(flFrom->feFiles[0].szFullPath);
1123
1124     if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (!bTrash && lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1125         if (!confirm_delete_list(lpFileOp->hwnd, lpFileOp->fFlags, bTrash, flFrom))
1126         {
1127             lpFileOp->fAnyOperationsAborted = TRUE;
1128             return 0;
1129         }
1130
1131     for (i = 0; i < flFrom->dwNumFiles; i++)
1132     {
1133         bPathExists = TRUE;
1134         fileEntry = &flFrom->feFiles[i];
1135
1136         if (!IsAttribFile(fileEntry->attributes) &&
1137             (lpFileOp->fFlags & FOF_FILESONLY && fileEntry->bFromWildcard))
1138             continue;
1139
1140         if (bTrash)
1141         {
1142             BOOL bDelete;
1143             if (TRASH_TrashFile(fileEntry->szFullPath))
1144                 continue;
1145
1146             /* Note: Windows silently deletes the file in such a situation, we show a dialog */
1147             if (!(lpFileOp->fFlags & FOF_NOCONFIRMATION) || (lpFileOp->fFlags & FOF_WANTNUKEWARNING))
1148                 bDelete = SHELL_ConfirmDialogW(lpFileOp->hwnd, ASK_CANT_TRASH_ITEM, fileEntry->szFullPath);
1149             else
1150                 bDelete = TRUE;
1151
1152             if (!bDelete)
1153             {
1154                 lpFileOp->fAnyOperationsAborted = TRUE;
1155                 break;
1156             }
1157         }
1158         
1159         /* delete the file or directory */
1160         if (IsAttribFile(fileEntry->attributes))
1161             bPathExists = DeleteFileW(fileEntry->szFullPath);
1162         else
1163             bPathExists = SHELL_DeleteDirectoryW(lpFileOp->hwnd, fileEntry->szFullPath, FALSE);
1164
1165         if (!bPathExists)
1166             return ERROR_PATH_NOT_FOUND;
1167     }
1168
1169     return ERROR_SUCCESS;
1170 }
1171
1172 static void move_dir_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, LPWSTR szDestPath)
1173 {
1174     WCHAR szFrom[MAX_PATH], szTo[MAX_PATH];
1175     SHFILEOPSTRUCTW fileOp;
1176
1177     static const WCHAR wildCardFiles[] = {'*','.','*',0};
1178
1179     if (IsDotDir(feFrom->szFilename))
1180         return;
1181
1182     SHNotifyCreateDirectoryW(szDestPath, NULL);
1183
1184     PathCombineW(szFrom, feFrom->szFullPath, wildCardFiles);
1185     szFrom[lstrlenW(szFrom) + 1] = '\0';
1186
1187     lstrcpyW(szTo, szDestPath);
1188     szTo[lstrlenW(szDestPath) + 1] = '\0';
1189
1190     memcpy(&fileOp, lpFileOp, sizeof(fileOp));
1191     fileOp.pFrom = szFrom;
1192     fileOp.pTo = szTo;
1193
1194     SHFileOperationW(&fileOp);
1195 }
1196
1197 /* moves a file or directory to another directory */
1198 static void move_to_dir(LPSHFILEOPSTRUCTW lpFileOp, FILE_ENTRY *feFrom, FILE_ENTRY *feTo)
1199 {
1200     WCHAR szDestPath[MAX_PATH];
1201
1202     PathCombineW(szDestPath, feTo->szFullPath, feFrom->szFilename);
1203
1204     if (IsAttribFile(feFrom->attributes))
1205         SHNotifyMoveFileW(feFrom->szFullPath, szDestPath);
1206     else if (!(lpFileOp->fFlags & FOF_FILESONLY && feFrom->bFromWildcard))
1207         move_dir_to_dir(lpFileOp, feFrom, szDestPath);
1208 }
1209
1210 /* the FO_MOVE operation */
1211 static HRESULT move_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
1212 {
1213     DWORD i;
1214     FILE_ENTRY *entryToMove;
1215     FILE_ENTRY *fileDest;
1216
1217     if (!flFrom->dwNumFiles || !flTo->dwNumFiles)
1218         return ERROR_CANCELLED;
1219
1220     if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1221         flTo->dwNumFiles > 1 && flFrom->dwNumFiles > 1)
1222     {
1223         return ERROR_CANCELLED;
1224     }
1225
1226     if (!(lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1227         !flFrom->bAnyDirectories &&
1228         flFrom->dwNumFiles > flTo->dwNumFiles)
1229     {
1230         return ERROR_CANCELLED;
1231     }
1232
1233     if (!PathFileExistsW(flTo->feFiles[0].szDirectory))
1234         return ERROR_CANCELLED;
1235
1236     if ((lpFileOp->fFlags & FOF_MULTIDESTFILES) &&
1237         flFrom->dwNumFiles != flTo->dwNumFiles)
1238     {
1239         return ERROR_CANCELLED;
1240     }
1241
1242     fileDest = &flTo->feFiles[0];
1243     for (i = 0; i < flFrom->dwNumFiles; i++)
1244     {
1245         entryToMove = &flFrom->feFiles[i];
1246
1247         if (lpFileOp->fFlags & FOF_MULTIDESTFILES)
1248             fileDest = &flTo->feFiles[i];
1249
1250         if (!PathFileExistsW(fileDest->szDirectory))
1251             return ERROR_CANCELLED;
1252
1253         if (fileDest->bExists && IsAttribDir(fileDest->attributes))
1254             move_to_dir(lpFileOp, entryToMove, fileDest);
1255         else
1256             SHNotifyMoveFileW(entryToMove->szFullPath, fileDest->szFullPath);
1257     }
1258
1259     return ERROR_SUCCESS;
1260 }
1261
1262 /* the FO_RENAME files */
1263 static HRESULT rename_files(LPSHFILEOPSTRUCTW lpFileOp, FILE_LIST *flFrom, FILE_LIST *flTo)
1264 {
1265     FILE_ENTRY *feFrom;
1266     FILE_ENTRY *feTo;
1267
1268     if (flFrom->dwNumFiles != 1)
1269         return ERROR_GEN_FAILURE;
1270
1271     if (flTo->dwNumFiles != 1)
1272         return ERROR_CANCELLED;
1273
1274     feFrom = &flFrom->feFiles[0];
1275     feTo= &flTo->feFiles[0];
1276
1277     /* fail if destination doesn't exist */
1278     if (!feFrom->bExists)
1279         return ERROR_SHELL_INTERNAL_FILE_NOT_FOUND;
1280
1281     /* fail if destination already exists */
1282     if (feTo->bExists)
1283         return ERROR_ALREADY_EXISTS;
1284
1285     return SHNotifyMoveFileW(feFrom->szFullPath, feTo->szFullPath);
1286 }
1287
1288 /* alert the user if an unsupported flag is used */
1289 static void check_flags(FILEOP_FLAGS fFlags)
1290 {
1291     WORD wUnsupportedFlags = FOF_NO_CONNECTED_ELEMENTS |
1292         FOF_NOCOPYSECURITYATTRIBS | FOF_NORECURSEREPARSE |
1293         FOF_RENAMEONCOLLISION | FOF_WANTMAPPINGHANDLE;
1294
1295     if (fFlags & wUnsupportedFlags)
1296         FIXME("Unsupported flags: %04x\n", fFlags);
1297 }
1298
1299 /*************************************************************************
1300  * SHFileOperationW          [SHELL32.@]
1301  *
1302  * See SHFileOperationA
1303  */
1304 int WINAPI SHFileOperationW(LPSHFILEOPSTRUCTW lpFileOp)
1305 {
1306     FILE_LIST flFrom, flTo;
1307     int ret = 0;
1308
1309     if (!lpFileOp)
1310         return ERROR_INVALID_PARAMETER;
1311
1312     check_flags(lpFileOp->fFlags);
1313
1314     ZeroMemory(&flFrom, sizeof(FILE_LIST));
1315     ZeroMemory(&flTo, sizeof(FILE_LIST));
1316
1317     if ((ret = parse_file_list(&flFrom, lpFileOp->pFrom)))
1318         return ret;
1319
1320     if (lpFileOp->wFunc != FO_DELETE)
1321         parse_file_list(&flTo, lpFileOp->pTo);
1322
1323     switch (lpFileOp->wFunc)
1324     {
1325         case FO_COPY:
1326             ret = copy_files(lpFileOp, &flFrom, &flTo);
1327             break;
1328         case FO_DELETE:
1329             ret = delete_files(lpFileOp, &flFrom);
1330             break;
1331         case FO_MOVE:
1332             ret = move_files(lpFileOp, &flFrom, &flTo);
1333             break;
1334         case FO_RENAME:
1335             ret = rename_files(lpFileOp, &flFrom, &flTo);
1336             break;
1337         default:
1338             ret = ERROR_INVALID_PARAMETER;
1339             break;
1340     }
1341
1342     destroy_file_list(&flFrom);
1343
1344     if (lpFileOp->wFunc != FO_DELETE)
1345         destroy_file_list(&flTo);
1346
1347     if (ret == ERROR_CANCELLED)
1348         lpFileOp->fAnyOperationsAborted = TRUE;
1349     
1350     return ret;
1351 }
1352
1353 #define SHDSA_GetItemCount(hdsa) (*(int*)(hdsa))
1354
1355 /*************************************************************************
1356  * SHFreeNameMappings      [shell32.246]
1357  *
1358  * Free the mapping handle returned by SHFileoperation if FOF_WANTSMAPPINGHANDLE
1359  * was specified.
1360  *
1361  * PARAMS
1362  *  hNameMapping [I] handle to the name mappings used during renaming of files
1363  *
1364  * RETURNS
1365  *  Nothing
1366  */
1367 void WINAPI SHFreeNameMappings(HANDLE hNameMapping)
1368 {
1369         if (hNameMapping)
1370         {
1371           int i = SHDSA_GetItemCount((HDSA)hNameMapping) - 1;
1372
1373           for (; i>= 0; i--)
1374           {
1375             LPSHNAMEMAPPINGW lp = DSA_GetItemPtr((HDSA)hNameMapping, i);
1376
1377             SHFree(lp->pszOldPath);
1378             SHFree(lp->pszNewPath);
1379           }
1380           DSA_Destroy((HDSA)hNameMapping);
1381         }
1382 }
1383
1384 /*************************************************************************
1385  * SheGetDirA [SHELL32.@]
1386  *
1387  */
1388 HRESULT WINAPI SheGetDirA(LPSTR u, LPSTR v)
1389 {   FIXME("%p %p stub\n",u,v);
1390     return 0;
1391 }
1392
1393 /*************************************************************************
1394  * SheGetDirW [SHELL32.@]
1395  *
1396  */
1397 HRESULT WINAPI SheGetDirW(LPWSTR u, LPWSTR v)
1398 {       FIXME("%p %p stub\n",u,v);
1399         return 0;
1400 }
1401
1402 /*************************************************************************
1403  * SheChangeDirA [SHELL32.@]
1404  *
1405  */
1406 HRESULT WINAPI SheChangeDirA(LPSTR u)
1407 {   FIXME("(%s),stub\n",debugstr_a(u));
1408     return 0;
1409 }
1410
1411 /*************************************************************************
1412  * SheChangeDirW [SHELL32.@]
1413  *
1414  */
1415 HRESULT WINAPI SheChangeDirW(LPWSTR u)
1416 {       FIXME("(%s),stub\n",debugstr_w(u));
1417         return 0;
1418 }
1419
1420 /*************************************************************************
1421  * IsNetDrive                   [SHELL32.66]
1422  */
1423 BOOL WINAPI IsNetDrive(DWORD drive)
1424 {
1425         char root[4];
1426         strcpy(root, "A:\\");
1427         root[0] += (char)drive;
1428         return (GetDriveTypeA(root) == DRIVE_REMOTE);
1429 }
1430
1431
1432 /*************************************************************************
1433  * RealDriveType                [SHELL32.524]
1434  */
1435 INT WINAPI RealDriveType(INT drive, BOOL bQueryNet)
1436 {
1437     char root[] = "A:\\";
1438     root[0] += (char)drive;
1439     return GetDriveTypeA(root);
1440 }