From 4abab5ad6c8465a7528ccdd5f49367da05f78bbd Mon Sep 17 00:00:00 2001 From: Vladimir Azarov Date: Tue, 1 Oct 2024 15:47:05 +0200 Subject: Initial version --- src/stdio/tempnam.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 src/stdio/tempnam.c (limited to 'src/stdio/tempnam.c') diff --git a/src/stdio/tempnam.c b/src/stdio/tempnam.c new file mode 100644 index 0000000..0c65b1f --- /dev/null +++ b/src/stdio/tempnam.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include +#include +#include +#include +#include "syscall.h" + +#define MAXTRIES 100 + +char *tempnam(const char *dir, const char *pfx) +{ + char s[PATH_MAX]; + size_t l, dl, pl; + int try; + int r; + + if (!dir) dir = P_tmpdir; + if (!pfx) pfx = "temp"; + + dl = strlen(dir); + pl = strlen(pfx); + l = dl + 1 + pl + 1 + 6; + + if (l >= PATH_MAX) { + errno = ENAMETOOLONG; + return 0; + } + + memcpy(s, dir, dl); + s[dl] = '/'; + memcpy(s+dl+1, pfx, pl); + s[dl+1+pl] = '_'; + s[l] = 0; + + for (try=0; try