scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 2cc10bb10f465b64b0dfcbf34a17d0c55a099b2b
parent 34a73222efca993ba7e86a283640b8b51bfa893c
Author: Quentin Rameau <quinq@fifth.space>
Date:   Thu,  2 Jun 2016 17:45:27 +0200

[driver] remove temporary files for failed tools

Keep the files that has been correctly generated, but remove those from
the failed tool and subsequente ones.

Diffstat:
Mdriver/posix/scc.c | 34+++++++++++++++++++++++++++-------
1 file changed, 27 insertions(+), 7 deletions(-)

diff --git a/driver/posix/scc.c b/driver/posix/scc.c @@ -30,7 +30,6 @@ static struct tool { char cmd[PATH_MAX]; char *args[NARGS]; char bin[16]; - char *outfile; int nargs, in, out; pid_t pid; } tools[NR_TOOLS] = { @@ -43,8 +42,22 @@ static struct tool { char *argv0; static char *arch; +static char *outfiles[NR_TOOLS]; +static int failedtool = NR_TOOLS; static int Eflag, Sflag, kflag; +void +cleanup(void) +{ + int i; + + for (i = 0; i < NR_TOOLS; ++i) { + if (i > failedtool && outfiles[i]) + unlink(outfiles[i]); + free(outfiles[i]); + } +} + static void terminate(void) { @@ -54,6 +67,8 @@ terminate(void) if (tools[i].pid) kill(tools[i].pid, SIGTERM); } + + cleanup(); } int @@ -137,8 +152,8 @@ settool(int tool, char *input, int output) case AS: ext = "as"; break; } - t->outfile = newfileext(input, ext); - t->args[1] = t->outfile; + outfiles[output] = newfileext(input, ext); + t->args[1] = outfiles[output]; break; default: break; @@ -230,18 +245,23 @@ build(char *file) } spawn(settool(inittool(tool), file, out)); - - free(tools[tool].outfile); } + for (i = 0; i < NR_TOOLS; ++i) { if ((pid = tools[i].pid) == 0) continue; - if (waitpid(pid, &st, 0) < 0) + if (waitpid(pid, &st, 0) < 0) { + failedtool = i; exit(-1); + } tools[i].pid = 0; - if (!WIFEXITED(st) || WEXITSTATUS(st) != 0) + if (!WIFEXITED(st) || WEXITSTATUS(st) != 0) { + failedtool = i; exit(-1); + } } + + cleanup(); } static void