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

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

changes for setting current dir of new process

File size: 20.8 KB
Line 
1/* $Id: directory.cpp,v 1.31 2000-10-06 15:16:03 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:
17 * - System/window directories should be created by install program!
18 *
19 * Project Odin Software License can be found in LICENSE.TXT
20 *
21 */
22
23
24/*****************************************************************************
25 * Includes *
26 *****************************************************************************/
27
28#include <odin.h>
29#include <odinwrap.h>
30#include <os2win.h>
31#include <stdlib.h>
32#include <unicode.h>
33#include <heapstring.h>
34#include <options.h>
35#include "initterm.h"
36#include <win\file.h>
37#include <string.h>
38#include "oslibdos.h"
39#include "profile.h"
40
41#define DBG_LOCALLOG DBG_directory
42#include "dbglocal.h"
43
44ODINDEBUGCHANNEL(KERNEL32-DIRECTORY)
45
46
47/*****************************************************************************
48 * Local Prototypes *
49 *****************************************************************************/
50
51
52static char DIR_Windows[MAX_PATHNAME_LEN];
53static char DIR_System[MAX_PATHNAME_LEN];
54
55//******************************************************************************
56//******************************************************************************
57char *InternalGetWindowsDirectoryA()
58{
59 return DIR_Windows;
60}
61//******************************************************************************
62//******************************************************************************
63char *InternalGetSystemDirectoryA()
64{
65 return DIR_System;
66}
67//******************************************************************************
68//******************************************************************************
69void InitDirectories()
70{
71 char *endofwinpath, *tmp;
72 int len;
73
74 strcpy(DIR_System, kernel32Path);
75 len = strlen(DIR_System);
76 if(DIR_System[len-1] == '\\') {
77 DIR_System[len-1] = 0;
78 }
79 len = PROFILE_GetOdinIniString(ODINDIRECTORIES,"WINDOWS","",DIR_Windows,sizeof(DIR_Windows));
80 if (len > 2) {
81 if(DIR_Windows[len-1] == '\\') {
82 DIR_Windows[len-1] = 0;
83 }
84 }
85 else {
86 strcpy(DIR_Windows, DIR_System);
87 endofwinpath = tmp = strchr(DIR_Windows, '\\');
88 while(tmp) {
89 tmp = strchr(endofwinpath+1, '\\');
90 if(tmp)
91 endofwinpath = tmp;
92 }
93 if(endofwinpath) {
94 *endofwinpath = 0; //remove \SYSTEM32
95 }
96 else DebugInt3();
97 }
98}
99/*****************************************************************************
100 * Name : GetCurrentDirectoryA
101 * Purpose : query the current directory
102 * Parameters:
103 * Variables :
104 * Result :
105 * Remark :
106 * Status :
107 *
108 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
109 *****************************************************************************/
110
111ODINFUNCTION2(UINT, GetCurrentDirectoryA, UINT, nBufferLength,
112 LPSTR, lpBuffer)
113{
114 UINT rc;
115
116 rc = O32_GetCurrentDirectory(nBufferLength, lpBuffer);
117 if(rc) {
118 dprintf(("CurrentDirectory = %s", lpBuffer));
119 }
120 return rc;
121}
122
123
124/*****************************************************************************
125 * Name : GetCurrentDirectoryW
126 * Purpose : query the current directory
127 * Parameters:
128 * Variables :
129 * Result :
130 * Remark :
131 * Status :
132 *
133 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
134 *****************************************************************************/
135
136ODINFUNCTION2(UINT, GetCurrentDirectoryW, UINT, nBufferLength,
137 LPWSTR, lpBuffer)
138{
139 char *asciidir = (char *)malloc(nBufferLength+1);
140 int rc;
141
142 rc = O32_GetCurrentDirectory(nBufferLength, asciidir);
143 if(rc != 0)
144 AsciiToUnicode(asciidir, lpBuffer);
145 free(asciidir);
146 return(rc);
147}
148
149
150/*****************************************************************************
151 * Name : SetCurrentDirectoryA
152 * Purpose :
153 * Parameters:
154 * Variables :
155 * Result :
156 * Remark :
157 * Status :
158 *
159 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
160 *****************************************************************************/
161
162
163ODINFUNCTION1(BOOL, SetCurrentDirectoryA,
164 LPCSTR, lpstrDirectory)
165{
166 if(HIWORD(lpstrDirectory) == 0)
167 {
168 SetLastError(ERROR_INVALID_PARAMETER);
169 return FALSE;
170 }
171
172 // cut off trailing backslashes
173 // not if a process wants to change to the root directory
174 int len = lstrlenA(lpstrDirectory);
175 if ( ( (lpstrDirectory[len - 1] == '\\') ||
176 (lpstrDirectory[len - 1] == '/') ) &&
177 (len != 1) )
178 {
179 LPSTR lpTemp = (LPSTR)_alloca(len);
180 lstrcpynA(lpTemp,
181 lpstrDirectory,
182 len); // len is including trailing NULL!!
183 lpstrDirectory = lpTemp;
184 }
185
186 dprintf(("SetCurrentDirectoryA %s", lpstrDirectory));
187 return O32_SetCurrentDirectory((LPSTR)lpstrDirectory);
188}
189
190
191/*****************************************************************************
192 * Name : SetCurrentDirectoryW
193 * Purpose :
194 * Parameters:
195 * Variables :
196 * Result :
197 * Remark :
198 * Status :
199 *
200 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
201 *****************************************************************************/
202
203ODINFUNCTION1(BOOL,SetCurrentDirectoryW,LPCWSTR,lpPathName)
204{
205 char *asciipath;
206 BOOL rc;
207
208 asciipath = UnicodeToAsciiString((LPWSTR)lpPathName);
209 rc = SetCurrentDirectoryA(asciipath);
210 FreeAsciiString(asciipath);
211 return(rc);
212}
213
214
215/*****************************************************************************
216 * Name : CreateDirectoryA
217 * Purpose :
218 * Parameters:
219 * Variables :
220 * Result :
221 * Remark :
222 * Status :
223 *
224 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
225 *****************************************************************************/
226
227ODINFUNCTION2(BOOL, CreateDirectoryA,
228 LPCSTR, lpstrDirectory,
229 PSECURITY_ATTRIBUTES,arg2)
230{
231 int len = strlen(lpstrDirectory);
232
233 // cut off trailing backslashes
234 if ( (lpstrDirectory[len - 1] == '\\') ||
235 (lpstrDirectory[len - 1] == '/') )
236 {
237 LPSTR lpTemp = (LPSTR)_alloca(len);
238 lstrcpynA(lpTemp,
239 lpstrDirectory,
240 len ); // len is including trailing NULL!!
241 lpstrDirectory = lpTemp;
242 }
243
244 dprintf(("CreateDirectoryA %s",
245 lpstrDirectory));
246
247 // PH Note 2000/06/12:
248 // Creation of an existing directory is NO ERROR it seems.
249 DWORD dwAttr = GetFileAttributesA(lpstrDirectory);
250 if (dwAttr != -1)
251 if (dwAttr & FILE_ATTRIBUTE_DIRECTORY)
252 {
253 SetLastError(ERROR_SUCCESS);
254 return TRUE;
255 }
256
257 return(O32_CreateDirectory(lpstrDirectory,
258 arg2));
259}
260
261/*****************************************************************************
262 * Name : CreateDirectoryW
263 * Purpose :
264 * Parameters:
265 * Variables :
266 * Result :
267 * Remark :
268 * Status :
269 *
270 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
271 *****************************************************************************/
272
273ODINFUNCTION2(BOOL,CreateDirectoryW,LPCWSTR, arg1,
274 PSECURITY_ATTRIBUTES,arg2)
275{
276 BOOL rc;
277 char *astring;
278
279 astring = UnicodeToAsciiString((LPWSTR)arg1);
280 rc = CreateDirectoryA(astring, arg2);
281 FreeAsciiString(astring);
282 return(rc);
283}
284
285/*****************************************************************************
286 * Name : BOOL WIN32API CreateDirectoryExA
287 * Purpose : The CreateDirectoryExA function creates a new directory with a
288 * specified path that retains the attributes of a specified
289 * template directory. If the underlying file system supports
290 * security on files and directories, the function applies a
291 * specified security descriptor to the new directory.
292 * The new directory retains the other attributes of the specified
293 * template directory. Note that CreateDirectoryEx has a template
294 * parameter, while CreateDirectory does not.
295 * Parameters: LPCSTR lpTemplateDirectory pointer to path string of template
296 * directory
297 * LPCSTR lpNewDirectory pointer to path string of directory
298 * to create
299 * LPSECURITY_ATTRIBUTES lpSecurityAttributes pointer to security
300 * descriptor
301 *
302 * Variables :
303 * Result : If the function succeeds, the return value is nonzero.
304 * If the function fails, the return value is zero.
305 * To get extended error information, call GetLastError.
306 * Remark :
307 * Status : UNTESTED STUB
308 *
309 * Author : Markus Montkowski [Tha, 1998/05/21 17:46]
310 *****************************************************************************/
311
312BOOL WIN32API CreateDirectoryExA( LPCSTR lpTemplateDirectory,
313 LPCSTR lpNewDirectory,
314 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
315{
316
317 dprintf(("KERNEL32:CreateDirectoryExA(%08x,%08x,%08x) not properly implemented\n",
318 lpTemplateDirectory,lpNewDirectory,lpSecurityAttributes
319 ));
320
321 return CreateDirectoryA(lpNewDirectory, lpSecurityAttributes);
322}
323
324/*****************************************************************************
325 * Name : BOOL WIN32API CreateDirectoryExW
326 * Purpose : The CreateDirectoryExW function creates a new directory with a
327 * specified path that retains the attributes of a specified
328 * template directory. If the underlying file system supports
329 * security on files and directories, the function applies a
330 * specified security descriptor to the new directory.
331 * The new directory retains the other attributes of the specified
332 * template directory. Note that CreateDirectoryEx has a template
333 * parameter, while CreateDirectory does not.
334 * Parameters: LPCWSTR lpTemplateDirectory pointer to path string of template
335 * directory
336 * LPCWSTR lpNewDirectory pointer to path string of directory
337 * to create
338 * LPSECURITY_ATTRIBUTES lpSecurityAttributes pointer to security
339 * descriptor
340 *
341 * Variables :
342 * Result : If the function succeeds, the return value is nonzero.
343 * If the function fails, the return value is zero.
344 * To get extended error information, call GetLastError.
345 * Remark :
346 * Status : UNTESTED STUB
347 *
348 * Author : Markus Montkowski [Tha, 1998/05/21 17:46]
349 *****************************************************************************/
350
351BOOL WIN32API CreateDirectoryExW( LPCWSTR lpTemplateDirectory,
352 LPCWSTR lpNewDirectory,
353 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
354{
355
356 dprintf(("KERNEL32:CreateDirectoryExW(%08x,%08x,%08x) not properly implemented\n",
357 lpTemplateDirectory,lpNewDirectory,lpSecurityAttributes
358 ));
359
360 return CreateDirectoryW(lpNewDirectory, lpSecurityAttributes);
361}
362
363/*****************************************************************************
364 * Name : GetSystemDirectoryA
365 * Purpose :
366 * Parameters:
367 * Variables :
368 * Result :
369 * Remark : Should return length of system dir even if lpBuffer == NULL
370 * Status :
371 *
372 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
373 *****************************************************************************/
374
375ODINFUNCTION2(UINT,GetSystemDirectoryA,LPSTR,lpBuffer,
376 UINT,uSize)
377{
378 int len;
379 char *dir;
380
381 dir = InternalGetSystemDirectoryA();
382 len = lstrlenA(dir);
383 if(lpBuffer)
384 lstrcpynA(lpBuffer, dir, uSize);
385 return len;
386}
387
388
389/*****************************************************************************
390 * Name : GetSystemDirectoryW
391 * Purpose :
392 * Parameters:
393 * Variables :
394 * Result :
395 * Remark : Should return length of system dir even if lpBuffer == NULL
396 * Status :
397 *
398 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
399 *****************************************************************************/
400
401ODINFUNCTION2(UINT,GetSystemDirectoryW,LPWSTR,lpBuffer,
402 UINT, uSize)
403{
404 char *asciibuffer = NULL;
405 UINT rc;
406
407 if(lpBuffer)
408 asciibuffer = (char *)alloca(uSize+1);
409
410 if(lpBuffer && asciibuffer == NULL)
411 {
412 DebugInt3();
413 }
414
415 rc = GetSystemDirectoryA(asciibuffer, uSize);
416 if(rc && asciibuffer)
417 AsciiToUnicode(asciibuffer, lpBuffer);
418
419 return(rc);
420}
421
422
423/*****************************************************************************
424 * Name : GetWindowsDirectoryA
425 * Purpose :
426 * Parameters:
427 * Variables :
428 * Result :
429 * Remark : Should return length of system dir even if lpBuffer == NULL
430 * Status :
431 *
432 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
433 *****************************************************************************/
434
435ODINFUNCTION2(UINT,GetWindowsDirectoryA,LPSTR,lpBuffer,
436 UINT,uSize)
437{
438 char *dir;
439 int len;
440
441 dir = InternalGetWindowsDirectoryA();
442 len = lstrlenA(dir);
443 if(lpBuffer)
444 lstrcpynA(lpBuffer, dir, uSize);
445 return len;
446}
447
448
449/*****************************************************************************
450 * Name : GetWindowsDirectoryW
451 * Purpose :
452 * Parameters:
453 * Variables :
454 * Result :
455 * Remark : Should return length of system dir even if lpBuffer == NULL
456 * Status :
457 *
458 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
459 *****************************************************************************/
460
461ODINFUNCTION2(UINT,GetWindowsDirectoryW,LPWSTR,lpBuffer,
462 UINT, uSize)
463{
464 char *asciibuffer = NULL;
465 UINT rc;
466
467 if(lpBuffer)
468 asciibuffer = (char *)alloca(uSize+1);
469
470 if(lpBuffer && asciibuffer == NULL)
471 {
472 DebugInt3();
473 }
474
475 rc = GetWindowsDirectoryA(asciibuffer, uSize);
476 if(rc && asciibuffer)
477 AsciiToUnicode(asciibuffer, lpBuffer);
478
479 return(rc);
480}
481
482
483/*****************************************************************************
484 * Name : RemoveDirectoryA
485 * Purpose :
486 * Parameters:
487 * Variables :
488 * Result :
489 * Remark :
490 * Status :
491 *
492 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
493 *****************************************************************************/
494
495
496ODINFUNCTION1(BOOL, RemoveDirectoryA,
497 LPCSTR, lpstrDirectory)
498{
499 int len = strlen(lpstrDirectory);
500
501 // cut off trailing backslashes
502 if ( (lpstrDirectory[len - 1] == '\\') ||
503 (lpstrDirectory[len - 1] == '/') )
504 {
505 LPSTR lpTemp = (LPSTR)_alloca(len);
506 lstrcpynA(lpTemp,
507 lpstrDirectory,
508 len ); // len is including trailing NULL!!
509 lpstrDirectory = lpTemp;
510 }
511
512 dprintf(("RemoveDirectory %s",
513 lpstrDirectory));
514
515 return O32_RemoveDirectory(lpstrDirectory);
516}
517
518
519/*****************************************************************************
520 * Name : RemoveDirectoryW
521 * Purpose :
522 * Parameters:
523 * Variables :
524 * Result :
525 * Remark :
526 * Status :
527 *
528 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
529 *****************************************************************************/
530
531ODINFUNCTION1(BOOL,RemoveDirectoryW,LPCWSTR,lpPathName)
532{
533 char *asciipath;
534 BOOL rc;
535
536 asciipath = UnicodeToAsciiString((LPWSTR)lpPathName);
537 rc = RemoveDirectoryA(asciipath);
538 FreeAsciiString(asciipath);
539 return(rc);
540}
541
542/***********************************************************************
543 * DIR_TryModulePath
544 *
545 * Helper function for DIR_SearchPath.
546 */
547static BOOL DIR_TryModulePath( LPCSTR name, char *full_name )
548{
549 char buffer[OFS_MAXPATHNAME];
550 LPSTR p;
551
552 if (!GetModuleFileNameA( 0, buffer, sizeof(buffer) ))
553 buffer[0]='\0';
554
555 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
556 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
557 strcpy( p, name );
558
559 return OSLibDosSearchPath(OSLIB_SEARCHFILE, NULL, buffer, full_name, MAX_PATHNAME_LEN);
560}
561
562
563/***********************************************************************
564 * DIR_SearchPath
565 *
566 * Implementation of SearchPath32A. 'win32' specifies whether the search
567 * order is Win16 (module path last) or Win32 (module path first).
568 *
569 * FIXME: should return long path names.
570 */
571DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
572 char *full_name )
573{
574 DWORD len;
575 LPCSTR p;
576 LPSTR tmp = NULL;
577 BOOL ret = TRUE;
578
579 /* First check the supplied parameters */
580
581 p = strrchr( name, '.' );
582 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
583 ext = NULL; /* Ignore the specified extension */
584 if ((*name && (name[1] == ':')) ||
585 strchr( name, '/' ) || strchr( name, '\\' ))
586 path = NULL; /* Ignore path if name already contains a path */
587 if (path && !*path) path = NULL; /* Ignore empty path */
588
589 len = strlen(name);
590 if (ext) len += strlen(ext);
591 if (path) len += strlen(path) + 1;
592
593 /* Allocate a buffer for the file name and extension */
594
595 if (path || ext)
596 {
597 if (!(tmp = (LPSTR)HeapAlloc( GetProcessHeap(), 0, len + 1 )))
598 {
599 SetLastError( ERROR_OUTOFMEMORY );
600 return 0;
601 }
602 if (path)
603 {
604 strcpy( tmp, path );
605 strcat( tmp, "\\" );
606 strcat( tmp, name );
607 }
608 else strcpy( tmp, name );
609 if (ext) strcat( tmp, ext );
610 name = tmp;
611 }
612
613 /* If we have an explicit path, everything's easy */
614
615 if (path || (*name && (name[1] == ':')) ||
616 strchr( name, '/' ) || strchr( name, '\\' ))
617 {
618 ret = OSLibDosSearchPath(OSLIB_SEARCHFILE, NULL, (LPSTR)name, full_name, MAX_PATHNAME_LEN);
619 goto done;
620 }
621
622 /* Try the path of the current executable (for Win32 search order) */
623 if (DIR_TryModulePath( name, full_name )) goto done;
624
625 /* Try the current directory */
626 if (OSLibDosSearchPath(OSLIB_SEARCHCURDIR, NULL, (LPSTR)name, full_name, MAX_PATHNAME_LEN))
627 goto done;
628
629 /* Try the Windows system directory */
630 if (OSLibDosSearchPath(OSLIB_SEARCHDIR, (LPSTR)&DIR_System, (LPSTR)name, full_name, MAX_PATHNAME_LEN))
631 goto done;
632
633 /* Try the Windows directory */
634 if (OSLibDosSearchPath(OSLIB_SEARCHDIR, (LPSTR)&DIR_Windows, (LPSTR)name, full_name, MAX_PATHNAME_LEN))
635 goto done;
636
637 /* Try all directories in path */
638 ret = OSLibDosSearchPath(OSLIB_SEARCHENV, "PATH", (LPSTR)name, full_name, MAX_PATHNAME_LEN);
639
640done:
641 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
642 return ret;
643}
644
645
646/***********************************************************************
647 * SearchPath32A [KERNEL32.447]
648 *
649 * Searches for a specified file in the search path.
650 *
651 * PARAMS
652 * path [I] Path to search
653 * name [I] Filename to search for.
654 * ext [I] File extension to append to file name. The first
655 * character must be a period. This parameter is
656 * specified only if the filename given does not
657 * contain an extension.
658 * buflen [I] size of buffer, in characters
659 * buffer [O] buffer for found filename
660 * lastpart [O] address of pointer to last used character in
661 * buffer (the final '\')
662 *
663 * RETURNS
664 * Success: length of string copied into buffer, not including
665 * terminating null character. If the filename found is
666 * longer than the length of the buffer, the length of the
667 * filename is returned.
668 * Failure: Zero
669 *
670 * NOTES
671 * Should call SetLastError(but currently doesn't).
672 */
673DWORD WINAPI SearchPathA(LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
674 LPSTR buffer, LPSTR *lastpart )
675{
676 char full_name[MAX_PATHNAME_LEN];
677
678 if (!DIR_SearchPath( path, name, ext, (LPSTR)full_name )) return 0;
679 lstrcpynA( buffer, (LPSTR)full_name, buflen);
680 SetLastError(0);
681 return strlen(buffer);
682}
683
684
685/***********************************************************************
686 * SearchPath32W (KERNEL32.448)
687 */
688DWORD WINAPI SearchPathW(LPCWSTR path, LPCWSTR name, LPCWSTR ext,
689 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
690{
691 char full_name[MAX_PATHNAME_LEN];
692
693 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
694 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
695 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
696 DWORD ret = DIR_SearchPath( pathA, nameA, extA, (LPSTR)full_name );
697 HeapFree( GetProcessHeap(), 0, extA );
698 HeapFree( GetProcessHeap(), 0, nameA );
699 HeapFree( GetProcessHeap(), 0, pathA );
700 if (!ret) return 0;
701
702 lstrcpynAtoW( buffer, full_name, buflen);
703 SetLastError(0);
704 return strlen(full_name);
705}
Note: See TracBrowser for help on using the repository browser.