xref: /onnv-gate/usr/src/lib/libc/port/stdio/tmpfile.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  *	tmpfile - return a pointer to an update file that can be
340Sstevel@tonic-gate  *		used for scratch. The file will automatically
350Sstevel@tonic-gate  *		go away if the program using it terminates.
360Sstevel@tonic-gate  */
370Sstevel@tonic-gate 
38*6812Sraf #include "lint.h"
390Sstevel@tonic-gate #include "mtlib.h"
400Sstevel@tonic-gate #include <sys/types.h>
410Sstevel@tonic-gate #include <unistd.h>
420Sstevel@tonic-gate #include <stdio.h>
430Sstevel@tonic-gate #include <string.h>
440Sstevel@tonic-gate #include <stdlib.h>
450Sstevel@tonic-gate #include <thread.h>
460Sstevel@tonic-gate #include <synch.h>
470Sstevel@tonic-gate #include <sys/stat.h>
480Sstevel@tonic-gate 
490Sstevel@tonic-gate static char seed[] = { 'a', 'a', 'a', '\0' };
500Sstevel@tonic-gate static mutex_t seed_lk = DEFAULTMUTEX;
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #define	XS "\bXXXXXX"		/* a '\b' character is prepended to this */
530Sstevel@tonic-gate 				/* string to avoid conflicts with names */
540Sstevel@tonic-gate 				/* generated by tmpnam() */
550Sstevel@tonic-gate /*ARGSUSED*/
560Sstevel@tonic-gate static FILE *
_common(boolean_t large_file)570Sstevel@tonic-gate _common(boolean_t large_file)
580Sstevel@tonic-gate {
590Sstevel@tonic-gate 	char	tfname[L_tmpnam];
600Sstevel@tonic-gate 	FILE	*p;
610Sstevel@tonic-gate 	char	*q;
620Sstevel@tonic-gate 	int	mkret;
630Sstevel@tonic-gate 	mode_t	current_umask;
640Sstevel@tonic-gate 
650Sstevel@tonic-gate 	(void) strcpy(tfname, P_tmpdir);
660Sstevel@tonic-gate 	lmutex_lock(&seed_lk);
670Sstevel@tonic-gate 	(void) strcat(tfname, seed);
680Sstevel@tonic-gate 	(void) strcat(tfname, XS);
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	q = seed;
710Sstevel@tonic-gate 	while (*q == 'z')
720Sstevel@tonic-gate 		*q++ = 'a';
730Sstevel@tonic-gate 	if (*q != '\0')
740Sstevel@tonic-gate 		++*q;
750Sstevel@tonic-gate 	lmutex_unlock(&seed_lk);
760Sstevel@tonic-gate 
770Sstevel@tonic-gate #if !defined(_LP64)
780Sstevel@tonic-gate 	if (large_file == B_TRUE) {
790Sstevel@tonic-gate 		if ((mkret = mkstemp64(tfname)) == -1)
800Sstevel@tonic-gate 			return (NULL);
810Sstevel@tonic-gate 	} else
820Sstevel@tonic-gate #endif
830Sstevel@tonic-gate 		if ((mkret = mkstemp(tfname)) == -1)
840Sstevel@tonic-gate 			return (NULL);
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	(void) unlink(tfname);
870Sstevel@tonic-gate 	current_umask = umask(0777);
880Sstevel@tonic-gate 	(void) umask(current_umask);
890Sstevel@tonic-gate 	(void) fchmod(mkret, 0666 & ~current_umask);
900Sstevel@tonic-gate 	if ((p = fdopen(mkret, "w+")) == NULL) {
910Sstevel@tonic-gate 		(void) close(mkret);
920Sstevel@tonic-gate 		return (NULL);
930Sstevel@tonic-gate 	}
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	return (p);
960Sstevel@tonic-gate }
970Sstevel@tonic-gate 
980Sstevel@tonic-gate #if !defined(_LP64)
990Sstevel@tonic-gate FILE *
tmpfile64(void)1000Sstevel@tonic-gate tmpfile64(void)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	return (_common(B_TRUE));
1030Sstevel@tonic-gate }
1040Sstevel@tonic-gate #endif	/* _LP64 */
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate /*
1070Sstevel@tonic-gate  * This is a bit confusing -- some explanation is in order.
1080Sstevel@tonic-gate  *
1090Sstevel@tonic-gate  * When we're compiled 64-bit, there's no point in distinguishing
1100Sstevel@tonic-gate  * a "large" file from a "small" file -- they're all "large".
1110Sstevel@tonic-gate  * The argument we pass to '_common' is ignored -- we always call
1120Sstevel@tonic-gate  * mkstemp which will just do the right thing for us.
1130Sstevel@tonic-gate  */
1140Sstevel@tonic-gate FILE *
tmpfile(void)1150Sstevel@tonic-gate tmpfile(void)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate 	return (_common(B_FALSE));
1180Sstevel@tonic-gate }
119