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

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

.

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