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

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

create reg keys for CSD version

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