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

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

removed free's for alloca

File size: 16.9 KB
Line 
1/* $Id: directory.cpp,v 1.27 2000-06-13 21:19:32 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 = 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 return(rc);
339}
340
341
342/*****************************************************************************
343 * Name : GetWindowsDirectoryA
344 * Purpose :
345 * Parameters:
346 * Variables :
347 * Result :
348 * Remark : Should return length of system dir even if lpBuffer == NULL
349 * Status :
350 *
351 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
352 *****************************************************************************/
353
354ODINFUNCTION2(UINT,GetWindowsDirectoryA,LPSTR,lpBuffer,
355 UINT,uSize)
356{
357 char *dir;
358 int len;
359
360 dir = InternalGetWindowsDirectoryA();
361 len = lstrlenA(dir);
362 if(lpBuffer)
363 lstrcpynA(lpBuffer, dir, uSize);
364 return len;
365}
366
367
368/*****************************************************************************
369 * Name : GetWindowsDirectoryW
370 * Purpose :
371 * Parameters:
372 * Variables :
373 * Result :
374 * Remark : Should return length of system dir even if lpBuffer == NULL
375 * Status :
376 *
377 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
378 *****************************************************************************/
379
380ODINFUNCTION2(UINT,GetWindowsDirectoryW,LPWSTR,lpBuffer,
381 UINT, uSize)
382{
383 char *asciibuffer = NULL;
384 UINT rc;
385
386 if(lpBuffer)
387 asciibuffer = (char *)alloca(uSize+1);
388
389 if(lpBuffer && asciibuffer == NULL)
390 {
391 DebugInt3();
392 }
393
394 rc = GetWindowsDirectoryA(asciibuffer, uSize);
395 if(rc && asciibuffer)
396 AsciiToUnicode(asciibuffer, lpBuffer);
397
398 return(rc);
399}
400
401
402/*****************************************************************************
403 * Name : RemoveDirectoryA
404 * Purpose :
405 * Parameters:
406 * Variables :
407 * Result :
408 * Remark :
409 * Status :
410 *
411 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
412 *****************************************************************************/
413
414
415ODINFUNCTION1(BOOL, RemoveDirectoryA,
416 LPCSTR, lpstrDirectory)
417{
418 int len = strlen(lpstrDirectory);
419
420 // cut off trailing backslashes
421 if ( (lpstrDirectory[len - 1] == '\\') ||
422 (lpstrDirectory[len - 1] == '/') )
423 {
424 LPSTR lpTemp = (LPSTR)_alloca(len);
425 lstrcpynA(lpTemp,
426 lpstrDirectory,
427 len - 1);
428 lpTemp[len - 1] = 0;
429 lpstrDirectory = lpTemp;
430 }
431
432 dprintf(("RemoveDirectory %s",
433 lpstrDirectory));
434
435 return O32_RemoveDirectory(lpstrDirectory);
436}
437
438
439/*****************************************************************************
440 * Name : RemoveDirectoryW
441 * Purpose :
442 * Parameters:
443 * Variables :
444 * Result :
445 * Remark :
446 * Status :
447 *
448 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
449 *****************************************************************************/
450
451ODINFUNCTION1(BOOL,RemoveDirectoryW,LPCWSTR,lpPathName)
452{
453 char *asciipath;
454 BOOL rc;
455
456 asciipath = UnicodeToAsciiString((LPWSTR)lpPathName);
457 rc = RemoveDirectoryA(asciipath);
458 FreeAsciiString(asciipath);
459 return(rc);
460}
461
462/***********************************************************************
463 * DIR_TryModulePath
464 *
465 * Helper function for DIR_SearchPath.
466 */
467static BOOL DIR_TryModulePath( LPCSTR name, char *full_name )
468{
469 char buffer[OFS_MAXPATHNAME];
470 LPSTR p;
471
472 if (!GetModuleFileNameA( 0, buffer, sizeof(buffer) ))
473 buffer[0]='\0';
474
475 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
476 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
477 strcpy( p, name );
478
479 return OSLibDosSearchPath(OSLIB_SEARCHFILE, NULL, buffer, full_name, MAX_PATHNAME_LEN);
480}
481
482
483/***********************************************************************
484 * DIR_SearchPath
485 *
486 * Implementation of SearchPath32A. 'win32' specifies whether the search
487 * order is Win16 (module path last) or Win32 (module path first).
488 *
489 * FIXME: should return long path names.
490 */
491DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
492 char *full_name )
493{
494 DWORD len;
495 LPCSTR p;
496 LPSTR tmp = NULL;
497 BOOL ret = TRUE;
498
499 /* First check the supplied parameters */
500
501 p = strrchr( name, '.' );
502 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
503 ext = NULL; /* Ignore the specified extension */
504 if ((*name && (name[1] == ':')) ||
505 strchr( name, '/' ) || strchr( name, '\\' ))
506 path = NULL; /* Ignore path if name already contains a path */
507 if (path && !*path) path = NULL; /* Ignore empty path */
508
509 len = strlen(name);
510 if (ext) len += strlen(ext);
511 if (path) len += strlen(path) + 1;
512
513 /* Allocate a buffer for the file name and extension */
514
515 if (path || ext)
516 {
517 if (!(tmp = (LPSTR)HeapAlloc( GetProcessHeap(), 0, len + 1 )))
518 {
519 SetLastError( ERROR_OUTOFMEMORY );
520 return 0;
521 }
522 if (path)
523 {
524 strcpy( tmp, path );
525 strcat( tmp, "\\" );
526 strcat( tmp, name );
527 }
528 else strcpy( tmp, name );
529 if (ext) strcat( tmp, ext );
530 name = tmp;
531 }
532
533 /* If we have an explicit path, everything's easy */
534
535 if (path || (*name && (name[1] == ':')) ||
536 strchr( name, '/' ) || strchr( name, '\\' ))
537 {
538 ret = OSLibDosSearchPath(OSLIB_SEARCHFILE, NULL, (LPSTR)name, full_name, MAX_PATHNAME_LEN);
539 goto done;
540 }
541
542 /* Try the path of the current executable (for Win32 search order) */
543 if (DIR_TryModulePath( name, full_name )) goto done;
544
545 /* Try the current directory */
546 if (OSLibDosSearchPath(OSLIB_SEARCHCURDIR, NULL, (LPSTR)name, full_name, MAX_PATHNAME_LEN))
547 goto done;
548
549 /* Try the Windows system directory */
550 if (OSLibDosSearchPath(OSLIB_SEARCHDIR, (LPSTR)&DIR_System, (LPSTR)name, full_name, MAX_PATHNAME_LEN))
551 goto done;
552
553 /* Try the Windows directory */
554 if (OSLibDosSearchPath(OSLIB_SEARCHDIR, (LPSTR)&DIR_Windows, (LPSTR)name, full_name, MAX_PATHNAME_LEN))
555 goto done;
556
557 /* Try all directories in path */
558 ret = OSLibDosSearchPath(OSLIB_SEARCHENV, "PATH", (LPSTR)name, full_name, MAX_PATHNAME_LEN);
559
560done:
561 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
562 return ret;
563}
564
565
566/***********************************************************************
567 * SearchPath32A [KERNEL32.447]
568 *
569 * Searches for a specified file in the search path.
570 *
571 * PARAMS
572 * path [I] Path to search
573 * name [I] Filename to search for.
574 * ext [I] File extension to append to file name. The first
575 * character must be a period. This parameter is
576 * specified only if the filename given does not
577 * contain an extension.
578 * buflen [I] size of buffer, in characters
579 * buffer [O] buffer for found filename
580 * lastpart [O] address of pointer to last used character in
581 * buffer (the final '\')
582 *
583 * RETURNS
584 * Success: length of string copied into buffer, not including
585 * terminating null character. If the filename found is
586 * longer than the length of the buffer, the length of the
587 * filename is returned.
588 * Failure: Zero
589 *
590 * NOTES
591 * Should call SetLastError(but currently doesn't).
592 */
593DWORD WINAPI SearchPathA(LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
594 LPSTR buffer, LPSTR *lastpart )
595{
596 char full_name[MAX_PATHNAME_LEN];
597
598 if (!DIR_SearchPath( path, name, ext, (LPSTR)full_name )) return 0;
599 lstrcpynA( buffer, (LPSTR)full_name, buflen-1);
600 buffer[buflen-2] = 0;
601 SetLastError(0);
602 return strlen(buffer);
603}
604
605
606/***********************************************************************
607 * SearchPath32W (KERNEL32.448)
608 */
609DWORD WINAPI SearchPathW(LPCWSTR path, LPCWSTR name, LPCWSTR ext,
610 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
611{
612 char full_name[MAX_PATHNAME_LEN];
613
614 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
615 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
616 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
617 DWORD ret = DIR_SearchPath( pathA, nameA, extA, (LPSTR)full_name );
618 HeapFree( GetProcessHeap(), 0, extA );
619 HeapFree( GetProcessHeap(), 0, nameA );
620 HeapFree( GetProcessHeap(), 0, pathA );
621 if (!ret) return 0;
622
623 lstrcpynAtoW( buffer, full_name, buflen-1 );
624 buffer[buflen-2] = 0;
625 SetLastError(0);
626 return strlen(full_name);
627}
Note: See TracBrowser for help on using the repository browser.