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

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

version loading fixes + heap corruption fix

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