| 1 |
/* Copyright (c) 1987, 1989, 2012, 2025 University of Maryland Department of |
| 2 |
Computer Science. |
| 3 |
|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining |
| 5 |
a copy of this software and associated documentation files (the |
| 6 |
"Software"), to deal in the Software without restriction, including |
| 7 |
without limitation, the rights to use, copy, modify, merge, publish, |
| 8 |
distribute, sublicense, and/or sell copies of the Software, and to |
| 9 |
permit persons to whom the Software is furnished to do so, subject to |
| 10 |
the following conditions: The above copyright notice, this permission |
| 11 |
notice and the disclaimer statement shall be included in all copies |
| 12 |
or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 15 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 16 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 17 |
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 18 |
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 19 |
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 20 |
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include <config.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <stdio.h> |
| 28 |
#include <sys/types.h> |
| 29 |
|
| 30 |
#ifdef HAVE_UNISTD_H |
| 31 |
#include <unistd.h> |
| 32 |
#endif |
| 33 |
|
| 34 |
#ifdef _AMIGA |
| 35 |
#define getpid() 42 |
| 36 |
#endif /* _AMIGA */ |
| 37 |
|
| 38 |
#if defined( WIN32 ) || defined( _AMIGA ) |
| 39 |
#include "types.h" |
| 40 |
#else |
| 41 |
#include <sys/file.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
#include "tempfile.h" |
| 45 |
|
| 46 |
#ifdef KPATHSEA |
| 47 |
#include <kpathsea/config.h> |
| 48 |
#include <kpathsea/c-fopen.h> |
| 49 |
#else |
| 50 |
#ifndef O_BINARY |
| 51 |
#define O_BINARY 0 |
| 52 |
#endif |
| 53 |
#endif |
| 54 |
|
| 55 |
#if defined(WIN32) || defined(MSDOS) |
| 56 |
#define TMP_OPEN_MODE (O_RDWR | O_BINARY) |
| 57 |
#else |
| 58 |
#define TMP_OPEN_MODE 2 |
| 59 |
#endif |
| 60 |
|
| 61 |
#ifdef DOSISH |
| 62 |
#ifndef WIN32 |
| 63 |
#define __cdecl |
| 64 |
#endif |
| 65 |
static char *TmpFile; |
| 66 |
static void __cdecl |
| 67 |
RemoveTempFile(void) |
| 68 |
{ |
| 69 |
if (TmpFile) { |
| 70 |
unlink(TmpFile); |
| 71 |
TmpFile = (char *)0; |
| 72 |
} |
| 73 |
} |
| 74 |
#endif |
| 75 |
|
| 76 |
/* |
| 77 |
* Create an open but unlinked (i.e., remove-on-exit-or-file-close) |
| 78 |
* file. Return the file descriptor, or -1 if unable to create such |
| 79 |
* a file. The buffer `name' is used to hold the file name (so that |
| 80 |
* if this returns -1, it is useful for error messages). |
| 81 |
* |
| 82 |
* We use the despicable trick of unlinking an open temporary file. |
| 83 |
* The alternatives are too painful. If it becomes necessary to |
| 84 |
* do this another way, however, here is a method suggested by Fred |
| 85 |
* Blonder: fork, and have the parent wait for the child to exit. |
| 86 |
* (The parent must avoid being killed during this phase.) When |
| 87 |
* the child exits, the parent should then remove the temporary file, |
| 88 |
* then exit with the same status, or send itself the same signal. |
| 89 |
*/ |
| 90 |
int |
| 91 |
MakeRWTempFile(char *name) |
| 92 |
{ |
| 93 |
register int tf, n; |
| 94 |
int mypid, tries; |
| 95 |
const char *tmpdir; |
| 96 |
|
| 97 |
if ((tmpdir = getenv("TMP")) == NULL |
| 98 |
&& (tmpdir = getenv("TEMP")) == NULL |
| 99 |
&& (tmpdir = getenv("TMPDIR")) == NULL) |
| 100 |
#if defined(WIN32) || defined(MSDOS) |
| 101 |
tmpdir = "."; |
| 102 |
#else |
| 103 |
tmpdir = "/tmp"; |
| 104 |
#endif |
| 105 |
/* compose a suitable temporary file name, and get an r/w descriptor */ |
| 106 |
mypid = getpid(); |
| 107 |
n = 0; |
| 108 |
tries = 0; |
| 109 |
do { |
| 110 |
(void) sprintf(name, "%s/#%d.%d", tmpdir, mypid, n++); |
| 111 |
#ifndef DOSISH |
| 112 |
(void) unlink(name); |
| 113 |
#endif |
| 114 |
#ifdef O_CREAT /* have three-argument open syscall */ |
| 115 |
tries++; |
| 116 |
tf = open(name, O_RDWR | O_CREAT | O_EXCL | O_BINARY, 0666); |
| 117 |
#else |
| 118 |
if (access(name, 0) == 0) { |
| 119 |
/* |
| 120 |
* Skip existing files. Note that tf might |
| 121 |
* not be set yet. |
| 122 |
*/ |
| 123 |
tf = -1; |
| 124 |
continue; |
| 125 |
} |
| 126 |
tries++; |
| 127 |
tf = creat(name, 0666); |
| 128 |
if (tf >= 0) { |
| 129 |
(void) close(tf); |
| 130 |
tf = open(name, TMP_OPEN_MODE); |
| 131 |
if (tf < 0) |
| 132 |
(void) unlink(name); |
| 133 |
} |
| 134 |
#endif |
| 135 |
} while (tf < 0 && tries < 20); |
| 136 |
if (tf < 0) /* probably unrecoverable user error */ |
| 137 |
return (-1); |
| 138 |
|
| 139 |
#ifdef DOSISH |
| 140 |
/* Can't use the despicable trick of unlinking an open file, |
| 141 |
since the filesystem won't wait for it to be closed. Let's |
| 142 |
be somewhat less despicable. */ |
| 143 |
TmpFile = (char *)malloc(strlen(name) + 1); |
| 144 |
if (TmpFile) { |
| 145 |
strcpy(TmpFile, name); |
| 146 |
atexit(RemoveTempFile); |
| 147 |
} |
| 148 |
#else |
| 149 |
(void) unlink(name); |
| 150 |
#endif |
| 151 |
return (tf); |
| 152 |
} |