From a1ac9f031c8564b6cfbd1e31549177c28defab17 Mon Sep 17 00:00:00 2001 From: Werner LEMBERG Date: Sat, 12 Feb 2000 07:46:07 +0000 Subject: [PATCH] * src/include/lib.h: Added xtmptemplate and made xtmpfile parametrically polymorphic. * src/libs/libgroff/tmpfile.cc: Implemented xtmptemplate and the alterations to xtmpfile. xtmpfile can be requested to return the filename created and asked not to unlink the temp file. The default behaviour if parameters are absent is exactly the same as before. --- ChangeLog | 11 +++++++++++ src/include/lib.h | 3 ++- src/libs/libgroff/tmpfile.cc | 38 ++++++++++++++++++++++++++++++++------ 3 files changed, 45 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index 067de146..ba33ba39 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2000-02-11 Gaius Mulley + + * src/include/lib.h: Added xtmptemplate and made xtmpfile + parametrically polymorphic. + + * src/libs/libgroff/tmpfile.cc: Implemented xtmptemplate + and the alterations to xtmpfile. + xtmpfile can be requested to return the filename created + and asked not to unlink the temp file. The default behaviour + if parameters are absent is exactly the same as before. + 2000-02-11 Abramo Bagnara A new request `length' is available which returns the length of a diff --git a/src/include/lib.h b/src/include/lib.h index b309a77b..b92457e8 100644 --- a/src/include/lib.h +++ b/src/include/lib.h @@ -53,7 +53,8 @@ int is_prime(unsigned); #include -FILE *xtmpfile(); +FILE *xtmpfile(char **namep=0, char *postfix=0, int do_unlink=1); +char *xtmptemplate(char *extension=0); #ifndef STDIO_H_DECLARES_POPEN diff --git a/src/libs/libgroff/tmpfile.cc b/src/libs/libgroff/tmpfile.cc index 280d87c5..6d5ea7c1 100644 --- a/src/libs/libgroff/tmpfile.cc +++ b/src/libs/libgroff/tmpfile.cc @@ -43,11 +43,22 @@ extern "C" { // Use this as the prefix for temporary filenames. #define TMPFILE_PREFIX "groff" -// Open a temporary file with fatal error on failure. +/* + * Generate a temporary name template with a postfix + * immediately after the TMPFILE_PREFIX. + * It uses the groff preferences for a temporary directory. + * Note that no file name is either created or opened, + * only the *template* is returned. + */ -FILE *xtmpfile() +char *xtmptemplate(char *postfix=0) { const char *dir = getenv(GROFF_TMPDIR_ENVVAR); + int postlen = 0; + + if (postfix) + postlen = strlen(postfix); + if (!dir) { dir = getenv(TMPDIR_ENVVAR); if (!dir) @@ -57,13 +68,24 @@ FILE *xtmpfile() const char *p = strrchr(dir, '/'); int needs_slash = (!p || p[1]); char *templ = new char[strlen(dir) + needs_slash - + sizeof(TMPFILE_PREFIX) - 1 + 6 + 1]; + + sizeof(TMPFILE_PREFIX) - 1 + 6 + 1 + postlen]; strcpy(templ, dir); if (needs_slash) strcat(templ, "/"); strcat(templ, TMPFILE_PREFIX); + if (postlen > 0) + strcat(templ, postfix); strcat(templ, "XXXXXX"); + return( templ ); +} + +// Open a temporary file and with fatal error on failure. + +FILE *xtmpfile(char **namep=0, char *postfix=0, int do_unlink=1) +{ + char *templ = xtmptemplate(postfix); + #ifdef HAVE_MKSTEMP errno = 0; int fd = mkstemp(templ); @@ -81,15 +103,19 @@ FILE *xtmpfile() if (!fp) fatal("cannot open `%1': %2", templ, strerror(errno)); #endif /* not HAVE_MKSTEMP */ - if (unlink(templ) < 0) + if ((do_unlink) && (unlink(templ) < 0)) error("cannot unlink `%1': %2", templ, strerror(errno)); - a_delete templ; + if ((namep != 0) && ((*namep) != 0)) { + *namep = templ; + } else { + a_delete templ; + } return fp; } #if 0 // If you're not running Unix, the following will do: -FILE *xtmpfile() +FILE *xtmpfile(char **namep, char *postfix=0, int do_unlink=1) { FILE *fp = tmpfile(); if (!fp) -- 2.11.4.GIT