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

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

Added new logging feature

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