Skip to content
42 changes: 24 additions & 18 deletions src/runtime/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,27 @@ char* appimage_hexlify(const char* bytes, const size_t numBytes) {
return hexlified;
}

void set_portable_home_and_config(char* basepath) {
char portable_home_dir[PATH_MAX];
char portable_config_dir[PATH_MAX];

/* If there is a directory with the same name as the AppImage plus ".home", then export $HOME */
strcpy(portable_home_dir, basepath);
strcat(portable_home_dir, ".home");
if (is_writable_directory(portable_home_dir)) {
fprintf(stderr, "Setting $HOME to %s\n", portable_home_dir);
setenv("HOME", portable_home_dir, 1);
}

/* If there is a directory with the same name as the AppImage plus ".config", then export $XDG_CONFIG_HOME */
strcpy(portable_config_dir, basepath);
strcat(portable_config_dir, ".config");
if (is_writable_directory(portable_config_dir)) {
fprintf(stderr, "Setting $XDG_CONFIG_HOME to %s\n", portable_config_dir);
setenv("XDG_CONFIG_HOME", portable_config_dir, 1);
}
}

int main(int argc, char* argv[]) {
const bool verbose = (getenv("VERBOSE") != NULL);
if (verbose) {
Expand Down Expand Up @@ -1614,6 +1635,8 @@ int main(int argc, char* argv[]) {
setenv("ARGV0", argv0_path, 1);
setenv("APPDIR", prefix, 1);

set_portable_home_and_config(fullpath);

execv(apprun_path, new_argv);

int error = errno;
Expand Down Expand Up @@ -1803,24 +1826,7 @@ int main(int argc, char* argv[]) {
setenv("ARGV0", argv0_path, 1);
setenv("APPDIR", mount_dir, 1);

char portable_home_dir[PATH_MAX];
char portable_config_dir[PATH_MAX];

/* If there is a directory with the same name as the AppImage plus ".home", then export $HOME */
strcpy(portable_home_dir, fullpath);
strcat(portable_home_dir, ".home");
if (is_writable_directory(portable_home_dir)) {
fprintf(stderr, "Setting $HOME to %s\n", portable_home_dir);
setenv("HOME", portable_home_dir, 1);
}

/* If there is a directory with the same name as the AppImage plus ".config", then export $XDG_CONFIG_HOME */
strcpy(portable_config_dir, fullpath);
strcat(portable_config_dir, ".config");
if (is_writable_directory(portable_config_dir)) {
fprintf(stderr, "Setting $XDG_CONFIG_HOME to %s\n", portable_config_dir);
setenv("XDG_CONFIG_HOME", portable_config_dir, 1);
}
set_portable_home_and_config(fullpath);

/* Original working directory */
char cwd[1024];
Expand Down
Loading