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

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

* empty log message *

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