- implementation of LdrLoadDll out of loader/module.c
[wine/wine-kai.git] / library / config.c
bloba2e3c152f7f9b017e32ccb99a4ed0340f47430bc
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 <sys/stat.h>
29 #ifdef HAVE_UNISTD_H
30 # include <unistd.h>
31 #endif
32 #ifdef HAVE_PWD_H
33 #include <pwd.h>
34 #endif
36 static const char * const server_config_dir = "/.wine"; /* config dir relative to $HOME */
37 static const char * const server_root_prefix = "/tmp/.wine-"; /* prefix for server root dir */
38 static const char * const server_dir_prefix = "/server-"; /* prefix for server dir */
40 static char *config_dir;
41 static char *server_dir;
42 static char *user_name;
44 #ifdef __GNUC__
45 static void fatal_error( const char *err, ... ) __attribute__((noreturn,format(printf,1,2)));
46 static void fatal_perror( const char *err, ... ) __attribute__((noreturn,format(printf,1,2)));
47 #endif
49 /* die on a fatal error */
50 static void fatal_error( const char *err, ... )
52 va_list args;
54 va_start( args, err );
55 fprintf( stderr, "wine: " );
56 vfprintf( stderr, err, args );
57 va_end( args );
58 exit(1);
61 /* die on a fatal error */
62 static void fatal_perror( const char *err, ... )
64 va_list args;
66 va_start( args, err );
67 fprintf( stderr, "wine: " );
68 vfprintf( stderr, err, args );
69 perror( " " );
70 va_end( args );
71 exit(1);
74 /* malloc wrapper */
75 static void *xmalloc( size_t size )
77 void *res;
79 if (!size) size = 1;
80 if (!(res = malloc( size ))) fatal_error( "virtual memory exhausted\n");
81 return res;
84 /* strdup wrapper */
85 static char *xstrdup( const char *str )
87 size_t len = strlen(str) + 1;
88 char *res = xmalloc( len );
89 memcpy( res, str, len );
90 return res;
93 /* remove all trailing slashes from a path name */
94 inline static void remove_trailing_slashes( char *path )
96 int len = strlen( path );
97 while (len > 1 && path[len-1] == '/') path[--len] = 0;
100 /* initialize all the paths values */
101 static void init_paths(void)
103 struct stat st;
104 char *p;
106 const char *home = getenv( "HOME" );
107 const char *user = NULL;
108 const char *prefix = getenv( "WINEPREFIX" );
110 #ifdef HAVE_GETPWUID
111 char uid_str[32];
112 struct passwd *pwd = getpwuid( getuid() );
114 if (pwd)
116 user = pwd->pw_name;
117 if (!home) home = pwd->pw_dir;
119 if (!user)
121 sprintf( uid_str, "%d", getuid() );
122 user = uid_str;
124 #else /* HAVE_GETPWUID */
125 if (!(user = getenv( "USER" )))
126 fatal_error( "cannot determine your user name, set the USER environment variable\n" );
127 #endif /* HAVE_GETPWUID */
128 user_name = xstrdup( user );
130 /* build config_dir */
132 if (prefix)
134 if (!(config_dir = strdup( prefix ))) fatal_error( "virtual memory exhausted\n");
135 remove_trailing_slashes( config_dir );
136 if (config_dir[0] != '/')
137 fatal_error( "invalid directory %s in WINEPREFIX: not an absolute path\n", prefix );
138 if (stat( config_dir, &st ) == -1)
139 fatal_perror( "cannot open %s as specified in WINEPREFIX", config_dir );
141 else
143 if (!home) fatal_error( "could not determine your home directory\n" );
144 if (home[0] != '/') fatal_error( "your home directory %s is not an absolute path\n", home );
145 config_dir = xmalloc( strlen(home) + strlen(server_config_dir) + 1 );
146 strcpy( config_dir, home );
147 remove_trailing_slashes( config_dir );
148 strcat( config_dir, server_config_dir );
149 if (stat( config_dir, &st ) == -1)
150 fatal_perror( "cannot open %s", config_dir );
152 if (!S_ISDIR(st.st_mode)) fatal_error( "%s is not a directory\n", config_dir );
154 /* build server_dir */
156 server_dir = xmalloc( strlen(server_root_prefix) + strlen(user) + strlen( server_dir_prefix ) +
157 2*sizeof(st.st_dev) + 2*sizeof(st.st_ino) + 2 );
158 strcpy( server_dir, server_root_prefix );
159 p = server_dir + strlen(server_dir);
160 strcpy( p, user );
161 while (*p)
163 if (*p == '/') *p = '!';
164 p++;
166 strcpy( p, server_dir_prefix );
168 if (sizeof(st.st_dev) > sizeof(unsigned long) && st.st_dev > ~0UL)
169 sprintf( server_dir + strlen(server_dir), "%lx%08lx-",
170 (unsigned long)(st.st_dev >> 32), (unsigned long)st.st_dev );
171 else
172 sprintf( server_dir + strlen(server_dir), "%lx-", (unsigned long)st.st_dev );
174 if (sizeof(st.st_ino) > sizeof(unsigned long) && st.st_ino > ~0UL)
175 sprintf( server_dir + strlen(server_dir), "%lx%08lx",
176 (unsigned long)(st.st_ino >> 32), (unsigned long)st.st_ino );
177 else
178 sprintf( server_dir + strlen(server_dir), "%lx", (unsigned long)st.st_ino );
181 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
182 const char *wine_get_config_dir(void)
184 if (!config_dir) init_paths();
185 return config_dir;
188 /* return the full name of the server directory (the one containing the socket) */
189 const char *wine_get_server_dir(void)
191 if (!server_dir) init_paths();
192 return server_dir;
195 /* return the current user name */
196 const char *wine_get_user_name(void)
198 if (!user_name) init_paths();
199 return user_name;