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

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

Rewrote file io apis

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