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

Last change on this file since 3347 was 3347, checked in by sandervl, 25 years ago

fixed GetWindows/SystemDir (NULL pointer)

File size: 16.3 KB
Line 
1/* $Id: directory.cpp,v 1.18 2000-04-07 19:43:46 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 * Project Odin Software License can be found in LICENSE.TXT
17 *
18 */
19
20
21/*****************************************************************************
22 * Includes *
23 *****************************************************************************/
24
25#include <odin.h>
26#include <odinwrap.h>
27#include <os2win.h>
28#include <stdlib.h>
29#include <unicode.h>
30#include <heapstring.h>
31#include <options.h>
32#include "initterm.h"
33#include <win\file.h>
34#include <string.h>
35#include "oslibdos.h"
36#include "profile.h"
37
38#define DBG_LOCALLOG DBG_directory
39#include "dbglocal.h"
40
41ODINDEBUGCHANNEL(KERNEL32-DIRECTORY)
42
43
44static char DIR_Windows[MAX_PATHNAME_LEN];
45static char DIR_System[MAX_PATHNAME_LEN];
46
47//******************************************************************************
48//******************************************************************************
49char *InternalGetWindowsDirectoryA()
50{
51 return DIR_Windows;
52}
53//******************************************************************************
54//******************************************************************************
55char *InternalGetSystemDirectoryA()
56{
57 return DIR_System;
58}
59//******************************************************************************
60//******************************************************************************
61void InitDirectories()
62{
63 GetWindowsDirectoryA((LPSTR)&DIR_Windows, sizeof(DIR_Windows));
64 GetSystemDirectoryA((LPSTR)&DIR_System, sizeof(DIR_System));
65}
66
67/*****************************************************************************
68 * Name : GetCurrentDirectoryA
69 * Purpose : query the current directory
70 * Parameters:
71 * Variables :
72 * Result :
73 * Remark :
74 * Status :
75 *
76 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
77 *****************************************************************************/
78
79ODINFUNCTION2(UINT, GetCurrentDirectoryA, UINT, nBufferLength,
80 LPSTR, lpBuffer)
81{
82 return O32_GetCurrentDirectory(nBufferLength, lpBuffer);
83}
84
85
86/*****************************************************************************
87 * Name : GetCurrentDirectoryW
88 * Purpose : query the current directory
89 * Parameters:
90 * Variables :
91 * Result :
92 * Remark :
93 * Status :
94 *
95 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
96 *****************************************************************************/
97
98ODINFUNCTION2(UINT, GetCurrentDirectoryW, UINT, nBufferLength,
99 LPWSTR, lpBuffer)
100{
101 char *asciidir = (char *)malloc(nBufferLength+1);
102 int rc;
103
104 rc = O32_GetCurrentDirectory(nBufferLength, asciidir);
105 if(rc != 0)
106 AsciiToUnicode(asciidir, lpBuffer);
107 free(asciidir);
108 return(rc);
109}
110
111
112/*****************************************************************************
113 * Name : SetCurrentDirectoryA
114 * Purpose :
115 * Parameters:
116 * Variables :
117 * Result :
118 * Remark :
119 * Status :
120 *
121 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
122 *****************************************************************************/
123
124
125ODINFUNCTION1(BOOL,SetCurrentDirectoryA,LPCSTR,lpPathName)
126{
127 if(HIWORD(lpPathName) == 0) {
128 SetLastError(ERROR_INVALID_PARAMETER);
129 return FALSE;
130 }
131 int len = strlen(lpPathName);
132 char *tmp=(char *)alloca(len + 1);
133
134 strcpy(tmp, lpPathName);
135 //SvL: Don't remove terminating backslash if it wants to chdir to root dir
136 if(tmp[len -1] == '\\' && len != 1)
137 tmp[len -1] = 0;
138
139 dprintf(("SetCurrentDirectoryA %s", tmp));
140 return O32_SetCurrentDirectory((LPSTR)tmp);
141}
142
143
144/*****************************************************************************
145 * Name : SetCurrentDirectoryW
146 * Purpose :
147 * Parameters:
148 * Variables :
149 * Result :
150 * Remark :
151 * Status :
152 *
153 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
154 *****************************************************************************/
155
156ODINFUNCTION1(BOOL,SetCurrentDirectoryW,LPCWSTR,lpPathName)
157{
158 char *asciipath;
159 BOOL rc;
160
161 asciipath = UnicodeToAsciiString((LPWSTR)lpPathName);
162 rc = SetCurrentDirectoryA(asciipath);
163 FreeAsciiString(asciipath);
164 return(rc);
165}
166
167
168/*****************************************************************************
169 * Name : CreateDirectoryA
170 * Purpose :
171 * Parameters:
172 * Variables :
173 * Result :
174 * Remark :
175 * Status :
176 *
177 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
178 *****************************************************************************/
179
180ODINFUNCTION2(BOOL,CreateDirectoryA,LPCSTR, arg1,PSECURITY_ATTRIBUTES,arg2)
181{
182 int len = strlen(arg1);
183 char *tmp=(char *)alloca(len + 1);
184
185 strcpy(tmp, arg1);
186 if(tmp[len -1] == '\\')
187 tmp[len -1] = 0;
188 return O32_CreateDirectory(tmp, arg2);
189}
190
191/*****************************************************************************
192 * Name : CreateDirectoryW
193 * Purpose :
194 * Parameters:
195 * Variables :
196 * Result :
197 * Remark :
198 * Status :
199 *
200 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
201 *****************************************************************************/
202
203ODINFUNCTION2(BOOL,CreateDirectoryW,LPCWSTR, arg1,
204 PSECURITY_ATTRIBUTES,arg2)
205{
206 BOOL rc;
207 char *astring;
208
209 astring = UnicodeToAsciiString((LPWSTR)arg1);
210 rc = CreateDirectoryA(astring, arg2);
211 FreeAsciiString(astring);
212 return(rc);
213}
214
215
216/*****************************************************************************
217 * Name : GetSystemDirectoryA
218 * Purpose :
219 * Parameters:
220 * Variables :
221 * Result :
222 * Remark : Should return length of system dir even if lpBuffer == NULL
223 * Status :
224 *
225 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
226 *****************************************************************************/
227
228ODINFUNCTION2(UINT,GetSystemDirectoryA,LPSTR,lpBuffer,
229 UINT,uSize)
230{
231 LPSTR lpstrEnv = getenv("WIN32.DIR.SYSTEM"); /* query environment */
232 CHAR buf[255];
233
234 if (lpstrEnv != NULL)
235 {
236 if(lpBuffer)
237 lstrcpynA(lpBuffer, lpstrEnv, uSize);
238
239 return (lstrlenA(lpstrEnv)); /* return number of copies bytes */
240 }
241 else
242 {
243 int len;
244
245 len = ODIN_PROFILE_GetOdinIniString(ODINDIRECTORIES,"SYSTEM","",buf,sizeof(buf));
246 if (len > 2) {
247 if(buf[len-1] == '\\') {
248 buf[len-1] = 0;
249 len--;
250 }
251 if(lpBuffer)
252 lstrcpynA(lpBuffer, buf, uSize);
253
254 return len;
255 }
256 else {//SvL: Use path of kernel32.dll instead of calling Open32 api (which returns \OS2\SYSTEM)
257 lstrcpynA(buf, kernel32Path, sizeof(buf)-1);
258 len = lstrlenA(buf);;
259 if(buf[len-1] == '\\') {
260 buf[len-1] = 0;
261 len--;
262 }
263 if(lpBuffer)
264 lstrcpynA(lpBuffer, buf, uSize);
265 return len;
266 }
267 }
268}
269
270
271/*****************************************************************************
272 * Name : GetSystemDirectoryW
273 * Purpose :
274 * Parameters:
275 * Variables :
276 * Result :
277 * Remark : Should return length of system dir even if lpBuffer == NULL
278 * Status :
279 *
280 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
281 *****************************************************************************/
282
283ODINFUNCTION2(UINT,GetSystemDirectoryW,LPWSTR,lpBuffer,
284 UINT, uSize)
285{
286 char *asciibuffer = NULL;
287 UINT rc;
288
289 if(lpBuffer)
290 asciibuffer = (char *)malloc(uSize+1);
291
292 rc = GetSystemDirectoryA(asciibuffer, uSize);
293 if(rc && asciibuffer)
294 AsciiToUnicode(asciibuffer, lpBuffer);
295
296 if(asciibuffer)
297 free(asciibuffer);
298 return(rc);
299}
300
301
302/*****************************************************************************
303 * Name : GetWindowsDirectoryA
304 * Purpose :
305 * Parameters:
306 * Variables :
307 * Result :
308 * Remark : Should return length of system dir even if lpBuffer == NULL
309 * Status :
310 *
311 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
312 *****************************************************************************/
313
314ODINFUNCTION2(UINT,GetWindowsDirectoryA,LPSTR,lpBuffer,
315 UINT,uSize)
316{
317 LPSTR lpstrEnv = getenv("WIN32.DIR.WINDOWS"); /* query environment */
318 CHAR buf[255];
319
320 if (lpstrEnv != NULL)
321 {
322 if(lpBuffer)
323 lstrcpynA(lpBuffer, lpstrEnv, uSize);
324
325 return (lstrlenA(lpstrEnv)); /* return number of copies bytes */
326 }
327 else
328 {
329 int len;
330
331 len = ODIN_PROFILE_GetOdinIniString(ODINDIRECTORIES,"WINDOWS","",buf,sizeof(buf));
332 if (len > 2) {
333 if(buf[len-1] == '\\') {
334 buf[len-1] = 0;
335 len--;
336 }
337 if(lpBuffer)
338 lstrcpynA(lpBuffer, buf, uSize);
339
340 return len;
341 }
342 else {//SvL: Use path of kernel32.dll instead of calling Open32 api (which returns \OS2\SYSTEM)
343
344 lstrcpynA(buf, kernel32Path, sizeof(buf)-1);
345 strcat(buf, "WIN");
346 O32_CreateDirectory(buf, NULL);
347
348 len = lstrlenA(buf);;
349 if(buf[len-1] == '\\') {
350 buf[len-1] = 0;
351 len--;
352 }
353 if(lpBuffer)
354 lstrcpynA(lpBuffer, buf, uSize);
355 return len;
356 }
357 }
358}
359
360
361/*****************************************************************************
362 * Name : GetWindowsDirectoryW
363 * Purpose :
364 * Parameters:
365 * Variables :
366 * Result :
367 * Remark : Should return length of system dir even if lpBuffer == NULL
368 * Status :
369 *
370 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
371 *****************************************************************************/
372
373ODINFUNCTION2(UINT,GetWindowsDirectoryW,LPWSTR,lpBuffer,
374 UINT, uSize)
375{
376 char *asciibuffer = NULL;
377 UINT rc;
378
379 if(lpBuffer)
380 asciibuffer = (char *)malloc(uSize+1);
381
382 rc = GetWindowsDirectoryA(asciibuffer, uSize);
383 if(rc && asciibuffer)
384 AsciiToUnicode(asciibuffer, lpBuffer);
385
386 if(asciibuffer)
387 free(asciibuffer);
388 return(rc);
389}
390
391
392/*****************************************************************************
393 * Name : RemoveDirectoryA
394 * Purpose :
395 * Parameters:
396 * Variables :
397 * Result :
398 * Remark :
399 * Status :
400 *
401 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
402 *****************************************************************************/
403
404
405ODINFUNCTION1(BOOL,RemoveDirectoryA,LPCSTR,arg1)
406{
407 int len = strlen(arg1);
408 char *tmp=(char *)alloca(len + 1);
409
410 strcpy(tmp, arg1);
411 if(tmp[len -1] == '\\')
412 tmp[len -1] = 0;
413
414 dprintf(("RemoveDirectory %S", arg1));
415
416 return O32_RemoveDirectory(tmp);
417}
418
419
420/*****************************************************************************
421 * Name : RemoveDirectoryW
422 * Purpose :
423 * Parameters:
424 * Variables :
425 * Result :
426 * Remark :
427 * Status :
428 *
429 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
430 *****************************************************************************/
431
432ODINFUNCTION1(BOOL,RemoveDirectoryW,LPCWSTR,lpPathName)
433{
434 char *asciipath;
435 BOOL rc;
436
437 asciipath = UnicodeToAsciiString((LPWSTR)lpPathName);
438 rc = RemoveDirectoryA(asciipath);
439 FreeAsciiString(asciipath);
440 return(rc);
441}
442
443/***********************************************************************
444 * DIR_TryModulePath
445 *
446 * Helper function for DIR_SearchPath.
447 */
448static BOOL DIR_TryModulePath( LPCSTR name, char *full_name )
449{
450 char buffer[OFS_MAXPATHNAME];
451 LPSTR p;
452
453 if (!GetModuleFileNameA( 0, buffer, sizeof(buffer) ))
454 buffer[0]='\0';
455
456 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
457 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
458 strcpy( p, name );
459
460 return OSLibDosSearchPath(OSLIB_SEARCHFILE, NULL, buffer, full_name, MAX_PATHNAME_LEN);
461}
462
463
464/***********************************************************************
465 * DIR_SearchPath
466 *
467 * Implementation of SearchPath32A. 'win32' specifies whether the search
468 * order is Win16 (module path last) or Win32 (module path first).
469 *
470 * FIXME: should return long path names.
471 */
472DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
473 char *full_name )
474{
475 DWORD len;
476 LPCSTR p;
477 LPSTR tmp = NULL;
478 BOOL ret = TRUE;
479
480 /* First check the supplied parameters */
481
482 p = strrchr( name, '.' );
483 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
484 ext = NULL; /* Ignore the specified extension */
485 if ((*name && (name[1] == ':')) ||
486 strchr( name, '/' ) || strchr( name, '\\' ))
487 path = NULL; /* Ignore path if name already contains a path */
488 if (path && !*path) path = NULL; /* Ignore empty path */
489
490 len = strlen(name);
491 if (ext) len += strlen(ext);
492 if (path) len += strlen(path) + 1;
493
494 /* Allocate a buffer for the file name and extension */
495
496 if (path || ext)
497 {
498 if (!(tmp = (LPSTR)HeapAlloc( GetProcessHeap(), 0, len + 1 )))
499 {
500 SetLastError( ERROR_OUTOFMEMORY );
501 return 0;
502 }
503 if (path)
504 {
505 strcpy( tmp, path );
506 strcat( tmp, "\\" );
507 strcat( tmp, name );
508 }
509 else strcpy( tmp, name );
510 if (ext) strcat( tmp, ext );
511 name = tmp;
512 }
513
514 /* If we have an explicit path, everything's easy */
515
516 if (path || (*name && (name[1] == ':')) ||
517 strchr( name, '/' ) || strchr( name, '\\' ))
518 {
519 ret = OSLibDosSearchPath(OSLIB_SEARCHFILE, NULL, (LPSTR)name, full_name, MAX_PATHNAME_LEN);
520 goto done;
521 }
522
523 /* Try the path of the current executable (for Win32 search order) */
524 if (DIR_TryModulePath( name, full_name )) goto done;
525
526 /* Try the current directory */
527 if (OSLibDosSearchPath(OSLIB_SEARCHCURDIR, NULL, (LPSTR)name, full_name, MAX_PATHNAME_LEN))
528 goto done;
529
530 /* Try the Windows system directory */
531 if (OSLibDosSearchPath(OSLIB_SEARCHDIR, (LPSTR)&DIR_System, (LPSTR)name, full_name, MAX_PATHNAME_LEN))
532 goto done;
533
534 /* Try the Windows directory */
535 if (OSLibDosSearchPath(OSLIB_SEARCHDIR, (LPSTR)&DIR_Windows, (LPSTR)name, full_name, MAX_PATHNAME_LEN))
536 goto done;
537
538 /* Try all directories in path */
539 ret = OSLibDosSearchPath(OSLIB_SEARCHENV, "PATH", (LPSTR)name, full_name, MAX_PATHNAME_LEN);
540
541done:
542 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
543 return ret;
544}
545
546
547/***********************************************************************
548 * SearchPath32A [KERNEL32.447]
549 *
550 * Searches for a specified file in the search path.
551 *
552 * PARAMS
553 * path [I] Path to search
554 * name [I] Filename to search for.
555 * ext [I] File extension to append to file name. The first
556 * character must be a period. This parameter is
557 * specified only if the filename given does not
558 * contain an extension.
559 * buflen [I] size of buffer, in characters
560 * buffer [O] buffer for found filename
561 * lastpart [O] address of pointer to last used character in
562 * buffer (the final '\')
563 *
564 * RETURNS
565 * Success: length of string copied into buffer, not including
566 * terminating null character. If the filename found is
567 * longer than the length of the buffer, the length of the
568 * filename is returned.
569 * Failure: Zero
570 *
571 * NOTES
572 * Should call SetLastError(but currently doesn't).
573 */
574DWORD WINAPI SearchPathA(LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
575 LPSTR buffer, LPSTR *lastpart )
576{
577 char full_name[MAX_PATHNAME_LEN];
578
579 if (!DIR_SearchPath( path, name, ext, (LPSTR)full_name )) return 0;
580 lstrcpynA( buffer, (LPSTR)full_name, buflen-1);
581 buffer[buflen-2] = 0;
582 SetLastError(0);
583 return strlen(buffer);
584}
585
586
587/***********************************************************************
588 * SearchPath32W (KERNEL32.448)
589 */
590DWORD WINAPI SearchPathW(LPCWSTR path, LPCWSTR name, LPCWSTR ext,
591 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
592{
593 char full_name[MAX_PATHNAME_LEN];
594
595 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
596 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
597 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
598 DWORD ret = DIR_SearchPath( pathA, nameA, extA, (LPSTR)full_name );
599 HeapFree( GetProcessHeap(), 0, extA );
600 HeapFree( GetProcessHeap(), 0, nameA );
601 HeapFree( GetProcessHeap(), 0, pathA );
602 if (!ret) return 0;
603
604 lstrcpynAtoW( buffer, full_name, buflen-1 );
605 buffer[buflen-2] = 0;
606 SetLastError(0);
607 return strlen(full_name);
608}
Note: See TracBrowser for help on using the repository browser.