Release 20020904.
[wine/gsoc-2012-control.git] / library / config.c
blobf4b8daba9a5c4b797d2a561de80b988135ca2b60
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 <errno.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.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
37 static const char * const server_config_dir = "/.wine"; /* config dir relative to $HOME */
38 static const char * const server_root_prefix = "/tmp/.wine-"; /* prefix for server root dir */
39 static const char * const server_dir_prefix = "/server-"; /* prefix for server dir */
41 static char *config_dir;
42 static char *server_dir;
43 static char *user_name;
45 #ifdef __GNUC__
46 static void fatal_error( const char *err, ... ) __attribute__((noreturn,format(printf,1,2)));
47 static void fatal_perror( const char *err, ... ) __attribute__((noreturn,format(printf,1,2)));
48 #endif
50 /* die on a fatal error */
51 static void fatal_error( const char *err, ... )
53 va_list args;
55 va_start( args, err );
56 fprintf( stderr, "wine: " );
57 vfprintf( stderr, err, args );
58 va_end( args );
59 exit(1);
62 /* die on a fatal error */
63 static void fatal_perror( const char *err, ... )
65 va_list args;
67 va_start( args, err );
68 fprintf( stderr, "wine: " );
69 vfprintf( stderr, err, args );
70 perror( " " );
71 va_end( args );
72 exit(1);
75 /* malloc wrapper */
76 static void *xmalloc( size_t size )
78 void *res;
80 if (!size) size = 1;
81 if (!(res = malloc( size ))) fatal_error( "virtual memory exhausted\n");
82 return res;
85 /* strdup wrapper */
86 static char *xstrdup( const char *str )
88 size_t len = strlen(str) + 1;
89 char *res = xmalloc( len );
90 memcpy( res, str, len );
91 return res;
94 /* remove all trailing slashes from a path name */
95 inline static void remove_trailing_slashes( char *path )
97 int len = strlen( path );
98 while (len > 1 && path[len-1] == '/') path[--len] = 0;
101 /* initialize all the paths values */
102 static void init_paths(void)
104 struct stat st;
105 char *p;
107 const char *home = getenv( "HOME" );
108 const char *user = NULL;
109 const char *prefix = getenv( "WINEPREFIX" );
111 #ifdef HAVE_GETPWUID
112 char uid_str[32];
113 struct passwd *pwd = getpwuid( getuid() );
115 if (pwd)
117 user = pwd->pw_name;
118 if (!home) home = pwd->pw_dir;
120 if (!user)
122 sprintf( uid_str, "%d", getuid() );
123 user = uid_str;
125 #else /* HAVE_GETPWUID */
126 if (!(user = getenv( "USER" )))
127 fatal_error( "cannot determine your user name, set the USER environment variable\n" );
128 #endif /* HAVE_GETPWUID */
129 user_name = xstrdup( user );
131 /* build config_dir */
133 if (prefix)
135 if (!(config_dir = strdup( prefix ))) fatal_error( "virtual memory exhausted\n");
136 remove_trailing_slashes( config_dir );
137 if (config_dir[0] != '/')
138 fatal_error( "invalid directory %s in WINEPREFIX: not an absolute path\n", prefix );
139 if (stat( config_dir, &st ) == -1)
140 fatal_perror( "cannot open %s as specified in WINEPREFIX", config_dir );
142 else
144 if (!home) fatal_error( "could not determine your home directory\n" );
145 if (home[0] != '/') fatal_error( "your home directory %s is not an absolute path\n", home );
146 config_dir = xmalloc( strlen(home) + strlen(server_config_dir) + 1 );
147 strcpy( config_dir, home );
148 remove_trailing_slashes( config_dir );
149 strcat( config_dir, server_config_dir );
150 if (stat( config_dir, &st ) == -1)
151 fatal_perror( "cannot open %s", config_dir );
153 if (!S_ISDIR(st.st_mode)) fatal_error( "%s is not a directory\n", config_dir );
155 /* build server_dir */
157 server_dir = xmalloc( strlen(server_root_prefix) + strlen(user) + strlen( server_dir_prefix ) +
158 2*sizeof(st.st_dev) + 2*sizeof(st.st_ino) + 2 );
159 strcpy( server_dir, server_root_prefix );
160 p = server_dir + strlen(server_dir);
161 strcpy( p, user );
162 while (*p)
164 if (*p == '/') *p = '!';
165 p++;
167 strcpy( p, server_dir_prefix );
169 if (sizeof(st.st_dev) > sizeof(unsigned long) && st.st_dev > ~0UL)
170 sprintf( server_dir + strlen(server_dir), "%lx%08lx-",
171 (unsigned long)(st.st_dev >> 32), (unsigned long)st.st_dev );
172 else
173 sprintf( server_dir + strlen(server_dir), "%lx-", (unsigned long)st.st_dev );
175 if (sizeof(st.st_ino) > sizeof(unsigned long) && st.st_ino > ~0UL)
176 sprintf( server_dir + strlen(server_dir), "%lx%08lx",
177 (unsigned long)(st.st_ino >> 32), (unsigned long)st.st_ino );
178 else
179 sprintf( server_dir + strlen(server_dir), "%lx", (unsigned long)st.st_ino );
182 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
183 const char *wine_get_config_dir(void)
185 if (!config_dir) init_paths();
186 return config_dir;
189 /* return the full name of the server directory (the one containing the socket) */
190 const char *wine_get_server_dir(void)
192 if (!server_dir) init_paths();
193 return server_dir;
196 /* return the current user name */
197 const char *wine_get_user_name(void)
199 if (!user_name) init_paths();
200 return user_name;