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 2008 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/* Copyright (c) 1988 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#pragma weak _tempnam = tempnam
2N/A
2N/A#include "lint.h"
2N/A#include <mtlib.h>
2N/A#include <sys/types.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 <unistd.h>
2N/A#include <sys/stat.h>
2N/A
2N/A#define max(A, B) (((A) < (B))?(B):(A))
2N/A
2N/Astatic char *pcopy(char *, const char *);
2N/A
2N/Astatic char seed[] = "AAA";
2N/A
2N/Astatic mutex_t seed_lk = DEFAULTMUTEX;
2N/A
2N/Achar *
2N/Atempnam(const char *dir, /* use this directory please (if non-NULL) */
2N/A const char *pfx) /* use this (if non-NULL) as filename prefix */
2N/A{
2N/A char *p, *q, *tdir;
2N/A size_t x = 0, y = 0, z;
2N/A struct stat64 statbuf;
2N/A
2N/A z = sizeof (P_tmpdir) - 1;
2N/A if ((tdir = getenv("TMPDIR")) != NULL) {
2N/A x = strlen(tdir);
2N/A }
2N/A if (dir != NULL) {
2N/A if (stat64(dir, &statbuf) == 0 && S_ISDIR(statbuf.st_mode))
2N/A y = strlen(dir);
2N/A }
2N/A if ((p = malloc(max(max(x, y), z)+16)) == NULL)
2N/A return (NULL);
2N/A if (x > 0 && access(pcopy(p, tdir), (W_OK | X_OK)) == 0)
2N/A goto OK;
2N/A if (y > 0 && access(pcopy(p, dir), (W_OK | X_OK)) == 0)
2N/A goto OK;
2N/A if (access(pcopy(p, P_tmpdir), (W_OK | X_OK)) == 0)
2N/A goto OK;
2N/A if (access(pcopy(p, "/tmp"), (W_OK | X_OK)) != 0) {
2N/A free(p);
2N/A return (NULL);
2N/A }
2N/AOK:
2N/A (void) strcat(p, "/");
2N/A if (pfx) {
2N/A *(p+strlen(p)+5) = '\0';
2N/A (void) strncat(p, pfx, 5);
2N/A }
2N/A lmutex_lock(&seed_lk);
2N/A (void) strcat(p, seed);
2N/A (void) strcat(p, "XXXXXX");
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 if (*mktemp(p) == '\0') {
2N/A free(p);
2N/A return (NULL);
2N/A }
2N/A return (p);
2N/A}
2N/A
2N/Astatic char *
2N/Apcopy(char *space, const char *arg)
2N/A{
2N/A char *p;
2N/A
2N/A if (arg) {
2N/A (void) strcpy(space, arg);
2N/A p = space-1+strlen(space);
2N/A while ((p >= space) && (*p == '/'))
2N/A *p-- = '\0';
2N/A }
2N/A return (space);
2N/A}