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();
81 #include <glib/gutils.h>
83 const char* LINK_NAME =
84 #if defined (__linux__)
86 #else // FreeBSD and OSX
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()) << ".netradiant/";
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");
154 void environment_init(int argc, char* argv[])
156 args_init(argc, argv);
159 char *appdata = getenv("APPDATA");
161 StringOutputStream home(256);
162 if(!appdata || string_empty(appdata))
164 ERROR_MESSAGE("Application Data folder not available.\n"
165 "Radiant will use C:\\ for user preferences.\n");
170 home << PathCleaned(appdata);
172 home << "/NetRadiantSettings/";
173 Q_mkdir(home.c_str());
174 home_path = home.c_str();
177 // get path to the editor
178 char filename[MAX_PATH+1];
179 GetModuleFileName(0, filename, MAX_PATH);
180 char* last_separator = strrchr(filename, '\\');
181 if(last_separator != 0)
183 *(last_separator+1) = '\0';
189 StringOutputStream app(256);
190 app << PathCleaned(filename);
191 app_path = app.c_str();
196 #error "unsupported platform"