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

Last change on this file since 3699 was 3699, checked in by phaller, 25 years ago

Fix reverted: alloca back in

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