1
0
mirror of https://github.com/openbsd/src.git synced 2025-01-10 06:47:55 -08:00

Get rid of mktemp in the handling of diversion.

Simply put, mkstemp/unlink/rewind has the proper semantics under Unix,
and so we don't have to keep track about temp file names and remove them.
This commit is contained in:
espie 1999-09-06 13:20:40 +00:00
parent 5f22054722
commit 3f42598db2
4 changed files with 18 additions and 44 deletions

View File

@ -1,4 +1,4 @@
/* $OpenBSD: eval.c,v 1.12 1999/09/06 13:10:48 espie Exp $ */
/* $OpenBSD: eval.c,v 1.13 1999/09/06 13:20:40 espie Exp $ */
/* $NetBSD: eval.c,v 1.7 1996/11/10 21:21:29 pk Exp $ */
/*
@ -41,7 +41,7 @@
#if 0
static char sccsid[] = "@(#)eval.c 8.2 (Berkeley) 4/27/95";
#else
static char rcsid[] = "$OpenBSD: eval.c,v 1.12 1999/09/06 13:10:48 espie Exp $";
static char rcsid[] = "$OpenBSD: eval.c,v 1.13 1999/09/06 13:20:40 espie Exp $";
#endif
#endif /* not lint */
@ -56,6 +56,7 @@ static char rcsid[] = "$OpenBSD: eval.c,v 1.12 1999/09/06 13:10:48 espie Exp $";
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <fcntl.h>
#include <err.h>
@ -668,10 +669,13 @@ register int n;
if (n < 0 || n >= MAXOUT)
n = 0; /* bitbucket */
if (outfile[n] == NULL) {
m4temp[UNIQUE] = n + '0';
if ((fd = open(m4temp, O_CREAT|O_EXCL|O_WRONLY, 0600)) < 0 ||
(outfile[n] = fdopen(fd, "w")) == NULL)
err(1, "%s: cannot divert", m4temp);
char fname[] = _PATH_DIVNAME;
if ((fd = mkstemp(fname)) < 0 ||
(outfile[n] = fdopen(fd, "w+")) == NULL)
err(1, "%s: cannot divert", fname);
if (unlink(fname) == -1)
err(1, "%s: cannot unlink", fname);
}
active = outfile[n];
}

View File

@ -1,4 +1,4 @@
/* $OpenBSD: extern.h,v 1.6 1999/09/06 13:15:33 espie Exp $ */
/* $OpenBSD: extern.h,v 1.7 1999/09/06 13:20:40 espie Exp $ */
/* $NetBSD: extern.h,v 1.3 1996/01/13 23:25:24 pk Exp $ */
/*-
@ -89,7 +89,6 @@ extern char *endest; /* end of string space */
extern pbent *endpbb; /* end of push-back buffer */
extern char *ep; /* first free char in strspace */
extern char lquote[]; /* left quote character (`) */
extern char *m4temp; /* filename for diversions */
extern char *m4wraps; /* m4wrap string default. */
extern char *null; /* as it says.. just a null. */
extern char rquote[]; /* right quote character (') */

View File

@ -1,4 +1,4 @@
/* $OpenBSD: main.c,v 1.11 1999/09/06 13:10:48 espie Exp $ */
/* $OpenBSD: main.c,v 1.12 1999/09/06 13:20:40 espie Exp $ */
/* $NetBSD: main.c,v 1.12 1997/02/08 23:54:49 cgd Exp $ */
/*-
@ -47,7 +47,7 @@ static char copyright[] =
#if 0
static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/6/93";
#else
static char rcsid[] = "$OpenBSD: main.c,v 1.11 1999/09/06 13:10:48 espie Exp $";
static char rcsid[] = "$OpenBSD: main.c,v 1.12 1999/09/06 13:20:40 espie Exp $";
#endif
#endif /* not lint */
@ -85,7 +85,6 @@ int fp; /* m4 call frame pointer */
FILE *infile[MAXINP]; /* input file stack (0=stdin) */
FILE *outfile[MAXOUT]; /* diversion array(0=bitbucket)*/
FILE *active; /* active output file pointer */
char *m4temp; /* filename for diversions */
int ilevel = 0; /* input file stack pointer */
int oindex = 0; /* diversion index.. */
char *null = ""; /* as it says.. just a null.. */
@ -188,9 +187,6 @@ main(argc,argv)
argv += optind;
active = stdout; /* default active output */
/* filename for diversions */
m4temp = mktemp(xstrdup(_PATH_DIVNAME));
bbase[0] = bufbase;
if (!argc) {
sp = -1; /* stack pointer initialized */
@ -228,12 +224,6 @@ main(argc,argv)
/* remove bitbucket if used */
if (outfile[0] != NULL) {
(void) fclose(outfile[0]);
m4temp[UNIQUE] = '0';
#ifdef vms
(void) remove(m4temp);
#else
(void) unlink(m4temp);
#endif
}
return 0;

View File

@ -1,4 +1,4 @@
/* $OpenBSD: misc.c,v 1.7 1999/09/06 13:10:49 espie Exp $ */
/* $OpenBSD: misc.c,v 1.8 1999/09/06 13:20:40 espie Exp $ */
/* $NetBSD: misc.c,v 1.6 1995/09/28 05:37:41 tls Exp $ */
/*
@ -41,7 +41,7 @@
#if 0
static char sccsid[] = "@(#)misc.c 8.1 (Berkeley) 6/6/93";
#else
static char rcsid[] = "$OpenBSD: misc.c,v 1.7 1999/09/06 13:10:49 espie Exp $";
static char rcsid[] = "$OpenBSD: misc.c,v 1.8 1999/09/06 13:20:40 espie Exp $";
#endif
#endif /* not lint */
@ -152,26 +152,13 @@ getdiv(n)
int n;
{
register int c;
register FILE *dfil;
if (active == outfile[n])
errx(1, "undivert: diversion still active");
rewind(outfile[n]);
while ((c = getc(outfile[n])) != EOF)
putc(c, active);
(void) fclose(outfile[n]);
outfile[n] = NULL;
m4temp[UNIQUE] = n + '0';
if ((dfil = fopen(m4temp, "r")) == NULL)
err(1, "%s: cannot undivert", m4temp);
else
while ((c = getc(dfil)) != EOF)
putc(c, active);
(void) fclose(dfil);
#ifdef vms
if (remove(m4temp))
#else
if (unlink(m4temp) == -1)
#endif
err(1, "%s: cannot unlink", m4temp);
}
void
@ -192,12 +179,6 @@ killdiv()
for (n = 0; n < MAXOUT; n++)
if (outfile[n] != NULL) {
(void) fclose(outfile[n]);
m4temp[UNIQUE] = n + '0';
#ifdef vms
(void) remove(m4temp);
#else
(void) unlink(m4temp);
#endif
}
}