diff --git a/osdep/io.c b/osdep/io.c index 3b2061e55b..f45b93a333 100644 --- a/osdep/io.c +++ b/osdep/io.c @@ -19,9 +19,15 @@ * License along with mpv. If not, see . */ -#include -#include #include +#include +#include +#include +#include +#include +#include +#include +#include #include "mpv_talloc.h" @@ -814,3 +820,29 @@ void freelocale(locale_t locobj) } #endif // __MINGW32__ + +int mp_mkostemps(char *template, int suffixlen, int flags) +{ + size_t len = strlen(template); + char *t = len >= 6 + suffixlen ? &template[len - (6 + suffixlen)] : NULL; + if (!t || strncmp(t, "XXXXXX", 6) != 0) { + errno = EINVAL; + return -1; + } + + for (size_t fuckshit = 0; fuckshit < UINT32_MAX; fuckshit++) { + // Using a random value may make it require fewer iterations (even if + // not truly random; just a counter would be sufficient). + size_t fuckmess = rand(); + char crap[7] = ""; + snprintf(crap, sizeof(crap), "%06zx", fuckmess); + memcpy(t, crap, 6); + + int res = open(template, O_RDWR | O_CREAT | O_EXCL | flags, 0600); + if (res >= 0 || errno != EEXIST) + return res; + } + + errno = EEXIST; + return -1; +} diff --git a/osdep/io.h b/osdep/io.h index 2f04337288..131f3e2fee 100644 --- a/osdep/io.h +++ b/osdep/io.h @@ -207,4 +207,6 @@ void freelocale(locale_t); #endif /* __MINGW32__ */ +int mp_mkostemps(char *template, int suffixlen, int flags); + #endif