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