-void environment_init(int argc, char const *argv[])
-{
- // Give away unnecessary root privileges.
- // Important: must be done before calling gtk_init().
- char *loginname;
- struct passwd *pw;
- seteuid(getuid());
- if (geteuid() == 0 && (loginname = getlogin()) != 0 &&
- (pw = getpwnam(loginname)) != 0) {
- setuid(pw->pw_uid);
- }
-
- args_init(argc, argv);
-
- {
- char real[PATH_MAX];
- app_path = getexename(real);
- ASSERT_MESSAGE(!string_empty(app_path.c_str()), "failed to deduce app path");
- }
-
- if (!portable_app_setup()) {
- StringOutputStream home(256);
- home << DirectoryCleaned(g_get_user_config_dir()) << "netradiant/";
- Q_mkdir(home.c_str());
- home_path = home.c_str();
- }
- gamedetect();
+char const* getexepath( char *buf ) {
+ /* delete the program name */
+ *( strrchr( buf, '/' ) ) = '\0';
+
+ // NOTE: we build app path with a trailing '/'
+ // it's a general convention in Radiant to have the slash at the end of directories
+ if ( buf[strlen( buf ) - 1] != '/' ) {
+ strcat( buf, "/" );
+ }
+
+ return buf;
+}
+
+void environment_init( int argc, char const* argv[] ){
+ // Give away unnecessary root privileges.
+ // Important: must be done before calling gtk_init().
+ char *loginname;
+ struct passwd *pw;
+ seteuid( getuid() );
+ if ( geteuid() == 0 && ( loginname = getlogin() ) != 0 &&
+ ( pw = getpwnam( loginname ) ) != 0 ) {
+ setuid( pw->pw_uid );
+ }
+
+ args_init( argc, argv );
+
+ {
+ char real[PATH_MAX];
+ app_filepath = getexename( real );
+ ASSERT_MESSAGE( !string_empty( app_filepath.c_str() ), "failed to deduce app path" );
+
+ strncpy( real, app_filepath.c_str(), strlen( app_filepath.c_str() ) );
+ app_path = getexepath( real );
+ }
+
+ {
+ StringOutputStream buffer;
+ buffer << app_path.c_str() << "../lib/" << RADIANT_BASENAME << "/";
+ if ( file_is_directory( buffer.c_str() ) ) {
+ lib_path = buffer.c_str();
+ }
+ else {
+ lib_path = app_path.c_str();
+ }
+ }
+
+ {
+ StringOutputStream buffer;
+ buffer << app_path.c_str() << "../share/" << RADIANT_BASENAME << "/";
+ if ( file_is_directory( buffer.c_str() ) ) {
+ data_path = buffer.c_str();
+ }
+ else {
+ data_path = app_path.c_str();
+ }
+ }
+
+ if ( !portable_app_setup() ) {
+ // this is used on both Linux and macOS
+ // but a macOS specific code may be written instead
+ StringOutputStream home( 256 );
+ home << DirectoryCleaned(g_get_user_config_dir()) << "/" << RADIANT_BASENAME << "/";
+ // first create ~/.config
+ // since it may be missing on brand new home directory
+ Q_mkdir( g_get_user_config_dir() );
+ // then create ~/.config/netradiant
+ Q_mkdir( home.c_str() );
+ home_path = home.c_str();
+ }
+ gamedetect();