2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 1988, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/* Copyright (c) 1988 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A/*
2N/A * tmpfile - return a pointer to an update file that can be
2N/A * used for scratch. The file will automatically
2N/A * go away if the program using it terminates.
2N/A */
2N/A
2N/A#include "lint.h"
2N/A#include "mtlib.h"
2N/A#include <sys/types.h>
2N/A#include <paths.h>
2N/A#include <unistd.h>
2N/A#include <stdio.h>
2N/A#include <string.h>
2N/A#include <stdlib.h>
2N/A#include <thread.h>
2N/A#include <synch.h>
2N/A#include <sys/stat.h>
2N/A
2N/Astatic char seed[] = { 'a', 'a', 'a', '\0' };
2N/Astatic mutex_t seed_lk = DEFAULTMUTEX;
2N/A
2N/A#define XS "\bXXXXXX" /* a '\b' character is prepended to this */
2N/A /* string to avoid conflicts with names */
2N/A /* generated by tmpnam() */
2N/A/*ARGSUSED*/
2N/Astatic FILE *
2N/A_common(boolean_t large_file)
2N/A{
2N/A char tfname[L_tmpnam];
2N/A FILE *p;
2N/A char *q;
2N/A int mkret;
2N/A mode_t current_umask;
2N/A
2N/A /*
2N/A * Originally, the manual claimed that it always created a file in
2N/A * /var/tmp; but if it can't write to /var/tmp, it now tries /tmp.
2N/A * This is similar to tempnam(3C).
2N/A */
2N/A if (access(P_tmpdir, W_OK | X_OK) == 0)
2N/A (void) strcpy(tfname, P_tmpdir);
2N/A else
2N/A (void) strcpy(tfname, _PATH_TMP);
2N/A lmutex_lock(&seed_lk);
2N/A (void) strcat(tfname, seed);
2N/A (void) strcat(tfname, XS);
2N/A
2N/A q = seed;
2N/A while (*q == 'z')
2N/A *q++ = 'a';
2N/A if (*q != '\0')
2N/A ++*q;
2N/A lmutex_unlock(&seed_lk);
2N/A
2N/A#if !defined(_LP64)
2N/A if (large_file == B_TRUE) {
2N/A if ((mkret = mkstemp64(tfname)) == -1)
2N/A return (NULL);
2N/A } else
2N/A#endif
2N/A if ((mkret = mkstemp(tfname)) == -1)
2N/A return (NULL);
2N/A
2N/A (void) unlink(tfname);
2N/A current_umask = umask(0777);
2N/A (void) umask(current_umask);
2N/A (void) fchmod(mkret, 0666 & ~current_umask);
2N/A if ((p = fdopen(mkret, "w+")) == NULL) {
2N/A (void) close(mkret);
2N/A return (NULL);
2N/A }
2N/A
2N/A return (p);
2N/A}
2N/A
2N/A#if !defined(_LP64)
2N/AFILE *
2N/Atmpfile64(void)
2N/A{
2N/A return (_common(B_TRUE));
2N/A}
2N/A#endif /* _LP64 */
2N/A
2N/A/*
2N/A * This is a bit confusing -- some explanation is in order.
2N/A *
2N/A * When we're compiled 64-bit, there's no point in distinguishing
2N/A * a "large" file from a "small" file -- they're all "large".
2N/A * The argument we pass to '_common' is ignored -- we always call
2N/A * mkstemp which will just do the right thing for us.
2N/A */
2N/AFILE *
2N/Atmpfile(void)
2N/A{
2N/A return (_common(B_FALSE));
2N/A}