Skip to content

Commit a6d6405

Browse files
committed
Include PID in temporary name
1 parent 8f19188 commit a6d6405

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

tests/include.c

+20-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
#include <stdlib.h>
1414
#include <cmocka.h>
1515
#include <limits.h>
16-
#if !_WIN32
16+
#if _WIN32
17+
#include <process.h>
18+
#else
1719
#include <unistd.h>
1820
#endif
1921

@@ -126,17 +128,30 @@ int setup(void **state)
126128
return -1;
127129
}
128130

131+
static char *temporary_name(void)
132+
{
133+
#if _WIN32
134+
int pid = _getpid();
135+
#else
136+
pid_t pid = getpid();
137+
#endif
138+
139+
char format[128];
140+
snprintf(format, sizeof(format), "zone.%d", pid);
141+
return tempnam(NULL, format);
142+
}
143+
129144
static char *generate_include(const char *text)
130145
{
131146
for (int i=0; i < 100; i++) {
132-
char *path = tempnam(NULL, "zone");
147+
char *path = temporary_name();
133148
if (path) {
134149
FILE *handle = fopen(path, "wbx");
135150
if (handle) {
136-
int result = fputs(text, handle);
151+
int error = fputs(text, handle);
137152
fflush(handle);
138153
(void)fclose(handle);
139-
if (result != EOF)
154+
if (error != EOF)
140155
return path;
141156
}
142157
free(path);
@@ -285,7 +300,7 @@ void the_include_that_wasnt(void **state)
285300

286301
(void)state;
287302

288-
char *non_include = tempnam(NULL, "zone");
303+
char *non_include = temporary_name();
289304
assert_non_null(non_include);
290305

291306
char buffer[16];

0 commit comments

Comments
 (0)