d3dx9: Shader assembler ps_2_x support.
[wine.git] / libs / wine / config.c
blob6bb87b06a3d8ebddd2af6be6723db8dce567f5ee
1 /*
2 * Configuration parameters shared between Wine server and clients
4 * Copyright 2002 Alexandre Julliard
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <sys/stat.h>
30 #ifdef HAVE_UNISTD_H
31 # include <unistd.h>
32 #endif
33 #ifdef HAVE_PWD_H
34 #include <pwd.h>
35 #endif
36 #include "wine/library.h"
38 static const char server_config_dir[] = "/.wine"; /* config dir relative to $HOME */
39 static const char server_root_prefix[] = "/tmp/.wine-"; /* prefix for server root dir */
40 static const char server_dir_prefix[] = "/server-"; /* prefix for server dir */
42 static char *bindir;
43 static char *dlldir;
44 static char *datadir;
45 static char *config_dir;
46 static char *server_dir;
47 static char *build_dir;
48 static char *user_name;
49 static char *argv0_name;
51 #ifdef __GNUC__
52 static void fatal_error( const char *err, ... ) __attribute__((noreturn,format(printf,1,2)));
53 static void fatal_perror( const char *err, ... ) __attribute__((noreturn,format(printf,1,2)));
54 #endif
56 /* die on a fatal error */
57 static void fatal_error( const char *err, ... )
59 va_list args;
61 va_start( args, err );
62 fprintf( stderr, "wine: " );
63 vfprintf( stderr, err, args );
64 va_end( args );
65 exit(1);
68 /* die on a fatal error */
69 static void fatal_perror( const char *err, ... )
71 va_list args;
73 va_start( args, err );
74 fprintf( stderr, "wine: " );
75 vfprintf( stderr, err, args );
76 perror( " " );
77 va_end( args );
78 exit(1);
81 /* malloc wrapper */
82 static void *xmalloc( size_t size )
84 void *res;
86 if (!size) size = 1;
87 if (!(res = malloc( size ))) fatal_error( "virtual memory exhausted\n");
88 return res;
91 /* strdup wrapper */
92 static char *xstrdup( const char *str )
94 size_t len = strlen(str) + 1;
95 char *res = xmalloc( len );
96 memcpy( res, str, len );
97 return res;
100 /* remove all trailing slashes from a path name */
101 static inline void remove_trailing_slashes( char *path )
103 int len = strlen( path );
104 while (len > 1 && path[len-1] == '/') path[--len] = 0;
107 /* build a path from the specified dir and name */
108 static char *build_path( const char *dir, const char *name )
110 size_t len = strlen(dir);
111 char *ret = xmalloc( len + strlen(name) + 2 );
113 memcpy( ret, dir, len );
114 if (len && ret[len-1] != '/') ret[len++] = '/';
115 strcpy( ret + len, name );
116 return ret;
119 /* return the directory that contains the library at run-time */
120 static char *get_runtime_libdir(void)
122 #ifdef HAVE_DLADDR
123 Dl_info info;
124 char *libdir;
126 if (dladdr( get_runtime_libdir, &info ) && info.dli_fname[0] == '/')
128 const char *p = strrchr( info.dli_fname, '/' );
129 unsigned int len = p - info.dli_fname;
130 if (!len) len++; /* include initial slash */
131 libdir = xmalloc( len + 1 );
132 memcpy( libdir, info.dli_fname, len );
133 libdir[len] = 0;
134 return libdir;
136 #endif /* HAVE_DLADDR */
137 return NULL;
140 /* return the directory that contains the main exe at run-time */
141 static char *get_runtime_bindir( const char *argv0 )
143 char *p, *bindir, *cwd;
144 size_t len, size;
146 #ifdef linux
147 for (size = 256; ; size *= 2)
149 int ret;
150 if (!(bindir = malloc( size ))) break;
151 if ((ret = readlink( "/proc/self/exe", bindir, size )) == -1) break;
152 if (ret != size)
154 if (!(p = memrchr( bindir, '/', ret ))) break;
155 if (p == bindir) p++;
156 *p = 0;
157 return bindir;
159 free( bindir );
161 free( bindir );
162 #endif
164 if (!(p = strrchr( argv0, '/' ))) return NULL;
166 len = p - argv0;
167 if (!len) len++; /* include leading slash */
169 if (argv0[0] == '/') /* absolute path */
171 bindir = xmalloc( len + 1 );
172 memcpy( bindir, argv0, len );
173 bindir[len] = 0;
175 else
177 /* relative path, make it absolute */
178 for (size = 256 + len; ; size *= 2)
180 if (!(cwd = malloc( size ))) return NULL;
181 if (getcwd( cwd, size - len ))
183 bindir = cwd;
184 cwd += strlen(cwd);
185 *cwd++ = '/';
186 memcpy( cwd, argv0, len );
187 cwd[len] = 0;
188 break;
190 free( cwd );
191 if (errno != ERANGE) return NULL;
194 return bindir;
197 /* initialize the server directory value */
198 static void init_server_dir( dev_t dev, ino_t ino )
200 char *p;
201 #ifdef HAVE_GETUID
202 const unsigned int uid = getuid();
203 #else
204 const unsigned int uid = 0;
205 #endif
207 server_dir = xmalloc( sizeof(server_root_prefix) + 32 + sizeof(server_dir_prefix) +
208 2*sizeof(dev) + 2*sizeof(ino) );
209 sprintf( server_dir, "%s%u%s", server_root_prefix, uid, server_dir_prefix );
210 p = server_dir + strlen(server_dir);
212 if (sizeof(dev) > sizeof(unsigned long) && dev > ~0UL)
213 p += sprintf( p, "%lx%08lx-", (unsigned long)((unsigned long long)dev >> 32), (unsigned long)dev );
214 else
215 p += sprintf( p, "%lx-", (unsigned long)dev );
217 if (sizeof(ino) > sizeof(unsigned long) && ino > ~0UL)
218 sprintf( p, "%lx%08lx", (unsigned long)((unsigned long long)ino >> 32), (unsigned long)ino );
219 else
220 sprintf( p, "%lx", (unsigned long)ino );
223 /* retrieve the default dll dir */
224 const char *get_dlldir( const char **default_dlldir )
226 *default_dlldir = DLLDIR;
227 return dlldir;
230 /* initialize all the paths values */
231 static void init_paths(void)
233 struct stat st;
235 const char *home = getenv( "HOME" );
236 const char *user = NULL;
237 const char *prefix = getenv( "WINEPREFIX" );
239 #ifdef HAVE_GETPWUID
240 char uid_str[32];
241 struct passwd *pwd = getpwuid( getuid() );
243 if (pwd)
245 user = pwd->pw_name;
246 if (!home) home = pwd->pw_dir;
248 if (!user)
250 sprintf( uid_str, "%lu", (unsigned long)getuid() );
251 user = uid_str;
253 #else /* HAVE_GETPWUID */
254 if (!(user = getenv( "USER" )))
255 fatal_error( "cannot determine your user name, set the USER environment variable\n" );
256 #endif /* HAVE_GETPWUID */
257 user_name = xstrdup( user );
259 /* build config_dir */
261 if (prefix)
263 config_dir = xstrdup( prefix );
264 remove_trailing_slashes( config_dir );
265 if (config_dir[0] != '/')
266 fatal_error( "invalid directory %s in WINEPREFIX: not an absolute path\n", prefix );
267 if (stat( config_dir, &st ) == -1)
269 if (errno == ENOENT) return; /* will be created later on */
270 fatal_perror( "cannot open %s as specified in WINEPREFIX", config_dir );
273 else
275 if (!home) fatal_error( "could not determine your home directory\n" );
276 if (home[0] != '/') fatal_error( "your home directory %s is not an absolute path\n", home );
277 config_dir = xmalloc( strlen(home) + sizeof(server_config_dir) );
278 strcpy( config_dir, home );
279 remove_trailing_slashes( config_dir );
280 strcat( config_dir, server_config_dir );
281 if (stat( config_dir, &st ) == -1)
283 if (errno == ENOENT) return; /* will be created later on */
284 fatal_perror( "cannot open %s", config_dir );
287 if (!S_ISDIR(st.st_mode)) fatal_error( "%s is not a directory\n", config_dir );
288 #ifdef HAVE_GETUID
289 if (st.st_uid != getuid()) fatal_error( "%s is not owned by you\n", config_dir );
290 #endif
292 init_server_dir( st.st_dev, st.st_ino );
295 /* check if bindir is valid by checking for wineserver */
296 static int is_valid_bindir( const char *bindir )
298 struct stat st;
299 char *path = build_path( bindir, "wineserver" );
300 int ret = (stat( path, &st ) != -1);
301 free( path );
302 return ret;
305 /* check if basedir is a valid build dir by checking for wineserver and ntdll */
306 /* helper for running_from_build_dir */
307 static inline int is_valid_build_dir( char *basedir, int baselen )
309 struct stat st;
311 strcpy( basedir + baselen, "/server/wineserver" );
312 if (stat( basedir, &st ) == -1) return 0; /* no wineserver found */
313 /* check for ntdll too to make sure */
314 strcpy( basedir + baselen, "/dlls/ntdll/ntdll.dll.so" );
315 if (stat( basedir, &st ) == -1) return 0; /* no ntdll found */
317 basedir[baselen] = 0;
318 return 1;
321 /* check if we are running from the build directory */
322 static char *running_from_build_dir( const char *basedir )
324 const char *p;
325 char *path;
327 /* remove last component from basedir */
328 p = basedir + strlen(basedir) - 1;
329 while (p > basedir && *p == '/') p--;
330 while (p > basedir && *p != '/') p--;
331 if (p == basedir) return NULL;
332 path = xmalloc( p - basedir + sizeof("/dlls/ntdll/ntdll.dll.so") );
333 memcpy( path, basedir, p - basedir );
335 if (!is_valid_build_dir( path, p - basedir ))
337 /* remove another component */
338 while (p > basedir && *p == '/') p--;
339 while (p > basedir && *p != '/') p--;
340 if (p == basedir || !is_valid_build_dir( path, p - basedir ))
342 free( path );
343 return NULL;
346 return path;
349 /* initialize the argv0 path */
350 void wine_init_argv0_path( const char *argv0 )
352 const char *basename;
353 char *libdir;
355 if (!(basename = strrchr( argv0, '/' ))) basename = argv0;
356 else basename++;
358 bindir = get_runtime_bindir( argv0 );
359 libdir = get_runtime_libdir();
361 if (bindir && !is_valid_bindir( bindir ))
363 build_dir = running_from_build_dir( bindir );
364 free( bindir );
365 bindir = NULL;
367 if (libdir && !bindir && !build_dir)
369 build_dir = running_from_build_dir( libdir );
370 if (!build_dir) bindir = build_path( libdir, LIB_TO_BINDIR );
373 if (build_dir)
375 argv0_name = build_path( "loader/", basename );
377 else
379 if (libdir) dlldir = build_path( libdir, LIB_TO_DLLDIR );
380 else if (bindir) dlldir = build_path( bindir, BIN_TO_DLLDIR );
382 if (bindir) datadir = build_path( bindir, BIN_TO_DATADIR );
383 argv0_name = xstrdup( basename );
385 free( libdir );
388 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
389 const char *wine_get_config_dir(void)
391 if (!config_dir) init_paths();
392 return config_dir;
395 /* retrieve the wine data dir */
396 const char *wine_get_data_dir(void)
398 return datadir;
401 /* retrieve the wine build dir (if we are running from there) */
402 const char *wine_get_build_dir(void)
404 return build_dir;
407 /* return the full name of the server directory (the one containing the socket) */
408 const char *wine_get_server_dir(void)
410 if (!server_dir)
412 if (!config_dir) init_paths();
413 else
415 struct stat st;
417 if (stat( config_dir, &st ) == -1)
419 if (errno != ENOENT) fatal_error( "cannot stat %s\n", config_dir );
420 return NULL; /* will have to try again once config_dir has been created */
422 init_server_dir( st.st_dev, st.st_ino );
425 return server_dir;
428 /* return the current user name */
429 const char *wine_get_user_name(void)
431 if (!user_name) init_paths();
432 return user_name;
435 /* return the standard version string */
436 const char *wine_get_version(void)
438 return PACKAGE_VERSION;
441 /* return the build id string */
442 const char *wine_get_build_id(void)
444 extern const char wine_build[];
445 return wine_build;
448 /* exec a binary using the preloader if requested; helper for wine_exec_wine_binary */
449 static void preloader_exec( char **argv, int use_preloader )
451 #if defined(linux) && defined(__i386__)
452 if (use_preloader)
454 static const char preloader[] = "wine-preloader";
455 char *p, *full_name;
456 char **last_arg = argv, **new_argv;
458 if (!(p = strrchr( argv[0], '/' ))) p = argv[0];
459 else p++;
461 full_name = xmalloc( p - argv[0] + sizeof(preloader) );
462 memcpy( full_name, argv[0], p - argv[0] );
463 memcpy( full_name + (p - argv[0]), preloader, sizeof(preloader) );
465 /* make a copy of argv */
466 while (*last_arg) last_arg++;
467 new_argv = xmalloc( (last_arg - argv + 2) * sizeof(*argv) );
468 memcpy( new_argv + 1, argv, (last_arg - argv + 1) * sizeof(*argv) );
469 new_argv[0] = full_name;
470 execv( full_name, new_argv );
471 free( new_argv );
472 free( full_name );
473 return;
475 #endif
476 execv( argv[0], argv );
479 /* exec a wine internal binary (either the wine loader or the wine server) */
480 void wine_exec_wine_binary( const char *name, char **argv, const char *env_var )
482 const char *path, *pos, *ptr;
483 int use_preloader = 0;
485 if (!name) /* no name means default loader */
487 name = argv0_name;
488 use_preloader = 1;
491 if ((ptr = strrchr( name, '/' )))
493 /* if we are in build dir and name contains a path, try that */
494 if (build_dir)
496 argv[0] = build_path( build_dir, name );
497 preloader_exec( argv, use_preloader );
498 free( argv[0] );
500 name = ptr + 1; /* get rid of path */
503 /* first, bin directory from the current libdir or argv0 */
504 if (bindir)
506 argv[0] = build_path( bindir, name );
507 preloader_exec( argv, use_preloader );
508 free( argv[0] );
511 /* then specified environment variable */
512 if (env_var)
514 argv[0] = (char *)env_var;
515 preloader_exec( argv, use_preloader );
518 /* now search in the Unix path */
519 if ((path = getenv( "PATH" )))
521 argv[0] = xmalloc( strlen(path) + strlen(name) + 2 );
522 pos = path;
523 for (;;)
525 while (*pos == ':') pos++;
526 if (!*pos) break;
527 if (!(ptr = strchr( pos, ':' ))) ptr = pos + strlen(pos);
528 memcpy( argv[0], pos, ptr - pos );
529 strcpy( argv[0] + (ptr - pos), "/" );
530 strcat( argv[0] + (ptr - pos), name );
531 preloader_exec( argv, use_preloader );
532 pos = ptr;
534 free( argv[0] );
537 /* and finally try BINDIR */
538 argv[0] = build_path( BINDIR, name );
539 preloader_exec( argv, use_preloader );
540 free( argv[0] );