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

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

GetFileAttributes, pe loader & command line fixes

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