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