import less(1)
[unleashed/tickless.git] / usr / src / lib / libc / port / stdio / tmpfile.c
blob884a7a369ab88f776b4e863f4912dc8ab772e135
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1988 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
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.
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>
49 static char seed[] = { 'a', 'a', 'a', '\0' };
50 static mutex_t seed_lk = DEFAULTMUTEX;
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)
59 char tfname[L_tmpnam];
60 FILE *p;
61 char *q;
62 int mkret;
63 mode_t current_umask;
65 (void) strcpy(tfname, P_tmpdir);
66 lmutex_lock(&seed_lk);
67 (void) strcat(tfname, seed);
68 (void) strcat(tfname, XS);
70 q = seed;
71 while (*q == 'z')
72 *q++ = 'a';
73 if (*q != '\0')
74 ++*q;
75 lmutex_unlock(&seed_lk);
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);
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);
95 return (p);
98 #if !defined(_LP64)
99 FILE *
100 tmpfile64(void)
102 return (_common(B_TRUE));
104 #endif /* _LP64 */
107 * This is a bit confusing -- some explanation is in order.
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.
114 FILE *
115 tmpfile(void)
117 return (_common(B_FALSE));