Release 960521
[wine.git] / files / directory.c
blob85542dd1752ca5d3e99394b6725055a4c0a35061
1 /*
2 * DOS directories functions
4 * Copyright 1995 Alexandre Julliard
5 */
7 #include <stdlib.h>
8 #include <string.h>
9 #include <unistd.h>
11 #include "windows.h"
12 #include "dos_fs.h"
13 #include "drive.h"
14 #include "file.h"
15 #include "msdos.h"
16 #include "options.h"
17 #include "xmalloc.h"
18 #include "stddebug.h"
19 #include "debug.h"
21 #define MAX_PATH_ELEMENTS 20
23 static char *DIR_WindowsDosDir;
24 static char *DIR_WindowsUnixDir;
25 static char *DIR_SystemDosDir;
26 static char *DIR_SystemUnixDir;
27 static char *DIR_TempDosDir;
28 static char *DIR_TempUnixDir;
30 static char *DIR_DosPath[MAX_PATH_ELEMENTS]; /* Path in DOS format */
31 static char *DIR_UnixPath[MAX_PATH_ELEMENTS]; /* Path in Unix format */
32 static int DIR_PathElements = 0;
34 /***********************************************************************
35 * DIR_GetPath
37 * Get a path name from the wine.ini file and make sure it is valid.
39 static int DIR_GetPath( const char *keyname, const char *defval,
40 char **dos_path, char **unix_path )
42 char path[MAX_PATHNAME_LEN];
43 const char *dos_name ,*unix_name;
44 BYTE attr;
46 PROFILE_GetWineIniString( "wine", keyname, defval, path, sizeof(path) );
47 if (!(unix_name = DOSFS_GetUnixFileName( path, TRUE )) ||
48 !FILE_Stat( unix_name, &attr, NULL, NULL, NULL ) ||
49 !(attr & FA_DIRECTORY))
51 fprintf(stderr, "Invalid path '%s' for %s directory\n", path, keyname);
52 return 0;
54 if (!(dos_name = DOSFS_GetDosTrueName( unix_name, TRUE )))
56 fprintf( stderr, "Could not get DOS name for %s directory '%s'\n",
57 keyname, unix_name );
58 return 0;
60 *unix_path = xstrdup( unix_name );
61 *dos_path = xstrdup( dos_name );
62 return 1;
66 /***********************************************************************
67 * DIR_ParseWindowsPath
69 void DIR_ParseWindowsPath( char *path )
71 char *p;
72 const char *dos_name ,*unix_name;
73 BYTE attr;
74 int i;
76 for ( ; path && *path; path = p)
78 p = strchr( path, ';' );
79 if (p) while (*p == ';') *p++ = '\0';
81 if (DIR_PathElements >= MAX_PATH_ELEMENTS)
83 fprintf( stderr, "Warning: path has more than %d elements.\n",
84 MAX_PATH_ELEMENTS );
85 break;
87 if (!(unix_name = DOSFS_GetUnixFileName( path, TRUE )) ||
88 !FILE_Stat( unix_name, &attr, NULL, NULL, NULL ) ||
89 !(attr & FA_DIRECTORY))
91 fprintf(stderr,"Warning: invalid dir '%s' in path, deleting it.\n",
92 path );
93 continue;
95 if (!(dos_name = DOSFS_GetDosTrueName( unix_name, TRUE )))
97 fprintf( stderr, "Warning: could not get DOS name for '%s' in path, deleting it.\n",
98 unix_name );
99 continue;
101 DIR_UnixPath[DIR_PathElements] = xstrdup( unix_name );
102 DIR_DosPath[DIR_PathElements] = xstrdup( dos_name );
103 DIR_PathElements++;
106 if (debugging_dosfs)
107 for (i = 0; i < DIR_PathElements; i++)
109 dprintf_dosfs( stddeb, "Path[%d]: %s = %s\n",
110 i, DIR_DosPath[i], DIR_UnixPath[i] );
115 /***********************************************************************
116 * DIR_Init
118 int DIR_Init(void)
120 char path[MAX_PATHNAME_LEN], *env_p;
121 int drive;
122 const char *cwd;
124 if (!getcwd( path, MAX_PATHNAME_LEN ))
126 perror( "Could not get current directory" );
127 return 0;
129 cwd = path;
130 if ((drive = DRIVE_FindDriveRoot( &cwd )) == -1)
132 fprintf( stderr, "Warning: could not find DOS drive for cwd %s; starting in windows directory.\n",
133 cwd );
135 else
137 cwd = DOSFS_GetDosTrueName( path, TRUE );
138 DRIVE_SetCurrentDrive( drive );
139 DRIVE_Chdir( drive, cwd + 2 );
142 if (!(DIR_GetPath( "windows", "c:\\windows",
143 &DIR_WindowsDosDir, &DIR_WindowsUnixDir ))) return 0;
144 if (!(DIR_GetPath( "system", "c:\\windows\\system",
145 &DIR_SystemDosDir, &DIR_SystemUnixDir ))) return 0;
146 if (!(DIR_GetPath( "temp", "c:\\windows",
147 &DIR_TempDosDir, &DIR_TempUnixDir ))) return 0;
149 if (drive == -1)
151 drive = DIR_WindowsDosDir[0] - 'A';
152 DRIVE_SetCurrentDrive( drive );
153 DRIVE_Chdir( drive, DIR_WindowsDosDir + 2 );
156 PROFILE_GetWineIniString("wine", "path", "c:\\windows;c:\\windows\\system",
157 path, sizeof(path) );
158 DIR_ParseWindowsPath( path );
160 dprintf_dosfs( stddeb, "WindowsDir = %s\nSystemDir = %s\n",
161 DIR_WindowsDosDir, DIR_SystemDosDir );
162 dprintf_dosfs( stddeb, "TempDir = %s\nCwd = %c:\\%s\n",
163 DIR_TempDosDir, 'A' + drive, DRIVE_GetDosCwd( drive ) );
165 /* Put the temp and Windows directories into the environment */
167 env_p = (char *)xmalloc( strlen(DIR_TempDosDir) + 6 );
168 strcpy( env_p, "TEMP=" );
169 strcpy( env_p + 5, DIR_TempDosDir );
170 putenv( env_p );
171 env_p = (char *)xmalloc( strlen(DIR_WindowsDosDir) + 8 );
172 strcpy( env_p, "windir=" );
173 strcpy( env_p + 7, DIR_WindowsDosDir );
174 putenv( env_p );
176 return 1;
180 /***********************************************************************
181 * DIR_GetTempDosDir
183 UINT DIR_GetTempDosDir( LPSTR path, UINT count )
185 if (path) lstrcpyn( path, DIR_TempDosDir, count );
186 return strlen( DIR_TempDosDir );
190 /***********************************************************************
191 * DIR_GetTempUnixDir
193 UINT DIR_GetTempUnixDir( LPSTR path, UINT count )
195 if (path) lstrcpyn( path, DIR_TempUnixDir, count );
196 return strlen( DIR_TempUnixDir );
200 /***********************************************************************
201 * DIR_GetWindowsUnixDir
203 UINT DIR_GetWindowsUnixDir( LPSTR path, UINT count )
205 if (path) lstrcpyn( path, DIR_WindowsUnixDir, count );
206 return strlen( DIR_WindowsUnixDir );
210 /***********************************************************************
211 * DIR_GetSystemUnixDir
213 UINT DIR_GetSystemUnixDir( LPSTR path, UINT count )
215 if (path) lstrcpyn( path, DIR_SystemUnixDir, count );
216 return strlen( DIR_SystemUnixDir );
220 /***********************************************************************
221 * DIR_GetDosPath
223 UINT DIR_GetDosPath( int element, LPSTR path, UINT count )
225 if ((element < 0) || (element >= DIR_PathElements)) return 0;
226 if (path) lstrcpyn( path, DIR_DosPath[element], count );
227 return strlen( DIR_DosPath[element] );
231 /***********************************************************************
232 * GetTempDrive (KERNEL.92)
234 BYTE GetTempDrive( BYTE ignored )
236 return DIR_TempDosDir[0];
240 /***********************************************************************
241 * GetWindowsDirectory (KERNEL.134)
243 UINT GetWindowsDirectory( LPSTR path, UINT count )
245 if (path) lstrcpyn( path, DIR_WindowsDosDir, count );
246 return strlen( DIR_WindowsDosDir );
250 /***********************************************************************
251 * GetSystemDirectory (KERNEL.135)
253 UINT GetSystemDirectory( LPSTR path, UINT count )
255 if (path) lstrcpyn( path, DIR_SystemDosDir, count );
256 return strlen( DIR_SystemDosDir );