fb80e38679087bcac3dfd3fcbe6e8884d22adbdc
[p5sagit/p5-mst-13.2.git] / win32 / vdir.h
1 /* vdir.h
2  *
3  * (c) 1999 Microsoft Corporation. All rights reserved. 
4  * Portions (c) 1999 ActiveState Tool Corp, http://www.ActiveState.com/
5  *
6  *    You may distribute under the terms of either the GNU General Public
7  *    License or the Artistic License, as specified in the README file.
8  */
9
10 #ifndef ___VDir_H___
11 #define ___VDir_H___
12
13 /*
14  * Allow one slot for each possible drive letter
15  * and one additional slot for a UNC name
16  */
17 const int driveCount = ('Z'-'A')+1+1;
18
19 class VDir
20 {
21 public:
22     VDir(int bManageDir = 1);
23     ~VDir() {};
24
25     void Init(VDir* pDir, VMem *pMem);
26     void SetDefaultA(char const *pDefault);
27     void SetDefaultW(WCHAR const *pDefault);
28     char* MapPathA(const char *pInName);
29     WCHAR* MapPathW(const WCHAR *pInName);
30     int SetCurrentDirectoryA(char *lpBuffer);
31     int SetCurrentDirectoryW(WCHAR *lpBuffer);
32     inline int GetDefault(void) { return nDefault; };
33
34     inline char* GetCurrentDirectoryA(int dwBufSize, char *lpBuffer)
35     {
36         char* ptr = dirTableA[nDefault];
37         while (--dwBufSize)
38         {
39             if ((*lpBuffer++ = *ptr++) == '\0')
40                 break;
41         }
42         *lpBuffer = '\0';
43         return /* unused */ NULL;
44     };
45     inline WCHAR* GetCurrentDirectoryW(int dwBufSize, WCHAR *lpBuffer)
46     {
47         WCHAR* ptr = dirTableW[nDefault];
48         while (--dwBufSize)
49         {
50             if ((*lpBuffer++ = *ptr++) == '\0')
51                 break;
52         }
53         *lpBuffer = '\0';
54         return /* unused */ NULL;
55     };
56
57     DWORD CalculateEnvironmentSpace(void);
58     LPSTR BuildEnvironmentSpace(LPSTR lpStr);
59
60 protected:
61     int SetDirA(char const *pPath, int index);
62     int SetDirW(WCHAR const *pPath, int index);
63     void FromEnvA(char *pEnv, int index);
64     void FromEnvW(WCHAR *pEnv, int index);
65
66     inline const char *GetDefaultDirA(void)
67     {
68         return dirTableA[nDefault];
69     };
70     inline void SetDefaultDirA(char const *pPath, int index)
71     {
72         SetDirA(pPath, index);
73         nDefault = index;
74     };
75     inline const WCHAR *GetDefaultDirW(void)
76     {
77         return dirTableW[nDefault];
78     };
79     inline void SetDefaultDirW(WCHAR const *pPath, int index)
80     {
81         SetDirW(pPath, index);
82         nDefault = index;
83     };
84     inline const char *GetDirA(int index)
85     {
86         char *ptr = dirTableA[index];
87         if (!ptr) {
88             /* simulate the existance of this drive */
89             ptr = szLocalBufferA;
90             ptr[0] = 'A' + index;
91             ptr[1] = ':';
92             ptr[2] = '\\';
93             ptr[3] = 0;
94         }
95         return ptr;
96     };
97     inline const WCHAR *GetDirW(int index)
98     {
99         WCHAR *ptr = dirTableW[index];
100         if (!ptr) {
101             /* simulate the existance of this drive */
102             ptr = szLocalBufferW;
103             ptr[0] = 'A' + index;
104             ptr[1] = ':';
105             ptr[2] = '\\';
106             ptr[3] = 0;
107         }
108         return ptr;
109     };
110
111     inline int DriveIndex(char chr)
112     {
113         if (chr == '\\' || chr == '/')
114             return ('Z'-'A')+1;
115         return (chr | 0x20)-'a';
116     };
117
118     VMem *pMem;
119     int nDefault, bManageDirectory;
120     char *dirTableA[driveCount];
121     char szLocalBufferA[MAX_PATH+1];
122     WCHAR *dirTableW[driveCount];
123     WCHAR szLocalBufferW[MAX_PATH+1];
124 };
125
126
127 VDir::VDir(int bManageDir /* = 1 */)
128 {
129     nDefault = 0;
130     bManageDirectory = bManageDir;
131     memset(dirTableA, 0, sizeof(dirTableA));
132     memset(dirTableW, 0, sizeof(dirTableW));
133 }
134
135 void VDir::Init(VDir* pDir, VMem *p)
136 {
137     int index;
138
139     pMem = p;
140     if (pDir) {
141         for (index = 0; index < driveCount; ++index) {
142             SetDirW(pDir->GetDirW(index), index);
143         }
144         nDefault = pDir->GetDefault();
145     }
146     else {
147         int bSave = bManageDirectory;
148         DWORD driveBits = GetLogicalDrives();
149         OSVERSIONINFO osver;
150
151         memset(&osver, 0, sizeof(osver));
152         osver.dwOSVersionInfoSize = sizeof(osver);
153         GetVersionEx(&osver);
154
155         bManageDirectory = 0;
156         if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) {
157             char szBuffer[MAX_PATH*driveCount];
158             if (GetLogicalDriveStringsA(sizeof(szBuffer), szBuffer)) {
159                 char* pEnv = (char*)GetEnvironmentStringsA();
160                 char* ptr = szBuffer;
161                 for (index = 0; index < driveCount; ++index) {
162                     if (driveBits & (1<<index)) {
163                         ptr += SetDirA(ptr, index) + 1;
164                         FromEnvA(pEnv, index);
165                     }
166                 }
167                 FreeEnvironmentStringsA(pEnv);
168             }
169             SetDefaultA(".");
170         }
171         else { /* Windows NT or later */
172             WCHAR szBuffer[MAX_PATH*driveCount];
173             if (GetLogicalDriveStringsW(sizeof(szBuffer), szBuffer)) {
174                 WCHAR* pEnv = GetEnvironmentStringsW();
175                 WCHAR* ptr = szBuffer;
176                 for (index = 0; index < driveCount; ++index) {
177                     if (driveBits & (1<<index)) {
178                         ptr += SetDirW(ptr, index) + 1;
179                         FromEnvW(pEnv, index);
180                     }
181                 }
182                 FreeEnvironmentStringsW(pEnv);
183             }
184             SetDefaultW(L".");
185         }
186         bManageDirectory = bSave;
187   }
188 }
189
190 int VDir::SetDirA(char const *pPath, int index)
191 {
192     char chr, *ptr;
193     int length = 0;
194     WCHAR wBuffer[MAX_PATH+1];
195     if (index < driveCount && pPath != NULL) {
196         length = strlen(pPath);
197         pMem->Free(dirTableA[index]);
198         ptr = dirTableA[index] = (char*)pMem->Malloc(length+2);
199         if (ptr != NULL) {
200             strcpy(ptr, pPath);
201             ptr += length-1;
202             chr = *ptr++;
203             if (chr != '\\' && chr != '/') {
204                 *ptr++ = '\\';
205                 *ptr = '\0';
206             }
207             MultiByteToWideChar(CP_ACP, 0, dirTableA[index], -1,
208                     wBuffer, (sizeof(wBuffer)/sizeof(WCHAR)));
209             length = wcslen(wBuffer);
210             pMem->Free(dirTableW[index]);
211             dirTableW[index] = (WCHAR*)pMem->Malloc((length+1)*2);
212             if (dirTableW[index] != NULL) {
213                 wcscpy(dirTableW[index], wBuffer);
214             }
215         }
216     }
217
218     if(bManageDirectory)
219         ::SetCurrentDirectoryA(pPath);
220
221     return length;
222 }
223
224 void VDir::FromEnvA(char *pEnv, int index)
225 {   /* gets the directory for index from the environment variable. */
226     while (*pEnv != '\0') {
227         if ((pEnv[0] == '=') && (DriveIndex(pEnv[1]) == index)) {
228             SetDirA(&pEnv[4], index);
229             break;
230         }
231         else
232             pEnv += strlen(pEnv)+1;
233     }
234 }
235
236 void VDir::FromEnvW(WCHAR *pEnv, int index)
237 {   /* gets the directory for index from the environment variable. */
238     while (*pEnv != '\0') {
239         if ((pEnv[0] == '=') && (DriveIndex((char)pEnv[1]) == index)) {
240             SetDirW(&pEnv[4], index);
241             break;
242         }
243         else
244             pEnv += wcslen(pEnv)+1;
245     }
246 }
247
248 void VDir::SetDefaultA(char const *pDefault)
249 {
250     char szBuffer[MAX_PATH+1];
251     char *pPtr;
252
253     if (GetFullPathNameA(pDefault, sizeof(szBuffer), szBuffer, &pPtr)) {
254         if (*pDefault != '.' && pPtr != NULL)
255             *pPtr = '\0';
256
257         SetDefaultDirA(szBuffer, DriveIndex(szBuffer[0]));
258     }
259 }
260
261 int VDir::SetDirW(WCHAR const *pPath, int index)
262 {
263     WCHAR chr, *ptr;
264     char szBuffer[MAX_PATH+1];
265     int length = 0;
266     if (index < driveCount && pPath != NULL) {
267         length = wcslen(pPath);
268         pMem->Free(dirTableW[index]);
269         ptr = dirTableW[index] = (WCHAR*)pMem->Malloc((length+2)*2);
270         if (ptr != NULL) {
271             wcscpy(ptr, pPath);
272             ptr += length-1;
273             chr = *ptr++;
274             if (chr != '\\' && chr != '/') {
275                 *ptr++ = '\\';
276                 *ptr = '\0';
277             }
278             WideCharToMultiByte(CP_ACP, 0, dirTableW[index], -1, szBuffer, sizeof(szBuffer), NULL, NULL);
279             length = strlen(szBuffer);
280             pMem->Free(dirTableA[index]);
281             dirTableA[index] = (char*)pMem->Malloc(length+1);
282             if (dirTableA[index] != NULL) {
283                 strcpy(dirTableA[index], szBuffer);
284             }
285         }
286     }
287
288     if(bManageDirectory)
289         ::SetCurrentDirectoryW(pPath);
290
291     return length;
292 }
293
294 void VDir::SetDefaultW(WCHAR const *pDefault)
295 {
296     WCHAR szBuffer[MAX_PATH+1];
297     WCHAR *pPtr;
298
299     if (GetFullPathNameW(pDefault, (sizeof(szBuffer)/sizeof(WCHAR)), szBuffer, &pPtr)) {
300         if (*pDefault != '.' && pPtr != NULL)
301             *pPtr = '\0';
302
303         SetDefaultDirW(szBuffer, DriveIndex((char)szBuffer[0]));
304     }
305 }
306
307 inline BOOL IsPathSep(char ch)
308 {
309     return (ch == '\\' || ch == '/');
310 }
311
312 inline void DoGetFullPathNameA(char* lpBuffer, DWORD dwSize, char* Dest)
313 {
314     char *pPtr;
315
316     /*
317      * On WinNT GetFullPathName does not fail, (or at least always
318      * succeeds when the drive is valid) WinNT does set *Dest to Nullch
319      * On Win98 GetFullPathName will set last error if it fails, but
320      * does not touch *Dest
321      */
322     *Dest = '\0';
323     GetFullPathNameA(lpBuffer, dwSize, Dest, &pPtr);
324 }
325
326 inline bool IsSpecialFileName(const char* pName)
327 {
328     /* specical file names are devices that the system can open
329      * these include AUX, CON, NUL, PRN, COMx, LPTx, CLOCK$, CONIN$, CONOUT$
330      * (x is a single digit, and names are case-insensitive)
331      */
332     char ch = (pName[0] & ~0x20);
333     switch (ch)
334     {
335         case 'A': /* AUX */
336             if (((pName[1] & ~0x20) == 'U')
337                 && ((pName[2] & ~0x20) == 'X')
338                 && !pName[3])
339                     return true;
340             break;
341         case 'C': /* CLOCK$, COMx,  CON, CONIN$ CONOUT$ */
342             ch = (pName[1] & ~0x20);
343             switch (ch)
344             {
345                 case 'L': /* CLOCK$ */
346                     if (((pName[2] & ~0x20) == 'O')
347                         && ((pName[3] & ~0x20) == 'C')
348                         && ((pName[4] & ~0x20) == 'K')
349                         && (pName[5] == '$')
350                         && !pName[6])
351                             return true;
352                     break;
353                 case 'O': /* COMx,  CON, CONIN$ CONOUT$ */
354                     if ((pName[2] & ~0x20) == 'M') {
355                         if ((pName[3] >= '1') && (pName[3] <= '9')
356                             && !pName[4])
357                             return true;
358                     }
359                     else if ((pName[2] & ~0x20) == 'N') {
360                         if (!pName[3])
361                             return true;
362                         else if ((pName[3] & ~0x20) == 'I') {
363                             if (((pName[4] & ~0x20) == 'N')
364                                 && (pName[5] == '$')
365                                 && !pName[6])
366                             return true;
367                         }
368                         else if ((pName[3] & ~0x20) == 'O') {
369                             if (((pName[4] & ~0x20) == 'U')
370                                 && ((pName[5] & ~0x20) == 'T')
371                                 && (pName[6] == '$')
372                                 && !pName[7])
373                             return true;
374                         }
375                     }
376                     break;
377             }
378             break;
379         case 'L': /* LPTx */
380             if (((pName[1] & ~0x20) == 'U')
381                 && ((pName[2] & ~0x20) == 'X')
382                 && (pName[3] >= '1') && (pName[3] <= '9')
383                 && !pName[4])
384                     return true;
385             break;
386         case 'N': /* NUL */
387             if (((pName[1] & ~0x20) == 'U')
388                 && ((pName[2] & ~0x20) == 'L')
389                 && !pName[3])
390                     return true;
391             break;
392         case 'P': /* PRN */
393             if (((pName[1] & ~0x20) == 'R')
394                 && ((pName[2] & ~0x20) == 'N')
395                 && !pName[3])
396                     return true;
397             break;
398     }
399     return false;
400 }
401
402 char *VDir::MapPathA(const char *pInName)
403 {   /*
404      * possiblities -- relative path or absolute path with or without drive letter
405      * OR UNC name
406      */
407     char szBuffer[(MAX_PATH+1)*2];
408     char szlBuf[MAX_PATH+1];
409     int length = strlen(pInName);
410
411     if (!length)
412         return (char*)pInName;
413
414     if (length > MAX_PATH) {
415         strncpy(szlBuf, pInName, MAX_PATH);
416         if (IsPathSep(pInName[0]) && !IsPathSep(pInName[1])) {   
417             /* absolute path - reduce length by 2 for drive specifier */
418             szlBuf[MAX_PATH-2] = '\0';
419         }
420         else
421             szlBuf[MAX_PATH] = '\0';
422         pInName = szlBuf;
423     }
424     /* strlen(pInName) is now <= MAX_PATH */
425
426     if (pInName[1] == ':') {
427         /* has drive letter */
428         if (IsPathSep(pInName[2])) {
429             /* absolute with drive letter */
430             DoGetFullPathNameA((char*)pInName, sizeof(szLocalBufferA), szLocalBufferA);
431         }
432         else {
433             /* relative path with drive letter */
434             strcpy(szBuffer, GetDirA(DriveIndex(*pInName)));
435             strcat(szBuffer, &pInName[2]);
436             if(strlen(szBuffer) > MAX_PATH)
437                 szBuffer[MAX_PATH] = '\0';
438
439             DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
440         }
441     }
442     else {
443         /* no drive letter */
444         if (IsPathSep(pInName[1]) && IsPathSep(pInName[0])) {
445             /* UNC name */
446             DoGetFullPathNameA((char*)pInName, sizeof(szLocalBufferA), szLocalBufferA);
447         }
448         else {
449             strcpy(szBuffer, GetDefaultDirA());
450             if (IsPathSep(pInName[0])) {
451                 /* absolute path */
452                 strcpy(&szBuffer[2], pInName);
453                 DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
454             }
455             else {
456                 /* relative path */
457                 if (IsSpecialFileName(pInName)) {
458                     return (char*)pInName;
459                 }
460                 else {
461                     strcat(szBuffer, pInName);
462                     if (strlen(szBuffer) > MAX_PATH)
463                         szBuffer[MAX_PATH] = '\0';
464
465                     DoGetFullPathNameA(szBuffer, sizeof(szLocalBufferA), szLocalBufferA);
466                 }
467             }
468         }
469     }
470
471     return szLocalBufferA;
472 }
473
474 int VDir::SetCurrentDirectoryA(char *lpBuffer)
475 {
476     char *pPtr;
477     int length, nRet = -1;
478
479     pPtr = MapPathA(lpBuffer);
480     length = strlen(pPtr);
481     if(length > 3 && IsPathSep(pPtr[length-1])) {
482         /* don't remove the trailing slash from 'x:\'  */
483         pPtr[length-1] = '\0';
484     }
485
486     DWORD r = GetFileAttributesA(pPtr);
487     if ((r != 0xffffffff) && (r & FILE_ATTRIBUTE_DIRECTORY))
488     {
489         char szBuffer[(MAX_PATH+1)*2];
490         DoGetFullPathNameA(pPtr, sizeof(szBuffer), szBuffer);
491         SetDefaultDirA(szBuffer, DriveIndex(szBuffer[0]));
492         nRet = 0;
493     }
494
495     return nRet;
496 }
497
498 DWORD VDir::CalculateEnvironmentSpace(void)
499 {   /* the current directory environment strings are stored as '=D:=d:\path' */
500     int index;
501     DWORD dwSize = 0;
502     for (index = 0; index < driveCount; ++index) {
503         if (dirTableA[index] != NULL) {
504             dwSize += strlen(dirTableA[index]) + 5;  /* add 1 for trailing NULL and 4 for '=D:=' */
505         }
506     }
507     return dwSize;
508 }
509
510 LPSTR VDir::BuildEnvironmentSpace(LPSTR lpStr)
511 {   /* store the current directory environment strings as '=D:=d:\path' */
512     int index, length;
513     LPSTR lpDirStr;
514     for (index = 0; index < driveCount; ++index) {
515         lpDirStr = dirTableA[index];
516         if (lpDirStr != NULL) {
517             lpStr[0] = '=';
518             lpStr[1] = lpDirStr[0];
519             lpStr[2] = '\0';
520             CharUpper(&lpStr[1]);
521             lpStr[2] = ':';
522             lpStr[3] = '=';
523             strcpy(&lpStr[4], lpDirStr);
524             length = strlen(lpDirStr);
525             lpStr += length + 5; /* add 1 for trailing NULL and 4 for '=D:=' */
526             if (length > 3 && IsPathSep(lpStr[-2])) {
527                 lpStr[-2] = '\0';   /* remove the trailing path separator */
528                 --lpStr;
529             }
530         }
531     }
532     return lpStr;
533 }
534
535 inline BOOL IsPathSep(WCHAR ch)
536 {
537     return (ch == '\\' || ch == '/');
538 }
539
540 inline void DoGetFullPathNameW(WCHAR* lpBuffer, DWORD dwSize, WCHAR* Dest)
541 {
542     WCHAR *pPtr;
543
544     /*
545      * On WinNT GetFullPathName does not fail, (or at least always
546      * succeeds when the drive is valid) WinNT does set *Dest to Nullch
547      * On Win98 GetFullPathName will set last error if it fails, but
548      * does not touch *Dest
549      */
550     *Dest = '\0';
551     GetFullPathNameW(lpBuffer, dwSize, Dest, &pPtr);
552 }
553
554 inline bool IsSpecialFileName(const WCHAR* pName)
555 {
556     /* specical file names are devices that the system can open
557      * these include AUX, CON, NUL, PRN, COMx, LPTx, CLOCK$, CONIN$, CONOUT$
558      * (x is a single digit, and names are case-insensitive)
559      */
560     WCHAR ch = (pName[0] & ~0x20);
561     switch (ch)
562     {
563         case 'A': /* AUX */
564             if (((pName[1] & ~0x20) == 'U')
565                 && ((pName[2] & ~0x20) == 'X')
566                 && !pName[3])
567                     return true;
568             break;
569         case 'C': /* CLOCK$, COMx,  CON, CONIN$ CONOUT$ */
570             ch = (pName[1] & ~0x20);
571             switch (ch)
572             {
573                 case 'L': /* CLOCK$ */
574                     if (((pName[2] & ~0x20) == 'O')
575                         && ((pName[3] & ~0x20) == 'C')
576                         && ((pName[4] & ~0x20) == 'K')
577                         && (pName[5] == '$')
578                         && !pName[6])
579                             return true;
580                     break;
581                 case 'O': /* COMx,  CON, CONIN$ CONOUT$ */
582                     if ((pName[2] & ~0x20) == 'M') {
583                         if ((pName[3] >= '1') && (pName[3] <= '9')
584                             && !pName[4])
585                             return true;
586                     }
587                     else if ((pName[2] & ~0x20) == 'N') {
588                         if (!pName[3])
589                             return true;
590                         else if ((pName[3] & ~0x20) == 'I') {
591                             if (((pName[4] & ~0x20) == 'N')
592                                 && (pName[5] == '$')
593                                 && !pName[6])
594                             return true;
595                         }
596                         else if ((pName[3] & ~0x20) == 'O') {
597                             if (((pName[4] & ~0x20) == 'U')
598                                 && ((pName[5] & ~0x20) == 'T')
599                                 && (pName[6] == '$')
600                                 && !pName[7])
601                             return true;
602                         }
603                     }
604                     break;
605             }
606             break;
607         case 'L': /* LPTx */
608             if (((pName[1] & ~0x20) == 'U')
609                 && ((pName[2] & ~0x20) == 'X')
610                 && (pName[3] >= '1') && (pName[3] <= '9')
611                 && !pName[4])
612                     return true;
613             break;
614         case 'N': /* NUL */
615             if (((pName[1] & ~0x20) == 'U')
616                 && ((pName[2] & ~0x20) == 'L')
617                 && !pName[3])
618                     return true;
619             break;
620         case 'P': /* PRN */
621             if (((pName[1] & ~0x20) == 'R')
622                 && ((pName[2] & ~0x20) == 'N')
623                 && !pName[3])
624                     return true;
625             break;
626     }
627     return false;
628 }
629
630 WCHAR* VDir::MapPathW(const WCHAR *pInName)
631 {   /*
632      * possiblities -- relative path or absolute path with or without drive letter
633      * OR UNC name
634      */
635     WCHAR szBuffer[(MAX_PATH+1)*2];
636     WCHAR szlBuf[MAX_PATH+1];
637     int length = wcslen(pInName);
638
639     if (!length)
640         return (WCHAR*)pInName;
641
642     if (length > MAX_PATH) {
643         wcsncpy(szlBuf, pInName, MAX_PATH);
644         if (IsPathSep(pInName[0]) && !IsPathSep(pInName[1])) {   
645             /* absolute path - reduce length by 2 for drive specifier */
646             szlBuf[MAX_PATH-2] = '\0';
647         }
648         else
649             szlBuf[MAX_PATH] = '\0';
650         pInName = szlBuf;
651     }
652     /* strlen(pInName) is now <= MAX_PATH */
653
654     if (pInName[1] == ':') {
655         /* has drive letter */
656         if (IsPathSep(pInName[2])) {
657             /* absolute with drive letter */
658             DoGetFullPathNameW((WCHAR*)pInName, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
659         }
660         else {
661             /* relative path with drive letter */
662             wcscpy(szBuffer, GetDirW(DriveIndex((char)*pInName)));
663             wcscat(szBuffer, &pInName[2]);
664             if(wcslen(szBuffer) > MAX_PATH)
665                 szBuffer[MAX_PATH] = '\0';
666
667             DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
668         }
669     }
670     else {
671         /* no drive letter */
672         if (IsPathSep(pInName[1]) && IsPathSep(pInName[0])) {
673             /* UNC name */
674             DoGetFullPathNameW((WCHAR*)pInName, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
675         }
676         else {
677             wcscpy(szBuffer, GetDefaultDirW());
678             if (IsPathSep(pInName[0])) {
679                 /* absolute path */
680                 wcscpy(&szBuffer[2], pInName);
681                 DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
682             }
683             else {
684                 /* relative path */
685                 if (IsSpecialFileName(pInName)) {
686                     return (WCHAR*)pInName;
687                 }
688                 else {
689                     wcscat(szBuffer, pInName);
690                     if (wcslen(szBuffer) > MAX_PATH)
691                         szBuffer[MAX_PATH] = '\0';
692
693                     DoGetFullPathNameW(szBuffer, (sizeof(szLocalBufferW)/sizeof(WCHAR)), szLocalBufferW);
694                 }
695             }
696         }
697     }
698     return szLocalBufferW;
699 }
700
701 int VDir::SetCurrentDirectoryW(WCHAR *lpBuffer)
702 {
703     WCHAR *pPtr;
704     int length, nRet = -1;
705
706     pPtr = MapPathW(lpBuffer);
707     length = wcslen(pPtr);
708     if(length > 3 && IsPathSep(pPtr[length-1])) {
709         /* don't remove the trailing slash from 'x:\'  */
710         pPtr[length-1] = '\0';
711     }
712
713     DWORD r = GetFileAttributesW(pPtr);
714     if ((r != 0xffffffff) && (r & FILE_ATTRIBUTE_DIRECTORY))
715     {
716         WCHAR wBuffer[(MAX_PATH+1)*2];
717         DoGetFullPathNameW(pPtr, (sizeof(wBuffer)/sizeof(WCHAR)), wBuffer);
718         SetDefaultDirW(wBuffer, DriveIndex((char)wBuffer[0]));
719         nRet = 0;
720     }
721
722     return nRet;
723 }
724
725 #endif  /* ___VDir_H___ */