blob: 9d5cae6368da859f5c8ebcd0a4e7d2fafcfc1bf7 [file] [log] [blame]
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +00001/*
2 * DOS directories functions
3 *
4 * Copyright 1995 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000019 */
20
Patrik Stridvall96336321999-10-24 22:13:47 +000021#include "config.h"
22
Alexandre Julliardc7c217b1998-04-13 12:21:30 +000023#include <ctype.h>
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000024#include <stdlib.h>
Jeremy Whited3e22d92000-02-10 19:03:02 +000025#include <stdio.h>
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000026#include <string.h>
Alexandre Julliardc6c09441997-01-12 18:32:19 +000027#include <sys/types.h>
Alexandre Julliard9ea19e51997-01-01 17:29:55 +000028#include <sys/stat.h>
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000029#include <unistd.h>
Alexandre Julliard139a4b11996-11-02 14:24:07 +000030#include <errno.h>
Howard Abrams13277481999-07-10 13:16:29 +000031#ifdef HAVE_SYS_ERRNO_H
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000032#include <sys/errno.h>
Howard Abrams13277481999-07-10 13:16:29 +000033#endif
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000034
Marcus Meissner04c3e1d1999-02-19 10:37:02 +000035#include "winbase.h"
36#include "wine/winbase16.h"
Jeremy Whited3e22d92000-02-10 19:03:02 +000037#include "windef.h"
38#include "wingdi.h"
Patrik Stridvall1ed4ecf1999-06-26 14:58:24 +000039#include "wine/winuser16.h"
Alexandre Julliard9ea19e51997-01-01 17:29:55 +000040#include "winerror.h"
Andreas Mohr8f925ae2002-02-27 18:49:40 +000041#include "winreg.h"
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000042#include "drive.h"
43#include "file.h"
Alexandre Julliard9ea19e51997-01-01 17:29:55 +000044#include "heap.h"
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000045#include "msdos.h"
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000046#include "wine/debug.h"
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000047
Alexandre Julliard0799c1a2002-03-09 23:29:33 +000048WINE_DEFAULT_DEBUG_CHANNEL(dosfs);
49WINE_DECLARE_DEBUG_CHANNEL(file);
Patrik Stridvallb4b9fae1999-04-19 14:56:29 +000050
Alexandre Julliardc7c217b1998-04-13 12:21:30 +000051static DOS_FULL_NAME DIR_Windows;
52static DOS_FULL_NAME DIR_System;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000053
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000054
55/***********************************************************************
56 * DIR_GetPath
57 *
58 * Get a path name from the wine.ini file and make sure it is valid.
59 */
60static int DIR_GetPath( const char *keyname, const char *defval,
Juergen Schmied6b279772002-05-09 19:35:22 +000061 DOS_FULL_NAME *full_name, char * longname, BOOL warn )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000062{
63 char path[MAX_PATHNAME_LEN];
Alexandre Julliard9ea19e51997-01-01 17:29:55 +000064 BY_HANDLE_FILE_INFORMATION info;
Lawson Whitney03482122000-12-14 20:30:13 +000065 const char *mess = "does not exist";
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000066
Alexandre Julliard7e56f681996-01-31 19:02:28 +000067 PROFILE_GetWineIniString( "wine", keyname, defval, path, sizeof(path) );
Alexandre Julliardc7c217b1998-04-13 12:21:30 +000068 if (!DOSFS_GetFullName( path, TRUE, full_name ) ||
Lawson Whitney03482122000-12-14 20:30:13 +000069 (!FILE_Stat( full_name->long_name, &info ) && (mess=strerror(errno)))||
Vincent Béron9a624912002-05-31 23:06:46 +000070 (!(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && (mess="not a directory")) ||
Juergen Schmied6b279772002-05-09 19:35:22 +000071 (!(GetLongPathNameA(full_name->short_name, longname, MAX_PATHNAME_LEN))) )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000072 {
Gerard Pateld52e1c42001-02-16 19:05:42 +000073 if (warn)
74 MESSAGE("Invalid path '%s' for %s directory: %s\n", path, keyname, mess);
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000075 return 0;
76 }
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000077 return 1;
78}
79
80
81/***********************************************************************
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000082 * DIR_Init
83 */
84int DIR_Init(void)
85{
Alexandre Julliardc7c217b1998-04-13 12:21:30 +000086 char path[MAX_PATHNAME_LEN];
Juergen Schmied6b279772002-05-09 19:35:22 +000087 char longpath[MAX_PATHNAME_LEN];
Juergen Schmied8573cc72000-01-30 03:03:23 +000088 DOS_FULL_NAME tmp_dir, profile_dir;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +000089 int drive;
90 const char *cwd;
91
92 if (!getcwd( path, MAX_PATHNAME_LEN ))
93 {
94 perror( "Could not get current directory" );
95 return 0;
96 }
97 cwd = path;
98 if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
99 {
Chris Morgane4055502001-01-12 19:55:19 +0000100 MESSAGE("Warning: could not find wine config [Drive x] entry "
Andreas Mohrdf8cdfd1999-02-28 16:56:48 +0000101 "for current working directory %s; "
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000102 "starting in windows directory.\n", cwd );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000103 }
104 else
105 {
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000106 DRIVE_SetCurrentDrive( drive );
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000107 DRIVE_Chdir( drive, cwd );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000108 }
109
Juergen Schmied6b279772002-05-09 19:35:22 +0000110 if (!(DIR_GetPath( "windows", "c:\\windows", &DIR_Windows, longpath, TRUE )) ||
111 !(DIR_GetPath( "system", "c:\\windows\\system", &DIR_System, longpath, TRUE )) ||
112 !(DIR_GetPath( "temp", "c:\\windows", &tmp_dir, longpath, TRUE )))
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000113 {
114 PROFILE_UsageWineIni();
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000115 return 0;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000116 }
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000117 if (-1 == access( tmp_dir.long_name, W_OK ))
118 {
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000119 if (errno==EACCES)
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000120 {
Andreas Mohra6d83eb2000-12-27 04:02:46 +0000121 MESSAGE("Warning: the temporary directory '%s' (specified in wine configuration file) is not writeable.\n", tmp_dir.long_name);
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000122 PROFILE_UsageWineIni();
123 }
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000124 else
Andreas Mohra6d83eb2000-12-27 04:02:46 +0000125 MESSAGE("Warning: access to temporary directory '%s' failed (%s).\n",
126 tmp_dir.long_name, strerror(errno));
Alexandre Julliard8bbf8181996-09-13 16:50:47 +0000127 }
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000128
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000129 if (drive == -1)
130 {
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000131 drive = DIR_Windows.drive;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000132 DRIVE_SetCurrentDrive( drive );
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000133 DRIVE_Chdir( drive, DIR_Windows.short_name + 2 );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000134 }
135
Alexandre Julliard7e56f681996-01-31 19:02:28 +0000136 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
137 path, sizeof(path) );
Andreas Mohr0b160062000-01-26 02:04:09 +0000138 if (strchr(path, '/'))
139 {
Andreas Mohr9cef2d02001-11-19 02:30:01 +0000140 MESSAGE("Fix your wine config to use DOS drive syntax in [wine] 'Path=' statement! (no '/' allowed)\n");
Andreas Mohr0b160062000-01-26 02:04:09 +0000141 PROFILE_UsageWineIni();
142 ExitProcess(1);
143 }
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000144
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000145 /* Set the environment variables */
146
Alexandre Julliarda3960291999-02-26 11:11:13 +0000147 SetEnvironmentVariableA( "PATH", path );
Alexandre Julliarda3960291999-02-26 11:11:13 +0000148 SetEnvironmentVariableA( "TEMP", tmp_dir.short_name );
Dmitry Timoshkovb6ac7702000-12-24 20:01:12 +0000149 SetEnvironmentVariableA( "TMP", tmp_dir.short_name );
Alexandre Julliarda3960291999-02-26 11:11:13 +0000150 SetEnvironmentVariableA( "windir", DIR_Windows.short_name );
151 SetEnvironmentVariableA( "winsysdir", DIR_System.short_name );
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000152
Alexandre Julliard2e9f7862000-08-01 23:34:01 +0000153 /* set COMSPEC only if it doesn't exist already */
154 if (!GetEnvironmentVariableA( "COMSPEC", NULL, 0 ))
155 SetEnvironmentVariableA( "COMSPEC", "c:\\command.com" );
156
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000157 TRACE("WindowsDir = %s (%s)\n",
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000158 DIR_Windows.short_name, DIR_Windows.long_name );
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000159 TRACE("SystemDir = %s (%s)\n",
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000160 DIR_System.short_name, DIR_System.long_name );
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000161 TRACE("TempDir = %s (%s)\n",
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000162 tmp_dir.short_name, tmp_dir.long_name );
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000163 TRACE("Path = %s\n", path );
164 TRACE("Cwd = %c:\\%s\n",
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000165 'A' + drive, DRIVE_GetDosCwd( drive ) );
Alexandre Julliard7e56f681996-01-31 19:02:28 +0000166
Juergen Schmied6b279772002-05-09 19:35:22 +0000167 if (DIR_GetPath( "profile", "", &profile_dir, longpath, FALSE ))
Juergen Schmied8573cc72000-01-30 03:03:23 +0000168 {
Juergen Schmied6b279772002-05-09 19:35:22 +0000169 TRACE("USERPROFILE= %s\n", longpath );
170 SetEnvironmentVariableA( "USERPROFILE", longpath );
Vincent Béron9a624912002-05-31 23:06:46 +0000171 }
Juergen Schmied8573cc72000-01-30 03:03:23 +0000172
173 TRACE("SYSTEMROOT = %s\n", DIR_Windows.short_name );
174 SetEnvironmentVariableA( "SYSTEMROOT", DIR_Windows.short_name );
175
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000176 return 1;
177}
178
179
180/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000181 * GetTempPathA (KERNEL32.@)
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000182 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000183UINT WINAPI GetTempPathA( UINT count, LPSTR path )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000184{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000185 UINT ret;
186 if (!(ret = GetEnvironmentVariableA( "TMP", path, count )))
187 if (!(ret = GetEnvironmentVariableA( "TEMP", path, count )))
188 if (!(ret = GetCurrentDirectoryA( count, path )))
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000189 return 0;
Huw D M Daviesa03f0641998-12-15 18:04:58 +0000190 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000191 {
192 path[ret++] = '\\';
193 path[ret] = '\0';
194 }
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000195 return ret;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000196}
197
198
199/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000200 * GetTempPathW (KERNEL32.@)
Alexandre Julliard3051b641996-07-05 17:14:13 +0000201 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000202UINT WINAPI GetTempPathW( UINT count, LPWSTR path )
Alexandre Julliard3051b641996-07-05 17:14:13 +0000203{
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000204 static const WCHAR tmp[] = { 'T', 'M', 'P', 0 };
205 static const WCHAR temp[] = { 'T', 'E', 'M', 'P', 0 };
Alexandre Julliarda3960291999-02-26 11:11:13 +0000206 UINT ret;
207 if (!(ret = GetEnvironmentVariableW( tmp, path, count )))
208 if (!(ret = GetEnvironmentVariableW( temp, path, count )))
209 if (!(ret = GetCurrentDirectoryW( count, path )))
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000210 return 0;
Huw D M Daviesa03f0641998-12-15 18:04:58 +0000211 if (count && (ret < count - 1) && (path[ret-1] != '\\'))
Alexandre Julliard46ea8b31998-05-03 19:01:20 +0000212 {
213 path[ret++] = '\\';
214 path[ret] = '\0';
215 }
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000216 return ret;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000217}
218
219
220/***********************************************************************
221 * DIR_GetWindowsUnixDir
222 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000223UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000224{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000225 if (path) lstrcpynA( path, DIR_Windows.long_name, count );
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000226 return strlen( DIR_Windows.long_name );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000227}
228
229
230/***********************************************************************
231 * DIR_GetSystemUnixDir
232 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000233UINT DIR_GetSystemUnixDir( LPSTR path, UINT count )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000234{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000235 if (path) lstrcpynA( path, DIR_System.long_name, count );
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000236 return strlen( DIR_System.long_name );
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000237}
238
239
240/***********************************************************************
241 * GetTempDrive (KERNEL.92)
Alexandre Julliardac7efef2000-11-27 21:54:01 +0000242 * A closer look at krnl386.exe shows what the SDK doesn't mention:
243 *
244 * returns:
245 * AL: driveletter
246 * AH: ':' - yes, some kernel code even does stosw with
247 * the returned AX.
248 * DX: 1 for success
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000249 */
Alexandre Julliardac7efef2000-11-27 21:54:01 +0000250UINT WINAPI GetTempDrive( BYTE ignored )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000251{
Uwe Bonnes3dbc7881999-02-14 17:32:46 +0000252 char *buffer;
253 BYTE ret;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000254 UINT len = GetTempPathA( 0, NULL );
Uwe Bonnes3dbc7881999-02-14 17:32:46 +0000255
256 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + 1 )) )
Alexandre Julliardac7efef2000-11-27 21:54:01 +0000257 ret = DRIVE_GetCurrentDrive() + 'A';
258 else
259 {
260 /* FIXME: apparently Windows does something with the ignored byte */
261 if (!GetTempPathA( len, buffer )) buffer[0] = 'C';
262 ret = toupper(buffer[0]);
263 HeapFree( GetProcessHeap(), 0, buffer );
264 }
265 return MAKELONG( ret | (':' << 8), 1 );
Alexandre Julliardca22b331996-07-12 19:02:39 +0000266}
267
Alexandre Julliard2c69f6d1996-09-28 18:11:01 +0000268
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000269/***********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +0000270 * GetWindowsDirectory (KERNEL.134)
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000271 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000272UINT16 WINAPI GetWindowsDirectory16( LPSTR path, UINT16 count )
Alexandre Julliard2c69f6d1996-09-28 18:11:01 +0000273{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000274 return (UINT16)GetWindowsDirectoryA( path, count );
Alexandre Julliard2c69f6d1996-09-28 18:11:01 +0000275}
276
277
278/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000279 * GetWindowsDirectoryA (KERNEL32.@)
Dmitry Timoshkov4291e452002-01-18 18:53:11 +0000280 *
281 * See comment for GetWindowsDirectoryW.
Alexandre Julliard2c69f6d1996-09-28 18:11:01 +0000282 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000283UINT WINAPI GetWindowsDirectoryA( LPSTR path, UINT count )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000284{
Dmitry Timoshkov4291e452002-01-18 18:53:11 +0000285 UINT len = strlen( DIR_Windows.short_name ) + 1;
286 if (path && count >= len)
287 {
288 strcpy( path, DIR_Windows.short_name );
289 len--;
290 }
291 return len;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000292}
293
294
295/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000296 * GetWindowsDirectoryW (KERNEL32.@)
Dmitry Timoshkov4291e452002-01-18 18:53:11 +0000297 *
298 * Return value:
299 * If buffer is large enough to hold full path and terminating '\0' character
300 * function copies path to buffer and returns length of the path without '\0'.
301 * Otherwise function returns required size including '\0' character and
302 * does not touch the buffer.
Alexandre Julliard2c69f6d1996-09-28 18:11:01 +0000303 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000304UINT WINAPI GetWindowsDirectoryW( LPWSTR path, UINT count )
Alexandre Julliard2c69f6d1996-09-28 18:11:01 +0000305{
Alexandre Julliard24a62ab2000-11-28 22:40:56 +0000306 UINT len = MultiByteToWideChar( CP_ACP, 0, DIR_Windows.short_name, -1, NULL, 0 );
Dmitry Timoshkov4291e452002-01-18 18:53:11 +0000307 if (path && count >= len)
Alexandre Julliard24a62ab2000-11-28 22:40:56 +0000308 {
Dmitry Timoshkov4291e452002-01-18 18:53:11 +0000309 MultiByteToWideChar( CP_ACP, 0, DIR_Windows.short_name, -1, path, count );
310 len--;
Alexandre Julliard24a62ab2000-11-28 22:40:56 +0000311 }
312 return len;
Alexandre Julliard2c69f6d1996-09-28 18:11:01 +0000313}
314
315
316/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +0000317 * GetSystemWindowsDirectoryA (KERNEL32.@) W2K, TS4.0SP4
David Elliott44f84b52000-10-29 01:24:54 +0000318 */
319UINT WINAPI GetSystemWindowsDirectoryA( LPSTR path, UINT count )
320{
321 return GetWindowsDirectoryA( path, count );
322}
323
324
325/***********************************************************************
Patrik Stridvall3ca98232001-06-20 23:03:14 +0000326 * GetSystemWindowsDirectoryW (KERNEL32.@) W2K, TS4.0SP4
David Elliott44f84b52000-10-29 01:24:54 +0000327 */
328UINT WINAPI GetSystemWindowsDirectoryW( LPWSTR path, UINT count )
329{
330 return GetWindowsDirectoryW( path, count );
331}
332
333
334/***********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +0000335 * GetSystemDirectory (KERNEL.135)
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000336 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000337UINT16 WINAPI GetSystemDirectory16( LPSTR path, UINT16 count )
Alexandre Julliard3051b641996-07-05 17:14:13 +0000338{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000339 return (UINT16)GetSystemDirectoryA( path, count );
Alexandre Julliard3051b641996-07-05 17:14:13 +0000340}
341
342
343/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000344 * GetSystemDirectoryA (KERNEL32.@)
Dmitry Timoshkov4291e452002-01-18 18:53:11 +0000345 *
346 * See comment for GetWindowsDirectoryW.
Alexandre Julliard3051b641996-07-05 17:14:13 +0000347 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000348UINT WINAPI GetSystemDirectoryA( LPSTR path, UINT count )
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000349{
Dmitry Timoshkov4291e452002-01-18 18:53:11 +0000350 UINT len = strlen( DIR_System.short_name ) + 1;
351 if (path && count >= len)
352 {
353 strcpy( path, DIR_System.short_name );
354 len--;
355 }
356 return len;
Alexandre Julliard4f8c37b1996-01-14 18:12:01 +0000357}
Alexandre Julliard3051b641996-07-05 17:14:13 +0000358
359
360/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000361 * GetSystemDirectoryW (KERNEL32.@)
Dmitry Timoshkov4291e452002-01-18 18:53:11 +0000362 *
363 * See comment for GetWindowsDirectoryW.
Alexandre Julliard3051b641996-07-05 17:14:13 +0000364 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000365UINT WINAPI GetSystemDirectoryW( LPWSTR path, UINT count )
Alexandre Julliard3051b641996-07-05 17:14:13 +0000366{
Alexandre Julliard24a62ab2000-11-28 22:40:56 +0000367 UINT len = MultiByteToWideChar( CP_ACP, 0, DIR_System.short_name, -1, NULL, 0 );
Dmitry Timoshkov4291e452002-01-18 18:53:11 +0000368 if (path && count >= len)
Alexandre Julliard24a62ab2000-11-28 22:40:56 +0000369 {
Dmitry Timoshkov4291e452002-01-18 18:53:11 +0000370 MultiByteToWideChar( CP_ACP, 0, DIR_System.short_name, -1, path, count );
371 len--;
Alexandre Julliard24a62ab2000-11-28 22:40:56 +0000372 }
373 return len;
Alexandre Julliard3051b641996-07-05 17:14:13 +0000374}
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000375
376
377/***********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +0000378 * CreateDirectory (KERNEL.144)
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000379 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000380BOOL16 WINAPI CreateDirectory16( LPCSTR path, LPVOID dummy )
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000381{
Alexandre Julliard06c275a1999-05-02 14:32:27 +0000382 TRACE_(file)("(%s,%p)\n", path, dummy );
Alexandre Julliarda3960291999-02-26 11:11:13 +0000383 return (BOOL16)CreateDirectoryA( path, NULL );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000384}
385
386
387/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000388 * CreateDirectoryA (KERNEL32.@)
Marcus Meissner412d0251999-02-28 19:50:39 +0000389 * RETURNS:
390 * TRUE : success
391 * FALSE : failure
392 * ERROR_DISK_FULL: on full disk
393 * ERROR_ALREADY_EXISTS: if directory name exists (even as file)
394 * ERROR_ACCESS_DENIED: on permission problems
395 * ERROR_FILENAME_EXCED_RANGE: too long filename(s)
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000396 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000397BOOL WINAPI CreateDirectoryA( LPCSTR path,
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000398 LPSECURITY_ATTRIBUTES lpsecattribs )
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000399{
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000400 DOS_FULL_NAME full_name;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000401
Alexandre Julliard06c275a1999-05-02 14:32:27 +0000402 TRACE_(file)("(%s,%p)\n", path, lpsecattribs );
Alexandre Julliard829fe321998-07-26 14:27:39 +0000403 if (DOSFS_GetDevice( path ))
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000404 {
Alexandre Julliard06c275a1999-05-02 14:32:27 +0000405 TRACE_(file)("cannot use device '%s'!\n",path);
Alexandre Julliard4ff2a271999-01-31 15:23:45 +0000406 SetLastError( ERROR_ACCESS_DENIED );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000407 return FALSE;
408 }
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000409 if (!DOSFS_GetFullName( path, FALSE, &full_name )) return 0;
Marcus Meissner412d0251999-02-28 19:50:39 +0000410 if (mkdir( full_name.long_name, 0777 ) == -1) {
Andreas Mohr8c2f4b12001-03-03 00:22:50 +0000411 WARN_(file)("Error '%s' trying to create directory '%s'\n", strerror(errno), full_name.long_name);
Vincent Béron9a624912002-05-31 23:06:46 +0000412 /* the FILE_SetDosError() generated error codes don't match the
Marcus Meissner412d0251999-02-28 19:50:39 +0000413 * CreateDirectory ones for some errnos */
414 switch (errno) {
415 case EEXIST: SetLastError(ERROR_ALREADY_EXISTS); break;
416 case ENOSPC: SetLastError(ERROR_DISK_FULL); break;
417 default: FILE_SetDosError();break;
418 }
419 return FALSE;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000420 }
421 return TRUE;
422}
423
424
425/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000426 * CreateDirectoryW (KERNEL32.@)
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000427 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000428BOOL WINAPI CreateDirectoryW( LPCWSTR path,
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000429 LPSECURITY_ATTRIBUTES lpsecattribs )
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000430{
431 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
Alexandre Julliarda3960291999-02-26 11:11:13 +0000432 BOOL ret = CreateDirectoryA( xpath, lpsecattribs );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000433 HeapFree( GetProcessHeap(), 0, xpath );
434 return ret;
435}
436
437
438/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000439 * CreateDirectoryExA (KERNEL32.@)
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000440 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000441BOOL WINAPI CreateDirectoryExA( LPCSTR template, LPCSTR path,
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000442 LPSECURITY_ATTRIBUTES lpsecattribs)
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000443{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000444 return CreateDirectoryA(path,lpsecattribs);
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000445}
446
447
448/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000449 * CreateDirectoryExW (KERNEL32.@)
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000450 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000451BOOL WINAPI CreateDirectoryExW( LPCWSTR template, LPCWSTR path,
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000452 LPSECURITY_ATTRIBUTES lpsecattribs)
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000453{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000454 return CreateDirectoryW(path,lpsecattribs);
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000455}
456
457
458/***********************************************************************
Patrik Stridvall01d5e5b2001-07-02 19:59:40 +0000459 * RemoveDirectory (KERNEL.145)
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000460 */
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000461BOOL16 WINAPI RemoveDirectory16( LPCSTR path )
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000462{
Alexandre Julliarda3960291999-02-26 11:11:13 +0000463 return (BOOL16)RemoveDirectoryA( path );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000464}
465
466
467/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000468 * RemoveDirectoryA (KERNEL32.@)
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000469 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000470BOOL WINAPI RemoveDirectoryA( LPCSTR path )
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000471{
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000472 DOS_FULL_NAME full_name;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000473
Alexandre Julliard06c275a1999-05-02 14:32:27 +0000474 TRACE_(file)("'%s'\n", path );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000475
Alexandre Julliard829fe321998-07-26 14:27:39 +0000476 if (DOSFS_GetDevice( path ))
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000477 {
Alexandre Julliard06c275a1999-05-02 14:32:27 +0000478 TRACE_(file)("cannot remove device '%s'!\n", path);
Alexandre Julliard4ff2a271999-01-31 15:23:45 +0000479 SetLastError( ERROR_FILE_NOT_FOUND );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000480 return FALSE;
481 }
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000482 if (!DOSFS_GetFullName( path, TRUE, &full_name )) return FALSE;
483 if (rmdir( full_name.long_name ) == -1)
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000484 {
485 FILE_SetDosError();
486 return FALSE;
487 }
488 return TRUE;
489}
490
491
492/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000493 * RemoveDirectoryW (KERNEL32.@)
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000494 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000495BOOL WINAPI RemoveDirectoryW( LPCWSTR path )
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000496{
497 LPSTR xpath = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
Alexandre Julliarda3960291999-02-26 11:11:13 +0000498 BOOL ret = RemoveDirectoryA( xpath );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000499 HeapFree( GetProcessHeap(), 0, xpath );
500 return ret;
501}
502
503
504/***********************************************************************
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000505 * DIR_TryPath
506 *
507 * Helper function for DIR_SearchPath.
508 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000509static BOOL DIR_TryPath( const DOS_FULL_NAME *dir, LPCSTR name,
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000510 DOS_FULL_NAME *full_name )
511{
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000512 LPSTR p_l = full_name->long_name + strlen(dir->long_name) + 1;
513 LPSTR p_s = full_name->short_name + strlen(dir->short_name) + 1;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000514
515 if ((p_s >= full_name->short_name + sizeof(full_name->short_name) - 14) ||
516 (p_l >= full_name->long_name + sizeof(full_name->long_name) - 1))
517 {
Alexandre Julliard4ff2a271999-01-31 15:23:45 +0000518 SetLastError( ERROR_PATH_NOT_FOUND );
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000519 return FALSE;
520 }
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000521 if (!DOSFS_FindUnixName( dir->long_name, name, p_l,
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000522 sizeof(full_name->long_name) - (p_l - full_name->long_name),
Andreas Mohr04718411999-08-07 12:36:11 +0000523 p_s, !(DRIVE_GetFlags(dir->drive) & DRIVE_CASE_SENSITIVE) ))
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000524 return FALSE;
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000525 strcpy( full_name->long_name, dir->long_name );
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000526 p_l[-1] = '/';
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000527 strcpy( full_name->short_name, dir->short_name );
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000528 p_s[-1] = '\\';
529 return TRUE;
530}
531
Andreas Mohr8f925ae2002-02-27 18:49:40 +0000532static BOOL DIR_SearchSemicolonedPaths(LPCSTR name, DOS_FULL_NAME *full_name, LPSTR pathlist)
533{
534 LPSTR next, buffer = NULL;
535 INT len = strlen(name), newlen, currlen = 0;
536 BOOL ret = FALSE;
Vincent Béron9a624912002-05-31 23:06:46 +0000537
Andreas Mohr8f925ae2002-02-27 18:49:40 +0000538 next = pathlist;
539 while (!ret && next)
540 {
541 LPSTR cur = next;
542 while (*cur == ';') cur++;
543 if (!*cur) break;
544 next = strchr( cur, ';' );
545 if (next) *next++ = '\0';
546 newlen = strlen(cur) + len + 2;
547 if (newlen > currlen)
548 {
549 if (!(buffer = HeapReAlloc( GetProcessHeap(), 0, buffer, newlen)))
550 goto done;
551 currlen = newlen;
552 }
553 strcpy( buffer, cur );
554 strcat( buffer, "\\" );
555 strcat( buffer, name );
556 ret = DOSFS_GetFullName( buffer, TRUE, full_name );
557 }
558done:
559 HeapFree( GetProcessHeap(), 0, buffer );
560 return ret;
561}
562
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000563
564/***********************************************************************
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000565 * DIR_TryEnvironmentPath
566 *
567 * Helper function for DIR_SearchPath.
Alexandre Julliard9400e2d2000-11-01 21:48:38 +0000568 * Search in the specified path, or in $PATH if NULL.
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000569 */
Alexandre Julliard9400e2d2000-11-01 21:48:38 +0000570static BOOL DIR_TryEnvironmentPath( LPCSTR name, DOS_FULL_NAME *full_name, LPCSTR envpath )
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000571{
Andreas Mohr8f925ae2002-02-27 18:49:40 +0000572 LPSTR path;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000573 BOOL ret = FALSE;
Alexandre Julliard9400e2d2000-11-01 21:48:38 +0000574 DWORD size;
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000575
Alexandre Julliard9400e2d2000-11-01 21:48:38 +0000576 size = envpath ? strlen(envpath)+1 : GetEnvironmentVariableA( "PATH", NULL, 0 );
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000577 if (!size) return FALSE;
578 if (!(path = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
Alexandre Julliard9400e2d2000-11-01 21:48:38 +0000579 if (envpath) strcpy( path, envpath );
580 else if (!GetEnvironmentVariableA( "PATH", path, size )) goto done;
Andreas Mohr8f925ae2002-02-27 18:49:40 +0000581
582 ret = DIR_SearchSemicolonedPaths(name, full_name, path);
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000583
584done:
585 HeapFree( GetProcessHeap(), 0, path );
586 return ret;
587}
588
589
590/***********************************************************************
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000591 * DIR_TryModulePath
592 *
593 * Helper function for DIR_SearchPath.
594 */
Alexandre Julliardb18dbd22000-07-25 12:11:53 +0000595static BOOL DIR_TryModulePath( LPCSTR name, DOS_FULL_NAME *full_name, BOOL win32 )
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000596{
Patrik Stridvall2d6457c2000-03-28 20:22:59 +0000597 /* FIXME: for now, GetModuleFileNameA can't return more */
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000598 /* than OFS_MAXPATHNAME. This may change with Win32. */
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000599
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000600 char buffer[OFS_MAXPATHNAME];
601 LPSTR p;
602
Alexandre Julliardb18dbd22000-07-25 12:11:53 +0000603 if (!win32)
604 {
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000605 if (!GetCurrentTask()) return FALSE;
Alexandre Julliarda845b881998-06-01 10:44:35 +0000606 if (!GetModuleFileName16( GetCurrentTask(), buffer, sizeof(buffer) ))
607 buffer[0]='\0';
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000608 } else {
Alexandre Julliarda3960291999-02-26 11:11:13 +0000609 if (!GetModuleFileNameA( 0, buffer, sizeof(buffer) ))
Alexandre Julliarda845b881998-06-01 10:44:35 +0000610 buffer[0]='\0';
Alexandre Julliarddadf78f1998-05-17 17:13:43 +0000611 }
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000612 if (!(p = strrchr( buffer, '\\' ))) return FALSE;
613 if (sizeof(buffer) - (++p - buffer) <= strlen(name)) return FALSE;
614 strcpy( p, name );
615 return DOSFS_GetFullName( buffer, TRUE, full_name );
616}
617
618
619/***********************************************************************
Andreas Mohr8f925ae2002-02-27 18:49:40 +0000620 * DIR_TryAppPath
621 *
622 * Helper function for DIR_SearchPath.
623 */
624static BOOL DIR_TryAppPath( LPCSTR name, DOS_FULL_NAME *full_name )
625{
626 HKEY hkAppPaths, hkApp;
627 char lpAppName[MAX_PATHNAME_LEN], lpAppPaths[MAX_PATHNAME_LEN];
628 LPSTR lpFileName;
629 BOOL res = FALSE;
630 DWORD type, count;
631
632 if (RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths", &hkAppPaths) != ERROR_SUCCESS)
633 return FALSE;
634
635 if (GetModuleFileNameA(0, lpAppName, sizeof(lpAppName)) == 0)
636 {
637 WARN("huh, module not found ??\n");
638 goto end;
639 }
640 lpFileName = strrchr(lpAppName, '\\');
641 if (!lpFileName)
642 goto end;
643 else lpFileName++; /* skip '\\' */
644 if (RegOpenKeyA(hkAppPaths, lpFileName, &hkApp) != ERROR_SUCCESS)
645 goto end;
646 count = sizeof(lpAppPaths);
647 if (RegQueryValueExA(hkApp, "Path", 0, &type, (LPBYTE)lpAppPaths, &count) != ERROR_SUCCESS)
648 goto end;
649 TRACE("successfully opened App Paths for '%s'\n", lpFileName);
650
651 res = DIR_SearchSemicolonedPaths(name, full_name, lpAppPaths);
652end:
653 if (hkApp)
654 RegCloseKey(hkApp);
655 if (hkAppPaths)
656 RegCloseKey(hkAppPaths);
657 return res;
658}
659
660/***********************************************************************
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000661 * DIR_SearchPath
662 *
Patrik Stridvall2d6457c2000-03-28 20:22:59 +0000663 * Implementation of SearchPathA. 'win32' specifies whether the search
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000664 * order is Win16 (module path last) or Win32 (module path first).
665 *
666 * FIXME: should return long path names.
667 */
668DWORD DIR_SearchPath( LPCSTR path, LPCSTR name, LPCSTR ext,
Alexandre Julliarda3960291999-02-26 11:11:13 +0000669 DOS_FULL_NAME *full_name, BOOL win32 )
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000670{
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000671 LPCSTR p;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000672 LPSTR tmp = NULL;
Alexandre Julliarda3960291999-02-26 11:11:13 +0000673 BOOL ret = TRUE;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000674
675 /* First check the supplied parameters */
676
Alexandre Julliard641ee761997-08-04 16:34:36 +0000677 p = strrchr( name, '.' );
678 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
679 ext = NULL; /* Ignore the specified extension */
Bill Medland65fc1c92001-08-24 21:13:02 +0000680 if (FILE_contains_path (name))
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000681 path = NULL; /* Ignore path if name already contains a path */
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000682 if (path && !*path) path = NULL; /* Ignore empty path */
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000683
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000684 /* Allocate a buffer for the file name and extension */
685
Alexandre Julliard9400e2d2000-11-01 21:48:38 +0000686 if (ext)
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000687 {
Alexandre Julliard9400e2d2000-11-01 21:48:38 +0000688 DWORD len = strlen(name) + strlen(ext);
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000689 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
690 {
691 SetLastError( ERROR_OUTOFMEMORY );
692 return 0;
693 }
Alexandre Julliard9400e2d2000-11-01 21:48:38 +0000694 strcpy( tmp, name );
695 strcat( tmp, ext );
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000696 name = tmp;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000697 }
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000698
Alexandre Julliard9400e2d2000-11-01 21:48:38 +0000699 /* If the name contains an explicit path, everything's easy */
700
Bill Medland65fc1c92001-08-24 21:13:02 +0000701 if (FILE_contains_path(name))
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000702 {
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000703 ret = DOSFS_GetFullName( name, TRUE, full_name );
704 goto done;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000705 }
706
Alexandre Julliard9400e2d2000-11-01 21:48:38 +0000707 /* Search in the specified path */
708
709 if (path)
710 {
711 ret = DIR_TryEnvironmentPath( name, full_name, path );
712 goto done;
713 }
714
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000715 /* Try the path of the current executable (for Win32 search order) */
716
Alexandre Julliardb18dbd22000-07-25 12:11:53 +0000717 if (win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000718
719 /* Try the current directory */
720
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000721 if (DOSFS_GetFullName( name, TRUE, full_name )) goto done;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000722
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000723 /* Try the Windows system directory */
724
Alexandre Julliardc7c217b1998-04-13 12:21:30 +0000725 if (DIR_TryPath( &DIR_System, name, full_name ))
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000726 goto done;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000727
Guy Albertellie40c9b01999-06-05 11:47:25 +0000728 /* Try the Windows directory */
729
730 if (DIR_TryPath( &DIR_Windows, name, full_name ))
731 goto done;
732
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000733 /* Try the path of the current executable (for Win16 search order) */
734
Alexandre Julliardb18dbd22000-07-25 12:11:53 +0000735 if (!win32 && DIR_TryModulePath( name, full_name, win32 )) goto done;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000736
Andreas Mohr8f925ae2002-02-27 18:49:40 +0000737 /* Try the "App Paths" entry if existing (undocumented ??) */
738 if (DIR_TryAppPath(name, full_name))
739 goto done;
Vincent Béron9a624912002-05-31 23:06:46 +0000740
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000741 /* Try all directories in path */
742
Alexandre Julliard9400e2d2000-11-01 21:48:38 +0000743 ret = DIR_TryEnvironmentPath( name, full_name, NULL );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000744
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000745done:
746 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
747 return ret;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000748}
749
750
751/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000752 * SearchPathA [KERNEL32.@]
Alexandre Julliarda845b881998-06-01 10:44:35 +0000753 *
754 * Searches for a specified file in the search path.
755 *
756 * PARAMS
757 * path [I] Path to search
758 * name [I] Filename to search for.
759 * ext [I] File extension to append to file name. The first
760 * character must be a period. This parameter is
761 * specified only if the filename given does not
762 * contain an extension.
763 * buflen [I] size of buffer, in characters
764 * buffer [O] buffer for found filename
765 * lastpart [O] address of pointer to last used character in
766 * buffer (the final '\')
767 *
768 * RETURNS
769 * Success: length of string copied into buffer, not including
770 * terminating null character. If the filename found is
771 * longer than the length of the buffer, the length of the
772 * filename is returned.
773 * Failure: Zero
Vincent Béron9a624912002-05-31 23:06:46 +0000774 *
Alexandre Julliarda845b881998-06-01 10:44:35 +0000775 * NOTES
James Juran65020b32000-05-23 01:13:50 +0000776 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
777 * (tested on NT 4.0)
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000778 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000779DWORD WINAPI SearchPathA( LPCSTR path, LPCSTR name, LPCSTR ext, DWORD buflen,
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000780 LPSTR buffer, LPSTR *lastpart )
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000781{
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000782 LPSTR p, res;
783 DOS_FULL_NAME full_name;
784
James Juran65020b32000-05-23 01:13:50 +0000785 if (!DIR_SearchPath( path, name, ext, &full_name, TRUE ))
786 {
787 SetLastError(ERROR_FILE_NOT_FOUND);
788 return 0;
789 }
Alexandre Julliarda3960291999-02-26 11:11:13 +0000790 lstrcpynA( buffer, full_name.short_name, buflen );
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000791 res = full_name.long_name +
792 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
Alexandre Julliard349a9531997-02-02 19:01:52 +0000793 while (*res == '/') res++;
Alexandre Julliard3db94ef1997-09-28 17:43:24 +0000794 if (buflen)
795 {
Alexandre Julliarda3960291999-02-26 11:11:13 +0000796 if (buflen > 3) lstrcpynA( buffer + 3, res, buflen - 3 );
Alexandre Julliard3db94ef1997-09-28 17:43:24 +0000797 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
798 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
799 }
Dimitrie O. Paundd03cc11999-12-08 03:56:23 +0000800 TRACE("Returning %d\n", strlen(res) + 3 );
Guy Albertellie711ba21999-04-06 07:14:09 +0000801 return strlen(res) + 3;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000802}
803
804
805/***********************************************************************
Patrik Stridvalldae8de62001-06-13 20:13:18 +0000806 * SearchPathW (KERNEL32.@)
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000807 */
Alexandre Julliarda3960291999-02-26 11:11:13 +0000808DWORD WINAPI SearchPathW( LPCWSTR path, LPCWSTR name, LPCWSTR ext,
Alexandre Julliard670cdc41997-08-24 16:00:30 +0000809 DWORD buflen, LPWSTR buffer, LPWSTR *lastpart )
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000810{
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000811 LPWSTR p;
812 LPSTR res;
813 DOS_FULL_NAME full_name;
814
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000815 LPSTR pathA = HEAP_strdupWtoA( GetProcessHeap(), 0, path );
816 LPSTR nameA = HEAP_strdupWtoA( GetProcessHeap(), 0, name );
817 LPSTR extA = HEAP_strdupWtoA( GetProcessHeap(), 0, ext );
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000818 DWORD ret = DIR_SearchPath( pathA, nameA, extA, &full_name, TRUE );
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000819 HeapFree( GetProcessHeap(), 0, extA );
820 HeapFree( GetProcessHeap(), 0, nameA );
821 HeapFree( GetProcessHeap(), 0, pathA );
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000822 if (!ret) return 0;
823
Alexandre Julliard24a62ab2000-11-28 22:40:56 +0000824 if (buflen > 0 && !MultiByteToWideChar( CP_ACP, 0, full_name.short_name, -1, buffer, buflen ))
825 buffer[buflen-1] = 0;
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000826 res = full_name.long_name +
827 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
Alexandre Julliard349a9531997-02-02 19:01:52 +0000828 while (*res == '/') res++;
Alexandre Julliard3db94ef1997-09-28 17:43:24 +0000829 if (buflen)
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000830 {
Alexandre Julliard24a62ab2000-11-28 22:40:56 +0000831 if (buflen > 3)
832 {
833 if (!MultiByteToWideChar( CP_ACP, 0, res, -1, buffer+3, buflen-3 ))
834 buffer[buflen-1] = 0;
835 }
Alexandre Julliard3db94ef1997-09-28 17:43:24 +0000836 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
837 if (lastpart)
838 {
839 for (p = *lastpart = buffer; *p; p++)
840 if (*p == '\\') *lastpart = p + 1;
841 }
Alexandre Julliardc6c09441997-01-12 18:32:19 +0000842 }
Guy Albertellie711ba21999-04-06 07:14:09 +0000843 return strlen(res) + 3;
Alexandre Julliard9ea19e51997-01-01 17:29:55 +0000844}
845
846
Bill Medland65fc1c92001-08-24 21:13:02 +0000847/***********************************************************************
848 * search_alternate_path
849 *
850 *
851 * FIXME: should return long path names.?
852 */
853static BOOL search_alternate_path(LPCSTR dll_path, LPCSTR name, LPCSTR ext,
854 DOS_FULL_NAME *full_name)
855{
856 LPCSTR p;
857 LPSTR tmp = NULL;
858 BOOL ret = TRUE;
859
860 /* First check the supplied parameters */
861
862 p = strrchr( name, '.' );
863 if (p && !strchr( p, '/' ) && !strchr( p, '\\' ))
864 ext = NULL; /* Ignore the specified extension */
865
866 /* Allocate a buffer for the file name and extension */
867
868 if (ext)
869 {
870 DWORD len = strlen(name) + strlen(ext);
871 if (!(tmp = HeapAlloc( GetProcessHeap(), 0, len + 1 )))
872 {
873 SetLastError( ERROR_OUTOFMEMORY );
874 return 0;
875 }
876 strcpy( tmp, name );
877 strcat( tmp, ext );
878 name = tmp;
879 }
880
881 if (DIR_TryEnvironmentPath (name, full_name, dll_path))
882 ;
883 else if (DOSFS_GetFullName (name, TRUE, full_name)) /* current dir */
884 ;
885 else if (DIR_TryPath (&DIR_System, name, full_name)) /* System dir */
886 ;
887 else if (DIR_TryPath (&DIR_Windows, name, full_name)) /* Windows dir */
888 ;
889 else
890 ret = DIR_TryEnvironmentPath( name, full_name, NULL );
891
892 if (tmp) HeapFree( GetProcessHeap(), 0, tmp );
893 return ret;
894}
895
896
897/***********************************************************************
898 * DIR_SearchAlternatePath
899 *
900 * Searches for a specified file in the search path.
901 *
902 * PARAMS
903 * dll_path [I] Path to search
904 * name [I] Filename to search for.
905 * ext [I] File extension to append to file name. The first
906 * character must be a period. This parameter is
907 * specified only if the filename given does not
908 * contain an extension.
909 * buflen [I] size of buffer, in characters
910 * buffer [O] buffer for found filename
911 * lastpart [O] address of pointer to last used character in
912 * buffer (the final '\') (May be NULL)
913 *
914 * RETURNS
915 * Success: length of string copied into buffer, not including
916 * terminating null character. If the filename found is
917 * longer than the length of the buffer, the length of the
918 * filename is returned.
919 * Failure: Zero
Vincent Béron9a624912002-05-31 23:06:46 +0000920 *
Bill Medland65fc1c92001-08-24 21:13:02 +0000921 * NOTES
922 * If the file is not found, calls SetLastError(ERROR_FILE_NOT_FOUND)
923 */
924DWORD DIR_SearchAlternatePath( LPCSTR dll_path, LPCSTR name, LPCSTR ext,
925 DWORD buflen, LPSTR buffer, LPSTR *lastpart )
926{
927 LPSTR p, res;
928 DOS_FULL_NAME full_name;
929
930 if (!search_alternate_path( dll_path, name, ext, &full_name))
931 {
932 SetLastError(ERROR_FILE_NOT_FOUND);
933 return 0;
934 }
935 lstrcpynA( buffer, full_name.short_name, buflen );
936 res = full_name.long_name +
937 strlen(DRIVE_GetRoot( full_name.short_name[0] - 'A' ));
938 while (*res == '/') res++;
939 if (buflen)
940 {
941 if (buflen > 3) lstrcpynA( buffer + 3, res, buflen - 3 );
942 for (p = buffer; *p; p++) if (*p == '/') *p = '\\';
943 if (lastpart) *lastpart = strrchr( buffer, '\\' ) + 1;
944 }
945 TRACE("Returning %d\n", strlen(res) + 3 );
946 return strlen(res) + 3;
947}