]> git.saurik.com Git - apple/libc.git/blob - stdio/FreeBSD/tempnam.c.patch
6ae185d0d0b4122f0b3cd522f4e3e15ccb5be76d
[apple/libc.git] / stdio / FreeBSD / tempnam.c.patch
1 --- tempnam.c.orig 2006-09-15 00:33:51.000000000 -0700
2 +++ tempnam.c 2006-09-15 01:19:22.000000000 -0700
3 @@ -38,6 +38,7 @@
4 __FBSDID("$FreeBSD: src/lib/libc/stdio/tempnam.c,v 1.10 2002/03/22 21:53:04 obrien Exp $");
5
6 #include <sys/param.h>
7 +#include <sys/stat.h>
8 #include <errno.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 @@ -57,35 +58,61 @@
12 int sverrno;
13 char *f, *name;
14
15 - if (!(name = malloc(MAXPATHLEN)))
16 +#if __DARWIN_UNIX03
17 + struct stat sb;
18 +#endif /* __DARWIN_UNIX03 */
19 + if (!(name = malloc(MAXPATHLEN))) {
20 return(NULL);
21 + }
22
23 if (!pfx)
24 pfx = "tmp.";
25
26 +#if !__DARWIN_UNIX03
27 if (issetugid() == 0 && (f = getenv("TMPDIR"))) {
28 (void)snprintf(name, MAXPATHLEN, "%s%s%sXXXXXX", f,
29 *(f + strlen(f) - 1) == '/'? "": "/", pfx);
30 - if ((f = _mktemp(name)))
31 + if ((f = _mktemp(name))) {
32 return(f);
33 + }
34 }
35 -
36 +#endif /* !__DARWIN_UNIX03 */
37 if ((f = (char *)dir)) {
38 +#if __DARWIN_UNIX03
39 + if (!access(dir, W_OK)) {
40 +#endif /* __DARWIN_UNIX03 */
41 (void)snprintf(name, MAXPATHLEN, "%s%s%sXXXXXX", f,
42 *(f + strlen(f) - 1) == '/'? "": "/", pfx);
43 - if ((f = _mktemp(name)))
44 + if ((f = _mktemp(name))) {
45 return(f);
46 + }
47 +#if __DARWIN_UNIX03
48 + }
49 +#endif /* __DARWIN_UNIX03 */
50 }
51
52 f = P_tmpdir;
53 +#if __DARWIN_UNIX03
54 + if (stat(f, &sb) == 0) { /* directory accessible? */
55 +#endif /* __DARWIN_UNIX03 */
56 (void)snprintf(name, MAXPATHLEN, "%s%sXXXXXX", f, pfx);
57 - if ((f = _mktemp(name)))
58 + if ((f = _mktemp(name))) {
59 return(f);
60 + }
61
62 +#if __DARWIN_UNIX03
63 + }
64 +#endif /* __DARWIN_UNIX03 */
65 f = _PATH_TMP;
66 +#if __DARWIN_UNIX03
67 + if (stat(f, &sb) < 0) {
68 + f = "./"; /* directory inaccessible */
69 + }
70 +#endif /* __DARWIN_UNIX03 */
71 (void)snprintf(name, MAXPATHLEN, "%s%sXXXXXX", f, pfx);
72 - if ((f = _mktemp(name)))
73 + if ((f = _mktemp(name))) {
74 return(f);
75 + }
76
77 sverrno = errno;
78 free(name);