2 /* Copyright (C) 1991 Free Software Foundation, Inc.
3 Written by James Clark (jjc@jclark.uucp)
5 This file is part of groff.
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 1, or (at your option) any later
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
17 You should have received a copy of the GNU General Public License along
18 with groff; see the file LICENSE. If not, write to the Free Software
19 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
32 // Sun's stdlib.h fails to declare this.
36 // If this is set, create temporary files there
37 #define GROFF_TMPDIR_ENVVAR "GROFF_TMPDIR"
38 // otherwise if this is set, create temporary files there
39 #define TMPDIR_ENVVAR "TMPDIR"
40 // otherwise create temporary files here.
41 #define DEFAULT_TMPDIR "/tmp"
42 // Use this as the prefix for temporary filenames.
43 #define TMPFILE_PREFIX "groff"
45 // Open a temporary file with fatal error on failure.
49 const char *dir
= getenv(GROFF_TMPDIR_ENVVAR
);
51 dir
= getenv(TMPDIR_ENVVAR
);
56 const char *p
= strrchr(dir
, '/');
57 int needs_slash
= (!p
|| p
[1]);
58 char *templ
= new char[strlen(dir
) + needs_slash
59 + sizeof(TMPFILE_PREFIX
) - 1 + 6 + 1];
63 strcat(templ
, TMPFILE_PREFIX
);
64 strcat(templ
, "XXXXXX");
68 int fd
= mkstemp(templ
);
70 fatal("cannot create temporary file: %1", strerror(errno
));
72 FILE *fp
= fdopen(fd
, "w+");
74 fatal("fdopen: %1", strerror(errno
));
75 #else /* not HAVE_MKSTEMP */
76 if (!mktemp(templ
) || !templ
[0])
77 fatal("cannot create file name for temporary file");
79 FILE *fp
= fopen(templ
, "w+");
81 fatal("cannot open `%1': %2", templ
, strerror(errno
));
82 #endif /* not HAVE_MKSTEMP */
83 if (unlink(templ
) < 0)
84 error("cannot unlink `%1': %2", templ
, strerror(errno
));
90 // If you're not running Unix, the following will do:
95 fatal("couldn't create temporary file");