dvitomp fix from Akira
[mplib] / src / texk / kpathsea / home.c
1 /* Utility and Unix shadow routines for XEmacs on Windows NT.
2    Copyright (C) 1994, 1995 Free Software Foundation, Inc.
3
4 This file is part of XEmacs.
5
6 XEmacs is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
9 later version.
10
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with XEmacs; see the file COPYING.  If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20
21
22    Geoff Voelker (voelker@cs.washington.edu) 7-29-94 */
23
24 /* Adapted for XEmacs by David Hobley <david@spook-le0.cia.com.au> */
25 /* Sync'ed with Emacs 19.34.6 by Marc Paquette <marcpa@cam.org> */
26 /* Adapted to fpTeX 0.4 by Fabrice Popineau <Fabrice.Popineau@supelec.fr> */
27
28 #ifdef __MINGW32__
29
30 #include <kpathsea/config.h>
31 #include <kpathsea/win32lib.h>
32 #include <shlobj.h>
33
34 char initial_directory[MAXPATHLEN];
35
36 static char *cached_home_directory;
37
38 void
39 uncache_home_directory (void)
40 {
41   cached_home_directory = NULL; /* in some cases, this may cause the leaking
42                                                                    of a few bytes */
43 }
44
45 int output_home_warning = 0;
46
47 /* Returns the home directory, in external format */
48 char *
49 get_home_directory (void)
50 {
51
52   if (cached_home_directory != NULL)
53         goto done;
54
55   if ((cached_home_directory = getenv("HOME")) != NULL) {
56         char q[MAXPATHLEN];
57         /* In case it is %HOMEDRIVE%%HOMEPATH% */
58         if (ExpandEnvironmentStrings(cached_home_directory, q, sizeof(q)) == 0) {
59           /* Error */
60           cached_home_directory = NULL;
61         }
62         else {
63           cached_home_directory = xstrdup(q);
64           goto done;
65         }
66   }
67   
68   {
69         char    *homedrive, *homepath;
70         if ((homedrive = getenv("HOMEDRIVE")) != NULL &&
71                 (homepath = getenv("HOMEPATH")) != NULL) {
72           cached_home_directory = concat(homedrive, homepath);
73           goto done;
74         }
75   }
76   
77   /* This method is the prefered one because even if it requires a more recent shell32.dll,
78      it does not need to call SHMalloc()->Free() */
79   {
80         /* This will probably give the wrong value */
81         char q [MAXPATHLEN];
82         HINSTANCE h;
83         HRESULT (WINAPI * p1)(HWND, LPSTR, int, BOOL);
84         HWND (WINAPI * p2)(void);
85         HWND hwnd = NULL;
86
87         if (h = LoadLibrary("user32.dll")) {
88           if (p2 = GetProcAddress(h, "GetDesktopWindow"))
89             hwnd = (*p2)();
90           FreeLibrary(h);
91         }
92         
93         if (hwnd && (h = LoadLibrary("shell32.dll")) != NULL) {
94           if (p1 = GetProcAddress(h, "SHGetSpecialFolderPathA"))
95             if ((*p1)(hwnd, q, CSIDL_PERSONAL, TRUE)) {
96               cached_home_directory = xstrdup(q);
97             }
98           FreeLibrary(h);
99         }
100         if (cached_home_directory) goto done;
101   }
102
103   if (output_home_warning) {
104         fprintf(stderr, "fpTeX has been unable to determine a good value for the user's $HOME\n"
105                         "       directory, and will be using the value:\n"
106                         "               %s\n"
107                         "       This is probably incorrect.\n",
108                         cached_home_directory
109                         );
110   }
111  done:
112   return cached_home_directory;
113 }
114
115 #endif