summaryrefslogtreecommitdiff
path: root/src/libs/libgroff/tmpfile.cc
blob: 5ed1c5eeebeda32b1fac800e615b48fd30c0cb7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// -*- C++ -*-
/* Copyright (C) 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
     Written by James Clark (jjc@jclark.com)

This file is part of groff.

groff is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2, or (at your option) any later
version.

groff is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with groff; see the file COPYING.  If not, write to the Free Software
Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>

#include "posix.h"
#include "lib.h"
#include "errarg.h"
#include "error.h"

extern "C" {
  // Sun's stdlib.h fails to declare this.
  char *mktemp(char *);
  int mkstemp(char *);
}

// If this is set, create temporary files there
#define GROFF_TMPDIR_ENVVAR "GROFF_TMPDIR"
// otherwise if this is set, create temporary files there
#define TMPDIR_ENVVAR "TMPDIR"
// otherwise create temporary files here.
#define DEFAULT_TMPDIR "/tmp"
// Use this as the prefix for temporary filenames.
#define TMPFILE_PREFIX  "groff"

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

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)
      dir = DEFAULT_TMPDIR;
  }
  
  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 + 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.

#ifndef _MSC_VER

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);
  if (fd < 0)
    fatal("cannot create temporary file: %1", strerror(errno));
  errno = 0;
  FILE *fp = fdopen(fd, "w+");
  if (!fp)
    fatal("fdopen: %1", strerror(errno));
#else /* not HAVE_MKSTEMP */
  if (!mktemp(templ) || !templ[0])
    fatal("cannot create file name for temporary file");
  errno = 0;
  FILE *fp = fopen(templ, "w+");
  if (!fp)
    fatal("cannot open `%1': %2", templ, strerror(errno));
#endif /* not HAVE_MKSTEMP */
  if ((do_unlink) && (unlink(templ) < 0))
    error("cannot unlink `%1': %2", templ, strerror(errno));
  if ((namep != 0) && ((*namep) != 0)) {
    *namep = templ;
  } else {
    a_delete templ;
  }
  return fp;
}

#else

// If you're not running Unix, the following will do:
FILE *xtmpfile(char **namep, char *postfix=0, int do_unlink=1)
{
  FILE *fp = tmpfile();
  if (!fp)
    fatal("couldn't create temporary file");
  return fp;
}

#endif /* _MSC_VER */