Clean up temporary file(s) and directory when m4 exits without falling

through main() (e.g., signals or calls to errx()).

PR:		conf/25715
This commit is contained in:
Gregory Neil Shapiro 2001-04-16 18:36:35 +00:00
parent 573bc39917
commit 164c01f05b
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=75551
3 changed files with 18 additions and 7 deletions

View File

@ -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. */

View File

@ -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;
}

View File

@ -236,6 +236,19 @@ register char *s;
return ++p;
}
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()
{