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

Last change on this file since 6496 was 6094, checked in by sandervl, 24 years ago

rewrote CreateDirectory

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