Implement simple ExtTextOut for enhmfdrv.
[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 <errno.h>
25 #include <stdio.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/stat.h>
30 #include <unistd.h>
31
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
43 #ifdef __GNUC__
44 static void fatal_error( const char *err, ... )  __attribute__((noreturn,format(printf,1,2)));
45 static void fatal_perror( const char *err, ... )  __attribute__((noreturn,format(printf,1,2)));
46 #endif
47
48 /* die on a fatal error */
49 static void fatal_error( const char *err, ... )
50 {
51     va_list args;
52
53     va_start( args, err );
54     fprintf( stderr, "wine: " );
55     vfprintf( stderr, err, args );
56     va_end( args );
57     exit(1);
58 }
59
60 /* die on a fatal error */
61 static void fatal_perror( const char *err, ... )
62 {
63     va_list args;
64
65     va_start( args, err );
66     fprintf( stderr, "wine: " );
67     vfprintf( stderr, err, args );
68     perror( " " );
69     va_end( args );
70     exit(1);
71 }
72
73 /* malloc wrapper */
74 static void *xmalloc( size_t size )
75 {
76     void *res;
77
78     if (!size) size = 1;
79     if (!(res = malloc( size ))) fatal_error( "virtual memory exhausted\n");
80     return res;
81 }
82
83 /* remove all trailing slashes from a path name */
84 inline static void remove_trailing_slashes( char *path )
85 {
86     int len = strlen( path );
87     while (len > 1 && path[len-1] == '/') path[--len] = 0;
88 }
89
90 /* initialize all the paths values */
91 static void init_paths(void)
92 {
93     struct stat st;
94     char *p;
95
96     const char *home = getenv( "HOME" );
97     const char *user = NULL;
98     const char *prefix = getenv( "WINEPREFIX" );
99
100 #ifdef HAVE_GETPWUID
101     char uid_str[32];
102     struct passwd *pwd = getpwuid( getuid() );
103
104     if (pwd)
105     {
106         user = pwd->pw_name;
107         if (!home) home = pwd->pw_dir;
108     }
109     if (!user)
110     {
111         sprintf( uid_str, "%d", getuid() );
112         user = uid_str;
113     }
114 #else  /* HAVE_GETPWUID */
115     if (!(user = getenv( "USER" )))
116         fatal_error( "cannot determine your user name, set the USER environment variable\n" );
117 #endif  /* HAVE_GETPWUID */
118
119     /* build config_dir */
120
121     if (prefix)
122     {
123         if (!(config_dir = strdup( prefix ))) fatal_error( "virtual memory exhausted\n");
124         remove_trailing_slashes( config_dir );
125         if (config_dir[0] != '/')
126             fatal_error( "invalid directory %s in WINEPREFIX: not an absolute path\n", prefix );
127         if (stat( config_dir, &st ) == -1)
128             fatal_perror( "cannot open %s as specified in WINEPREFIX", config_dir );
129     }
130     else
131     {
132         if (!home) fatal_error( "could not determine your home directory\n" );
133         if (home[0] != '/') fatal_error( "your home directory %s is not an absolute path\n", home );
134         config_dir = xmalloc( strlen(home) + strlen(server_config_dir) + 1 );
135         strcpy( config_dir, home );
136         remove_trailing_slashes( config_dir );
137         strcat( config_dir, server_config_dir );
138         if (stat( config_dir, &st ) == -1)
139             fatal_perror( "cannot open %s", config_dir );
140     }
141     if (!S_ISDIR(st.st_mode)) fatal_error( "%s is not a directory\n", config_dir );
142
143     /* build server_dir */
144
145     server_dir = xmalloc( strlen(server_root_prefix) + strlen(user) + strlen( server_dir_prefix ) +
146                           2*sizeof(st.st_dev) + 2*sizeof(st.st_ino) + 2 );
147     strcpy( server_dir, server_root_prefix );
148     p = server_dir + strlen(server_dir);
149     strcpy( p, user );
150     while (*p)
151     {
152         if (*p == '/') *p = '!';
153         p++;
154     }
155     strcpy( p, server_dir_prefix );
156
157     if (sizeof(st.st_dev) > sizeof(unsigned long))
158         sprintf( server_dir + strlen(server_dir), "%llx-", (unsigned long long)st.st_dev );
159     else
160         sprintf( server_dir + strlen(server_dir), "%lx-", (unsigned long)st.st_dev );
161
162     if (sizeof(st.st_ino) > sizeof(unsigned long))
163         sprintf( server_dir + strlen(server_dir), "%llx", (unsigned long long)st.st_ino );
164     else
165         sprintf( server_dir + strlen(server_dir), "%lx", (unsigned long)st.st_ino );
166 }
167
168 /* return the configuration directory ($WINEPREFIX or $HOME/.wine) */
169 const char *wine_get_config_dir(void)
170 {
171     if (!config_dir) init_paths();
172     return config_dir;
173 }
174
175 /* return the full name of the server directory (the one containing the socket) */
176 const char *wine_get_server_dir(void)
177 {
178     if (!server_dir) init_paths();
179     return server_dir;
180 }