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