1 /*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
7 *
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
12 *
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
29
30 #pragma weak _tempnam = tempnam
31
32 #include "lint.h"
33 #include <mtlib.h>
34 #include <sys/types.h>
35 #include <stdio.h>
36 #include <string.h>
37 #include <stdlib.h>
38 #include <thread.h>
39 #include <synch.h>
40 #include <unistd.h>
41 #include <sys/stat.h>
42
43 #define max(A, B) (((A) < (B))?(B):(A))
44
45 static char *pcopy(char *, const char *);
46
47 static char seed[] = "AAA";
48
49 static mutex_t seed_lk = DEFAULTMUTEX;
50
51 char *
tempnam(const char * dir,const char * pfx)52 tempnam(const char *dir, /* use this directory please (if non-NULL) */
53 const char *pfx) /* use this (if non-NULL) as filename prefix */
54 {
55 char *p, *q, *tdir;
56 size_t x = 0, y = 0, z;
57 struct stat64 statbuf;
58
59 z = sizeof (P_tmpdir) - 1;
60 if ((tdir = getenv("TMPDIR")) != NULL) {
61 x = strlen(tdir);
62 }
63 if (dir != NULL) {
64 if (stat64(dir, &statbuf) == 0 && S_ISDIR(statbuf.st_mode))
65 y = strlen(dir);
66 }
67 if ((p = malloc(max(max(x, y), z)+16)) == NULL)
68 return (NULL);
69 if (x > 0 && access(pcopy(p, tdir), (W_OK | X_OK)) == 0)
70 goto OK;
71 if (y > 0 && access(pcopy(p, dir), (W_OK | X_OK)) == 0)
72 goto OK;
73 if (access(pcopy(p, P_tmpdir), (W_OK | X_OK)) == 0)
74 goto OK;
75 if (access(pcopy(p, "/tmp"), (W_OK | X_OK)) != 0) {
76 free(p);
77 return (NULL);
78 }
79 OK:
80 (void) strcat(p, "/");
81 if (pfx) {
82 *(p+strlen(p)+5) = '\0';
83 (void) strncat(p, pfx, 5);
84 }
85 lmutex_lock(&seed_lk);
86 (void) strcat(p, seed);
87 (void) strcat(p, "XXXXXX");
88 q = seed;
89 while (*q == 'Z')
90 *q++ = 'A';
91 if (*q != '\0')
92 ++*q;
93 lmutex_unlock(&seed_lk);
94 if (*mktemp(p) == '\0') {
95 free(p);
96 return (NULL);
97 }
98 return (p);
99 }
100
101 static char *
pcopy(char * space,const char * arg)102 pcopy(char *space, const char *arg)
103 {
104 char *p;
105
106 if (arg) {
107 (void) strcpy(space, arg);
108 p = space-1+strlen(space);
109 while ((p >= space) && (*p == '/'))
110 *p-- = '\0';
111 }
112 return (space);
113 }
114