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