2 /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001
3 Free Software Foundation, Inc.
4 Written by James Clark (jjc@jclark.com)
6 This file is part of groff.
8 groff is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 groff is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with groff; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
32 // If this is set, create temporary files there
33 #define GROFF_TMPDIR_ENVVAR "GROFF_TMPDIR"
34 // otherwise if this is set, create temporary files there
35 #define TMPDIR_ENVVAR "TMPDIR"
36 // otherwise if P_tmpdir is defined, create temporary files there
38 # define DEFAULT_TMPDIR P_tmpdir
40 // otherwise create temporary files here.
41 # define DEFAULT_TMPDIR "/tmp"
43 // Use this as the prefix for temporary filenames.
44 #define TMPFILE_PREFIX_SHORT ""
45 #define TMPFILE_PREFIX_LONG "groff"
48 size_t tmpfile_prefix_len;
49 int use_short_postfix = 0;
56 temp_init::temp_init()
58 const char *tem = getenv(GROFF_TMPDIR_ENVVAR);
60 tem = getenv(TMPDIR_ENVVAR);
64 size_t tem_len = strlen(tem);
65 const char *tem_end = tem + tem_len - 1;
66 int need_slash = strchr(DIR_SEPS, *tem_end) == NULL ? 1 : 0;
67 char *tem2 = new char[tem_len + need_slash + 1];
71 const char *tem3 = TMPFILE_PREFIX_LONG;
72 if (file_name_max(tem2) <= 14) {
73 tem3 = TMPFILE_PREFIX_SHORT;
74 use_short_postfix = 1;
76 tmpfile_prefix_len = tem_len + need_slash + strlen(tem3);
77 tmpfile_prefix = new char[tmpfile_prefix_len + 1];
78 strcpy(tmpfile_prefix, tem2);
79 strcat(tmpfile_prefix, tem3);
83 temp_init::~temp_init()
85 a_delete tmpfile_prefix;
89 * Generate a temporary name template with a postfix
90 * immediately after the TMPFILE_PREFIX.
91 * It uses the groff preferences for a temporary directory.
92 * Note that no file name is either created or opened,
93 * only the *template* is returned.
96 char *xtmptemplate(const char *postfix_long, const char *postfix_short)
98 const char *postfix = use_short_postfix ? postfix_short : postfix_long;
101 postlen = strlen(postfix);
102 char *templ = new char[tmpfile_prefix_len + postlen + 6 + 1];
103 strcpy(templ, tmpfile_prefix);
105 strcat(templ, postfix);
106 strcat(templ, "XXXXXX");
110 // The trick with unlinking the temporary file while it is still in
111 // use is not portable, it will fail on MS-DOS and most MS-Windows
112 // filesystems. So it cannot be used on non-Posix systems.
113 // Instead, we maintain a list of files to be deleted on exit.
114 // This should be portable to all platforms.
116 struct xtmpfile_list {
119 xtmpfile_list(char *fn) : fname(fn), next(0) {}
122 xtmpfile_list *xtmpfiles_to_delete = 0;
124 struct xtmpfile_list_init {
125 ~xtmpfile_list_init();
126 } _xtmpfile_list_init;
128 xtmpfile_list_init::~xtmpfile_list_init()
130 xtmpfile_list *x = xtmpfiles_to_delete;
132 if (unlink(x->fname) < 0)
133 error("cannot unlink `%1': %2", x->fname, strerror(errno));
134 xtmpfile_list *tmp = x;
141 static void add_tmp_file(const char *name)
143 char *s = new char[strlen(name)+1];
145 xtmpfile_list *x = new xtmpfile_list(s);
146 x->next = xtmpfiles_to_delete;
147 xtmpfiles_to_delete = x;
150 // Open a temporary file and with fatal error on failure.
152 FILE *xtmpfile(char **namep,
153 const char *postfix_long, const char *postfix_short,
156 char *templ = xtmptemplate(postfix_long, postfix_short);
158 int fd = mkstemp(templ);
160 fatal("cannot create temporary file: %1", strerror(errno));
162 FILE *fp = fdopen(fd, FOPEN_RWB); // many callers of xtmpfile use binary I/O
164 fatal("fdopen: %1", strerror(errno));