citrun

watch C/C++ source code execute
Log | Files | Refs | LICENSE

commit 102fe24e929e8008983bf3a36d69c5efd925d3be
parent 7364ea7864bfd2aa0c19b7daee4983a4d7678b80
Author: Kyle Milz <kyle@windows.krwm.net>
Date:   Wed,  4 Jan 2017 23:47:59 -0800

lib: add win32 mkstemp implementation

Diffstat:
Mlib.c | 49++++++++++++++++++++++++++++++++++++++-----------
1 file changed, 38 insertions(+), 11 deletions(-)

diff --git a/lib.c b/lib.c @@ -123,6 +123,39 @@ extend(size_t req_bytes) return mem; } +#ifdef _WIN32 +HANDLE +mkstemp(char *template) +{ + int i; + unsigned int r; + + char chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; + + for (i = strlen(template) - 1; i > 0; --i) { + if (template[i] != 'X') + break; + + if (rand_s(&r)) { + fprintf(stderr, "rand failed: %s\n", strerror(errno)); + exit(1); + } + + template[i] = chars[r % sizeof(chars)]; + } + + return CreateFile( + template, + GENERIC_READ | GENERIC_WRITE, + 0, + NULL, + CREATE_NEW, + 0, + NULL + ); +} +#endif /* _WIN32 */ + /* * Opens a file with a random suffix. Exits on error. */ @@ -142,17 +175,11 @@ open_fd() Err(1, "CreateDirectory"); strncpy(procfile, procdir, PATH_MAX); - strncat(procfile, "procfile.shm", PATH_MAX); - - if ((h = CreateFile("procfile.shm", - GENERIC_READ | GENERIC_WRITE, - 0, - NULL, - CREATE_NEW, - 0, - NULL - )) == INVALID_HANDLE_VALUE) - Err(1, "CreateFile"); + strncat(procfile, "program", PATH_MAX); + strncat(procfile, "_XXXXXXXXXX", PATH_MAX); + + if ((h = mkstemp(procfile)) == INVALID_HANDLE_VALUE) + Err(1, "mkstemp"); #else /* _WIN32 */ if (mkdir(procdir, S_IRWXU) && errno != EEXIST) err(1, "mkdir '%s'", procdir);