This is better than mkstemp+unlink in every aspect.
Unified · Split
| @@ -121,19 +121,25 @@ static int rt_mem(TCCState *s1, int size) · 120 unmodified lines | |||
| 121 | /* Using mmap instead of malloc */ | 121 | /* Using mmap instead of malloc */ |
| 122 | void *prw; | 122 | void *prw; |
| 123 | static const char *tmpdir; | 123 | static const char *tmpdir; |
| 124 | char tmpfname[256]; | 124 | int fd = -1; |
| 125 | int fd; | ||
| 126 | 125 | ||
| 127 | if (tmpdir == NULL) { | 126 | if (tmpdir == NULL) { |
| 128 | tmpdir = getenv("TMPDIR"); | 127 | tmpdir = getenv("TMPDIR"); |
| 129 | if (tmpdir == NULL || *tmpdir == 0) | 128 | if (tmpdir == NULL || *tmpdir == 0) |
| 130 | tmpdir = "/tmp"; | 129 | tmpdir = "/tmp"; |
| 131 | } | 130 | } |
| 132 | snprintf(tmpfname, sizeof(tmpfname), "%s/.tccrunXXXXXX", tmpdir); | 131 | #ifdef O_TMPFILE |
| 133 | fd = mkstemp(tmpfname); | 132 | fd = open(tmpdir, O_TMPFILE|O_RDWR|O_EXCL, 0700); |
| 133 | #endif | ||
| 134 | if (fd < 0) { | ||
| 135 | char tmpfname[256]; | ||
| 136 | snprintf(tmpfname, sizeof(tmpfname), "%s/.tccrunXXXXXX", tmpdir); | ||
| 137 | fd = mkstemp(tmpfname); | ||
| 138 | if (fd >= 0) | ||
| 139 | unlink(tmpfname); | ||
| 140 | } | ||
| 134 | if (fd < 0) | 141 | if (fd < 0) |
| 135 | return tcc_error_noabort("tccrun: could not create temp file"); | 142 | return tcc_error_noabort("tccrun: could not create temp file"); |
| 136 | unlink(tmpfname); | ||
| 137 | if (ftruncate(fd, size) < 0) | 143 | if (ftruncate(fd, size) < 0) |
| 138 | return tcc_error_noabort("tccrun: could not resize temp file"); | 144 | return tcc_error_noabort("tccrun: could not resize temp file"); |
| 139 | ptr = mmap(NULL, size * 2, PROT_READ|PROT_EXEC, MAP_SHARED, fd, 0); | 145 | ptr = mmap(NULL, size * 2, PROT_READ|PROT_EXEC, MAP_SHARED, fd, 0); |