3 Copyright (C) 2002 Free Software Foundation, Inc.
5 Author: Richard Frith-Macdonald <richard@brainstorm.co.uk>
8 This file is part of the GNUstep Makefile Package.
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License
12 as published by the Free Software Foundation; either version 2
13 of the License, or (at your option) any later version.
15 You should have received a copy of the GNU General Public
16 License along with this library; see the file COPYING.LIB.
17 If not, write to the Free Software Foundation,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
34 #if defined(__MINGW__)
39 # include <sys/types.h>
58 #define lowlevelstringify(X) #X
59 #define stringify(X) lowlevelstringify(X)
64 * This tool is intended to produce a definitive form of the
65 * user specific root directories for a GNUstep user. It must
66 * remain consistent with the code in the GNUstep base library
67 * which provides path information for all GNUstep applications.
70 * How to run this tool ...
72 * 1. With no arguments ... the tool should print the home directory of
73 * the current user to stdout.
75 * 2. With a 'user' argument ... the tool should print the
76 * GNUSTEP_USER_ROOT directory to stdout.
78 * 3. With a 'defaults' argument ... the tool should print the
79 * GNUSTEP_DEFAULTS_ROOT directory to stdout.
81 * Any other arguments will be ignored.
82 * On success the tool will terminate with an exit status of zero
83 * On failure, the tool will terminate with an exit status of one
84 * and will print an error message to stderr.
87 /* NOTE FOR DEVELOPERS.
88 * If you change the behavior of this method you must also change
89 * NSUser.m in the base library package to match.
91 int main (int argc, char** argv)
97 enum { NONE, DEFS, USER } type = NONE;
98 #if defined(__MINGW__)
108 if (strcmp(argv[1], "defaults") == 0)
112 else if (strcmp(argv[1], "user") == 0)
120 #if defined(__WIN32__)
121 /* The GetUserName function returns the current user name */
124 len0 = GetEnvironmentVariable("LOGNAME", buf0, 1024);
125 if (len0 > 0 && len0 < 1024)
128 loginName[len0] = '\0';
130 else if (GetUserName(buf0, &n))
140 #endif /* HAVE_GETEUID */
141 struct passwd *pwent = getpwuid (uid);
142 loginName = pwent->pw_name;
143 #endif /* HAVE_GETPWUID */
147 fprintf(stderr, "Unable to determine current user name.\n");
152 #if !defined(__MINGW__)
153 pw = getpwnam (loginName);
156 fprintf(stderr, "Unable to locate home directory for '%s'\n", loginName);
159 strncpy(home, pw->pw_dir, sizeof(home));
163 * The environment variable HOMEPATH holds the home directory
164 * for the user on Windows NT; Win95 has no concept of home.
165 * For OPENSTEP compatibility (and because USERPROFILE is usually
166 * unusable because it contains spaces), we use HOMEPATH in
167 * preference to USERPROFILE.
169 len0 = GetEnvironmentVariable("HOMEPATH", buf0, 1024);
170 if (len0 > 0 && len0 < 1024)
174 * Only use HOMEDRIVE is HOMEPATH does not already contain drive.
176 if (len0 < 2 || buf0[1] != ':')
178 len1 = GetEnvironmentVariable("HOMEDRIVE", buf1, 128);
179 if (len1 > 0 && len1 < 128)
182 sprintf(home, "%s%s", buf1, buf0);
186 sprintf(home, "C:%s", buf0);
196 /* The environment variable USERPROFILE may hold the home directory
197 for the user on modern versions of windoze. */
198 len0 = GetEnvironmentVariable("USERPROFILE", buf0, 1024);
199 if (len0 > 0 && len0 < 1024)
209 for (i = 0; i < strlen(home); i++)
211 if (isspace((unsigned int)home[i]))
214 * GNU make doesn't handle spaces in paths.
215 * Broken, wrong and totally unfixable.
217 fprintf(stderr, "Make cannot handle spaces in paths so the " \
218 "home directory '%s' may cause problems!\n", home);
237 #if defined (__MINGW32__)
238 len0 = GetEnvironmentVariable("GNUSTEP_SYSTEM_ROOT", buf0, sizeof(buf0));
245 const char *gnustep_system_root = (const char*)getenv("GNUSTEP_SYSTEM_ROOT");
247 if (gnustep_system_root != 0)
249 strcpy(path, gnustep_system_root);
253 /* On my machine the strcpy was segfaulting when
254 * gnustep_system_root == 0. */
260 strcat(path, ".GNUsteprc");
261 fptr = fopen(path, "r");
264 while (fgets(buf0, sizeof(buf0), fptr) != 0)
266 char *pos = strchr(buf0, '=');
274 while (isspace((int)*key))
276 while (strlen(key) > 0 && isspace((int)key[strlen(key)-1]))
277 key[strlen(key)-1] = '\0';
278 while (isspace(*val))
280 while (strlen(val) > 0 && isspace((int)val[strlen(val)-1]))
281 val[strlen(val)-1] = '\0';
285 while (isspace((int)*key))
287 while (strlen(key) > 0 && isspace((int)key[strlen(key)-1]))
288 key[strlen(key)-1] = '\0';
292 if (strcmp(key, "GNUSTEP_USER_ROOT") == 0)
296 user = malloc(strlen(val) + strlen(home));
298 strcat(user, &val[1]);
302 user = malloc(strlen(val) + 1);
306 else if (strcmp(key, "GNUSTEP_DEFAULTS_ROOT") == 0)
310 defs = malloc(strlen(val) + strlen(home));
312 strcat(defs, &val[1]);
316 defs = malloc(strlen(val) + 1);
320 else if (strcmp(key, "FORCE_USER_ROOT") == 0)
324 else if (strcmp(key, "FORCE_DEFAULTS_ROOT") == 0)
332 if (*user == '\0' || forceU == 0 || *defs == '\0' || forceD == 0)
336 strcat(path, ".GNUsteprc");
337 fptr = fopen(path, "r");
340 while (fgets(buf0, sizeof(buf0), fptr) != 0)
342 char *pos = strchr(buf0, '=');
350 while (isspace((int)*key))
352 while (strlen(key) > 0
353 && isspace((int)key[strlen(key)-1]))
354 key[strlen(key)-1] = '\0';
355 while (isspace((int)*val))
357 while (strlen(val) > 0
358 && isspace((int)val[strlen(val)-1]))
359 val[strlen(val)-1] = '\0';
361 if (strcmp(key, "GNUSTEP_USER_ROOT") == 0)
363 if (*user == '\0' || forceU == 0)
367 user = malloc(strlen(val) + strlen(home));
369 strcat(user, &val[1]);
373 user = malloc(strlen(val) + 1);
378 else if (strcmp(key, "GNUSTEP_DEFAULTS_ROOT") == 0)
380 if (*defs == '\0' || forceD == 0)
384 defs = malloc(strlen(val) + strlen(home));
386 strcat(defs, &val[1]);
390 defs = malloc(strlen(val) + 1);
418 strcat(path, "GNUstep");
421 #if defined(__MINGW__)
423 * We always want to use unix style paths.
425 if (strlen(path) > 1 && path[1] == ':')
435 if (*ptr == '/' && ptr > path && ptr[-1] == '/')
437 memmove(ptr, &ptr[1], strlen(ptr)+1);
444 if (path[2] == '/' || path[2] == '\0')
450 memmove(&path[1], path, strlen(path)+1);