Skip to content

Commit

Permalink
Remove temporary files
Browse files Browse the repository at this point in the history
  • Loading branch information
tyfkda committed Mar 1, 2024
1 parent c6e3623 commit a5453e2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions libsrc/stdio/tmpfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ FILE *tmpfile(void) {
int fd = mkstemp(template);
FILE *fp = NULL;
if (fd >= 0) {
unlink(template);
fp = fdopen(fd, "w+");
if (fp == NULL) {
close(fd);
Expand Down
15 changes: 14 additions & 1 deletion src/wcc/wcc.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@

static const char DEFAULT_IMPORT_MODULE_NAME[] = "env";

static Vector remove_on_exit;

static void remove_tmp_files(void) {
for (int i = 0; i < remove_on_exit.len; ++i) {
const char *fn = remove_on_exit.data[i];
remove(fn);
}
}

static void init_compiler(void) {
table_init(&func_info_table);
table_init(&gvar_info_table);
Expand Down Expand Up @@ -92,7 +101,8 @@ static void preprocess_and_compile(FILE *ppout, const char *filename, Vector *to
exit(1);
}

int compile_csource(const char *src, enum OutType out_type, const char *ofn, Vector *ld_cmd, const char *import_module_name) {
int compile_csource(const char *src, enum OutType out_type, const char *ofn, Vector *ld_cmd,
const char *import_module_name) {
FILE *ppout = tmpfile();
if (ppout == NULL)
error("cannot open temporary file");
Expand Down Expand Up @@ -142,6 +152,7 @@ int compile_csource(const char *src, enum OutType out_type, const char *ofn, Vec
char *tmpfn = strdup(template);
ofp = fdopen(obj_fd, "wb");
outfn = tmpfn;
vec_push(&remove_on_exit, tmpfn);
} else {
outfn = ofn;
if (outfn == NULL) {
Expand Down Expand Up @@ -386,6 +397,8 @@ int main(int argc, char *argv[]) {
#endif
}

atexit(remove_tmp_files);

for (int i = 0; i < sources->len; ++i) {
char *src = sources->data[i];
const char *outfn = ofn;
Expand Down
12 changes: 12 additions & 0 deletions src/xcc/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ static pid_t pipe_exec(char **command, int ofd, int fd[2]) {
return pid;
}

static Vector remove_on_exit;

static void remove_tmp_files(void) {
for (int i = 0; i < remove_on_exit.len; ++i) {
const char *fn = remove_on_exit.data[i];
remove(fn);
}
}

static int compile(const char *src, Vector *cpp_cmd, Vector *cc1_cmd, int ofd) {
int ofd2 = ofd;
int cc_fd[2];
Expand Down Expand Up @@ -164,6 +173,7 @@ static int compile_csource(const char *source_fn, enum OutType out_type, const c
exit(1);
}
objfn = strdup(template);
vec_push(&remove_on_exit, objfn);
}
}

Expand Down Expand Up @@ -540,6 +550,8 @@ int main(int argc, char *argv[]) {
#endif
}

atexit(remove_tmp_files);

int res = 0;
for (int i = 0; i < sources->len; ++i) {
char *src = sources->data[i];
Expand Down

0 comments on commit a5453e2

Please sign in to comment.