2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "environment.h"
24 #include "stream/textstream.h"
25 #include "string/string.h"
26 #include "stream/stringstream.h"
27 #include "debugging/debugging.h"
35 void args_init(int argc, char* argv[])
39 for (i = 1; i < argc; i++)
41 for (k = i; k < argc; k++)
48 for (j = i + k; j < argc; j++)
60 CopiedString home_path;
61 CopiedString app_path;
64 const char* environment_get_home_path()
66 return home_path.c_str();
69 const char* environment_get_app_path()
71 return app_path.c_str();
75 #if defined (__linux__) || defined (__APPLE__)
81 #include <glib/gutils.h>
83 const char* LINK_NAME =
84 #if defined (__linux__)
91 /// brief Returns the filename of the executable belonging to the current process, or 0 if not found.
92 char* getexename(char *buf)
94 /* Now read the symbolic link */
95 int ret = readlink(LINK_NAME, buf, PATH_MAX);
99 globalOutputStream() << "getexename: falling back to argv[0]: " << makeQuoted(g_argv[0]);
100 const char* path = realpath(g_argv[0], buf);
103 /* In case of an error, leave the handling up to the caller */
108 /* Ensure proper NUL termination */
111 /* delete the program name */
112 *(strrchr(buf, '/')) = '\0';
114 // NOTE: we build app path with a trailing '/'
115 // it's a general convention in Radiant to have the slash at the end of directories
116 if (buf[strlen(buf)-1] != '/')
124 void environment_init(int argc, char* argv[])
126 // Give away unnecessary root privileges.
127 // Important: must be done before calling gtk_init().
131 if (geteuid() == 0 && (loginname = getlogin()) != 0 &&
132 (pw = getpwnam(loginname)) != 0)
135 args_init(argc, argv);
138 StringOutputStream home(256);
139 home << DirectoryCleaned(g_get_home_dir()) << ".radiant/";
140 Q_mkdir(home.c_str());
141 home_path = home.c_str();
145 app_path = getexename(real);
146 ASSERT_MESSAGE(!string_empty(app_path.c_str()), "failed to deduce app path");
155 #include <shfolder.h>
157 void environment_init(int argc, char* argv[])
159 args_init(argc, argv);
162 char appdata[MAX_PATH+1];
163 SHGetFolderPath(0, CSIDL_APPDATA, 0, 0, appdata);
165 StringOutputStream home(256);
166 if(string_empty(appdata))
168 ERROR_MESSAGE("Application Data folder not available.\n"
169 "Please install shfolder redistributable package.\n"
170 "Radiant will use C:\\ for user preferences.\n");
175 home << PathCleaned(appdata);
177 home << "/RadiantSettings/";
178 Q_mkdir(home.c_str());
179 home_path = home.c_str();
182 // get path to the editor
183 char filename[MAX_PATH+1];
184 GetModuleFileName(0, filename, MAX_PATH);
185 char* last_separator = strrchr(filename, '\\');
186 if(last_separator != 0)
188 *(last_separator+1) = '\0';
194 StringOutputStream app(256);
195 app << PathCleaned(filename);
196 app_path = app.c_str();