Add libc mktemp and tempnam. Update test.

This commit is contained in:
Christoffer Lerno
2026-02-07 20:25:28 +01:00
parent 02a67254cc
commit 89b9f52f1e
3 changed files with 11 additions and 2 deletions

View File

@@ -126,6 +126,7 @@ extern fn CInt memcmp(void* buf1, void* buf2, usz count);
extern fn void* memcpy(void* dest, void* src, usz n);
extern fn void* memmove(void* dest, void* src, usz n);
extern fn void* memset(void* dest, CInt value, usz n);
extern fn ZString mktemp(char* template) @cname(env::WIN32 ??? "_mktemp" : "mktemp");
extern fn Time_t* mktime(Tm* time) @if(!env::WIN32);
extern fn void perror(ZString string);
extern fn CInt printf(ZString format, ...);
@@ -177,6 +178,7 @@ extern fn CLong strtol(char* str, char** endptr, CInt base);
extern fn CULong strtoul(char* str, char** endptr, CInt base);
extern fn usz strxfrm(char* dest, ZString src, usz n);
extern fn CInt system(ZString str);
extern fn ZString tempnam(ZString dir, ZString prefix) @cname(env::WIN32 ??? "_tempnam" : "tempnam") @nodiscard;
extern fn Time_t timegm(Tm* timeptr) @if(!env::WIN32 && !env::NETBSD);
extern fn ZString tmpnam(char *buffer);
extern fn CFile tmpfile();

View File

@@ -5,7 +5,11 @@ tlocal char[1024] buf;
fn String random_name()
{
return libc::tmpnam(&buf).str_view();
ZString str = libc::tempnam(null, "randfile_");
String s = str.str_view();
buf[:s.len] = s[..];
libc::free(str);
return (String)buf[:s.len];
}
fn void map_read_only() @test

View File

@@ -58,7 +58,10 @@ fn void read_write_any()
data.hello = 0xCC;
char[1024] buf;
String name = libc::tmpnam(&buf).str_view();
ZString zname = libc::tempnam(null, "testfile_");
defer libc::free(zname);
String name = zname.str_view();
File file = file::open(name, "wb")!!;
io::write_any(&file, &data)!!;