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

Last change on this file since 1683 was 1646, checked in by sandervl, 26 years ago

EB's dir fixes (backslash)

File size: 14.8 KB
Line 
1/* $Id: directory.cpp,v 1.10 1999-11-08 21:24:24 sandervl Exp $ */
2
3/*
4 * Win32 Directory functions for OS/2
5 *
6 * Copyright 1998 Sander van Leeuwen
7 *
8 * Parts based on Wine code (991031) (files\directory.c)
9 *
10 * DOS directories functions
11 *
12 * Copyright 1995 Alexandre Julliard
13 *
14 * Project Odin Software License can be found in LICENSE.TXT
15 *
16 */
17
18
19/*****************************************************************************
20 * Includes *
21 *****************************************************************************/
22
23#include <odin.h>
24#include <odinwrap.h>
25#include <os2win.h>
26#include <stdlib.h>
27#include <unicode.h>
28#include <heapstring.h>
29#include <options.h>
30#include "initterm.h"
31#include <win\file.h>
32#include <string.h>
33#include "oslibdos.h"
34
35ODINDEBUGCHANNEL(KERNEL32-DIRECTORY)
36
37
38static char DIR_Windows[MAX_PATHNAME_LEN];
39static char DIR_System[MAX_PATHNAME_LEN];
40
41//******************************************************************************
42//******************************************************************************
43void InitDirectories()
44{
45 GetWindowsDirectoryA((LPSTR)&DIR_Windows, sizeof(DIR_Windows));
46 GetSystemDirectoryA((LPSTR)&DIR_System, sizeof(DIR_System));
47}
48
49/*****************************************************************************
50 * Name : GetCurrentDirectoryA
51 * Purpose : query the current directory
52 * Parameters:
53 * Variables :
54 * Result :
55 * Remark :
56 * Status :
57 *
58 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
59 *****************************************************************************/
60
61ODINFUNCTION2(UINT, GetCurrentDirectoryA, UINT, nBufferLength,
62 LPSTR, lpBuffer)
63{
64 return O32_GetCurrentDirectory(nBufferLength, lpBuffer);
65}
66
67
68/*****************************************************************************
69 * Name : GetCurrentDirectoryW
70 * Purpose : query the current directory
71 * Parameters:
72 * Variables :
73 * Result :
74 * Remark :
75 * Status :
76 *
77 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
78 *****************************************************************************/
79
80ODINFUNCTION2(UINT, GetCurrentDirectoryW, UINT, nBufferLength,
81 LPWSTR, lpBuffer)
82{
83 char *asciidir = (char *)malloc(nBufferLength+1);
84 int rc;
85
86 rc = O32_GetCurrentDirectory(nBufferLength, asciidir);
87 if(rc != 0)
88 AsciiToUnicode(asciidir, lpBuffer);
89 free(asciidir);
90 return(rc);
91}
92
93
94/*****************************************************************************
95 * Name : SetCurrentDirectoryA
96 * Purpose :
97 * Parameters:
98 * Variables :
99 * Result :
100 * Remark :
101 * Status :
102 *
103 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
104 *****************************************************************************/
105
106
107ODINFUNCTION1(BOOL,SetCurrentDirectoryA,LPCSTR,lpPathName)
108{
109 int len = strlen(lpPathName);
110 char *tmp=(char *)alloca(len + 1);
111
112 strcpy(tmp, lpPathName);
113 if(tmp[len -1] == '\\')
114 tmp[len -1] = 0;
115 return O32_SetCurrentDirectory((LPSTR)tmp);
116}
117
118
119/*****************************************************************************
120 * Name : SetCurrentDirectoryW
121 * Purpose :
122 * Parameters:
123 * Variables :
124 * Result :
125 * Remark :
126 * Status :
127 *
128 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
129 *****************************************************************************/
130
131ODINFUNCTION1(BOOL,SetCurrentDirectoryW,LPCWSTR,lpPathName)
132{
133 char *asciipath;
134 BOOL rc;
135
136 asciipath = UnicodeToAsciiString((LPWSTR)lpPathName);
137 rc = SetCurrentDirectoryA(asciipath);
138 FreeAsciiString(asciipath);
139 return(rc);
140}
141
142
143/*****************************************************************************
144 * Name : CreateDirectoryA
145 * Purpose :
146 * Parameters:
147 * Variables :
148 * Result :
149 * Remark :
150 * Status :
151 *
152 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
153 *****************************************************************************/
154
155ODINFUNCTION2(BOOL,CreateDirectoryA,LPCSTR, arg1,PSECURITY_ATTRIBUTES,arg2)
156{
157 int len = strlen(arg1);
158 char *tmp=(char *)alloca(len + 1);
159
160 strcpy(tmp, arg1);
161 if(tmp[len -1] == '\\')
162 tmp[len -1] = 0;
163 return O32_CreateDirectory(tmp, arg2);
164}
165
166/*****************************************************************************
167 * Name : CreateDirectoryW
168 * Purpose :
169 * Parameters:
170 * Variables :
171 * Result :
172 * Remark :
173 * Status :
174 *
175 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
176 *****************************************************************************/
177
178ODINFUNCTION2(BOOL,CreateDirectoryW,LPCWSTR, arg1,
179 PSECURITY_ATTRIBUTES,arg2)
180{
181 BOOL rc;
182 char *astring;
183
184 astring = UnicodeToAsciiString((LPWSTR)arg1);
185 rc = CreateDirectoryA(astring, arg2);
186 FreeAsciiString(astring);
187 return(rc);
188}
189
190
191/*****************************************************************************
192 * Name : GetSystemDirectoryA
193 * Purpose :
194 * Parameters:
195 * Variables :
196 * Result :
197 * Remark :
198 * Status :
199 *
200 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
201 *****************************************************************************/
202
203ODINFUNCTION2(UINT,GetSystemDirectoryA,LPSTR,lpBuffer,
204 UINT,uSize)
205{
206 LPSTR lpstrEnv = getenv("WIN32.DIR.SYSTEM"); /* query environment */
207
208 if (lpstrEnv != NULL)
209 {
210 lstrcpynA(lpBuffer, /* copy environment variable to buffer */
211 lpstrEnv,
212 uSize);
213 return (lstrlenA(lpBuffer)); /* return number of copies bytes */
214 }
215 else
216 {
217 int len;
218
219 len = PROFILE_GetOdinIniString(ODINDIRECTORIES,"SYSTEM","",lpBuffer,uSize);
220 if (len > 2) {
221 if(lpBuffer[len-1] == '\\') {
222 lpBuffer[len-1] = 0;
223 len--;
224 }
225 return len;
226 }
227 else {//SvL: Use path of kernel32.dll instead of calling Open32 api (which returns \OS2\SYSTEM)
228 lstrcpynA(lpBuffer, kernel32Path, uSize);
229 len = lstrlenA(lpBuffer);;
230 if(lpBuffer[len-1] == '\\') {
231 lpBuffer[len-1] = 0;
232 len--;
233 }
234 return len;
235 }
236 }
237}
238
239
240/*****************************************************************************
241 * Name : GetSystemDirectoryW
242 * Purpose :
243 * Parameters:
244 * Variables :
245 * Result :
246 * Remark :
247 * Status :
248 *
249 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
250 *****************************************************************************/
251
252ODINFUNCTION2(UINT,GetSystemDirectoryW,LPWSTR,lpBuffer,
253 UINT, uSize)
254{
255 char *asciibuffer = (char *)malloc(uSize+1);
256 UINT rc;
257
258 rc = GetSystemDirectoryA(asciibuffer, uSize);
259 if(rc) AsciiToUnicode(asciibuffer, lpBuffer);
260 free(asciibuffer);
261 return(rc);
262}
263
264
265/*****************************************************************************
266 * Name : GetWindowsDirectoryA
267 * Purpose :
268 * Parameters:
269 * Variables :
270 * Result :
271 * Remark :
272 * Status :
273 *
274 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
275 *****************************************************************************/
276
277ODINFUNCTION2(UINT,GetWindowsDirectoryA,LPSTR,lpBuffer,
278 UINT,uSize)
279{
280 LPSTR lpstrEnv = getenv("WIN32.DIR.WINDOWS"); /* query environment */
281
282 if (lpstrEnv != NULL)
283 {
284 lstrcpynA(lpBuffer, /* copy environment variable to buffer */
285 lpstrEnv,
286 uSize);
287 return (lstrlenA(lpBuffer)); /* return number of copies bytes */
288 }
289 else
290 {
291 int len;
292
293 len = PROFILE_GetOdinIniString(ODINDIRECTORIES,"WINDOWS","",lpBuffer,uSize);
294 if (len > 2) {
295 if(lpBuffer[len-1] == '\\') {
296 lpBuffer[len-1] = 0;
297 len--;
298 }
299 return len;
300 }
301 else
302 /* if no override by environment is available */
303 return O32_GetWindowsDirectory(lpBuffer,uSize);
304 }
305}
306
307
308/*****************************************************************************
309 * Name : GetWindowsDirectoryW
310 * Purpose :
311 * Parameters:
312 * Variables :
313 * Result :
314 * Remark :
315 * Status :
316 *
317 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
318 *****************************************************************************/
319
320ODINFUNCTION2(UINT,GetWindowsDirectoryW,LPWSTR,lpBuffer,
321 UINT, uSize)
322{
323 char *asciibuffer = (char *)malloc(uSize+1);
324 UINT rc;
325
326 rc = GetWindowsDirectoryA(asciibuffer, uSize);
327 AsciiToUnicode(asciibuffer, lpBuffer);
328 free(asciibuffer);
329 return(rc);
330}
331
332
333/*****************************************************************************
334 * Name : RemoveDirectoryA
335 * Purpose :
336 * Parameters:
337 * Variables :
338 * Result :
339 * Remark :
340 * Status :
341 *
342 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
343 *****************************************************************************/
344
345
346ODINFUNCTION1(BOOL,RemoveDirectoryA,LPCSTR,arg1)
347{
348 int len = strlen(arg1);
349 char *tmp=(char *)alloca(len + 1);
350
351 strcpy(tmp, arg1);
352 if(tmp[len -1] == '\\')
353 tmp[len -1] = 0;
354 return O32_RemoveDirectory(tmp);
355}
356
357
358/*****************************************************************************
359 * Name : RemoveDirectoryW
360 * Purpose :
361 * Parameters:
362 * Variables :
363 * Result :
364 * Remark :
365 * Status :
366 *
367 * Author : Patrick Haller [Wed, 1999/09/28 20:44]
368 *****************************************************************************/
369
370ODINFUNCTION1(BOOL,RemoveDirectoryW,LPCWSTR,lpPathName)
371{
372 char *asciipath;
373 BOOL rc;
374
375 asciipath = UnicodeToAsciiString((LPWSTR)lpPathName);
376 rc = RemoveDirectoryA(asciipath);
377 FreeAsciiString(asciipath);
378 return(rc);
379}
380
381/***********************************************************************
382 * DIR_TryModulePath
383 *
384 * Helper function for DIR_SearchPath.
385 */
386static BOOL DIR_TryModulePath( LPCSTR name, char *full_name )
387{
388 char buffer[OFS_MAXPATHNAME];
389 LPSTR p;
390
391 if (!GetModuleFileNameA( 0, buffer, sizeof(buffer) ))
392 buffer[0]='\0';
393
394 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
395 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
396 strcpy( p, name );
397
398 return OSLibDosSearchPath(OSLIB_SEARCHFILE, NULL, buffer, full_name, MAX_PATHNAME_LEN);
399}
400
401
402/***********************************************************************
403 * DIR_SearchPath
404 *
405 * Implementation of SearchPath32A. 'win32' specifies whether the search
406 * order is Win16 (module path last) or Win32 (module path first).
407 *
408 * FIXME: should return long path names.
409 */
410DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
411 char *full_name )
412{
413 DWORD len;
414 LPCSTR p;
415 LPSTR tmp = NULL;
416 BOOL ret = TRUE;
417
418 /* First check the supplied parameters */
419
420 p = strrchr( name, '.' );
421 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
422 ext = NULL; /* Ignore the specified extension */
423 if ((*name && (name[1] == ':')) ||
424 strchr( name, '/' ) || strchr( name, '\\' ))
425 path = NULL; /* Ignore path if name already contains a path */
426 if (path && !*path) path = NULL; /* Ignore empty path */
427
428 len = strlen(name);
429 if (ext) len += strlen(ext);
430 if (path) len += strlen(path) + 1;
431
432 /* Allocate a buffer for the file name and extension */
433
434 if (path || ext)
435 {
436 if (!(tmp = (LPSTR)HeapAlloc( GetProcessHeap(), 0, len + 1 )))
437 {
438 SetLastError( ERROR_OUTOFMEMORY );
439 return 0;
440 }
441 if (path)
442 {
443 strcpy( tmp, path );
444 strcat( tmp, "\\" );
445 strcat( tmp, name );
446 }
447 else strcpy( tmp, name );
448 if (ext) strcat( tmp, ext );
449 name = tmp;
450 }
451
452 /* If we have an explicit path, everything's easy */
453
454 if (path || (*name && (name[1] == ':')) ||
455 strchr( name, '/' ) || strchr( name, '\\' ))
456 {
457 ret = OSLibDosSearchPath(OSLIB_SEARCHFILE, NULL, (LPSTR)name, full_name, MAX_PATHNAME_LEN);
458 goto done;
459 }
460
461 /* Try the path of the current executable (for Win32 search order) */
462 if (DIR_TryModulePath( name, full_name )) goto done;
463
464 /* Try the current directory */
465 if (OSLibDosSearchPath(OSLIB_SEARCHCURDIR, NULL, (LPSTR)name, full_name, MAX_PATHNAME_LEN))
466 goto done;
467
468 /* Try the Windows system directory */
469 if (OSLibDosSearchPath(OSLIB_SEARCHDIR, (LPSTR)&DIR_System, (LPSTR)name, full_name, MAX_PATHNAME_LEN))
470 goto done;
471
472 /* Try the Windows directory */
473 if (OSLibDosSearchPath(OSLIB_SEARCHDIR, (LPSTR)&DIR_Windows, (LPSTR)name, full_name, MAX_PATHNAME_LEN))
474 goto done;
475
476 /* Try all directories in path */
477 ret = OSLibDosSearchPath(OSLIB_SEARCHENV, "PATH", (LPSTR)name, full_name, MAX_PATHNAME_LEN);
478
479done:
480 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
481 return ret;
482}
483
484
485/***********************************************************************
486 * SearchPath32A [KERNEL32.447]
487 *
488 * Searches for a specified file in the search path.
489 *
490 * PARAMS
491 * path [I] Path to search
492 * name [I] Filename to search for.
493 * ext [I] File extension to append to file name. The first
494 * character must be a period. This parameter is
495 * specified only if the filename given does not
496 * contain an extension.
497 * buflen [I] size of buffer, in characters
498 * buffer [O] buffer for found filename
499 * lastpart [O] address of pointer to last used character in
500 * buffer (the final '\')
501 *
502 * RETURNS
503 * Success: length of string copied into buffer, not including
504 * terminating null character. If the filename found is
505 * longer than the length of the buffer, the length of the
506 * filename is returned.
507 * Failure: Zero
508 *
509 * NOTES
510 * Should call SetLastError(but currently doesn't).
511 */
512DWORD WINAPI SearchPathA(LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
513 LPSTR buffer, LPSTR *lastpart )
514{
515 char full_name[MAX_PATHNAME_LEN];
516
517 if (!DIR_SearchPath( path, name, ext, (LPSTR)full_name )) return 0;
518 lstrcpynA( buffer, (LPSTR)full_name, buflen-1);
519 buffer[buflen-2] = 0;
520 SetLastError(0);
521 return strlen(buffer);
522}
523
524
525/***********************************************************************
526 * SearchPath32W (KERNEL32.448)
527 */
528DWORD WINAPI SearchPathW(LPCWSTR path, LPCWSTR name, LPCWSTR ext,
529 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
530{
531 char full_name[MAX_PATHNAME_LEN];
532
533 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
534 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
535 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
536 DWORD ret = DIR_SearchPath( pathA, nameA, extA, (LPSTR)full_name );
537 HeapFree( GetProcessHeap(), 0, extA );
538 HeapFree( GetProcessHeap(), 0, nameA );
539 HeapFree( GetProcessHeap(), 0, pathA );
540 if (!ret) return 0;
541
542 lstrcpynAtoW( buffer, full_name, buflen-1 );
543 buffer[buflen-2] = 0;
544 SetLastError(0);
545 return strlen(full_name);
546}
Note: See TracBrowser for help on using the repository browser.