diff options
author | gshapiro <gshapiro@FreeBSD.org> | 2001-04-16 18:36:35 +0000 |
---|---|---|
committer | gshapiro <gshapiro@FreeBSD.org> | 2001-04-16 18:36:35 +0000 |
commit | 3f95f71144508430572f3e29a30b73b4d887388c (patch) | |
tree | 84cc26d755c60541e3d22d463b08b6eef725cbef | |
parent | 72bbb6cb402b781ec4f03b4e3369a2f13ad6a242 (diff) | |
download | FreeBSD-src-3f95f71144508430572f3e29a30b73b4d887388c.zip FreeBSD-src-3f95f71144508430572f3e29a30b73b4d887388c.tar.gz |
Clean up temporary file(s) and directory when m4 exits without falling
through main() (e.g., signals or calls to errx()).
PR: conf/25715
-rw-r--r-- | usr.bin/m4/extern.h | 3 | ||||
-rw-r--r-- | usr.bin/m4/main.c | 9 | ||||
-rw-r--r-- | usr.bin/m4/misc.c | 13 |
3 files changed, 18 insertions, 7 deletions
diff --git a/usr.bin/m4/extern.h b/usr.bin/m4/extern.h index 25fc741..e791ee2 100644 --- a/usr.bin/m4/extern.h +++ b/usr.bin/m4/extern.h @@ -34,6 +34,7 @@ * SUCH DAMAGE. * * @(#)extern.h 8.1 (Berkeley) 6/6/93 + * $FreeBSD$ */ char *basename __P((char *)); @@ -41,6 +42,7 @@ char *xalloc __P((unsigned long)); int expr __P((char *)); ndptr addent __P((char *)); void chrsave __P((int)); +void cleanup __P((int)); void dochc __P((char *[], int)); void dochq __P((char *[], int)); void dodefine __P((char *, char *)); @@ -87,6 +89,7 @@ extern char *endest; /* end of string space */ extern unsigned char *endpbb; /* end of push-back buffer */ extern char *ep; /* first free char in strspace */ extern char lquote; /* left quote character (`) */ +extern char *m4dir; /* directory for temporary files */ extern char *m4temp; /* filename for diversions */ extern char *m4wraps; /* m4wrap string default. */ extern char *null; /* as it says.. just a null. */ diff --git a/usr.bin/m4/main.c b/usr.bin/m4/main.c index 6b77df8..86253eb 100644 --- a/usr.bin/m4/main.c +++ b/usr.bin/m4/main.c @@ -183,6 +183,7 @@ main(argc,argv) active = stdout; /* default active output */ /* filename for diversions */ m4dir = mkdtemp(xstrdup(_PATH_DIVDIRNAME)); + err_set_exit(cleanup); (void) asprintf(&m4temp, "%s/%s", m4dir, _PATH_DIVNAME); bbase[0] = bufbase; @@ -220,13 +221,7 @@ main(argc,argv) if (outfile[n] != NULL) getdiv(n); /* remove bitbucket if used */ - if (outfile[0] != NULL) { - (void) fclose(outfile[0]); - m4temp[UNIQUE] = '0'; - (void) remove(m4temp); - } - (void) remove(m4dir); - + cleanup(0); return 0; } diff --git a/usr.bin/m4/misc.c b/usr.bin/m4/misc.c index f1c5101..3c64eb1 100644 --- a/usr.bin/m4/misc.c +++ b/usr.bin/m4/misc.c @@ -237,6 +237,19 @@ register char *s; } void +cleanup(n) +int n; +{ + if (outfile[0] != NULL) { + (void) fclose(outfile[0]); + outfile[0] = NULL; + m4temp[UNIQUE] = '0'; + (void) remove(m4temp); + } + (void) remove(m4dir); +} + +void usage() { fprintf(stderr, "usage: m4 [-Dname[=val]] [-Uname]\n"); |