Remove (buf && !buflen) checking.
[wine/dcerpc.git] / libs / wine / config.c
blobb2b039c7f1eccc957e32a02494f21bde41cb2800
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 *config_dir;
43 static char *server_dir;
44 static char *user_name;
45 static char *argv0_path;
46 static char *argv0_name;
48 #ifdef __GNUC__
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)));
51 #endif
53 /* die on a fatal error */
54 static void fatal_error( const char *err, ... )
56 va_list args;
58 va_start( args, err );
59 fprintf( stderr, "wine: " );
60 vfprintf( stderr, err, args );
61 va_end( args );
62 exit(1);
65 /* die on a fatal error */
66 static void fatal_perror( const char *err, ... )
68 va_list args;
70 va_start( args, err );
71 fprintf( stderr, "wine: " );
72 vfprintf( stderr, err, args );
73 perror( " " );
74 va_end( args );
75 exit(1);
78 /* malloc wrapper */
79 static void *xmalloc( size_t size )
81 void *res;
83 if (!size) size = 1;
84 if (!(res = malloc( size ))) fatal_error( "virtual memory exhausted\n");
85 return res;
88 /* strdup wrapper */
89 static char *xstrdup( const char *str )
91 size_t len = strlen(str) + 1;
92 char *res = xmalloc( len );
93 memcpy( res, str, len );
94 return res;
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 char *p;
108 #ifdef HAVE_GETUID
109 const unsigned int uid = getuid();
110 #else
111 const unsigned int uid = 0;
112 #endif
114 server_dir = xmalloc( sizeof(server_root_prefix) + 32 + sizeof(server_dir_prefix) +
115 2*sizeof(dev) + 2*sizeof(ino) );
116 sprintf( server_dir, "%s%u%s", server_root_prefix, uid, server_dir_prefix );
117 p = server_dir + strlen(server_dir);
119 if (sizeof(dev) > sizeof(unsigned long) && dev > ~0UL)
120 p += sprintf( p, "%lx%08lx-", (unsigned long)(dev >> 32), (unsigned long)dev );
121 else
122 p += sprintf( p, "%lx-", (unsigned long)dev );
124 if (sizeof(ino) > sizeof(unsigned long) && ino > ~0UL)
125 sprintf( p, "%lx%08lx", (unsigned long)(ino >> 32), (unsigned long)ino );
126 else
127 sprintf( p, "%lx", (unsigned long)ino );
130 /* initialize all the paths values */
131 static void init_paths(void)
133 struct stat st;
135 const char *home = getenv( "HOME" );
136 const char *user = NULL;
137 const char *prefix = getenv( "WINEPREFIX" );
139 #ifdef HAVE_GETPWUID
140 char uid_str[32];
141 struct passwd *pwd = getpwuid( getuid() );
143 if (pwd)
145 user = pwd->pw_name;
146 if (!home) home = pwd->pw_dir;
148 if (!user)
150 sprintf( uid_str, "%u", getuid() );
151 user = uid_str;
153 #else /* HAVE_GETPWUID */
154 if (!(user = getenv( "USER" )))
155 fatal_error( "cannot determine your user name, set the USER environment variable\n" );
156 #endif /* HAVE_GETPWUID */
157 user_name = xstrdup( user );
159 /* build config_dir */
161 if (prefix)
163 if (!(config_dir = strdup( prefix ))) fatal_error( "virtual memory exhausted\n");
164 remove_trailing_slashes( config_dir );
165 if (config_dir[0] != '/')
166 fatal_error( "invalid directory %s in WINEPREFIX: not an absolute path\n", prefix );
167 if (stat( config_dir, &st ) == -1)
169 if (errno != ENOENT)
170 fatal_perror( "cannot open %s as specified in WINEPREFIX", config_dir );
171 fatal_error( "the '%s' directory specified in WINEPREFIX doesn't exist.\n"
172 "You may want to create it by running 'wineprefixcreate'.\n", config_dir );
175 else
177 if (!home) fatal_error( "could not determine your home directory\n" );
178 if (home[0] != '/') fatal_error( "your home directory %s is not an absolute path\n", home );
179 config_dir = xmalloc( strlen(home) + sizeof(server_config_dir) );
180 strcpy( config_dir, home );
181 remove_trailing_slashes( config_dir );
182 strcat( config_dir, server_config_dir );
183 if (stat( config_dir, &st ) == -1)
185 if (errno == ENOENT) return; /* will be created later on */
186 fatal_perror( "cannot open %s", config_dir );
189 if (!S_ISDIR(st.st_mode)) fatal_error( "%s is not a directory\n", config_dir );
191 init_server_dir( st.st_dev, st.st_ino );
194 /* initialize the argv0 path */
195 void wine_init_argv0_path( const char *argv0 )
197 size_t size, len;
198 const char *p;
199 char *cwd;
201 if (!(p = strrchr( argv0, '/' )))
203 argv0_name = xstrdup( argv0 );
204 return; /* if argv0 doesn't contain a path, don't store any path */
206 else argv0_name = xstrdup( p + 1 );
208 len = p - argv0 + 1;
209 if (argv0[0] == '/') /* absolute path */
211 argv0_path = xmalloc( len + 1 );
212 memcpy( argv0_path, argv0, len );
213 argv0_path[len] = 0;
214 return;
217 /* relative path, make it absolute */
218 for (size = 256 + len; ; size *= 2)
220 if (!(cwd = malloc( size ))) break;
221 if (getcwd( cwd, size - len ))
223 argv0_path = cwd;
224 cwd += strlen(cwd);
225 *cwd++ = '/';
226 memcpy( cwd, argv0, len );
227 cwd[len] = 0;
228 return;
230 free( cwd );
231 if (errno != ERANGE) break;
235 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
236 const char *wine_get_config_dir(void)
238 if (!config_dir) init_paths();
239 return config_dir;
242 /* return the full name of the server directory (the one containing the socket) */
243 const char *wine_get_server_dir(void)
245 if (!server_dir)
247 if (!config_dir) init_paths();
248 else
250 struct stat st;
252 if (stat( config_dir, &st ) == -1)
254 if (errno != ENOENT) fatal_error( "cannot stat %s\n", config_dir );
255 return NULL; /* will have to try again once config_dir has been created */
257 init_server_dir( st.st_dev, st.st_ino );
260 return server_dir;
263 /* return the current user name */
264 const char *wine_get_user_name(void)
266 if (!user_name) init_paths();
267 return user_name;
270 /* exec a binary using the preloader if requested; helper for wine_exec_wine_binary */
271 static void preloader_exec( char **argv, char **envp, int use_preloader )
273 #ifdef linux
274 if (use_preloader)
276 static const char preloader[] = "wine-preloader";
277 char *p, *full_name;
278 char **last_arg = argv, **new_argv;
280 if (!(p = strrchr( argv[0], '/' ))) p = argv[0];
281 else p++;
283 full_name = xmalloc( p - argv[0] + sizeof(preloader) );
284 memcpy( full_name, argv[0], p - argv[0] );
285 memcpy( full_name + (p - argv[0]), preloader, sizeof(preloader) );
287 /* make a copy of argv */
288 while (*last_arg) last_arg++;
289 new_argv = xmalloc( (last_arg - argv + 2) * sizeof(*argv) );
290 memcpy( new_argv + 1, argv, (last_arg - argv + 1) * sizeof(*argv) );
291 new_argv[0] = full_name;
292 if (envp) execve( full_name, new_argv, envp );
293 else execv( full_name, new_argv );
294 free( new_argv );
295 free( full_name );
296 return;
298 #endif
299 if (envp) execve( argv[0], argv, envp );
300 else execv( argv[0], argv );
303 /* exec a wine internal binary (either the wine loader or the wine server) */
304 void wine_exec_wine_binary( const char *name, char **argv, char **envp, int use_preloader )
306 const char *path, *pos, *ptr;
308 if (name && strchr( name, '/' ))
310 argv[0] = (char *)name;
311 preloader_exec( argv, envp, use_preloader );
312 return;
314 else if (!name) name = argv0_name;
316 /* first, try bin directory */
317 argv[0] = xmalloc( sizeof(BINDIR "/") + strlen(name) );
318 strcpy( argv[0], BINDIR "/" );
319 strcat( argv[0], name );
320 preloader_exec( argv, envp, use_preloader );
321 free( argv[0] );
323 /* now try the path of argv0 of the current binary */
324 if (argv0_path)
326 argv[0] = xmalloc( strlen(argv0_path) + strlen(name) + 1 );
327 strcpy( argv[0], argv0_path );
328 strcat( argv[0], name );
329 preloader_exec( argv, envp, use_preloader );
330 free( argv[0] );
333 /* now search in the Unix path */
334 if ((path = getenv( "PATH" )))
336 argv[0] = xmalloc( strlen(path) + strlen(name) + 2 );
337 pos = path;
338 for (;;)
340 while (*pos == ':') pos++;
341 if (!*pos) break;
342 if (!(ptr = strchr( pos, ':' ))) ptr = pos + strlen(pos);
343 memcpy( argv[0], pos, ptr - pos );
344 strcpy( argv[0] + (ptr - pos), "/" );
345 strcat( argv[0] + (ptr - pos), name );
346 preloader_exec( argv, envp, use_preloader );
347 pos = ptr;
349 free( argv[0] );