source: trunk/src/kernel32/directory.cpp@ 7029

Last change on this file since 7029 was 6511, checked in by sandervl, 24 years ago

minor updates

File size: 21.7 KB
Line 
1/* $Id: directory.cpp,v 1.39 2001-08-10 19:32:26 sandervl Exp $ */
2
3/*
4 * Win32 Directory functions for OS/2
5 *
6 * Copyright 1998 Sander van Leeuwen
7 *
8 * NOTE: Directory creation has to be done in install program (odin\win)
9 *
10 * Parts based on Wine code (991031) (files\directory.c)
11 *
12 * DOS directories functions
13 *
14 * Copyright 1995 Alexandre Julliard
15 *
16 * TODO:
17 * - System/window directories should be created by install program!
18 *
19 * Project Odin Software License can be found in LICENSE.TXT
20 *
21 */
22
23
24/*****************************************************************************
25 * Includes *
26 *****************************************************************************/
27
28#include <odin.h>
29#include <odinwrap.h>
30#include <os2win.h>
31#include <stdlib.h>
32#include <unicode.h>
33#include <heapstring.h>
34#include <options.h>
35#include "initterm.h"
36#include <win\file.h>
37#include <string.h>
38#include "oslibdos.h"
39#include "profile.h"
40#include "fileio.h"
41
42#define DBG_LOCALLOG DBG_directory
43#include "dbglocal.h"
44
45ODINDEBUGCHANNEL(KERNEL32-DIRECTORY)
46
47
48/*****************************************************************************
49 * Local Prototypes *
50 *****************************************************************************/
51
52
53static char DIR_Windows[MAX_PATHNAME_LEN];
54static char DIR_System[MAX_PATHNAME_LEN];
55
56//******************************************************************************
57//******************************************************************************
58char *InternalGetWindowsDirectoryA()
59{
60 return DIR_Windows;
61}
62//******************************************************************************
63//******************************************************************************
64char *InternalGetSystemDirectoryA()
65{
66 return DIR_System;
67}
68//******************************************************************************
69//******************************************************************************
70void InitDirectories()
71{
72 char *endofwinpath, *tmp;
73 int len;
74
75 strcpy(DIR_System, kernel32Path);
76 len = strlen(DIR_System);
77 if(DIR_System[len-1] == '\\') {
78 DIR_System[len-1] = 0;
79 }
80 len = PROFILE_GetOdinIniString(ODINDIRECTORIES,"WINDOWS","",DIR_Windows,sizeof(DIR_Windows));
81 if (len > 2) {
82 if(DIR_Windows[len-1] == '\\') {
83 DIR_Windows[len-1] = 0;
84 }
85 }
86 else {
87 strcpy(DIR_Windows, DIR_System);
88 endofwinpath = tmp = strchr(DIR_Windows, '\\');
89 while(tmp) {
90 tmp = strchr(endofwinpath+1, '\\');
91 if(tmp)
92 endofwinpath = tmp;
93 }
94 if(endofwinpath) {
95 *endofwinpath = 0; //remove \SYSTEM32
96 }
97 else DebugInt3();
98 }
99 dprintf(("Windows dir: %s", DIR_Windows));
100 dprintf(("System32 dir: %s", DIR_System));
101}
102/*****************************************************************************
103 * Name : GetCurrentDirectoryA
104 * Purpose : query the current directory
105 * Parameters:
106 * Variables :
107 * Result :
108 * Remark : returned length is number of characters required or used for current dir
109 * *excluding* terminator
110 * Status :
111 *
112 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
113 *****************************************************************************/
114
115ODINFUNCTION2(UINT, GetCurrentDirectoryA, UINT, nBufferLength,
116 LPSTR, lpBuffer)
117{
118 UINT rc;
119
120 rc = OSLibDosQueryDir(nBufferLength, lpBuffer);
121 if(rc && rc < nBufferLength) {
122 dprintf(("CurrentDirectory = %s (%d)", lpBuffer, rc));
123 }
124 else dprintf(("CurrentDirectory returned %d", rc));
125 return rc;
126}
127
128
129/*****************************************************************************
130 * Name : GetCurrentDirectoryW
131 * Purpose : query the current directory
132 * Parameters:
133 * Variables :
134 * Result :
135 * Remark :
136 * Status :
137 *
138 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
139 *****************************************************************************/
140
141ODINFUNCTION2(UINT, GetCurrentDirectoryW, UINT, nBufferLength,
142 LPWSTR, lpBuffer)
143{
144 char *asciidir = (char *)malloc(nBufferLength+1);
145 int rc;
146
147 rc = CALL_ODINFUNC(GetCurrentDirectoryA)(nBufferLength, asciidir);
148 if(rc != 0)
149 AsciiToUnicode(asciidir, lpBuffer);
150 free(asciidir);
151 return(rc);
152}
153
154
155/*****************************************************************************
156 * Name : SetCurrentDirectoryA
157 * Purpose :
158 * Parameters:
159 * Variables :
160 * Result :
161 * Remark :
162 * Status :
163 *
164 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
165 *****************************************************************************/
166
167
168ODINFUNCTION1(BOOL, SetCurrentDirectoryA,
169 LPCSTR, lpstrDirectory)
170{
171 if(HIWORD(lpstrDirectory) == 0)
172 {
173 SetLastError(ERROR_INVALID_PARAMETER);
174 return FALSE;
175 }
176
177 // cut off trailing backslashes
178 // not if a process wants to change to the root directory
179 int len = lstrlenA(lpstrDirectory);
180 if ( ( (lpstrDirectory[len - 1] == '\\') ||
181 (lpstrDirectory[len - 1] == '/') ) &&
182 (len != 1) )
183 {
184 LPSTR lpTemp = (LPSTR)_alloca(len);
185 lstrcpynA(lpTemp,
186 lpstrDirectory,
187 len); // len is including trailing NULL!!
188 lpstrDirectory = lpTemp;
189 }
190
191 dprintf(("SetCurrentDirectoryA %s", lpstrDirectory));
192 return O32_SetCurrentDirectory((LPSTR)lpstrDirectory);
193}
194
195
196/*****************************************************************************
197 * Name : SetCurrentDirectoryW
198 * Purpose :
199 * Parameters:
200 * Variables :
201 * Result :
202 * Remark :
203 * Status :
204 *
205 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
206 *****************************************************************************/
207
208ODINFUNCTION1(BOOL,SetCurrentDirectoryW,LPCWSTR,lpPathName)
209{
210 char *asciipath;
211 BOOL rc;
212
213 asciipath = UnicodeToAsciiString((LPWSTR)lpPathName);
214 rc = SetCurrentDirectoryA(asciipath);
215 FreeAsciiString(asciipath);
216 return(rc);
217}
218
219
220/*****************************************************************************
221 * Name : CreateDirectoryA
222 * Purpose :
223 * Parameters:
224 * Variables :
225 * Result :
226 * Remark :
227 * Status :
228 *
229 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
230 *****************************************************************************/
231
232ODINFUNCTION2(BOOL, CreateDirectoryA,
233 LPCSTR, lpstrDirectory,
234 PSECURITY_ATTRIBUTES,arg2)
235{
236 // 2001-05-28 PH
237 // verify filename first (NT4SP6)
238 // @@@PH: if (IsBadStringPtr( (LPVOID)lpstrDirectory, 0xFFFF))
239 if (lpstrDirectory == NULL)
240 {
241 SetLastError(ERROR_PATH_NOT_FOUND);
242 return FALSE;
243 }
244
245 int len = strlen(lpstrDirectory);
246
247 // cut off trailing backslashes
248 if ( (lpstrDirectory[len - 1] == '\\') ||
249 (lpstrDirectory[len - 1] == '/') )
250 {
251 LPSTR lpTemp = (LPSTR)_alloca(len);
252 lstrcpynA(lpTemp,
253 lpstrDirectory,
254 len ); // len is including trailing NULL!!
255 lpstrDirectory = lpTemp;
256 }
257
258 dprintf(("CreateDirectoryA %s", lpstrDirectory));
259
260 // PH Note 2000/06/12:
261 // Creation of an existing directory is NO ERROR it seems.
262 DWORD dwAttr = GetFileAttributesA(lpstrDirectory);
263 if(dwAttr != -1)
264 {
265 if (dwAttr & FILE_ATTRIBUTE_DIRECTORY)
266 {
267 SetLastError(ERROR_SUCCESS);
268 return TRUE;
269 }
270 }
271 return(OSLibDosCreateDirectory(lpstrDirectory));
272}
273
274/*****************************************************************************
275 * Name : CreateDirectoryW
276 * Purpose :
277 * Parameters:
278 * Variables :
279 * Result :
280 * Remark :
281 * Status :
282 *
283 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
284 *****************************************************************************/
285
286ODINFUNCTION2(BOOL,CreateDirectoryW,LPCWSTR, arg1,
287 PSECURITY_ATTRIBUTES,arg2)
288{
289 BOOL rc;
290 char *astring;
291
292 astring = UnicodeToAsciiString((LPWSTR)arg1);
293 rc = CALL_ODINFUNC(CreateDirectoryA)(astring, arg2);
294 FreeAsciiString(astring);
295 return(rc);
296}
297
298/*****************************************************************************
299 * Name : BOOL WIN32API CreateDirectoryExA
300 * Purpose : The CreateDirectoryExA function creates a new directory with a
301 * specified path that retains the attributes of a specified
302 * template directory. If the underlying file system supports
303 * security on files and directories, the function applies a
304 * specified security descriptor to the new directory.
305 * The new directory retains the other attributes of the specified
306 * template directory. Note that CreateDirectoryEx has a template
307 * parameter, while CreateDirectory does not.
308 * Parameters: LPCSTR lpTemplateDirectory pointer to path string of template
309 * directory
310 * LPCSTR lpNewDirectory pointer to path string of directory
311 * to create
312 * LPSECURITY_ATTRIBUTES lpSecurityAttributes pointer to security
313 * descriptor
314 *
315 * Variables :
316 * Result : If the function succeeds, the return value is nonzero.
317 * If the function fails, the return value is zero.
318 * To get extended error information, call GetLastError.
319 * Remark :
320 * Status : UNTESTED STUB
321 *
322 * Author : Markus Montkowski [Tha, 1998/05/21 17:46]
323 *****************************************************************************/
324
325BOOL WIN32API CreateDirectoryExA( LPCSTR lpTemplateDirectory,
326 LPCSTR lpNewDirectory,
327 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
328{
329
330 dprintf(("KERNEL32:CreateDirectoryExA(%08x,%08x,%08x) not properly implemented\n",
331 lpTemplateDirectory,lpNewDirectory,lpSecurityAttributes
332 ));
333
334 return CreateDirectoryA(lpNewDirectory, lpSecurityAttributes);
335}
336
337/*****************************************************************************
338 * Name : BOOL WIN32API CreateDirectoryExW
339 * Purpose : The CreateDirectoryExW function creates a new directory with a
340 * specified path that retains the attributes of a specified
341 * template directory. If the underlying file system supports
342 * security on files and directories, the function applies a
343 * specified security descriptor to the new directory.
344 * The new directory retains the other attributes of the specified
345 * template directory. Note that CreateDirectoryEx has a template
346 * parameter, while CreateDirectory does not.
347 * Parameters: LPCWSTR lpTemplateDirectory pointer to path string of template
348 * directory
349 * LPCWSTR lpNewDirectory pointer to path string of directory
350 * to create
351 * LPSECURITY_ATTRIBUTES lpSecurityAttributes pointer to security
352 * descriptor
353 *
354 * Variables :
355 * Result : If the function succeeds, the return value is nonzero.
356 * If the function fails, the return value is zero.
357 * To get extended error information, call GetLastError.
358 * Remark :
359 * Status : UNTESTED STUB
360 *
361 * Author : Markus Montkowski [Tha, 1998/05/21 17:46]
362 *****************************************************************************/
363
364BOOL WIN32API CreateDirectoryExW( LPCWSTR lpTemplateDirectory,
365 LPCWSTR lpNewDirectory,
366 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
367{
368
369 dprintf(("KERNEL32:CreateDirectoryExW(%08x,%08x,%08x) not properly implemented\n",
370 lpTemplateDirectory,lpNewDirectory,lpSecurityAttributes
371 ));
372
373 return CreateDirectoryW(lpNewDirectory, lpSecurityAttributes);
374}
375
376/*****************************************************************************
377 * Name : GetSystemDirectoryA
378 * Purpose :
379 * Parameters:
380 * Variables :
381 * Result :
382 * Remark : Should return length of system dir even if lpBuffer == NULL
383 * Status :
384 *
385 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
386 *****************************************************************************/
387
388ODINFUNCTION2(UINT,GetSystemDirectoryA,LPSTR,lpBuffer,
389 UINT,uSize)
390{
391 int len;
392 char *dir;
393
394 dir = InternalGetSystemDirectoryA();
395 len = lstrlenA(dir);
396 if(lpBuffer)
397 lstrcpynA(lpBuffer, dir, uSize);
398 return len;
399}
400
401
402/*****************************************************************************
403 * Name : GetSystemDirectoryW
404 * Purpose :
405 * Parameters:
406 * Variables :
407 * Result :
408 * Remark : Should return length of system dir even if lpBuffer == NULL
409 * Status :
410 *
411 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
412 *****************************************************************************/
413
414ODINFUNCTION2(UINT,GetSystemDirectoryW,LPWSTR,lpBuffer,
415 UINT, uSize)
416{
417 char *asciibuffer = NULL;
418 UINT rc;
419
420 if(lpBuffer)
421 asciibuffer = (char *)alloca(uSize+1);
422
423 if(lpBuffer && asciibuffer == NULL)
424 {
425 DebugInt3();
426 }
427
428 rc = GetSystemDirectoryA(asciibuffer, uSize);
429 if(rc && asciibuffer)
430 AsciiToUnicode(asciibuffer, lpBuffer);
431
432 return(rc);
433}
434
435
436/*****************************************************************************
437 * Name : GetWindowsDirectoryA
438 * Purpose :
439 * Parameters:
440 * Variables :
441 * Result :
442 * Remark : Should return length of system dir even if lpBuffer == NULL
443 * Status :
444 *
445 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
446 *****************************************************************************/
447
448ODINFUNCTION2(UINT,GetWindowsDirectoryA,LPSTR,lpBuffer,
449 UINT,uSize)
450{
451 char *dir;
452 int len;
453
454 dir = InternalGetWindowsDirectoryA();
455 len = lstrlenA(dir);
456 if(lpBuffer)
457 lstrcpynA(lpBuffer, dir, uSize);
458 return len;
459}
460
461
462/*****************************************************************************
463 * Name : GetWindowsDirectoryW
464 * Purpose :
465 * Parameters:
466 * Variables :
467 * Result :
468 * Remark : Should return length of system dir even if lpBuffer == NULL
469 * Status :
470 *
471 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
472 *****************************************************************************/
473
474ODINFUNCTION2(UINT,GetWindowsDirectoryW,LPWSTR,lpBuffer,
475 UINT, uSize)
476{
477 char *asciibuffer = NULL;
478 UINT rc;
479
480 if(lpBuffer)
481 asciibuffer = (char *)alloca(uSize+1);
482
483 if(lpBuffer && asciibuffer == NULL)
484 {
485 DebugInt3();
486 }
487
488 rc = GetWindowsDirectoryA(asciibuffer, uSize);
489 if(rc && asciibuffer)
490 AsciiToUnicode(asciibuffer, lpBuffer);
491
492 return(rc);
493}
494
495
496/*****************************************************************************
497 * Name : RemoveDirectoryA
498 * Purpose :
499 * Parameters:
500 * Variables :
501 * Result :
502 * Remark :
503 * Status :
504 *
505 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
506 *****************************************************************************/
507
508
509ODINFUNCTION1(BOOL, RemoveDirectoryA, LPCSTR, lpstrDirectory)
510{
511 int len = strlen(lpstrDirectory);
512
513 if(lpstrDirectory == NULL) {
514 SetLastError(ERROR_INVALID_PARAMETER);
515 return FALSE;
516 }
517 // cut off trailing backslashes
518 if ( (lpstrDirectory[len - 1] == '\\') ||
519 (lpstrDirectory[len - 1] == '/') )
520 {
521 LPSTR lpTemp = (LPSTR)_alloca(len);
522 lstrcpynA(lpTemp,
523 lpstrDirectory,
524 len ); // len is including trailing NULL!!
525 lpstrDirectory = lpTemp;
526 }
527
528 dprintf(("RemoveDirectory %s", lpstrDirectory));
529
530 return OSLibDosRemoveDir(lpstrDirectory);
531}
532
533
534/*****************************************************************************
535 * Name : RemoveDirectoryW
536 * Purpose :
537 * Parameters:
538 * Variables :
539 * Result :
540 * Remark :
541 * Status :
542 *
543 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
544 *****************************************************************************/
545
546ODINFUNCTION1(BOOL,RemoveDirectoryW,LPCWSTR,lpPathName)
547{
548 char *asciipath;
549 BOOL rc;
550
551 asciipath = UnicodeToAsciiString((LPWSTR)lpPathName);
552 rc = RemoveDirectoryA(asciipath);
553 FreeAsciiString(asciipath);
554 return(rc);
555}
556
557/***********************************************************************
558 * DIR_TryModulePath
559 *
560 * Helper function for DIR_SearchPath.
561 */
562static BOOL DIR_TryModulePath( LPCSTR name, char *full_name )
563{
564 char buffer[OFS_MAXPATHNAME];
565 LPSTR p;
566
567 if (!GetModuleFileNameA( 0, buffer, sizeof(buffer) ))
568 buffer[0]='\0';
569
570 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
571 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
572 strcpy( p, name );
573
574 return OSLibDosSearchPath(OSLIB_SEARCHFILE, NULL, buffer, full_name, MAX_PATHNAME_LEN);
575}
576
577
578/***********************************************************************
579 * DIR_SearchPath
580 *
581 * Implementation of SearchPath32A. 'win32' specifies whether the search
582 * order is Win16 (module path last) or Win32 (module path first).
583 *
584 * FIXME: should return long path names.
585 */
586DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
587 char *full_name )
588{
589 DWORD len;
590 LPCSTR p;
591 LPSTR tmp = NULL;
592 BOOL ret = TRUE;
593
594 /* First check the supplied parameters */
595
596 p = strrchr( name, '.' );
597 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
598 ext = NULL; /* Ignore the specified extension */
599 if ((*name && (name[1] == ':')) ||
600 strchr( name, '/' ) || strchr( name, '\\' ))
601 path = NULL; /* Ignore path if name already contains a path */
602 if (path && !*path) path = NULL; /* Ignore empty path */
603
604 /* See if path is a list of directories to search. If so, only search
605 those (according to SDK docs) */
606 if ((path != NULL) && strchr(path, ';')) {
607 ret = OSLibDosSearchPath(OSLIB_SEARCHDIR, (LPSTR)path, (LPSTR)name,
608 full_name, MAX_PATHNAME_LEN);
609 goto done;
610 }
611
612 len = strlen(name);
613 if (ext) len += strlen(ext);
614 if (path) len += strlen(path) + 1;
615
616 /* Allocate a buffer for the file name and extension */
617
618 if (path || ext)
619 {
620 if (!(tmp = (LPSTR)HeapAlloc( GetProcessHeap(), 0, len + 1 )))
621 {
622 SetLastError( ERROR_OUTOFMEMORY );
623 return 0;
624 }
625 if (path)
626 {
627 strcpy( tmp, path );
628 strcat( tmp, "\\" );
629 strcat( tmp, name );
630 }
631 else strcpy( tmp, name );
632 if (ext) strcat( tmp, ext );
633 name = tmp;
634 }
635
636 /* If we have an explicit path, everything's easy */
637
638 if (path || (*name && (name[1] == ':')) ||
639 strchr( name, '/' ) || strchr( name, '\\' ))
640 {
641 ret = OSLibDosSearchPath(OSLIB_SEARCHFILE, NULL, (LPSTR)name, full_name, MAX_PATHNAME_LEN);
642 goto done;
643 }
644
645 /* Try the path of the current executable (for Win32 search order) */
646 if (DIR_TryModulePath( name, full_name )) goto done;
647
648 /* Try the current directory */
649 if (OSLibDosSearchPath(OSLIB_SEARCHCURDIR, NULL, (LPSTR)name, full_name, MAX_PATHNAME_LEN))
650 goto done;
651
652 /* Try the Windows system directory */
653 if (OSLibDosSearchPath(OSLIB_SEARCHDIR, (LPSTR)&DIR_System, (LPSTR)name, full_name, MAX_PATHNAME_LEN))
654 goto done;
655
656 /* Try the Windows directory */
657 if (OSLibDosSearchPath(OSLIB_SEARCHDIR, (LPSTR)&DIR_Windows, (LPSTR)name, full_name, MAX_PATHNAME_LEN))
658 goto done;
659
660 /* Try all directories in path */
661 ret = OSLibDosSearchPath(OSLIB_SEARCHENV, "PATH", (LPSTR)name, full_name, MAX_PATHNAME_LEN);
662
663done:
664 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
665 return ret;
666}
667
668
669/***********************************************************************
670 * SearchPath32A [KERNEL32.447]
671 *
672 * Searches for a specified file in the search path.
673 *
674 * PARAMS
675 * path [I] Path to search
676 * name [I] Filename to search for.
677 * ext [I] File extension to append to file name. The first
678 * character must be a period. This parameter is
679 * specified only if the filename given does not
680 * contain an extension.
681 * buflen [I] size of buffer, in characters
682 * buffer [O] buffer for found filename
683 * lastpart [O] address of pointer to last used character in
684 * buffer (the final '\')
685 *
686 * RETURNS
687 * Success: length of string copied into buffer, not including
688 * terminating null character. If the filename found is
689 * longer than the length of the buffer, the length of the
690 * filename is returned.
691 * Failure: Zero
692 *
693 * NOTES
694 * Should call SetLastError(but currently doesn't).
695 */
696DWORD WINAPI SearchPathA(LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
697 LPSTR buffer, LPSTR *lastpart )
698{
699 char full_name[MAX_PATHNAME_LEN];
700
701 if (!DIR_SearchPath( path, name, ext, (LPSTR)full_name )) return 0;
702 lstrcpynA( buffer, (LPSTR)full_name, buflen);
703 SetLastError(0);
704 return strlen(full_name);
705}
706
707
708/***********************************************************************
709 * SearchPath32W (KERNEL32.448)
710 */
711DWORD WINAPI SearchPathW(LPCWSTR path, LPCWSTR name, LPCWSTR ext,
712 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
713{
714 char full_name[MAX_PATHNAME_LEN];
715
716 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
717 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
718 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
719 DWORD ret = DIR_SearchPath( pathA, nameA, extA, (LPSTR)full_name );
720 HeapFree( GetProcessHeap(), 0, extA );
721 HeapFree( GetProcessHeap(), 0, nameA );
722 HeapFree( GetProcessHeap(), 0, pathA );
723 if (!ret) return 0;
724
725 lstrcpynAtoW( buffer, full_name, buflen);
726 SetLastError(0);
727 return ret;
728}
Note: See TracBrowser for help on using the repository browser.