Remove errno.h from files that do not need it.
[wine] / library / config.c
1 /*
2  * Configuration parameters shared between Wine server and clients
3  *
4  * Copyright 2002 Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
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
35
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 */
39
40 static char *config_dir;
41 static char *server_dir;
42 static char *user_name;
43
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
48
49 /* die on a fatal error */
50 static void fatal_error( const char *err, ... )
51 {
52     va_list args;
53
54     va_start( args, err );
55     fprintf( stderr, "wine: " );
56     vfprintf( stderr, err, args );
57     va_end( args );
58     exit(1);
59 }
60
61 /* die on a fatal error */
62 static void fatal_perror( const char *err, ... )
63 {
64     va_list args;
65
66     va_start( args, err );
67     fprintf( stderr, "wine: " );
68     vfprintf( stderr, err, args );
69     perror( " " );
70     va_end( args );
71     exit(1);
72 }
73
74 /* malloc wrapper */
75 static void *xmalloc( size_t size )
76 {
77     void *res;
78
79     if (!size) size = 1;
80     if (!(res = malloc( size ))) fatal_error( "virtual memory exhausted\n");
81     return res;
82 }
83
84 /* strdup wrapper */
85 static char *xstrdup( const char *str )
86 {
87     size_t len = strlen(str) + 1;
88     char *res = xmalloc( len );
89     memcpy( res, str, len );
90     return res;
91 }
92
93 /* remove all trailing slashes from a path name */
94 inline static void remove_trailing_slashes( char *path )
95 {
96     int len = strlen( path );
97     while (len > 1 && path[len-1] == '/') path[--len] = 0;
98 }
99
100 /* initialize all the paths values */
101 static void init_paths(void)
102 {
103     struct stat st;
104     char *p;
105
106     const char *home = getenv( "HOME" );
107     const char *user = NULL;
108     const char *prefix = getenv( "WINEPREFIX" );
109
110 #ifdef HAVE_GETPWUID
111     char uid_str[32];
112     struct passwd *pwd = getpwuid( getuid() );
113
114     if (pwd)
115     {
116         user = pwd->pw_name;
117         if (!home) home = pwd->pw_dir;
118     }
119     if (!user)
120     {
121         sprintf( uid_str, "%d", getuid() );
122         user = uid_str;
123     }
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 );
129
130     /* build config_dir */
131
132     if (prefix)
133     {
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 );
140     }
141     else
142     {
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 );
151     }
152     if (!S_ISDIR(st.st_mode)) fatal_error( "%s is not a directory\n", config_dir );
153
154     /* build server_dir */
155
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)
162     {
163         if (*p == '/') *p = '!';
164         p++;
165     }
166     strcpy( p, server_dir_prefix );
167
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 );
173
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 );
179 }
180
181 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
182 const char *wine_get_config_dir(void)
183 {
184     if (!config_dir) init_paths();
185     return config_dir;
186 }
187
188 /* return the full name of the server directory (the one containing the socket) */
189 const char *wine_get_server_dir(void)
190 {
191     if (!server_dir) init_paths();
192     return server_dir;
193 }
194
195 /* return the current user name */
196 const char *wine_get_user_name(void)
197 {
198     if (!user_name) init_paths();
199     return user_name;
200 }