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

Last change on this file since 6004 was 5819, checked in by phaller, 24 years ago

Fix CreateDirectory

File size: 21.6 KB
Line 
1/* $Id: directory.cpp,v 1.36 2001-05-28 16:23:42 phaller 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 = O32_GetCurrentDirectory(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",
258 lpstrDirectory));
259
260 // PH Note 2000/06/12:
261 // Creation of an existing directory is NO ERROR it seems.
262 DWORD dwAttr = GetFileAttributesA(lpstrDirectory);
263 if (dwAttr != -1)
264 if (dwAttr & FILE_ATTRIBUTE_DIRECTORY)
265 {
266 SetLastError(ERROR_SUCCESS);
267 return TRUE;
268 }
269
270 return(O32_CreateDirectory(lpstrDirectory,
271 arg2));
272}
273
274/*****************************************************************************
275 * Name : CreateDirectoryW
276 * Purpose :
277 * Parameters:
278 * Variables :
279 * Result :
280 * Remark :
281 * Status :
282 *
283 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
284 *****************************************************************************/
285
286ODINFUNCTION2(BOOL,CreateDirectoryW,LPCWSTR, arg1,
287 PSECURITY_ATTRIBUTES,arg2)
288{
289 BOOL rc;
290 char *astring;
291
292 astring = UnicodeToAsciiString((LPWSTR)arg1);
293 rc = CreateDirectoryA(astring, arg2);
294 FreeAsciiString(astring);
295 return(rc);
296}
297
298/*****************************************************************************
299 * Name : BOOL WIN32API CreateDirectoryExA
300 * Purpose : The CreateDirectoryExA function creates a new directory with a
301 * specified path that retains the attributes of a specified
302 * template directory. If the underlying file system supports
303 * security on files and directories, the function applies a
304 * specified security descriptor to the new directory.
305 * The new directory retains the other attributes of the specified
306 * template directory. Note that CreateDirectoryEx has a template
307 * parameter, while CreateDirectory does not.
308 * Parameters: LPCSTR lpTemplateDirectory pointer to path string of template
309 * directory
310 * LPCSTR lpNewDirectory pointer to path string of directory
311 * to create
312 * LPSECURITY_ATTRIBUTES lpSecurityAttributes pointer to security
313 * descriptor
314 *
315 * Variables :
316 * Result : If the function succeeds, the return value is nonzero.
317 * If the function fails, the return value is zero.
318 * To get extended error information, call GetLastError.
319 * Remark :
320 * Status : UNTESTED STUB
321 *
322 * Author : Markus Montkowski [Tha, 1998/05/21 17:46]
323 *****************************************************************************/
324
325BOOL WIN32API CreateDirectoryExA( LPCSTR lpTemplateDirectory,
326 LPCSTR lpNewDirectory,
327 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
328{
329
330 dprintf(("KERNEL32:CreateDirectoryExA(%08x,%08x,%08x) not properly implemented\n",
331 lpTemplateDirectory,lpNewDirectory,lpSecurityAttributes
332 ));
333
334 return CreateDirectoryA(lpNewDirectory, lpSecurityAttributes);
335}
336
337/*****************************************************************************
338 * Name : BOOL WIN32API CreateDirectoryExW
339 * Purpose : The CreateDirectoryExW function creates a new directory with a
340 * specified path that retains the attributes of a specified
341 * template directory. If the underlying file system supports
342 * security on files and directories, the function applies a
343 * specified security descriptor to the new directory.
344 * The new directory retains the other attributes of the specified
345 * template directory. Note that CreateDirectoryEx has a template
346 * parameter, while CreateDirectory does not.
347 * Parameters: LPCWSTR lpTemplateDirectory pointer to path string of template
348 * directory
349 * LPCWSTR lpNewDirectory pointer to path string of directory
350 * to create
351 * LPSECURITY_ATTRIBUTES lpSecurityAttributes pointer to security
352 * descriptor
353 *
354 * Variables :
355 * Result : If the function succeeds, the return value is nonzero.
356 * If the function fails, the return value is zero.
357 * To get extended error information, call GetLastError.
358 * Remark :
359 * Status : UNTESTED STUB
360 *
361 * Author : Markus Montkowski [Tha, 1998/05/21 17:46]
362 *****************************************************************************/
363
364BOOL WIN32API CreateDirectoryExW( LPCWSTR lpTemplateDirectory,
365 LPCWSTR lpNewDirectory,
366 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
367{
368
369 dprintf(("KERNEL32:CreateDirectoryExW(%08x,%08x,%08x) not properly implemented\n",
370 lpTemplateDirectory,lpNewDirectory,lpSecurityAttributes
371 ));
372
373 return CreateDirectoryW(lpNewDirectory, lpSecurityAttributes);
374}
375
376/*****************************************************************************
377 * Name : GetSystemDirectoryA
378 * Purpose :
379 * Parameters:
380 * Variables :
381 * Result :
382 * Remark : Should return length of system dir even if lpBuffer == NULL
383 * Status :
384 *
385 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
386 *****************************************************************************/
387
388ODINFUNCTION2(UINT,GetSystemDirectoryA,LPSTR,lpBuffer,
389 UINT,uSize)
390{
391 int len;
392 char *dir;
393
394 dir = InternalGetSystemDirectoryA();
395 len = lstrlenA(dir);
396 if(lpBuffer)
397 lstrcpynA(lpBuffer, dir, uSize);
398 return len;
399}
400
401
402/*****************************************************************************
403 * Name : GetSystemDirectoryW
404 * Purpose :
405 * Parameters:
406 * Variables :
407 * Result :
408 * Remark : Should return length of system dir even if lpBuffer == NULL
409 * Status :
410 *
411 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
412 *****************************************************************************/
413
414ODINFUNCTION2(UINT,GetSystemDirectoryW,LPWSTR,lpBuffer,
415 UINT, uSize)
416{
417 char *asciibuffer = NULL;
418 UINT rc;
419
420 if(lpBuffer)
421 asciibuffer = (char *)alloca(uSize+1);
422
423 if(lpBuffer && asciibuffer == NULL)
424 {
425 DebugInt3();
426 }
427
428 rc = GetSystemDirectoryA(asciibuffer, uSize);
429 if(rc && asciibuffer)
430 AsciiToUnicode(asciibuffer, lpBuffer);
431
432 return(rc);
433}
434
435
436/*****************************************************************************
437 * Name : GetWindowsDirectoryA
438 * Purpose :
439 * Parameters:
440 * Variables :
441 * Result :
442 * Remark : Should return length of system dir even if lpBuffer == NULL
443 * Status :
444 *
445 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
446 *****************************************************************************/
447
448ODINFUNCTION2(UINT,GetWindowsDirectoryA,LPSTR,lpBuffer,
449 UINT,uSize)
450{
451 char *dir;
452 int len;
453
454 dir = InternalGetWindowsDirectoryA();
455 len = lstrlenA(dir);
456 if(lpBuffer)
457 lstrcpynA(lpBuffer, dir, uSize);
458 return len;
459}
460
461
462/*****************************************************************************
463 * Name : GetWindowsDirectoryW
464 * Purpose :
465 * Parameters:
466 * Variables :
467 * Result :
468 * Remark : Should return length of system dir even if lpBuffer == NULL
469 * Status :
470 *
471 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
472 *****************************************************************************/
473
474ODINFUNCTION2(UINT,GetWindowsDirectoryW,LPWSTR,lpBuffer,
475 UINT, uSize)
476{
477 char *asciibuffer = NULL;
478 UINT rc;
479
480 if(lpBuffer)
481 asciibuffer = (char *)alloca(uSize+1);
482
483 if(lpBuffer && asciibuffer == NULL)
484 {
485 DebugInt3();
486 }
487
488 rc = GetWindowsDirectoryA(asciibuffer, uSize);
489 if(rc && asciibuffer)
490 AsciiToUnicode(asciibuffer, lpBuffer);
491
492 return(rc);
493}
494
495
496/*****************************************************************************
497 * Name : RemoveDirectoryA
498 * Purpose :
499 * Parameters:
500 * Variables :
501 * Result :
502 * Remark :
503 * Status :
504 *
505 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
506 *****************************************************************************/
507
508
509ODINFUNCTION1(BOOL, RemoveDirectoryA,
510 LPCSTR, lpstrDirectory)
511{
512 int len = strlen(lpstrDirectory);
513
514 // cut off trailing backslashes
515 if ( (lpstrDirectory[len - 1] == '\\') ||
516 (lpstrDirectory[len - 1] == '/') )
517 {
518 LPSTR lpTemp = (LPSTR)_alloca(len);
519 lstrcpynA(lpTemp,
520 lpstrDirectory,
521 len ); // len is including trailing NULL!!
522 lpstrDirectory = lpTemp;
523 }
524
525 dprintf(("RemoveDirectory %s",
526 lpstrDirectory));
527
528 return O32_RemoveDirectory(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.