|
23 | 23 | #include <windows.h> |
24 | 24 | #else |
25 | 25 | #include <unistd.h> |
| 26 | + #ifndef _POSIX_C_SOURCE |
| 27 | + extern int mkstemp(char *); |
| 28 | + #endif |
26 | 29 | #include <fcntl.h> |
27 | 30 | #endif |
28 | 31 |
|
@@ -501,6 +504,34 @@ int32_t fossil_fstream_is_readable(const char *filename) { |
501 | 504 | #endif |
502 | 505 | } |
503 | 506 |
|
| 507 | +fossil_fstream_t fossil_fstream_tempfile(void) { |
| 508 | + fossil_fstream_t temp_stream; |
| 509 | + char temp_filename[FOSSIL_BUFFER_MEDIUM]; |
| 510 | + |
| 511 | +#ifdef _WIN32 |
| 512 | + if (GetTempFileNameA(".", "fossil", 0, temp_filename) == 0) { |
| 513 | + fprintf(stderr, "Error: Failed to create temporary file\n"); |
| 514 | + return (fossil_fstream_t){NULL, ""}; |
| 515 | + } |
| 516 | +#else |
| 517 | + char template[] = "fossil_tempfile_XXXXXX"; |
| 518 | + int fd = mkstemp(template); |
| 519 | + if (fd == -1) { |
| 520 | + fprintf(stderr, "Error: Failed to create temporary file\n"); |
| 521 | + return (fossil_fstream_t){NULL, ""}; |
| 522 | + } |
| 523 | + close(fd); // Close the file descriptor as it's no longer needed |
| 524 | + strncpy(temp_filename, template, FOSSIL_BUFFER_MEDIUM); |
| 525 | +#endif |
| 526 | + |
| 527 | + if (fossil_fstream_open(&temp_stream, temp_filename, "wb+") != FOSSIL_ERROR_OK) { |
| 528 | + fprintf(stderr, "Error: Failed to open temporary file - %s\n", temp_filename); |
| 529 | + return (fossil_fstream_t){NULL, ""}; |
| 530 | + } |
| 531 | + |
| 532 | + return temp_stream; |
| 533 | +} |
| 534 | + |
504 | 535 | int32_t fossil_fstream_is_writable(const char *filename) { |
505 | 536 | #ifdef _WIN32 |
506 | 537 | DWORD attrs = GetFileAttributesA(filename); |
|
0 commit comments