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

Last change on this file since 2010 was 1833, checked in by sandervl, 26 years ago

exe loader fixes + updates

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