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

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

DosOpen (file handle error) & dll destruction bugfixes

File size: 15.9 KB
Line 
1/* $Id: directory.cpp,v 1.17 2000-03-04 19:52:35 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 :
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
233 if (lpstrEnv != NULL)
234 {
235 lstrcpynA(lpBuffer, /* copy environment variable to buffer */
236 lpstrEnv,
237 uSize);
238 return (lstrlenA(lpBuffer)); /* return number of copies bytes */
239 }
240 else
241 {
242 int len;
243
244 len = ODIN_PROFILE_GetOdinIniString(ODINDIRECTORIES,"SYSTEM","",lpBuffer,uSize);
245 if (len > 2) {
246 if(lpBuffer[len-1] == '\\') {
247 lpBuffer[len-1] = 0;
248 len--;
249 }
250 return len;
251 }
252 else {//SvL: Use path of kernel32.dll instead of calling Open32 api (which returns \OS2\SYSTEM)
253 lstrcpynA(lpBuffer, kernel32Path, uSize);
254 len = lstrlenA(lpBuffer);;
255 if(lpBuffer[len-1] == '\\') {
256 lpBuffer[len-1] = 0;
257 len--;
258 }
259 return len;
260 }
261 }
262}
263
264
265/*****************************************************************************
266 * Name : GetSystemDirectoryW
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(UINT,GetSystemDirectoryW,LPWSTR,lpBuffer,
278 UINT, uSize)
279{
280 char *asciibuffer = (char *)malloc(uSize+1);
281 UINT rc;
282
283 rc = GetSystemDirectoryA(asciibuffer, uSize);
284 if(rc) AsciiToUnicode(asciibuffer, lpBuffer);
285 free(asciibuffer);
286 return(rc);
287}
288
289
290/*****************************************************************************
291 * Name : GetWindowsDirectoryA
292 * Purpose :
293 * Parameters:
294 * Variables :
295 * Result :
296 * Remark :
297 * Status :
298 *
299 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
300 *****************************************************************************/
301
302ODINFUNCTION2(UINT,GetWindowsDirectoryA,LPSTR,lpBuffer,
303 UINT,uSize)
304{
305 LPSTR lpstrEnv = getenv("WIN32.DIR.WINDOWS"); /* query environment */
306
307 if (lpstrEnv != NULL)
308 {
309 lstrcpynA(lpBuffer, /* copy environment variable to buffer */
310 lpstrEnv,
311 uSize);
312 return (lstrlenA(lpBuffer)); /* return number of copies bytes */
313 }
314 else
315 {
316 int len;
317
318 len = ODIN_PROFILE_GetOdinIniString(ODINDIRECTORIES,"WINDOWS","",lpBuffer,uSize);
319 if (len > 2) {
320 if(lpBuffer[len-1] == '\\') {
321 lpBuffer[len-1] = 0;
322 len--;
323 }
324 return len;
325 }
326 else {//SvL: Use path of kernel32.dll instead of calling Open32 api (which returns \OS2\SYSTEM)
327 CHAR buf[255];
328
329 lstrcpynA(buf, kernel32Path, sizeof(buf)-1);
330 strcat(buf, "WIN");
331 O32_CreateDirectory(buf, NULL);
332
333 lstrcpynA(lpBuffer, buf, uSize);
334 len = lstrlenA(lpBuffer);;
335 if(lpBuffer[len-1] == '\\') {
336 lpBuffer[len-1] = 0;
337 len--;
338 }
339 return len;
340 }
341 }
342}
343
344
345/*****************************************************************************
346 * Name : GetWindowsDirectoryW
347 * Purpose :
348 * Parameters:
349 * Variables :
350 * Result :
351 * Remark :
352 * Status :
353 *
354 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
355 *****************************************************************************/
356
357ODINFUNCTION2(UINT,GetWindowsDirectoryW,LPWSTR,lpBuffer,
358 UINT, uSize)
359{
360 char *asciibuffer = (char *)malloc(uSize+1);
361 UINT rc;
362
363 rc = GetWindowsDirectoryA(asciibuffer, uSize);
364 if(rc)
365 AsciiToUnicode(asciibuffer, lpBuffer);
366 free(asciibuffer);
367 return(rc);
368}
369
370
371/*****************************************************************************
372 * Name : RemoveDirectoryA
373 * Purpose :
374 * Parameters:
375 * Variables :
376 * Result :
377 * Remark :
378 * Status :
379 *
380 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
381 *****************************************************************************/
382
383
384ODINFUNCTION1(BOOL,RemoveDirectoryA,LPCSTR,arg1)
385{
386 int len = strlen(arg1);
387 char *tmp=(char *)alloca(len + 1);
388
389 strcpy(tmp, arg1);
390 if(tmp[len -1] == '\\')
391 tmp[len -1] = 0;
392 return O32_RemoveDirectory(tmp);
393}
394
395
396/*****************************************************************************
397 * Name : RemoveDirectoryW
398 * Purpose :
399 * Parameters:
400 * Variables :
401 * Result :
402 * Remark :
403 * Status :
404 *
405 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
406 *****************************************************************************/
407
408ODINFUNCTION1(BOOL,RemoveDirectoryW,LPCWSTR,lpPathName)
409{
410 char *asciipath;
411 BOOL rc;
412
413 asciipath = UnicodeToAsciiString((LPWSTR)lpPathName);
414 rc = RemoveDirectoryA(asciipath);
415 FreeAsciiString(asciipath);
416 return(rc);
417}
418
419/***********************************************************************
420 * DIR_TryModulePath
421 *
422 * Helper function for DIR_SearchPath.
423 */
424static BOOL DIR_TryModulePath( LPCSTR name, char *full_name )
425{
426 char buffer[OFS_MAXPATHNAME];
427 LPSTR p;
428
429 if (!GetModuleFileNameA( 0, buffer, sizeof(buffer) ))
430 buffer[0]='\0';
431
432 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
433 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
434 strcpy( p, name );
435
436 return OSLibDosSearchPath(OSLIB_SEARCHFILE, NULL, buffer, full_name, MAX_PATHNAME_LEN);
437}
438
439
440/***********************************************************************
441 * DIR_SearchPath
442 *
443 * Implementation of SearchPath32A. 'win32' specifies whether the search
444 * order is Win16 (module path last) or Win32 (module path first).
445 *
446 * FIXME: should return long path names.
447 */
448DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
449 char *full_name )
450{
451 DWORD len;
452 LPCSTR p;
453 LPSTR tmp = NULL;
454 BOOL ret = TRUE;
455
456 /* First check the supplied parameters */
457
458 p = strrchr( name, '.' );
459 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
460 ext = NULL; /* Ignore the specified extension */
461 if ((*name && (name[1] == ':')) ||
462 strchr( name, '/' ) || strchr( name, '\\' ))
463 path = NULL; /* Ignore path if name already contains a path */
464 if (path && !*path) path = NULL; /* Ignore empty path */
465
466 len = strlen(name);
467 if (ext) len += strlen(ext);
468 if (path) len += strlen(path) + 1;
469
470 /* Allocate a buffer for the file name and extension */
471
472 if (path || ext)
473 {
474 if (!(tmp = (LPSTR)HeapAlloc( GetProcessHeap(), 0, len + 1 )))
475 {
476 SetLastError( ERROR_OUTOFMEMORY );
477 return 0;
478 }
479 if (path)
480 {
481 strcpy( tmp, path );
482 strcat( tmp, "\\" );
483 strcat( tmp, name );
484 }
485 else strcpy( tmp, name );
486 if (ext) strcat( tmp, ext );
487 name = tmp;
488 }
489
490 /* If we have an explicit path, everything's easy */
491
492 if (path || (*name && (name[1] == ':')) ||
493 strchr( name, '/' ) || strchr( name, '\\' ))
494 {
495 ret = OSLibDosSearchPath(OSLIB_SEARCHFILE, NULL, (LPSTR)name, full_name, MAX_PATHNAME_LEN);
496 goto done;
497 }
498
499 /* Try the path of the current executable (for Win32 search order) */
500 if (DIR_TryModulePath( name, full_name )) goto done;
501
502 /* Try the current directory */
503 if (OSLibDosSearchPath(OSLIB_SEARCHCURDIR, NULL, (LPSTR)name, full_name, MAX_PATHNAME_LEN))
504 goto done;
505
506 /* Try the Windows system directory */
507 if (OSLibDosSearchPath(OSLIB_SEARCHDIR, (LPSTR)&DIR_System, (LPSTR)name, full_name, MAX_PATHNAME_LEN))
508 goto done;
509
510 /* Try the Windows directory */
511 if (OSLibDosSearchPath(OSLIB_SEARCHDIR, (LPSTR)&DIR_Windows, (LPSTR)name, full_name, MAX_PATHNAME_LEN))
512 goto done;
513
514 /* Try all directories in path */
515 ret = OSLibDosSearchPath(OSLIB_SEARCHENV, "PATH", (LPSTR)name, full_name, MAX_PATHNAME_LEN);
516
517done:
518 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
519 return ret;
520}
521
522
523/***********************************************************************
524 * SearchPath32A [KERNEL32.447]
525 *
526 * Searches for a specified file in the search path.
527 *
528 * PARAMS
529 * path [I] Path to search
530 * name [I] Filename to search for.
531 * ext [I] File extension to append to file name. The first
532 * character must be a period. This parameter is
533 * specified only if the filename given does not
534 * contain an extension.
535 * buflen [I] size of buffer, in characters
536 * buffer [O] buffer for found filename
537 * lastpart [O] address of pointer to last used character in
538 * buffer (the final '\')
539 *
540 * RETURNS
541 * Success: length of string copied into buffer, not including
542 * terminating null character. If the filename found is
543 * longer than the length of the buffer, the length of the
544 * filename is returned.
545 * Failure: Zero
546 *
547 * NOTES
548 * Should call SetLastError(but currently doesn't).
549 */
550DWORD WINAPI SearchPathA(LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
551 LPSTR buffer, LPSTR *lastpart )
552{
553 char full_name[MAX_PATHNAME_LEN];
554
555 if (!DIR_SearchPath( path, name, ext, (LPSTR)full_name )) return 0;
556 lstrcpynA( buffer, (LPSTR)full_name, buflen-1);
557 buffer[buflen-2] = 0;
558 SetLastError(0);
559 return strlen(buffer);
560}
561
562
563/***********************************************************************
564 * SearchPath32W (KERNEL32.448)
565 */
566DWORD WINAPI SearchPathW(LPCWSTR path, LPCWSTR name, LPCWSTR ext,
567 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
568{
569 char full_name[MAX_PATHNAME_LEN];
570
571 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
572 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
573 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
574 DWORD ret = DIR_SearchPath( pathA, nameA, extA, (LPSTR)full_name );
575 HeapFree( GetProcessHeap(), 0, extA );
576 HeapFree( GetProcessHeap(), 0, nameA );
577 HeapFree( GetProcessHeap(), 0, pathA );
578 if (!ret) return 0;
579
580 lstrcpynAtoW( buffer, full_name, buflen-1 );
581 buffer[buflen-2] = 0;
582 SetLastError(0);
583 return strlen(full_name);
584}
Note: See TracBrowser for help on using the repository browser.