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

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

thread, SearchPath + handlemanager fixes

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