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

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

setcurrentdir fix + create shell dirs + keys during kernel32 init

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