Only add variables to environment if they are already set

This commit is contained in:
MaxHearnden 2023-11-28 02:39:40 +00:00
parent c4e12161b7
commit 53d1c416be

View file

@ -225,18 +225,35 @@ int main(int argc, char **argv, char **envp) {
chroot ("."); chroot (".");
} }
free(cwd); free(cwd);
char **newenv = malloc(3 * sizeof(char *)); char **newenv = malloc(3 * sizeof(char *));
int newenv_index = 0;
require(newenv != NULL, "Failed to allocate space for new environment.");
char *ARCH = getenv("ARCH"); char *ARCH = getenv("ARCH");
newenv[0] = malloc(6 + strlen(ARCH)); if (ARCH != NULL) {
strcpy(newenv[0], "ARCH="); newenv[0] = malloc(6 + strlen(ARCH));
strcpy(newenv[0] + 5, ARCH); require(newenv[0] != NULL, "Failed to allocate space for new environment.");
strcpy(newenv[0], "ARCH=");
strcpy(newenv[0] + 5, ARCH);
newenv_index += 1;
}
char *ARCH_DIR = getenv("ARCH_DIR"); char *ARCH_DIR = getenv("ARCH_DIR");
newenv[1] = malloc(10 + strlen(ARCH_DIR)); if (ARCH_DIR != NULL) {
strcpy(newenv[1], "ARCH_DIR="); newenv[newenv_index] = malloc(10 + strlen(ARCH_DIR));
strcpy(newenv[1] + 9, ARCH_DIR); require(newenv[newenv_index] != NULL, "Failed to allocate space for new environment.");
newenv[2] = NULL; strcpy(newenv[newenv_index], "ARCH_DIR=");
strcpy(newenv[newenv_index] + 9, ARCH_DIR);
newenv_index += 1;
}
newenv[newenv_index] = NULL;
#ifdef __M2__ #ifdef __M2__
return execve (argv[1], argv + sizeof(char *) , newenv); return execve (argv[1], argv + sizeof(char *), newenv);
#else #else
return execve (argv[1], argv + 1, newenv); return execve (argv[1], argv + 1, newenv);
#endif #endif