Enable more temp directory flexiblity

This commit is contained in:
Jeremiah Orians 2022-03-21 12:19:42 -04:00
parent 03bd6bda76
commit 9895dab8dd
No known key found for this signature in database
GPG Key ID: 6B3A3F198708F894
6 changed files with 51 additions and 20 deletions

52
cc.c
View File

@ -18,6 +18,7 @@
*/ */
#include"cc.h" #include"cc.h"
#include <unistd.h>
/* The core functions */ /* The core functions */
void populate_env(char** envp); void populate_env(char** envp);
@ -31,7 +32,7 @@ void init_macro_env(char* sym, char* value, char* source, int num);
void preprocess(); void preprocess();
void output_tokens(struct token_list *i, FILE* out); void output_tokens(struct token_list *i, FILE* out);
int strtoint(char *a); int strtoint(char *a);
void spawn_processes(int debug_flag, char* preprocessed_file, char* destination, char** envp); void spawn_processes(int debug_flag, char* prefix, char* preprocessed_file, char* destination, char** envp);
void prechecks(int argc, char** argv) void prechecks(int argc, char** argv)
{ {
@ -82,7 +83,7 @@ void prechecks(int argc, char** argv)
if(1 <= DEBUG_LEVEL) if(1 <= DEBUG_LEVEL)
{ {
fputs("M2LIBC_PATH set by -I to ", stderr); fputs("M2LIBC_PATH set by -I to ", stderr);
fputs(M2LIBC_PATH, stderr); fputs(hold, stderr);
fputc('\n', stderr); fputc('\n', stderr);
} }
M2LIBC_PATH = hold; M2LIBC_PATH = hold;
@ -140,6 +141,15 @@ int main(int argc, char** argv, char** envp)
fputc('\n', stderr); fputc('\n', stderr);
} }
TEMPDIR = env_lookup("TMPDIR");
if(NULL == TEMPDIR) TEMPDIR = "/tmp";
else if(1 <= DEBUG_LEVEL)
{
fputs("TEMPDIR set by environment variable to ", stderr);
fputs(TEMPDIR, stderr);
fputc('\n', stderr);
}
int i = 1; int i = 1;
while(i <= argc) while(i <= argc)
{ {
@ -245,6 +255,23 @@ int main(int argc, char** argv, char** envp)
debug_flag = FALSE; debug_flag = FALSE;
i += 1; i += 1;
} }
else if(match(argv[i], "--temp-directory"))
{
name = argv[i+1];
if(NULL == name)
{
fputs("--temp-directory requires a PATH\n", stderr);
exit(EXIT_FAILURE);
}
if(1 <= DEBUG_LEVEL)
{
fputs("TEMPDIR set by --temp-directory to ", stderr);
fputs(name, stderr);
fputc('\n', stderr);
}
TEMPDIR = name;
i += 2;
}
else else
{ {
fputs("UNKNOWN ARGUMENT\n", stdout); fputs("UNKNOWN ARGUMENT\n", stdout);
@ -288,18 +315,19 @@ int main(int argc, char** argv, char** envp)
} }
else else
{ {
char* prefix = calloc(10, sizeof(char)); /* Ensure we can write to the temp directory */
if(0 == access("/tmp", 0)) int permissions = access(TEMPDIR, 0);
if(0 != permissions)
{ {
strcpy(prefix, "/tmp/"); fputs("unable to access: ", stderr);
} fputs(TEMPDIR, stderr);
else fputs(" for use as a temp directory\nPlease use --temp-directory to set a directory you can use or set the TMPDIR variable\n", stderr);
{ exit(EXIT_FAILURE);
strcpy(prefix, "tmp-");
} }
name = calloc(100, sizeof(char)); name = calloc(100, sizeof(char));
strcpy(name, prefix); strcpy(name, TEMPDIR);
strcat(name, "M2-Mesoplanet-XXXXXX"); strcat(name, "/M2-Mesoplanet-XXXXXX");
i = mkstemp(name); i = mkstemp(name);
tempfile = fdopen(i, "w"); tempfile = fdopen(i, "w");
if(NULL != tempfile) if(NULL != tempfile)
@ -309,7 +337,7 @@ int main(int argc, char** argv, char** envp)
fclose(tempfile); fclose(tempfile);
/* Make me a real binary */ /* Make me a real binary */
spawn_processes(debug_flag, prefix, name, destination_name, envp); spawn_processes(debug_flag, TEMPDIR, name, destination_name, envp);
/* And clean up the donkey */ /* And clean up the donkey */
if(!DIRTY_MODE) remove(name); if(!DIRTY_MODE) remove(name);

View File

@ -321,7 +321,7 @@ void populate_env(char** envp)
require(4096 > strlen(envp[i]), "envp line exceeds 4096byte limit\n"); require(4096 > strlen(envp[i]), "envp line exceeds 4096byte limit\n");
strcpy(envp_line, envp[i]); strcpy(envp_line, envp[i]);
if(4 <= DEBUG_LEVEL) if(9 <= DEBUG_LEVEL)
{ {
fputs("trying envp_line: ", stderr); fputs("trying envp_line: ", stderr);
fputs(envp_line, stderr); fputs(envp_line, stderr);
@ -330,7 +330,7 @@ void populate_env(char** envp)
env = process_env_variable(envp_line, env); env = process_env_variable(envp_line, env);
if(8 <= DEBUG_LEVEL) if(9 <= DEBUG_LEVEL)
{ {
fputs("got var of: ", stderr); fputs("got var of: ", stderr);
fputs(env->var, stderr); fputs(env->var, stderr);

View File

@ -44,6 +44,7 @@ int WORDSIZE;
int ENDIAN; int ENDIAN;
char* BASEADDRESS; char* BASEADDRESS;
int STDIO_USED; int STDIO_USED;
char* TEMPDIR;
/* So we don't shoot ourself in the face */ /* So we don't shoot ourself in the face */
int FUZZING; int FUZZING;

View File

@ -45,6 +45,7 @@ extern int WORDSIZE;
extern int ENDIAN; extern int ENDIAN;
extern char* BASEADDRESS; extern char* BASEADDRESS;
extern int STDIO_USED; extern int STDIO_USED;
extern char* TEMPDIR;
/* So we don't shoot ourself in the face */ /* So we don't shoot ourself in the face */
extern int FUZZING; extern int FUZZING;

View File

@ -264,7 +264,6 @@ int get_token(int c)
c = consume_byte(c); c = consume_byte(c);
} }
new_token(hold_string, string_index + 2);
return c; return c;
} }
@ -313,8 +312,6 @@ int read_include(int c)
} }
} }
/* with just a little extra to put in the matching at the end */
new_token(hold_string, string_index + 3);
return c; return c;
} }
@ -348,6 +345,9 @@ int include_file(int ch)
/* Get new filename */ /* Get new filename */
read_include(ch); read_include(ch);
/* with just a little extra to put in the matching at the end */
new_token(hold_string, string_index + 3);
ch = '\n'; ch = '\n';
new_filename = token->s; new_filename = token->s;
/* Remove name from stream */ /* Remove name from stream */
@ -425,6 +425,7 @@ struct token_list* read_all_tokens(FILE* a, struct token_list* current, char* fi
while(EOF != ch) while(EOF != ch)
{ {
ch = get_token(ch); ch = get_token(ch);
new_token(hold_string, string_index + 2);
if(match("#include", token->s)) ch = include_file(ch); if(match("#include", token->s)) ch = include_file(ch);
} }

View File

@ -421,7 +421,7 @@ void spawn_processes(int debug_flag, char* prefix, char* preprocessed_file, char
char* M2_output = calloc(100, sizeof(char)); char* M2_output = calloc(100, sizeof(char));
strcpy(M2_output, prefix); strcpy(M2_output, prefix);
strcat(M2_output, "M2-Planet-XXXXXX"); strcat(M2_output, "/M2-Planet-XXXXXX");
int i = mkstemp(M2_output); int i = mkstemp(M2_output);
if(-1 != i) if(-1 != i)
{ {
@ -439,7 +439,7 @@ void spawn_processes(int debug_flag, char* prefix, char* preprocessed_file, char
{ {
blood_output = calloc(100, sizeof(char)); blood_output = calloc(100, sizeof(char));
strcpy(blood_output, prefix); strcpy(blood_output, prefix);
strcat(blood_output, "blood-elf-XXXXXX"); strcat(blood_output, "/blood-elf-XXXXXX");
i = mkstemp(blood_output); i = mkstemp(blood_output);
if(-1 != i) if(-1 != i)
{ {
@ -455,7 +455,7 @@ void spawn_processes(int debug_flag, char* prefix, char* preprocessed_file, char
char* M1_output = calloc(100, sizeof(char)); char* M1_output = calloc(100, sizeof(char));
strcpy(M1_output, prefix); strcpy(M1_output, prefix);
strcat(M1_output, "M1-macro-XXXXXX"); strcat(M1_output, "/M1-macro-XXXXXX");
i = mkstemp(M1_output); i = mkstemp(M1_output);
if(-1 != i) if(-1 != i)
{ {