]> git.saurik.com Git - apple/libc.git/blob - stdio/FreeBSD/tmpnam.c.patch
Libc-763.12.tar.gz
[apple/libc.git] / stdio / FreeBSD / tmpnam.c.patch
1 --- tmpnam.c.orig 2010-07-06 16:02:20.000000000 -0700
2 +++ tmpnam.c 2010-07-06 16:32:52.000000000 -0700
3 @@ -40,21 +40,35 @@ __FBSDID("$FreeBSD: src/lib/libc/stdio/t
4
5 #include <stdio.h>
6 #include <unistd.h>
7 +#include <pthread.h>
8 +#include <stdlib.h>
9
10 __warn_references(tmpnam,
11 "warning: tmpnam() possibly used unsafely; consider using mkstemp()");
12
13 extern char *_mktemp(char *);
14
15 +static char *tmpnam_buf = NULL;
16 +static pthread_once_t tmpnam_buf_control = PTHREAD_ONCE_INIT;
17 +
18 +static void tmpnam_buf_allocate(void)
19 +{
20 + tmpnam_buf = malloc(L_tmpnam);
21 +}
22 +
23 char *
24 tmpnam(s)
25 char *s;
26 {
27 static u_long tmpcount;
28 - static char buf[L_tmpnam];
29
30 - if (s == NULL)
31 - s = buf;
32 + if (s == NULL) {
33 + if (pthread_once(&tmpnam_buf_control, tmpnam_buf_allocate)
34 + || !tmpnam_buf) {
35 + return NULL;
36 + }
37 + s = tmpnam_buf;
38 + }
39 (void)snprintf(s, L_tmpnam, "%stmp.%lu.XXXXXX", P_tmpdir, tmpcount);
40 ++tmpcount;
41 return (_mktemp(s));