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

Last change on this file since 5655 was 5303, checked in by sandervl, 24 years ago

SearchPath & pe loader fixes

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