xref: /titanic_41/usr/src/lib/libc/port/stdio/tmpfile.c (revision 6185db853e024a486ff8837e6784dd290d866112)
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 /*
34  *	tmpfile - return a pointer to an update file that can be
35  *		used for scratch. The file will automatically
36  *		go away if the program using it terminates.
37  */
38 
39 #include "synonyms.h"
40 #include "mtlib.h"
41 #include <sys/types.h>
42 #include <unistd.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <stdlib.h>
46 #include <thread.h>
47 #include <synch.h>
48 #include <sys/stat.h>
49 
50 static char seed[] = { 'a', 'a', 'a', '\0' };
51 static mutex_t seed_lk = DEFAULTMUTEX;
52 
53 #define	XS "\bXXXXXX"		/* a '\b' character is prepended to this */
54 				/* string to avoid conflicts with names */
55 				/* generated by tmpnam() */
56 /*ARGSUSED*/
57 static FILE *
58 _common(boolean_t large_file)
59 {
60 	char	tfname[L_tmpnam];
61 	FILE	*p;
62 	char	*q;
63 	int	mkret;
64 	mode_t	current_umask;
65 
66 	(void) strcpy(tfname, P_tmpdir);
67 	lmutex_lock(&seed_lk);
68 	(void) strcat(tfname, seed);
69 	(void) strcat(tfname, XS);
70 
71 	q = seed;
72 	while (*q == 'z')
73 		*q++ = 'a';
74 	if (*q != '\0')
75 		++*q;
76 	lmutex_unlock(&seed_lk);
77 
78 #if !defined(_LP64)
79 	if (large_file == B_TRUE) {
80 		if ((mkret = mkstemp64(tfname)) == -1)
81 			return (NULL);
82 	} else
83 #endif
84 		if ((mkret = mkstemp(tfname)) == -1)
85 			return (NULL);
86 
87 	(void) unlink(tfname);
88 	current_umask = umask(0777);
89 	(void) umask(current_umask);
90 	(void) fchmod(mkret, 0666 & ~current_umask);
91 	if ((p = fdopen(mkret, "w+")) == NULL) {
92 		(void) close(mkret);
93 		return (NULL);
94 	}
95 
96 	return (p);
97 }
98 
99 #if !defined(_LP64)
100 FILE *
101 tmpfile64(void)
102 {
103 	return (_common(B_TRUE));
104 }
105 #endif	/* _LP64 */
106 
107 /*
108  * This is a bit confusing -- some explanation is in order.
109  *
110  * When we're compiled 64-bit, there's no point in distinguishing
111  * a "large" file from a "small" file -- they're all "large".
112  * The argument we pass to '_common' is ignored -- we always call
113  * mkstemp which will just do the right thing for us.
114  */
115 FILE *
116 tmpfile(void)
117 {
118 	return (_common(B_FALSE));
119 }
120