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 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
;
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)));
49 /* die on a fatal error */
50 static void fatal_error( const char *err
, ... )
54 va_start( args
, err
);
55 fprintf( stderr
, "wine: " );
56 vfprintf( stderr
, err
, args
);
61 /* die on a fatal error */
62 static void fatal_perror( const char *err
, ... )
66 va_start( args
, err
);
67 fprintf( stderr
, "wine: " );
68 vfprintf( stderr
, err
, args
);
75 static void *xmalloc( size_t size
)
80 if (!(res
= malloc( size
))) fatal_error( "virtual memory exhausted\n");
85 static char *xstrdup( const char *str
)
87 size_t len
= strlen(str
) + 1;
88 char *res
= xmalloc( len
);
89 memcpy( res
, str
, len
);
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)
106 const char *home
= getenv( "HOME" );
107 const char *user
= NULL
;
108 const char *prefix
= getenv( "WINEPREFIX" );
112 struct passwd
*pwd
= getpwuid( getuid() );
117 if (!home
) home
= pwd
->pw_dir
;
121 sprintf( uid_str
, "%d", getuid() );
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 */
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
);
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
);
163 if (*p
== '/') *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
);
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
);
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();
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();
195 /* return the current user name */
196 const char *wine_get_user_name(void)
198 if (!user_name
) init_paths();