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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
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 *config_dir;
43 static char *server_dir;
44 static char *user_name;
45 static char *argv0_path;
46 static char *argv0_name;
49 static void fatal_error( const char *err, ... ) __attribute__((noreturn,format(printf,1,2)));
50 static void fatal_perror( const char *err, ... ) __attribute__((noreturn,format(printf,1,2)));
53 /* die on a fatal error */
54 static void fatal_error( const char *err, ... )
58 va_start( args, err );
59 fprintf( stderr, "wine: " );
60 vfprintf( stderr, err, args );
65 /* die on a fatal error */
66 static void fatal_perror( const char *err, ... )
70 va_start( args, err );
71 fprintf( stderr, "wine: " );
72 vfprintf( stderr, err, args );
79 static void *xmalloc( size_t size )
84 if (!(res = malloc( size ))) fatal_error( "virtual memory exhausted\n");
89 static char *xstrdup( const char *str )
91 size_t len = strlen(str) + 1;
92 char *res = xmalloc( len );
93 memcpy( res, str, len );
97 /* remove all trailing slashes from a path name */
98 inline static void remove_trailing_slashes( char *path )
100 int len = strlen( path );
101 while (len > 1 && path[len-1] == '/') path[--len] = 0;
104 /* initialize the server directory value */
105 static void init_server_dir( dev_t dev, ino_t ino )
107 const char *user = wine_get_user_name();
110 server_dir = xmalloc( sizeof(server_root_prefix) + strlen(user) + sizeof(server_dir_prefix) +
111 2*sizeof(dev) + 2*sizeof(ino) );
112 strcpy( server_dir, server_root_prefix );
113 p = server_dir + sizeof(server_root_prefix) - 1;
117 if (*p == '/') *p = '!';
120 strcpy( p, server_dir_prefix );
121 p += sizeof(server_dir_prefix) - 1;
123 if (sizeof(dev) > sizeof(unsigned long) && dev > ~0UL)
124 sprintf( p, "%lx%08lx-", (unsigned long)(dev >> 32), (unsigned long)dev );
126 sprintf( p, "%lx-", (unsigned long)dev );
128 if (sizeof(ino) > sizeof(unsigned long) && ino > ~0UL)
129 sprintf( p, "%lx%08lx", (unsigned long)(ino >> 32), (unsigned long)ino );
131 sprintf( p, "%lx", (unsigned long)ino );
134 /* initialize all the paths values */
135 static void init_paths(void)
139 const char *home = getenv( "HOME" );
140 const char *user = NULL;
141 const char *prefix = getenv( "WINEPREFIX" );
145 struct passwd *pwd = getpwuid( getuid() );
150 if (!home) home = pwd->pw_dir;
154 sprintf( uid_str, "%d", getuid() );
157 #else /* HAVE_GETPWUID */
158 if (!(user = getenv( "USER" )))
159 fatal_error( "cannot determine your user name, set the USER environment variable\n" );
160 #endif /* HAVE_GETPWUID */
161 user_name = xstrdup( user );
163 /* build config_dir */
167 if (!(config_dir = strdup( prefix ))) fatal_error( "virtual memory exhausted\n");
168 remove_trailing_slashes( config_dir );
169 if (config_dir[0] != '/')
170 fatal_error( "invalid directory %s in WINEPREFIX: not an absolute path\n", prefix );
171 if (stat( config_dir, &st ) == -1)
174 fatal_perror( "cannot open %s as specified in WINEPREFIX", config_dir );
175 fatal_error( "the '%s' directory specified in WINEPREFIX doesn't exist.\n"
176 "You may want to create it by running 'wineprefixcreate'.\n", config_dir );
181 if (!home) fatal_error( "could not determine your home directory\n" );
182 if (home[0] != '/') fatal_error( "your home directory %s is not an absolute path\n", home );
183 config_dir = xmalloc( strlen(home) + sizeof(server_config_dir) );
184 strcpy( config_dir, home );
185 remove_trailing_slashes( config_dir );
186 strcat( config_dir, server_config_dir );
187 if (stat( config_dir, &st ) == -1)
189 if (errno == ENOENT) return; /* will be created later on */
190 fatal_perror( "cannot open %s", config_dir );
193 if (!S_ISDIR(st.st_mode)) fatal_error( "%s is not a directory\n", config_dir );
195 init_server_dir( st.st_dev, st.st_ino );
198 /* initialize the argv0 path */
199 void wine_init_argv0_path( const char *argv0 )
205 if (!(p = strrchr( argv0, '/' )))
207 argv0_name = xstrdup( argv0 );
208 return; /* if argv0 doesn't contain a path, don't store any path */
210 else argv0_name = xstrdup( p + 1 );
213 if (argv0[0] == '/') /* absolute path */
215 argv0_path = xmalloc( len + 1 );
216 memcpy( argv0_path, argv0, len );
221 /* relative path, make it absolute */
222 for (size = 256 + len; ; size *= 2)
224 if (!(cwd = malloc( size ))) break;
225 if (getcwd( cwd, size - len ))
230 memcpy( cwd, argv0, len );
235 if (errno != ERANGE) break;
239 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
240 const char *wine_get_config_dir(void)
242 if (!config_dir) init_paths();
246 /* return the full name of the server directory (the one containing the socket) */
247 const char *wine_get_server_dir(void)
251 if (!config_dir) init_paths();
256 if (stat( config_dir, &st ) == -1)
258 if (errno != ENOENT) fatal_error( "cannot stat %s\n", config_dir );
259 return NULL; /* will have to try again once config_dir has been created */
261 init_server_dir( st.st_dev, st.st_ino );
267 /* return the current user name */
268 const char *wine_get_user_name(void)
270 if (!user_name) init_paths();
274 /* exec a wine internal binary (either the wine loader or the wine server) */
275 /* if name is null, default to the name of the current binary */
276 void wine_exec_wine_binary( const char *name, char **argv, char **envp )
278 const char *path, *pos, *ptr;
279 extern char **environ;
281 if (!envp) envp = environ;
282 if (!name) name = argv0_name;
284 /* first, try bin directory */
285 argv[0] = xmalloc( sizeof(BINDIR "/") + strlen(name) );
286 strcpy( argv[0], BINDIR "/" );
287 strcat( argv[0], name );
288 execve( argv[0], argv, envp );
291 /* now try the path of argv0 of the current binary */
294 argv[0] = xmalloc( strlen(argv0_path) + strlen(name) + 1 );
295 strcpy( argv[0], argv0_path );
296 strcat( argv[0], name );
297 execve( argv[0], argv, envp );
301 /* now search in the Unix path */
302 if ((path = getenv( "PATH" )))
304 argv[0] = xmalloc( strlen(path) + strlen(name) + 2 );
308 while (*pos == ':') pos++;
310 if (!(ptr = strchr( pos, ':' ))) ptr = pos + strlen(pos);
311 memcpy( argv[0], pos, ptr - pos );
312 strcpy( argv[0] + (ptr - pos), "/" );
313 strcat( argv[0] + (ptr - pos), name );
314 execve( argv[0], argv, envp );