home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / GROFFSRC / SRC / LIBGROFF / TMPFILE.CC < prev    next >
Encoding:
C/C++ Source or Header  |  1994-01-03  |  2.8 KB  |  104 lines

  1. // -*- C++ -*-
  2. /* Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  3.      Written by James Clark (jjc@jclark.com)
  4.  
  5. This file is part of groff.
  6.  
  7. groff is free software; you can redistribute it and/or modify it under
  8. the terms of the GNU General Public License as published by the Free
  9. Software Foundation; either version 2, or (at your option) any later
  10. version.
  11.  
  12. groff is distributed in the hope that it will be useful, but WITHOUT ANY
  13. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License along
  18. with groff; see the file COPYING.  If not, write to the Free Software
  19. Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
  20.  
  21. #include <stdio.h>
  22. #include <errno.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25.  
  26. #include "posix.h"
  27. #include "lib.h"
  28. #include "errarg.h"
  29. #include "error.h"
  30.  
  31. extern "C" {
  32.   // Sun's stdlib.h fails to declare this.
  33.   char *mktemp(char *);
  34.   int mkstemp(char *);
  35. }
  36.  
  37. // If this is set, create temporary files there
  38. #define GROFF_TMPDIR_ENVVAR "GROFF_TMPDIR"
  39. // otherwise if this is set, create temporary files there
  40. #define TMPDIR_ENVVAR "TMPDIR"
  41. // otherwise create temporary files here.
  42. #define DEFAULT_TMPDIR "/tmp"
  43. // Use this as the prefix for temporary filenames.
  44. #define TMPFILE_PREFIX  "groff"
  45.  
  46. #ifndef __EMX__
  47.  
  48. // Open a temporary file with fatal error on failure.
  49.  
  50. FILE *xtmpfile()
  51. {
  52.   const char *dir = getenv(GROFF_TMPDIR_ENVVAR);
  53.   if (!dir) {
  54.     dir = getenv(TMPDIR_ENVVAR);
  55.     if (!dir)
  56.       dir = DEFAULT_TMPDIR;
  57.   }
  58.   
  59.   const char *p = strrchr(dir, '/');
  60.   int needs_slash = (!p || p[1]);
  61.   char *templ = new char[strlen(dir) + needs_slash
  62.                 + sizeof(TMPFILE_PREFIX) - 1 + 6 + 1];
  63.   strcpy(templ, dir);
  64.   if (needs_slash)
  65.     strcat(templ, "/");
  66.   strcat(templ, TMPFILE_PREFIX);
  67.   strcat(templ, "XXXXXX");
  68.  
  69. #ifdef HAVE_MKSTEMP
  70.   errno = 0;
  71.   int fd = mkstemp(templ);
  72.   if (fd < 0)
  73.     fatal("cannot create temporary file: %1", strerror(errno));
  74.   errno = 0;
  75.   FILE *fp = fdopen(fd, "w+");
  76.   if (!fp)
  77.     fatal("fdopen: %1", strerror(errno));
  78. #else /* not HAVE_MKSTEMP */
  79.   if (!mktemp(templ) || !templ[0])
  80.     fatal("cannot create file name for temporary file");
  81.   errno = 0;
  82.   FILE *fp = fopen(templ, "w+");
  83.   if (!fp)
  84.     fatal("cannot open `%1': %2", templ, strerror(errno));
  85. #endif /* not HAVE_MKSTEMP */
  86.   if (unlink(templ) < 0)
  87.     error("cannot unlink `%1': %2", templ, strerror(errno));
  88.   a_delete templ;
  89.   return fp;
  90. }
  91.  
  92. #else
  93.  
  94. // If you're not running Unix, the following will do:
  95. FILE *xtmpfile()
  96. {
  97.   FILE *fp = tmpfile();
  98.   if (!fp)
  99.     fatal("couldn't create temporary file");
  100.   return fp;
  101. }
  102.  
  103. #endif
  104.