Fix segmentation fault caused by incorrect referencing of client audio
[wine] / dlls / shell32 / shfldr_unixfs.c
1 /*
2  * UNIXFS - Shell namespace extension for the unix filesystem
3  *
4  * Copyright (C) 2005 Michael Jung
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22 #include <stdio.h>
23 #include <stdarg.h>
24 #include <limits.h>
25 #include <dirent.h>
26 #ifdef HAVE_SYS_STAT_H
27 # include <sys/stat.h>
28 #endif
29 #ifdef HAVE_PWD_H
30 # include <pwd.h>
31 #endif
32 #include <grp.h>
33
34 #define COBJMACROS
35 #define NONAMELESSUNION
36 #define NONAMELESSSTRUCT
37
38 #include "windef.h"
39 #include "winbase.h"
40 #include "winuser.h"
41 #include "objbase.h"
42 #include "winreg.h"
43 #include "winternl.h"
44 #include "wine/debug.h"
45
46 #include "shell32_main.h"
47 #include "shfldr.h"
48 #include "shresdef.h"
49 #include "pidl.h"
50
51 WINE_DEFAULT_DEBUG_CHANNEL(shell);
52
53 const GUID CLSID_UnixFolder = {0xcc702eb2, 0x7dc5, 0x11d9, {0xc6, 0x87, 0x00, 0x04, 0x23, 0x8a, 0x01, 0xcd}};
54
55 #define ADJUST_THIS(c,m,p) ((c*)(((long)p)-(long)&(((c*)0)->lp##m##Vtbl)))
56 #define STATIC_CAST(i,p) ((i*)&p->lp##i##Vtbl)
57
58 /* FileStruct reserves one byte for szNames, thus we don't have to 
59  * alloc a byte for the terminating '\0' of 'name'. Two of the
60  * additional bytes are for SHITEMID's cb field. One is for IDLDATA's
61  * type field. One is for FileStruct's szNames field, to terminate
62  * the alternate DOS name, which we don't use here. And then there's
63  * the additional StatStruct.
64  */
65 #define SHITEMID_LEN_FROM_NAME_LEN(n) \
66     (sizeof(USHORT)+sizeof(PIDLTYPE)+sizeof(FileStruct)+(n)+sizeof(char)+sizeof(StatStruct))
67 #define NAME_LEN_FROM_LPSHITEMID(s) \
68     (((LPSHITEMID)s)->cb-sizeof(USHORT)-sizeof(PIDLTYPE)-sizeof(FileStruct)-sizeof(char)-sizeof(StatStruct))
69 #define LPSTATSTRUCT_FROM_LPSHITEMID(s) ((StatStruct*)(((LPBYTE)s)+((LPSHITEMID)s)->cb-sizeof(StatStruct)))
70
71 /* This structure is appended to shell32's FileStruct type in IDLs to store unix
72  * filesystem specific informationen extracted with the stat system call.
73  */
74 typedef struct tagStatStruct {
75     mode_t st_mode;
76     uid_t  st_uid;
77     gid_t  st_gid;
78 } StatStruct;
79
80 /******************************************************************************
81  * UNIXFS_is_pidl_of_type [INTERNAL]
82  *
83  * Checks for the first SHITEMID of an ITEMIDLIST if it passes a filter.
84  *
85  * PARAMS
86  *  pIDL    [I] The ITEMIDLIST to be checked.
87  *  fFilter [I] Shell condition flags, which specify the filter.
88  *
89  * RETURNS
90  *  TRUE, if pIDL is accepted by fFilter
91  *  FALSE, otherwise
92  */
93 static inline BOOL UNIXFS_is_pidl_of_type(LPITEMIDLIST pIDL, SHCONTF fFilter) {
94     LPSTR pszText = _ILGetTextPointer(pIDL);
95     if (!pszText) return FALSE;
96     if (pszText[0] == '.' && !(fFilter & SHCONTF_INCLUDEHIDDEN)) return FALSE;
97     if (_ILIsFolder(pIDL) && (fFilter & SHCONTF_FOLDERS)) return TRUE;
98     if (_ILIsValue(pIDL) && (fFilter & SHCONTF_NONFOLDERS)) return TRUE;
99     return FALSE;
100 }
101
102 /******************************************************************************
103  * UNIXFS_build_shitemid [Internal]
104  *
105  * Constructs a new SHITEMID for a single path item (up to the next '/' or 
106  * '\0') into a buffer. Decorates the SHITEMID with information from a stat 
107  * system call.
108  *
109  * PARAMS
110  *  name   [I] The name of the next path item. Terminated by either '\0' or '/'.
111  *  pStat  [I] A stat struct variable obtained by a stat system call on the file.
112  *  buffer [O] SHITEMID will be constructed here.
113  *
114  * RETURNS
115  *  A pointer to the next '/' or '\0' character in name.
116  *  
117  * NOTES
118  *  Minimum size of buffer is SHITEMID_LEN_FROM_NAME_LEN(strlen(name)).
119  *  If what you need is a PIDLLIST with a single SHITEMID, don't forget to append
120  *  a 0 USHORT value.
121  */
122 static char* UNIXFS_build_shitemid(char *name, struct stat *pStat, void *buffer) {
123     LARGE_INTEGER time;
124     FILETIME fileTime;
125     LPPIDLDATA pIDLData;
126     StatStruct *pStatStruct;
127     int cNameLen;
128     char *pSlash;
129
130     TRACE("(name=%s, buffer=%p)\n", debugstr_a(name), buffer);
131
132     pSlash = strchr(name, '/');
133     cNameLen = pSlash ? pSlash - name : strlen(name); 
134     
135     memset(buffer, 0, SHITEMID_LEN_FROM_NAME_LEN(cNameLen));
136     ((LPSHITEMID)buffer)->cb = SHITEMID_LEN_FROM_NAME_LEN(cNameLen) ;
137     
138     pIDLData = _ILGetDataPointer((LPCITEMIDLIST)buffer);
139     pIDLData->type = S_ISDIR(pStat->st_mode) ? PT_FOLDER : PT_VALUE;
140     pIDLData->u.file.dwFileSize = (DWORD)pStat->st_size;
141     RtlSecondsSince1970ToTime( pStat->st_mtime, &time );
142     fileTime.dwLowDateTime = time.u.LowPart;
143     fileTime.dwHighDateTime = time.u.HighPart;
144     FileTimeToDosDateTime(&fileTime, &pIDLData->u.file.uFileDate, &pIDLData->u.file.uFileTime);
145     pIDLData->u.file.uFileAttribs = 0;
146     memcpy(pIDLData->u.file.szNames, name, cNameLen);
147
148     pStatStruct = LPSTATSTRUCT_FROM_LPSHITEMID(buffer);
149     pStatStruct->st_mode = pStat->st_mode;
150     pStatStruct->st_uid = pStat->st_uid;
151     pStatStruct->st_gid = pStat->st_gid;
152
153     return pSlash ? pSlash+1 : (name + cNameLen);
154 }
155
156 /******************************************************************************
157  * UNIXFS_path_to_pidl [Internal]
158  *
159  * PARAMS
160  *  path  [I] An absolute unix path 
161  *  ppidl [O] The corresponding ITEMIDLIST. Release with SHFree/ILFree
162  *  
163  * RETURNS
164  *  Success: TRUE
165  *  Failure: FALSE, invalid params, path not absolute or out of memory
166  *
167  * NOTES
168  *  'path' has to be an absolute unix filesystem path starting and
169  *  ending with a slash ('/'). Currently, only directories (no files)
170  *  are accepted.
171  */
172 static BOOL UNIXFS_path_to_pidl(char *path, LPITEMIDLIST *ppidl) {
173     LPITEMIDLIST pidl;
174     struct stat fileStat;
175     int cSubDirs, cPidlLen, res;
176     char *pSlash;
177
178     TRACE("path=%s, ppidl=%p", debugstr_a(path), ppidl);
179     
180     /* Fail, if no absolute path given */
181     if (!ppidl || !path || path[0] != '/') return FALSE;
182
183     /* Skip the '/' prefix */
184     path++;
185     
186     /* Count the number of sub-directories in the path */
187     cSubDirs = 0;
188     pSlash = strchr(path, '/');
189     while (pSlash) {
190         cSubDirs++;
191         pSlash = strchr(pSlash+1, '/');
192     }
193    
194     /* Allocate enough memory to hold the path. The -cSubDirs is for the '/' 
195      * characters, which are not stored in the ITEMIDLIST. */
196     cPidlLen = strlen(path) - cSubDirs + cSubDirs * SHITEMID_LEN_FROM_NAME_LEN(0) + sizeof(USHORT);
197     *ppidl = pidl = (LPITEMIDLIST)SHAlloc(cPidlLen);
198     if (!pidl) return FALSE;
199
200     /* Concatenate the SHITEMIDs of the sub-directories. */
201     while (*path) {
202         pSlash = strchr(path, '/');
203         if (pSlash) {
204             *pSlash = '\0';
205             res = stat(path, &fileStat);
206             *pSlash = '/';
207             if (res) return FALSE;
208         } else {
209             if (stat(path, &fileStat)) return FALSE;
210         }
211                 
212         path = UNIXFS_build_shitemid(path, &fileStat, pidl);
213         pidl = ILGetNext(pidl);
214     }
215     pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
216    
217     return TRUE;
218 }
219
220 /******************************************************************************
221  * UNIXFS_pidl_to_path [Internal]
222  *
223  * Construct the unix path that corresponds to a fully qualified ITEMIDLIST
224  *
225  * PARAMS
226  *  pidl [I] ITEMIDLIST that specifies the absolute location of the folder
227  *  path [O] The corresponding unix path as a zero terminated ascii string
228  *
229  * RETURNS
230  *  Success: TRUE
231  *  Failure: FALSE, pidl doesn't specify a unix path or out of memory
232  */
233 static BOOL UNIXFS_pidl_to_path(LPCITEMIDLIST pidl, PSZ *path) {
234     LPCITEMIDLIST current = pidl, root;
235     DWORD dwPathLen;
236     char *pNextDir;
237
238     TRACE("(pidl=%p, path=%p)\n", pidl, path);
239     
240     *path = NULL;
241
242     /* Find the UnixFolderClass root */
243     while (current->mkid.cb) {
244         LPPIDLDATA pData = _ILGetDataPointer(current);
245         if (!pData) return FALSE;
246         if (pData->type == PT_GUID && IsEqualIID(&CLSID_UnixFolder, &pData->u.guid.guid)) break;
247         current = ILGetNext(current);
248     }
249     root = current = ILGetNext(current);
250     
251     /* Determine the path's length bytes */
252     dwPathLen = 2; /* For the '/' prefix and the terminating '\0' */
253     while (current->mkid.cb) {
254         dwPathLen += NAME_LEN_FROM_LPSHITEMID(current) + 1; /* For the '/' */
255         current = ILGetNext(current);
256     };
257
258     /* Build the path */
259     *path = pNextDir = SHAlloc(dwPathLen);
260     if (!path) {
261         WARN("SHAlloc failed!\n");
262         return FALSE;
263     }
264     current = root;
265     *pNextDir++ = '/';
266     while (current->mkid.cb) {
267         memcpy(pNextDir, _ILGetTextPointer(current), NAME_LEN_FROM_LPSHITEMID(current));
268         pNextDir += NAME_LEN_FROM_LPSHITEMID(current);
269         *pNextDir++ = '/';
270         current = ILGetNext(current);
271     }
272     *pNextDir='\0';
273     
274     TRACE("resulting path: %s\n", *path);
275     return TRUE;
276 }
277
278 /******************************************************************************
279  * UNIXFS_build_subfolder_pidls [Internal]
280  *
281  * Builds an array of subfolder PIDLs relative to a unix directory
282  *
283  * PARAMS
284  *  path   [I] Name of a unix directory as a zero terminated ascii string
285  *  apidl  [O] The array of PIDLs
286  *  pCount [O] Size of apidl
287  *
288  * RETURNS
289  *  Success: TRUE
290  *  Failure: FALSE, path is not a valid unix directory or out of memory
291  *
292  * NOTES
293  *  The array of PIDLs and each PIDL are allocated with SHAlloc. You'll have
294  *  to release each PIDL as well as the array itself with SHFree.
295  */
296 static BOOL UNIXFS_build_subfolder_pidls(const char *path, LPITEMIDLIST **apidl, DWORD *pCount)
297 {
298     struct dirent *pDirEntry;
299     struct stat fileStat;
300     DIR *dir;
301     DWORD cDirEntries, i;
302     USHORT sLen;
303     char *pszFQPath;
304
305     TRACE("(path=%s, apidl=%p, pCount=%p)\n", debugstr_a(path), apidl, pCount);
306     
307     *apidl = NULL;
308     *pCount = 0;
309   
310     dir = opendir(path);
311     if (!dir) {
312         WARN("Failed to open directory '%s'.\n", path);
313         return FALSE;
314     }
315
316     /* Allocate space for fully qualified paths */
317     pszFQPath = SHAlloc(strlen(path) + NAME_MAX);
318     if (!pszFQPath) {
319         WARN("SHAlloc failed!\n");
320         return FALSE;
321     }
322  
323     /* Count number of directory entries. */
324     for (cDirEntries = 0, pDirEntry = readdir(dir); pDirEntry; pDirEntry = readdir(dir)) { 
325         if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
326         sprintf(pszFQPath, "%s%s", path, pDirEntry->d_name);
327         if (!stat(pszFQPath, &fileStat) && (S_ISDIR(fileStat.st_mode) || S_ISREG(fileStat.st_mode))) cDirEntries++;
328     }
329
330     /* If there are no entries, we are done. */
331     if (cDirEntries == 0) {
332         closedir(dir);
333         SHFree(pszFQPath);
334         return TRUE;
335     }
336
337     /* Allocate the array of PIDLs */
338     *apidl = SHAlloc(cDirEntries * sizeof(LPITEMIDLIST));
339     if (!apidl) {
340         WARN("SHAlloc failed!\n");
341         return FALSE;
342     }
343   
344     /* Allocate and initialize one SHITEMID per sub-directory. */
345     for (rewinddir(dir), pDirEntry = readdir(dir), i = 0; pDirEntry; pDirEntry = readdir(dir)) {
346         LPSHITEMID pid;
347             
348         if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
349
350         sprintf(pszFQPath, "%s%s", path, pDirEntry->d_name);
351         if (stat(pszFQPath, &fileStat)) continue;
352         if (!S_ISDIR(fileStat.st_mode) && !S_ISREG(fileStat.st_mode)) continue;
353    
354         sLen = strlen(pDirEntry->d_name);
355         pid = (LPSHITEMID)SHAlloc(SHITEMID_LEN_FROM_NAME_LEN(sLen)+sizeof(USHORT));
356         if (!pid) {
357             WARN("SHAlloc failed!\n");
358             return FALSE;
359         }
360         UNIXFS_build_shitemid(pDirEntry->d_name, &fileStat, pid);
361         memset(((PBYTE)pid)+pid->cb, 0, sizeof(USHORT));
362
363         (*apidl)[i++] = (LPITEMIDLIST)pid;
364     }
365     
366     *pCount = i;    
367     closedir(dir);
368     SHFree(pszFQPath);
369
370     return TRUE;
371 }
372
373 /******************************************************************************
374  * UnixFolderIcon 
375  *
376  * Singleton class, which is used by the shell to extract icons to represent
377  * folders in tree- and listviews. Currently, all this singleton does is to
378  * provide the shell with the absolute path to "shell32.dll" and with the 
379  * indices of the closed and opened folder icons in the resources of this dll.
380  */
381
382 /* UnixFolderIcon object layout and typedef.
383  */
384 typedef struct _UnixFolderIcon {
385     const IExtractIconWVtbl *lpIExtractIconWVtbl;
386     BOOL bFolder;
387 } UnixFolderIcon;
388
389 static HRESULT WINAPI UnixFolderIcon_IExtractIconW_QueryInterface(IExtractIconW *iface, REFIID riid, 
390     void **ppv) 
391 {
392     TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
393     
394     if (!ppv) return E_INVALIDARG;
395     
396     if (IsEqualIID(&IID_IUnknown, riid) ||
397         IsEqualIID(&IID_IExtractIconW, riid))
398     {
399         *ppv = iface;
400     } else {
401         *ppv = NULL;
402         return E_NOINTERFACE;
403     }
404
405     IExtractIconW_AddRef(iface);
406     return S_OK;
407 }
408
409 static ULONG WINAPI UnixFolderIcon_IExtractIconW_AddRef(IExtractIconW *iface) {
410     TRACE("(iface=%p)\n", iface);
411     return 2;
412 }
413
414 static ULONG WINAPI UnixFolderIcon_IExtractIconW_Release(IExtractIconW *iface) {
415     TRACE("(iface=%p)\n", iface);
416     return 1;
417 }
418
419 static HRESULT WINAPI UnixFolderIcon_IExtractIconW_GetIconLocation(IExtractIconW *iface, 
420     UINT uFlags, LPWSTR szIconFile, UINT cchMax, INT* piIndex, UINT* pwFlags)
421 {
422     UnixFolderIcon *This = ADJUST_THIS(UnixFolderIcon, IExtractIconW, iface);
423         
424     TRACE("(iface=%p, uFlags=%u, szIconFile=%s, cchMax=%u, piIndex=%p, pwFlags=%p)\n",
425             iface, uFlags, debugstr_w(szIconFile), cchMax, piIndex, pwFlags);
426     
427     lstrcpynW(szIconFile, swShell32Name, cchMax);
428     if (This->bFolder) {
429         *piIndex = (uFlags & GIL_OPENICON) ? -IDI_SHELL_FOLDER_OPEN : -IDI_SHELL_FOLDER;
430     } else {
431         *piIndex = -IDI_SHELL_DOCUMENT;
432     }
433     *pwFlags = 0;
434
435     return S_OK;
436 }
437
438 static HRESULT WINAPI UnixFolderIcon_IExtractIconW_Extract(
439     IExtractIconW *iface, LPCWSTR pszFile, UINT nIconIndex, HICON* phiconLarge, HICON* phiconSmall, 
440     UINT nIconSize)
441 {
442     TRACE("(iface=%p, pszFile=%s, nIconIndex=%u, phiconLarge=%p, phiconSmall=%p, nIconSize=%u)"
443           "stub\n", iface, debugstr_w(pszFile), nIconIndex, phiconLarge, phiconSmall, nIconSize);
444
445     return E_NOTIMPL;
446 }
447
448 /* VTable for the IExtractIconW interface of the UnixFolderIcon class. 
449  */
450 static const IExtractIconWVtbl UnixFolderIcon_IExtractIconW_Vtbl = {
451     UnixFolderIcon_IExtractIconW_QueryInterface,
452     UnixFolderIcon_IExtractIconW_AddRef,
453     UnixFolderIcon_IExtractIconW_Release,
454     UnixFolderIcon_IExtractIconW_GetIconLocation,
455     UnixFolderIcon_IExtractIconW_Extract
456 };
457
458 /* The singleton instance
459  */
460 UnixFolderIcon UnixFolderIconSingleton = { &UnixFolderIcon_IExtractIconW_Vtbl, TRUE };
461 UnixFolderIcon UnixDocumentIconSingleton = { &UnixFolderIcon_IExtractIconW_Vtbl, FALSE };
462
463 /******************************************************************************
464  * UnixFolder
465  *
466  * Class whose heap based instances represent unix filesystem directories.
467  */
468
469 /* UnixFolder object layout and typedef.
470  */
471 typedef struct _UnixFolder {
472     const IShellFolder2Vtbl  *lpIShellFolder2Vtbl;
473     const IPersistFolderVtbl *lpIPersistFolderVtbl;
474     ULONG m_cRef;
475     CHAR *m_pszPath;
476     LPITEMIDLIST m_pidlLocation;
477     LPITEMIDLIST *m_apidlSubDirs;
478     DWORD m_cSubDirs;
479 } UnixFolder;
480
481 static void UnixFolder_Destroy(UnixFolder *pUnixFolder) {
482     DWORD i;
483
484     TRACE("(pUnixFolder=%p)\n", pUnixFolder);
485     
486     if (pUnixFolder->m_apidlSubDirs) 
487         for (i=0; i < pUnixFolder->m_cSubDirs; i++) 
488             SHFree(pUnixFolder->m_apidlSubDirs[i]);
489     SHFree(pUnixFolder->m_apidlSubDirs);
490     SHFree(pUnixFolder->m_pszPath);
491     ILFree(pUnixFolder->m_pidlLocation);
492     SHFree(pUnixFolder);
493 }
494
495 static HRESULT WINAPI UnixFolder_IShellFolder2_QueryInterface(IShellFolder2 *iface, REFIID riid, 
496     void **ppv) 
497 {
498     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
499         
500     TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
501     
502     if (!ppv) return E_INVALIDARG;
503     
504     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IShellFolder, riid) || 
505         IsEqualIID(&IID_IShellFolder2, riid)) 
506     {
507         *ppv = &This->lpIShellFolder2Vtbl;
508     } else if (IsEqualIID(&IID_IPersistFolder, riid) || IsEqualIID(&IID_IPersist, riid)) {
509         *ppv = &This->lpIPersistFolderVtbl;
510     } else {
511         *ppv = NULL;
512         return E_NOINTERFACE;
513     }
514
515     IUnknown_AddRef((IUnknown*)*ppv);
516     return S_OK;
517 }
518
519 static ULONG WINAPI UnixFolder_IShellFolder2_AddRef(IShellFolder2 *iface) {
520     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
521
522     TRACE("(iface=%p)\n", iface);
523
524     return InterlockedIncrement(&This->m_cRef);
525 }
526
527 static ULONG WINAPI UnixFolder_IShellFolder2_Release(IShellFolder2 *iface) {
528     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
529     ULONG cRef;
530     
531     TRACE("(iface=%p)\n", iface);
532
533     cRef = InterlockedDecrement(&This->m_cRef);
534     
535     if (!cRef) 
536         UnixFolder_Destroy(This);
537
538     return cRef;
539 }
540
541 static HRESULT WINAPI UnixFolder_IShellFolder2_ParseDisplayName(IShellFolder2* iface, HWND hwndOwner, 
542     LPBC pbcReserved, LPOLESTR lpszDisplayName, ULONG* pchEaten, LPITEMIDLIST* ppidl, 
543     ULONG* pdwAttributes)
544 {
545     int cPathLen;
546     char *pszAnsiPath;
547     BOOL result;
548
549     TRACE("(iface=%p, hwndOwner=%p, pbcReserved=%p, lpszDisplayName=%s, pchEaten=%p, ppidl=%p, "
550           "pdwAttributes=%p) stub\n", iface, hwndOwner, pbcReserved, debugstr_w(lpszDisplayName), 
551           pchEaten, ppidl, pdwAttributes);
552
553     cPathLen = lstrlenW(lpszDisplayName);
554     pszAnsiPath = (char*)SHAlloc(cPathLen+1);
555     WideCharToMultiByte(CP_ACP, 0, lpszDisplayName, -1, pszAnsiPath, cPathLen+1, NULL, NULL);
556
557     result = UNIXFS_path_to_pidl(pszAnsiPath, ppidl);
558
559     SHFree(pszAnsiPath);
560     
561     return result ? S_OK : E_FAIL;
562 }
563
564 static IUnknown *UnixSubFolderIterator_Construct(UnixFolder *pUnixFolder, SHCONTF fFilter);
565
566 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumObjects(IShellFolder2* iface, HWND hwndOwner, 
567     SHCONTF grfFlags, IEnumIDList** ppEnumIDList)
568 {
569     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
570     IUnknown *newIterator;
571     HRESULT hr;
572     
573     TRACE("(iface=%p, hwndOwner=%p, grfFlags=%08lx, ppEnumIDList=%p)\n", 
574             iface, hwndOwner, grfFlags, ppEnumIDList);
575
576     newIterator = UnixSubFolderIterator_Construct(This, grfFlags);
577     hr = IUnknown_QueryInterface(newIterator, &IID_IEnumIDList, (void**)ppEnumIDList);
578     IUnknown_Release(newIterator);
579     
580     return hr;
581 }
582
583 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToObject(IShellFolder2* iface, LPCITEMIDLIST pidl,
584     LPBC pbcReserved, REFIID riid, void** ppvOut)
585 {
586     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
587     IPersistFolder *persistFolder;
588     LPITEMIDLIST pidlSubFolder;
589     HRESULT hr;
590         
591     TRACE("(iface=%p, pidl=%p, pbcReserver=%p, riid=%p, ppvOut=%p)\n", 
592             iface, pidl, pbcReserved, riid, ppvOut);
593
594     hr = UnixFolder_Constructor(NULL, &IID_IPersistFolder, (void**)&persistFolder);
595     if (!SUCCEEDED(hr)) return hr;
596     hr = IPersistFolder_QueryInterface(persistFolder, riid, (void**)ppvOut);
597     
598     pidlSubFolder = ILCombine(This->m_pidlLocation, pidl);
599     IPersistFolder_Initialize(persistFolder, pidlSubFolder);
600     IPersistFolder_Release(persistFolder);
601     ILFree(pidlSubFolder);
602
603     return hr;
604 }
605
606 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToStorage(IShellFolder2* This, LPCITEMIDLIST pidl, 
607     LPBC pbcReserved, REFIID riid, void** ppvObj)
608 {
609     TRACE("stub\n");
610     return E_NOTIMPL;
611 }
612
613 static HRESULT WINAPI UnixFolder_IShellFolder2_CompareIDs(IShellFolder2* iface, LPARAM lParam, 
614     LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
615 {
616     BOOL isEmpty1, isEmpty2;
617     HRESULT hr = E_FAIL;
618     LPITEMIDLIST firstpidl;
619     IShellFolder2 *psf;
620     int compare;
621
622     TRACE("(iface=%p, lParam=%ld, pidl1=%p, pidl2=%p)\n", iface, lParam, pidl1, pidl2);
623     
624     isEmpty1 = !pidl1 || !pidl1->mkid.cb;
625     isEmpty2 = !pidl2 || !pidl2->mkid.cb;
626
627     if (isEmpty1 && isEmpty2) 
628         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
629     else if (isEmpty1)
630         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
631     else if (isEmpty2)
632         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
633
634     if (_ILIsFolder(pidl1) && !_ILIsFolder(pidl2)) 
635         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
636     if (!_ILIsFolder(pidl1) && _ILIsFolder(pidl2))
637         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
638
639     compare = CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE, 
640                              _ILGetTextPointer(pidl1), NAME_LEN_FROM_LPSHITEMID(pidl1),
641                              _ILGetTextPointer(pidl2), NAME_LEN_FROM_LPSHITEMID(pidl2));
642     
643     if ((compare == CSTR_LESS_THAN) || (compare == CSTR_GREATER_THAN)) 
644         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)(compare == CSTR_LESS_THAN)?-1:1);
645
646     if (pidl1->mkid.cb < pidl2->mkid.cb)
647         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
648     else if (pidl1->mkid.cb > pidl2->mkid.cb)
649         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
650
651     firstpidl = ILCloneFirst(pidl1);
652     pidl1 = ILGetNext(pidl1);
653     pidl2 = ILGetNext(pidl2);
654     
655     hr = IShellFolder2_BindToObject(iface, firstpidl, NULL, &IID_IShellFolder, (LPVOID*)&psf);
656     if (SUCCEEDED(hr)) {
657         hr = IShellFolder_CompareIDs(psf, lParam, pidl1, pidl2);
658         IShellFolder2_Release(psf);
659     }
660
661     ILFree(firstpidl);
662     return hr;
663 }
664
665 static HRESULT WINAPI UnixFolder_IShellFolder2_CreateViewObject(IShellFolder2* iface, HWND hwndOwner,
666     REFIID riid, void** ppv)
667 {
668     HRESULT hr = E_INVALIDARG;
669         
670     TRACE("(iface=%p, hwndOwner=%p, riid=%p, ppv=%p) stub\n", iface, hwndOwner, riid, ppv);
671     
672     if (!ppv) return E_INVALIDARG;
673     *ppv = NULL;
674     
675     if (IsEqualIID(&IID_IShellView, riid)) {
676         LPSHELLVIEW pShellView;
677         
678         pShellView = IShellView_Constructor((IShellFolder*)iface);
679         if (pShellView) {
680             hr = IShellView_QueryInterface(pShellView, riid, ppv);
681             IShellView_Release(pShellView);
682         }
683     } 
684     
685     return hr;
686 }
687
688 static HRESULT WINAPI UnixFolder_IShellFolder2_GetAttributesOf(IShellFolder2* iface, UINT cidl, 
689     LPCITEMIDLIST* apidl, SFGAOF* rgfInOut)
690 {
691     UINT i;
692     SFGAOF flags= ~(SFGAOF)0;
693         
694     TRACE("(iface=%p, cidl=%u, apidl=%p, rgfInOut=%p) semi-stub\n", iface, cidl, apidl, rgfInOut);
695    
696     for (i=0; i<cidl; i++) {
697         LPPIDLDATA pData = _ILGetDataPointer(apidl[i]);
698         if (!pData) continue;
699         if (pData->type == PT_FOLDER) flags &= (SFGAO_FILESYSTEM|SFGAO_FOLDER|SFGAO_HASSUBFOLDER);
700         if (pData->type == PT_VALUE) flags &= SFGAO_FILESYSTEM;
701     }
702     
703     *rgfInOut = *rgfInOut & flags;
704             
705     return S_OK;
706 }
707
708 static HRESULT WINAPI UnixFolder_IShellFolder2_GetUIObjectOf(IShellFolder2* iface, HWND hwndOwner, 
709     UINT cidl, LPCITEMIDLIST* apidl, REFIID riid, UINT* prgfInOut, void** ppvOut)
710 {
711     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
712     
713     TRACE("(iface=%p, hwndOwner=%p, cidl=%d, apidl=%p, riid=%s, prgfInOut=%p, ppv=%p)\n",
714         iface, hwndOwner, cidl, apidl, debugstr_guid(riid), prgfInOut, ppvOut);
715
716     if (IsEqualIID(&IID_IContextMenu, riid)) {
717         *ppvOut = ISvItemCm_Constructor((IShellFolder*)iface, This->m_pidlLocation, apidl, cidl);
718         return S_OK;
719     } else if (IsEqualIID(&IID_IDataObject, riid)) {
720         *ppvOut = IDataObject_Constructor(hwndOwner, This->m_pidlLocation, apidl, cidl);
721         return S_OK;
722     } else if (IsEqualIID(&IID_IExtractIconA, riid)) {
723         FIXME("IExtractIconA\n");
724         return E_FAIL;
725     } else if (IsEqualIID(&IID_IExtractIconW, riid)) {
726         if (cidl != 1) return E_FAIL;
727         if (_ILIsFolder(apidl[0])) 
728             *ppvOut = &UnixFolderIconSingleton;
729         else
730             *ppvOut = &UnixDocumentIconSingleton;
731         return S_OK;
732     } else if (IsEqualIID(&IID_IDropTarget, riid)) {
733         FIXME("IDropTarget\n");
734         return E_FAIL;
735     } else if (IsEqualIID(&IID_IShellLinkW, riid)) {
736         FIXME("IShellLinkW\n");
737         return E_FAIL;
738     } else if (IsEqualIID(&IID_IShellLinkA, riid)) {
739         FIXME("IShellLinkA\n");
740         return E_FAIL;
741     } else {
742         FIXME("Unknown interface %s in GetUIObjectOf\n", debugstr_guid(riid));
743         return E_NOINTERFACE;
744     }
745 }
746
747 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDisplayNameOf(IShellFolder2* iface, 
748     LPCITEMIDLIST pidl, SHGDNF uFlags, STRRET* lpName)
749 {
750     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
751     HRESULT hr = S_OK;    
752
753     TRACE("(iface=%p, pidl=%p, uFlags=%lx, lpName=%p)\n", iface, pidl, uFlags, lpName);
754     
755     if ((GET_SHGDN_FOR(uFlags) == SHGDN_FORPARSING) &&
756         (GET_SHGDN_RELATION(uFlags) != SHGDN_INFOLDER))
757     {
758         if (!pidl->mkid.cb) {
759             lpName->uType = STRRET_CSTR;
760             strcpy(lpName->u.cStr, This->m_pszPath);
761         } else {
762             IShellFolder *pSubFolder;
763             USHORT emptyIDL = 0;
764
765             hr = IShellFolder_BindToObject(iface, pidl, NULL, &IID_IShellFolder, (void**)&pSubFolder);
766             if (!SUCCEEDED(hr)) return hr;
767        
768             hr = IShellFolder_GetDisplayNameOf(pSubFolder, (LPITEMIDLIST)&emptyIDL, uFlags, lpName);
769             IShellFolder_Release(pSubFolder);
770         }
771     } else {
772         char *pszFileName = _ILGetTextPointer(pidl);
773         lpName->uType = STRRET_CSTR;
774         strcpy(lpName->u.cStr, pszFileName ? pszFileName : "");
775     }
776
777     return hr;
778 }
779
780 static HRESULT WINAPI UnixFolder_IShellFolder2_SetNameOf(IShellFolder2* This, HWND hwnd, 
781     LPCITEMIDLIST pidl, LPCOLESTR lpszName, SHGDNF uFlags, LPITEMIDLIST* ppidlOut)
782 {
783     TRACE("stub\n");
784     return E_NOTIMPL;
785 }
786
787 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumSearches(IShellFolder2* iface, 
788     IEnumExtraSearch **ppEnum) 
789 {
790     TRACE("stub\n");
791     return E_NOTIMPL;
792 }
793
794 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumn(IShellFolder2* iface, 
795     DWORD dwReserved, ULONG *pSort, ULONG *pDisplay) 
796 {
797     TRACE("stub\n");
798     return E_NOTIMPL;
799 }
800
801 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumnState(IShellFolder2* iface, 
802     UINT iColumn, SHCOLSTATEF *pcsFlags)
803 {
804     TRACE("stub\n");
805     return E_NOTIMPL;
806 }
807
808 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultSearchGUID(IShellFolder2* iface, 
809     GUID *pguid)
810 {
811     TRACE("stub\n");
812     return E_NOTIMPL;
813 }
814
815 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsEx(IShellFolder2* iface, 
816     LPCITEMIDLIST pidl, const SHCOLUMNID *pscid, VARIANT *pv)
817 {
818     TRACE("stub\n");
819     return E_NOTIMPL;
820 }
821
822 #define SHELLVIEWCOLUMNS 6 
823
824 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsOf(IShellFolder2* iface, 
825     LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS *psd)
826 {
827     HRESULT hr = E_FAIL;
828     struct passwd *pPasswd;
829     struct group *pGroup;
830     static const shvheader SFHeader[SHELLVIEWCOLUMNS] = {
831         {IDS_SHV_COLUMN1,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
832         {IDS_SHV_COLUMN5,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
833         {IDS_SHV_COLUMN10, SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
834         {IDS_SHV_COLUMN11, SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
835         {IDS_SHV_COLUMN2,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 8},
836         {IDS_SHV_COLUMN4,  SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10}
837     };
838
839     TRACE("(iface=%p, pidl=%p, iColumn=%d, psd=%p) stub\n", iface, pidl, iColumn, psd);
840     
841     if (!psd || iColumn >= SHELLVIEWCOLUMNS)
842         return E_INVALIDARG;
843
844     if (!pidl) {
845         psd->fmt = SFHeader[iColumn].fmt;
846         psd->cxChar = SFHeader[iColumn].cxChar;
847         psd->str.uType = STRRET_CSTR;
848         LoadStringA(shell32_hInstance, SFHeader[iColumn].colnameid, psd->str.u.cStr, MAX_PATH);
849         return S_OK;
850     } else {
851         StatStruct *pStatStruct = LPSTATSTRUCT_FROM_LPSHITEMID(pidl);
852         psd->str.u.cStr[0] = '\0';
853         psd->str.uType = STRRET_CSTR;
854         switch (iColumn) {
855             case 0:
856                 hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_NORMAL|SHGDN_INFOLDER, &psd->str);
857                 break;
858             case 1:
859                 psd->str.u.cStr[0] = S_ISDIR(pStatStruct->st_mode) ? 'd' : '-';
860                 psd->str.u.cStr[1] = (pStatStruct->st_mode & S_IRUSR) ? 'r' : '-';
861                 psd->str.u.cStr[2] = (pStatStruct->st_mode & S_IWUSR) ? 'w' : '-';
862                 psd->str.u.cStr[3] = (pStatStruct->st_mode & S_IXUSR) ? 'x' : '-';
863                 psd->str.u.cStr[4] = (pStatStruct->st_mode & S_IRGRP) ? 'r' : '-';
864                 psd->str.u.cStr[5] = (pStatStruct->st_mode & S_IWGRP) ? 'w' : '-';
865                 psd->str.u.cStr[6] = (pStatStruct->st_mode & S_IXGRP) ? 'x' : '-';
866                 psd->str.u.cStr[7] = (pStatStruct->st_mode & S_IROTH) ? 'r' : '-';
867                 psd->str.u.cStr[8] = (pStatStruct->st_mode & S_IWOTH) ? 'w' : '-';
868                 psd->str.u.cStr[9] = (pStatStruct->st_mode & S_IXOTH) ? 'x' : '-';
869                 psd->str.u.cStr[10] = '\0';
870                 break;
871             case 2:
872                 pPasswd = getpwuid(pStatStruct->st_uid);
873                 if (pPasswd) strcpy(psd->str.u.cStr, pPasswd->pw_name);
874                 break;
875             case 3:
876                 pGroup = getgrgid(pStatStruct->st_gid);
877                 if (pGroup) strcpy(psd->str.u.cStr, pGroup->gr_name);
878                 break;
879             case 4:
880                 _ILGetFileSize(pidl, psd->str.u.cStr, MAX_PATH);
881                 break;
882             case 5:
883                 _ILGetFileDate(pidl, psd->str.u.cStr, MAX_PATH);
884                 break;
885         }
886     }
887     
888     return hr;
889 }
890
891 static HRESULT WINAPI UnixFolder_IShellFolder2_MapColumnToSCID(IShellFolder2* iface, UINT iColumn,
892     SHCOLUMNID *pscid) 
893 {
894     TRACE("stub\n");
895     return E_NOTIMPL;
896 }
897
898 /* VTable for UnixFolder's IShellFolder2 interface.
899  */
900 static const IShellFolder2Vtbl UnixFolder_IShellFolder2_Vtbl = {
901     UnixFolder_IShellFolder2_QueryInterface,
902     UnixFolder_IShellFolder2_AddRef,
903     UnixFolder_IShellFolder2_Release,
904     UnixFolder_IShellFolder2_ParseDisplayName,
905     UnixFolder_IShellFolder2_EnumObjects,
906     UnixFolder_IShellFolder2_BindToObject,
907     UnixFolder_IShellFolder2_BindToStorage,
908     UnixFolder_IShellFolder2_CompareIDs,
909     UnixFolder_IShellFolder2_CreateViewObject,
910     UnixFolder_IShellFolder2_GetAttributesOf,
911     UnixFolder_IShellFolder2_GetUIObjectOf,
912     UnixFolder_IShellFolder2_GetDisplayNameOf,
913     UnixFolder_IShellFolder2_SetNameOf,
914     UnixFolder_IShellFolder2_GetDefaultSearchGUID,
915     UnixFolder_IShellFolder2_EnumSearches,
916     UnixFolder_IShellFolder2_GetDefaultColumn,
917     UnixFolder_IShellFolder2_GetDefaultColumnState,
918     UnixFolder_IShellFolder2_GetDetailsEx,
919     UnixFolder_IShellFolder2_GetDetailsOf,
920     UnixFolder_IShellFolder2_MapColumnToSCID
921 };
922
923 static HRESULT WINAPI UnixFolder_IPersistFolder_QueryInterface(IPersistFolder* This, REFIID riid, 
924     void** ppvObject)
925 {
926     return UnixFolder_IShellFolder2_QueryInterface(
927                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder, This), riid, ppvObject);
928 }
929
930 static ULONG WINAPI UnixFolder_IPersistFolder_AddRef(IPersistFolder* This)
931 {
932     return UnixFolder_IShellFolder2_AddRef(
933                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder, This));
934 }
935
936 static ULONG WINAPI UnixFolder_IPersistFolder_Release(IPersistFolder* This)
937 {
938     return UnixFolder_IShellFolder2_Release(
939                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder, This));
940 }
941
942 static HRESULT WINAPI UnixFolder_IPersistFolder_GetClassID(IPersistFolder* This, CLSID* pClassID)
943 {
944     TRACE("stub\n");
945     return E_NOTIMPL;
946 }
947
948 static HRESULT WINAPI UnixFolder_IPersistFolder_Initialize(IPersistFolder* iface, LPCITEMIDLIST pidl)
949 {
950     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder, iface);
951     
952     TRACE("(iface=%p, pidl=%p)\n", iface, pidl);
953
954     This->m_pidlLocation = ILClone(pidl);
955     UNIXFS_pidl_to_path(pidl, &This->m_pszPath);
956     UNIXFS_build_subfolder_pidls(This->m_pszPath, &This->m_apidlSubDirs, &This->m_cSubDirs);
957     
958     return S_OK;
959 }
960
961 /* VTable for UnixFolder's IPersistFolder interface.
962  */
963 static const IPersistFolderVtbl UnixFolder_IPersistFolder_Vtbl = {
964     UnixFolder_IPersistFolder_QueryInterface,
965     UnixFolder_IPersistFolder_AddRef,
966     UnixFolder_IPersistFolder_Release,
967     UnixFolder_IPersistFolder_GetClassID,
968     UnixFolder_IPersistFolder_Initialize
969 };
970
971 /******************************************************************************
972  * UnixFolder_Constructor [Internal]
973  *
974  * PARAMS
975  *  pUnkOuter [I] Outer class for aggregation. Currently ignored.
976  *  riid      [I] Interface asked for by the client.
977  *  ppv       [O] Pointer to an riid interface to the UnixFolder object.
978  *
979  * NOTES
980  *  This is the only function exported from shfldr_unixfs.c. It's called from
981  *  shellole.c's default class factory and thus has to exhibit a LPFNCREATEINSTANCE
982  *  compatible signature.
983  */
984 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
985     HRESULT hr;
986     UnixFolder *pUnixFolder;
987     
988     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
989
990     pUnixFolder = SHAlloc((ULONG)sizeof(UnixFolder));
991     pUnixFolder->lpIShellFolder2Vtbl = &UnixFolder_IShellFolder2_Vtbl;
992     pUnixFolder->lpIPersistFolderVtbl = &UnixFolder_IPersistFolder_Vtbl;
993     pUnixFolder->m_cRef = 0;
994     pUnixFolder->m_pszPath = NULL;
995     pUnixFolder->m_apidlSubDirs = NULL;
996     pUnixFolder->m_cSubDirs = 0;
997
998     UnixFolder_IShellFolder2_AddRef(STATIC_CAST(IShellFolder2, pUnixFolder));
999     hr = UnixFolder_IShellFolder2_QueryInterface(STATIC_CAST(IShellFolder2, pUnixFolder), riid, ppv);
1000     UnixFolder_IShellFolder2_Release(STATIC_CAST(IShellFolder2, pUnixFolder));
1001     return hr;
1002 }
1003
1004 /******************************************************************************
1005  * UnixSubFolderIterator
1006  *
1007  * Class whose heap based objects represent iterators over the sub-directories
1008  * of a given UnixFolder object. 
1009  */
1010
1011 /* UnixSubFolderIterator object layout and typedef.
1012  */
1013 typedef struct _UnixSubFolderIterator {
1014     const IEnumIDListVtbl *lpIEnumIDListVtbl;
1015     ULONG m_cRef;
1016     UnixFolder *m_pUnixFolder;
1017     ULONG m_cIdx;
1018     SHCONTF m_fFilter;
1019 } UnixSubFolderIterator;
1020
1021 static void UnixSubFolderIterator_Destroy(UnixSubFolderIterator *iterator) {
1022     TRACE("(iterator=%p)\n", iterator);
1023         
1024     UnixFolder_IShellFolder2_Release((IShellFolder2*)iterator->m_pUnixFolder);
1025     SHFree(iterator);
1026 }
1027
1028 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_QueryInterface(IEnumIDList* iface, 
1029     REFIID riid, void** ppv)
1030 {
1031     TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
1032     
1033     if (!ppv) return E_INVALIDARG;
1034     
1035     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IEnumIDList, riid)) {
1036         *ppv = iface;
1037     } else {
1038         *ppv = NULL;
1039         return E_NOINTERFACE;
1040     }
1041
1042     IEnumIDList_AddRef(iface);
1043     return S_OK;
1044 }
1045                             
1046 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_AddRef(IEnumIDList* iface)
1047 {
1048     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1049
1050     TRACE("(iface=%p)\n", iface);
1051    
1052     return InterlockedIncrement(&This->m_cRef);
1053 }
1054
1055 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_Release(IEnumIDList* iface)
1056 {
1057     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1058     ULONG cRef;
1059     
1060     TRACE("(iface=%p)\n", iface);
1061
1062     cRef = InterlockedDecrement(&This->m_cRef);
1063     
1064     if (!cRef) 
1065         UnixSubFolderIterator_Destroy(This);
1066
1067     return cRef;
1068 }
1069
1070 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Next(IEnumIDList* iface, ULONG celt, 
1071     LPITEMIDLIST* rgelt, ULONG* pceltFetched)
1072 {
1073     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1074     ULONG i;
1075
1076     TRACE("(iface=%p, celt=%ld, rgelt=%p, pceltFetched=%p)\n", iface, celt, rgelt, pceltFetched);
1077    
1078     for (i=0; (i < celt) && (This->m_cIdx < This->m_pUnixFolder->m_cSubDirs); This->m_cIdx++) {
1079         LPITEMIDLIST pCurrent = This->m_pUnixFolder->m_apidlSubDirs[This->m_cIdx];
1080         if (UNIXFS_is_pidl_of_type(pCurrent, This->m_fFilter)) {
1081             rgelt[i] = ILClone(pCurrent);
1082             i++;
1083         }
1084     }
1085
1086     if (pceltFetched)
1087         *pceltFetched = i;
1088
1089     return i == celt ? S_OK : S_FALSE;
1090 }
1091
1092 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Skip(IEnumIDList* iface, ULONG celt)
1093 {
1094     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1095     ULONG i;
1096     
1097     TRACE("(iface=%p, celt=%ld)\n", iface, celt);
1098
1099     for (i=0; i < celt; i++) {
1100         while (This->m_cIdx < This->m_pUnixFolder->m_cSubDirs &&
1101                !UNIXFS_is_pidl_of_type(This->m_pUnixFolder->m_apidlSubDirs[This->m_cIdx], This->m_fFilter)) 
1102         {
1103             This->m_cIdx++;
1104         }
1105         This->m_cIdx++;
1106     }
1107     
1108     if (This->m_cIdx > This->m_pUnixFolder->m_cSubDirs) {
1109         This->m_cIdx = This->m_pUnixFolder->m_cSubDirs;
1110         return S_FALSE;
1111     } else {
1112         return S_OK;
1113     }
1114 }
1115
1116 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Reset(IEnumIDList* iface)
1117 {
1118     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1119         
1120     TRACE("(iface=%p)\n", iface);
1121
1122     This->m_cIdx = 0;
1123     
1124     return S_OK;
1125 }
1126
1127 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Clone(IEnumIDList* This, 
1128     IEnumIDList** ppenum)
1129 {
1130     TRACE("stub\n");
1131     return E_NOTIMPL;
1132 }
1133
1134 /* VTable for UnixSubFolderIterator's IEnumIDList interface.
1135  */
1136 static const IEnumIDListVtbl UnixSubFolderIterator_IEnumIDList_Vtbl = {
1137     UnixSubFolderIterator_IEnumIDList_QueryInterface,
1138     UnixSubFolderIterator_IEnumIDList_AddRef,
1139     UnixSubFolderIterator_IEnumIDList_Release,
1140     UnixSubFolderIterator_IEnumIDList_Next,
1141     UnixSubFolderIterator_IEnumIDList_Skip,
1142     UnixSubFolderIterator_IEnumIDList_Reset,
1143     UnixSubFolderIterator_IEnumIDList_Clone
1144 };
1145
1146 static IUnknown *UnixSubFolderIterator_Construct(UnixFolder *pUnixFolder, SHCONTF fFilter) {
1147     UnixSubFolderIterator *iterator;
1148
1149     TRACE("(pUnixFolder=%p)\n", pUnixFolder);
1150     
1151     iterator = SHAlloc((ULONG)sizeof(UnixSubFolderIterator));
1152     iterator->lpIEnumIDListVtbl = &UnixSubFolderIterator_IEnumIDList_Vtbl;
1153     iterator->m_cRef = 0;
1154     iterator->m_cIdx = 0;
1155     iterator->m_pUnixFolder = pUnixFolder;
1156     iterator->m_fFilter = fFilter;
1157
1158     UnixSubFolderIterator_IEnumIDList_AddRef((IEnumIDList*)iterator);
1159     UnixFolder_IShellFolder2_AddRef((IShellFolder2*)pUnixFolder);
1160     
1161     return (IUnknown*)iterator;
1162 }