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 | 121 | /* Using mmap instead of malloc */ |
| 122 | 122 | void *prw; |
| 123 | 123 | static const char *tmpdir; |
| 124 | − char tmpfname[256]; | |
| 125 | − int fd; | |
| 124 | + int fd = -1; | |
| 126 | 125 | |
| 127 | 126 | if (tmpdir == NULL) { |
| 128 | 127 | tmpdir = getenv("TMPDIR"); |
| 129 | 128 | if (tmpdir == NULL || *tmpdir == 0) |
| 130 | 129 | tmpdir = "/tmp"; |
| 131 | 130 | } |
| 132 | − snprintf(tmpfname, sizeof(tmpfname), "%s/.tccrunXXXXXX", tmpdir); | |
| 133 | − fd = mkstemp(tmpfname); | |
| 131 | +#ifdef O_TMPFILE | |
| 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 | 141 | if (fd < 0) |
| 135 | 142 | return tcc_error_noabort("tccrun: could not create temp file"); |
| 136 | − unlink(tmpfname); | |
| 137 | 143 | if (ftruncate(fd, size) < 0) |
| 138 | 144 | return tcc_error_noabort("tccrun: could not resize temp file"); |
| 139 | 145 | ptr = mmap(NULL, size * 2, PROT_READ|PROT_EXEC, MAP_SHARED, fd, 0); |