Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 1 | /* |
| 2 | * DOS directories functions |
| 3 | * |
| 4 | * Copyright 1995 Alexandre Julliard |
| 5 | */ |
| 6 | |
| 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | #include <unistd.h> |
| 10 | |
| 11 | #include "windows.h" |
| 12 | #include "dos_fs.h" |
| 13 | #include "drive.h" |
| 14 | #include "file.h" |
| 15 | #include "msdos.h" |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 16 | #include "options.h" |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 17 | #include "xmalloc.h" |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 18 | #include "string32.h" |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 19 | #include "stddebug.h" |
| 20 | #include "debug.h" |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 21 | #include "string32.h" |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 22 | |
| 23 | #define MAX_PATH_ELEMENTS 20 |
| 24 | |
| 25 | static char *DIR_WindowsDosDir; |
| 26 | static char *DIR_WindowsUnixDir; |
| 27 | static char *DIR_SystemDosDir; |
| 28 | static char *DIR_SystemUnixDir; |
| 29 | static char *DIR_TempDosDir; |
| 30 | static char *DIR_TempUnixDir; |
| 31 | |
| 32 | static char *DIR_DosPath[MAX_PATH_ELEMENTS]; /* Path in DOS format */ |
| 33 | static char *DIR_UnixPath[MAX_PATH_ELEMENTS]; /* Path in Unix format */ |
| 34 | static int DIR_PathElements = 0; |
| 35 | |
| 36 | /*********************************************************************** |
| 37 | * DIR_GetPath |
| 38 | * |
| 39 | * Get a path name from the wine.ini file and make sure it is valid. |
| 40 | */ |
| 41 | static int DIR_GetPath( const char *keyname, const char *defval, |
| 42 | char **dos_path, char **unix_path ) |
| 43 | { |
| 44 | char path[MAX_PATHNAME_LEN]; |
| 45 | const char *dos_name ,*unix_name; |
| 46 | BYTE attr; |
| 47 | |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 48 | PROFILE_GetWineIniString( "wine", keyname, defval, path, sizeof(path) ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 49 | if (!(unix_name = DOSFS_GetUnixFileName( path, TRUE )) || |
| 50 | !FILE_Stat( unix_name, &attr, NULL, NULL, NULL ) || |
| 51 | !(attr & FA_DIRECTORY)) |
| 52 | { |
| 53 | fprintf(stderr, "Invalid path '%s' for %s directory\n", path, keyname); |
| 54 | return 0; |
| 55 | } |
| 56 | if (!(dos_name = DOSFS_GetDosTrueName( unix_name, TRUE ))) |
| 57 | { |
| 58 | fprintf( stderr, "Could not get DOS name for %s directory '%s'\n", |
| 59 | keyname, unix_name ); |
| 60 | return 0; |
| 61 | } |
| 62 | *unix_path = xstrdup( unix_name ); |
| 63 | *dos_path = xstrdup( dos_name ); |
| 64 | return 1; |
| 65 | } |
| 66 | |
| 67 | |
| 68 | /*********************************************************************** |
| 69 | * DIR_ParseWindowsPath |
| 70 | */ |
| 71 | void DIR_ParseWindowsPath( char *path ) |
| 72 | { |
| 73 | char *p; |
| 74 | const char *dos_name ,*unix_name; |
| 75 | BYTE attr; |
| 76 | int i; |
| 77 | |
| 78 | for ( ; path && *path; path = p) |
| 79 | { |
| 80 | p = strchr( path, ';' ); |
| 81 | if (p) while (*p == ';') *p++ = '\0'; |
| 82 | |
| 83 | if (DIR_PathElements >= MAX_PATH_ELEMENTS) |
| 84 | { |
| 85 | fprintf( stderr, "Warning: path has more than %d elements.\n", |
| 86 | MAX_PATH_ELEMENTS ); |
| 87 | break; |
| 88 | } |
| 89 | if (!(unix_name = DOSFS_GetUnixFileName( path, TRUE )) || |
| 90 | !FILE_Stat( unix_name, &attr, NULL, NULL, NULL ) || |
| 91 | !(attr & FA_DIRECTORY)) |
| 92 | { |
| 93 | fprintf(stderr,"Warning: invalid dir '%s' in path, deleting it.\n", |
| 94 | path ); |
| 95 | continue; |
| 96 | } |
| 97 | if (!(dos_name = DOSFS_GetDosTrueName( unix_name, TRUE ))) |
| 98 | { |
| 99 | fprintf( stderr, "Warning: could not get DOS name for '%s' in path, deleting it.\n", |
| 100 | unix_name ); |
| 101 | continue; |
| 102 | } |
| 103 | DIR_UnixPath[DIR_PathElements] = xstrdup( unix_name ); |
| 104 | DIR_DosPath[DIR_PathElements] = xstrdup( dos_name ); |
| 105 | DIR_PathElements++; |
| 106 | } |
| 107 | |
| 108 | if (debugging_dosfs) |
| 109 | for (i = 0; i < DIR_PathElements; i++) |
| 110 | { |
| 111 | dprintf_dosfs( stddeb, "Path[%d]: %s = %s\n", |
| 112 | i, DIR_DosPath[i], DIR_UnixPath[i] ); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /*********************************************************************** |
| 118 | * DIR_Init |
| 119 | */ |
| 120 | int DIR_Init(void) |
| 121 | { |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 122 | char path[MAX_PATHNAME_LEN], *env_p; |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 123 | int drive; |
| 124 | const char *cwd; |
| 125 | |
| 126 | if (!getcwd( path, MAX_PATHNAME_LEN )) |
| 127 | { |
| 128 | perror( "Could not get current directory" ); |
| 129 | return 0; |
| 130 | } |
| 131 | cwd = path; |
| 132 | if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1) |
| 133 | { |
| 134 | fprintf( stderr, "Warning: could not find DOS drive for cwd %s; starting in windows directory.\n", |
| 135 | cwd ); |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | cwd = DOSFS_GetDosTrueName( path, TRUE ); |
| 140 | DRIVE_SetCurrentDrive( drive ); |
| 141 | DRIVE_Chdir( drive, cwd + 2 ); |
| 142 | } |
| 143 | |
| 144 | if (!(DIR_GetPath( "windows", "c:\\windows", |
| 145 | &DIR_WindowsDosDir, &DIR_WindowsUnixDir ))) return 0; |
| 146 | if (!(DIR_GetPath( "system", "c:\\windows\\system", |
| 147 | &DIR_SystemDosDir, &DIR_SystemUnixDir ))) return 0; |
| 148 | if (!(DIR_GetPath( "temp", "c:\\windows", |
| 149 | &DIR_TempDosDir, &DIR_TempUnixDir ))) return 0; |
Alexandre Julliard | 8bbf818 | 1996-09-13 16:50:47 +0000 | [diff] [blame^] | 150 | if (-1==access(DIR_TempUnixDir,W_OK)) { |
| 151 | if (errno==EACCES) |
| 152 | fprintf(stderr,"Warning: The Temporary Directory (as specified in wine.conf) is NOT writeable. Please check your configuration.\n"); |
| 153 | else |
| 154 | fprintf(stderr,"Warning: Access to Temporary Directory failed (%s).\n",strerror(errno)); |
| 155 | } |
| 156 | |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 157 | if (drive == -1) |
| 158 | { |
| 159 | drive = DIR_WindowsDosDir[0] - 'A'; |
| 160 | DRIVE_SetCurrentDrive( drive ); |
| 161 | DRIVE_Chdir( drive, DIR_WindowsDosDir + 2 ); |
| 162 | } |
| 163 | |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 164 | PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system", |
| 165 | path, sizeof(path) ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 166 | DIR_ParseWindowsPath( path ); |
| 167 | |
| 168 | dprintf_dosfs( stddeb, "WindowsDir = %s\nSystemDir = %s\n", |
| 169 | DIR_WindowsDosDir, DIR_SystemDosDir ); |
| 170 | dprintf_dosfs( stddeb, "TempDir = %s\nCwd = %c:\\%s\n", |
| 171 | DIR_TempDosDir, 'A' + drive, DRIVE_GetDosCwd( drive ) ); |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 172 | |
| 173 | /* Put the temp and Windows directories into the environment */ |
| 174 | |
Alexandre Julliard | 0c126c7 | 1996-02-18 18:44:41 +0000 | [diff] [blame] | 175 | env_p = (char *)xmalloc( strlen(DIR_TempDosDir) + 6 ); |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 176 | strcpy( env_p, "TEMP=" ); |
| 177 | strcpy( env_p + 5, DIR_TempDosDir ); |
| 178 | putenv( env_p ); |
Alexandre Julliard | 0c126c7 | 1996-02-18 18:44:41 +0000 | [diff] [blame] | 179 | env_p = (char *)xmalloc( strlen(DIR_WindowsDosDir) + 8 ); |
Alexandre Julliard | 7e56f68 | 1996-01-31 19:02:28 +0000 | [diff] [blame] | 180 | strcpy( env_p, "windir=" ); |
| 181 | strcpy( env_p + 7, DIR_WindowsDosDir ); |
| 182 | putenv( env_p ); |
| 183 | |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 184 | return 1; |
| 185 | } |
| 186 | |
| 187 | |
| 188 | /*********************************************************************** |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 189 | * GetTempPath32A (KERNEL32.292) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 190 | */ |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 191 | UINT32 GetTempPath32A( UINT32 count, LPSTR path ) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 192 | { |
Alexandre Julliard | d90840e | 1996-06-11 16:02:08 +0000 | [diff] [blame] | 193 | if (path) lstrcpyn32A( path, DIR_TempDosDir, count ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 194 | return strlen( DIR_TempDosDir ); |
| 195 | } |
| 196 | |
| 197 | |
| 198 | /*********************************************************************** |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 199 | * GetTempPath32W (KERNEL32.293) |
| 200 | */ |
| 201 | UINT32 GetTempPath32W( UINT32 count, LPWSTR path ) |
| 202 | { |
| 203 | LPSTR tmp = (char*)xmalloc(count); |
| 204 | UINT32 len = GetTempPath32A( count, tmp ); |
| 205 | if (path) STRING32_AnsiToUni( path, tmp ); |
| 206 | free(tmp); |
| 207 | return len; |
| 208 | } |
| 209 | |
| 210 | |
| 211 | /*********************************************************************** |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 212 | * DIR_GetTempUnixDir |
| 213 | */ |
| 214 | UINT DIR_GetTempUnixDir( LPSTR path, UINT count ) |
| 215 | { |
Alexandre Julliard | d90840e | 1996-06-11 16:02:08 +0000 | [diff] [blame] | 216 | if (path) lstrcpyn32A( path, DIR_TempUnixDir, count ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 217 | return strlen( DIR_TempUnixDir ); |
| 218 | } |
| 219 | |
| 220 | |
| 221 | /*********************************************************************** |
| 222 | * DIR_GetWindowsUnixDir |
| 223 | */ |
| 224 | UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count ) |
| 225 | { |
Alexandre Julliard | d90840e | 1996-06-11 16:02:08 +0000 | [diff] [blame] | 226 | if (path) lstrcpyn32A( path, DIR_WindowsUnixDir, count ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 227 | return strlen( DIR_WindowsUnixDir ); |
| 228 | } |
| 229 | |
| 230 | |
| 231 | /*********************************************************************** |
| 232 | * DIR_GetSystemUnixDir |
| 233 | */ |
| 234 | UINT DIR_GetSystemUnixDir( LPSTR path, UINT count ) |
| 235 | { |
Alexandre Julliard | d90840e | 1996-06-11 16:02:08 +0000 | [diff] [blame] | 236 | if (path) lstrcpyn32A( path, DIR_SystemUnixDir, count ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 237 | return strlen( DIR_SystemUnixDir ); |
| 238 | } |
| 239 | |
| 240 | |
| 241 | /*********************************************************************** |
| 242 | * DIR_GetDosPath |
| 243 | */ |
| 244 | UINT DIR_GetDosPath( int element, LPSTR path, UINT count ) |
| 245 | { |
| 246 | if ((element < 0) || (element >= DIR_PathElements)) return 0; |
Alexandre Julliard | d90840e | 1996-06-11 16:02:08 +0000 | [diff] [blame] | 247 | if (path) lstrcpyn32A( path, DIR_DosPath[element], count ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 248 | return strlen( DIR_DosPath[element] ); |
| 249 | } |
| 250 | |
| 251 | |
| 252 | /*********************************************************************** |
| 253 | * GetTempDrive (KERNEL.92) |
| 254 | */ |
| 255 | BYTE GetTempDrive( BYTE ignored ) |
| 256 | { |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 257 | /* FIXME: apparently Windows does something with the ignored byte */ |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 258 | return DIR_TempDosDir[0]; |
| 259 | } |
| 260 | |
| 261 | |
Alexandre Julliard | ca22b33 | 1996-07-12 19:02:39 +0000 | [diff] [blame] | 262 | UINT32 WIN16_GetTempDrive( BYTE ignored ) |
| 263 | { |
| 264 | /* A closer look at krnl386.exe shows what the SDK doesn't mention: |
| 265 | * |
| 266 | * returns: |
| 267 | * AL: driveletter |
| 268 | * AH: ':' - yes, some kernel code even does stosw with |
| 269 | * the returned AX. |
| 270 | * DX: 1 for success |
| 271 | */ |
| 272 | return MAKELONG( GetTempDrive(ignored) | (':' << 8), 1 ); |
| 273 | } |
| 274 | |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 275 | /*********************************************************************** |
| 276 | * GetWindowsDirectory (KERNEL.134) |
| 277 | */ |
| 278 | UINT GetWindowsDirectory( LPSTR path, UINT count ) |
| 279 | { |
Alexandre Julliard | d90840e | 1996-06-11 16:02:08 +0000 | [diff] [blame] | 280 | if (path) lstrcpyn32A( path, DIR_WindowsDosDir, count ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 281 | return strlen( DIR_WindowsDosDir ); |
| 282 | } |
| 283 | |
| 284 | |
| 285 | /*********************************************************************** |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 286 | * GetSystemDirectory16 (KERNEL.135) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 287 | */ |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 288 | UINT16 GetSystemDirectory16( LPSTR path, UINT16 count ) |
| 289 | { |
| 290 | return (UINT16)GetSystemDirectory32A( path, count ); |
| 291 | } |
| 292 | |
| 293 | |
| 294 | /*********************************************************************** |
| 295 | * GetSystemDirectory32A (KERNEL32.282) |
| 296 | */ |
| 297 | UINT32 GetSystemDirectory32A( LPSTR path, UINT32 count ) |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 298 | { |
Alexandre Julliard | d90840e | 1996-06-11 16:02:08 +0000 | [diff] [blame] | 299 | if (path) lstrcpyn32A( path, DIR_SystemDosDir, count ); |
Alexandre Julliard | 4f8c37b | 1996-01-14 18:12:01 +0000 | [diff] [blame] | 300 | return strlen( DIR_SystemDosDir ); |
| 301 | } |
Alexandre Julliard | 3051b64 | 1996-07-05 17:14:13 +0000 | [diff] [blame] | 302 | |
| 303 | |
| 304 | /*********************************************************************** |
| 305 | * GetSystemDirectory32W (KERNEL32.283) |
| 306 | */ |
| 307 | UINT32 GetSystemDirectory32W( LPWSTR path, UINT32 count ) |
| 308 | { |
| 309 | if (path) |
| 310 | { |
| 311 | LPWSTR tmp = STRING32_DupAnsiToUni( DIR_SystemDosDir ); |
| 312 | lstrcpyn32W( path, tmp, count ); |
| 313 | free (tmp); |
| 314 | } |
| 315 | return strlen( DIR_SystemDosDir ); |
| 316 | } |