17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57257d1braf * Common Development and Distribution License (the "License").
67257d1braf * You may not use this file except in compliance with the License.
77c478bdstevel@tonic-gate *
87c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bdstevel@tonic-gate * See the License for the specific language governing permissions
117c478bdstevel@tonic-gate * and limitations under the License.
127c478bdstevel@tonic-gate *
137c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bdstevel@tonic-gate *
197c478bdstevel@tonic-gate * CDDL HEADER END
207c478bdstevel@tonic-gate */
217257d1braf
227c478bdstevel@tonic-gate/*
237257d1braf * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bdstevel@tonic-gate * Use is subject to license terms.
257c478bdstevel@tonic-gate */
267c478bdstevel@tonic-gate
277c478bdstevel@tonic-gate/*	Copyright (c) 1988 AT&T	*/
287c478bdstevel@tonic-gate/*	  All Rights Reserved  	*/
297c478bdstevel@tonic-gate
307257d1braf#pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bdstevel@tonic-gate
327257d1braf#pragma weak _tempnam = tempnam
337c478bdstevel@tonic-gate
347257d1braf#include "lint.h"
357c478bdstevel@tonic-gate#include <mtlib.h>
367c478bdstevel@tonic-gate#include <sys/types.h>
377c478bdstevel@tonic-gate#include <stdio.h>
387c478bdstevel@tonic-gate#include <string.h>
397c478bdstevel@tonic-gate#include <stdlib.h>
407c478bdstevel@tonic-gate#include <thread.h>
417c478bdstevel@tonic-gate#include <synch.h>
427c478bdstevel@tonic-gate#include <unistd.h>
437c478bdstevel@tonic-gate#include <sys/stat.h>
447c478bdstevel@tonic-gate
457c478bdstevel@tonic-gate#define	max(A, B) (((A) < (B))?(B):(A))
467c478bdstevel@tonic-gate
477c478bdstevel@tonic-gatestatic char *pcopy(char *, const char *);
487c478bdstevel@tonic-gate
497c478bdstevel@tonic-gatestatic char seed[] = "AAA";
507c478bdstevel@tonic-gate
517c478bdstevel@tonic-gatestatic mutex_t seed_lk = DEFAULTMUTEX;
527c478bdstevel@tonic-gate
537c478bdstevel@tonic-gatechar *
547c478bdstevel@tonic-gatetempnam(const char *dir,	/* use this directory please (if non-NULL) */
557c478bdstevel@tonic-gate	const char *pfx)	/* use this (if non-NULL) as filename prefix */
567c478bdstevel@tonic-gate{
577c478bdstevel@tonic-gate	char *p, *q, *tdir;
587c478bdstevel@tonic-gate	size_t x = 0, y = 0, z;
597c478bdstevel@tonic-gate	struct stat64 statbuf;
607c478bdstevel@tonic-gate
617c478bdstevel@tonic-gate	z = sizeof (P_tmpdir) - 1;
627c478bdstevel@tonic-gate	if ((tdir = getenv("TMPDIR")) != NULL) {
637c478bdstevel@tonic-gate		x = strlen(tdir);
647c478bdstevel@tonic-gate	}
657c478bdstevel@tonic-gate	if (dir != NULL) {
667c478bdstevel@tonic-gate		if (stat64(dir, &statbuf) == 0 && S_ISDIR(statbuf.st_mode))
677c478bdstevel@tonic-gate			y = strlen(dir);
687c478bdstevel@tonic-gate	}
697c478bdstevel@tonic-gate	if ((p = malloc(max(max(x, y), z)+16)) == NULL)
707c478bdstevel@tonic-gate		return (NULL);
717c478bdstevel@tonic-gate	if (x > 0 && access(pcopy(p, tdir), (W_OK | X_OK)) == 0)
727c478bdstevel@tonic-gate		goto OK;
737c478bdstevel@tonic-gate	if (y > 0 && access(pcopy(p, dir), (W_OK | X_OK)) == 0)
747c478bdstevel@tonic-gate		goto OK;
757c478bdstevel@tonic-gate	if (access(pcopy(p, P_tmpdir), (W_OK | X_OK)) == 0)
767c478bdstevel@tonic-gate		goto OK;
777c478bdstevel@tonic-gate	if (access(pcopy(p, "/tmp"), (W_OK | X_OK)) != 0) {
787c478bdstevel@tonic-gate		free(p);
797c478bdstevel@tonic-gate		return (NULL);
807c478bdstevel@tonic-gate	}
817c478bdstevel@tonic-gateOK:
827c478bdstevel@tonic-gate	(void) strcat(p, "/");
837c478bdstevel@tonic-gate	if (pfx) {
847c478bdstevel@tonic-gate		*(p+strlen(p)+5) = '\0';
857c478bdstevel@tonic-gate		(void) strncat(p, pfx, 5);
867c478bdstevel@tonic-gate	}
877c478bdstevel@tonic-gate	lmutex_lock(&seed_lk);
887c478bdstevel@tonic-gate	(void) strcat(p, seed);
897c478bdstevel@tonic-gate	(void) strcat(p, "XXXXXX");
907c478bdstevel@tonic-gate	q = seed;
917c478bdstevel@tonic-gate	while (*q == 'Z')
927c478bdstevel@tonic-gate		*q++ = 'A';
937c478bdstevel@tonic-gate	if (*q != '\0')
947c478bdstevel@tonic-gate		++*q;
957c478bdstevel@tonic-gate	lmutex_unlock(&seed_lk);
967c478bdstevel@tonic-gate	if (*mktemp(p) == '\0') {
977c478bdstevel@tonic-gate		free(p);
987c478bdstevel@tonic-gate		return (NULL);
997c478bdstevel@tonic-gate	}
1007c478bdstevel@tonic-gate	return (p);
1017c478bdstevel@tonic-gate}
1027c478bdstevel@tonic-gate
1037c478bdstevel@tonic-gatestatic char *
1047c478bdstevel@tonic-gatepcopy(char *space, const char *arg)
1057c478bdstevel@tonic-gate{
1067c478bdstevel@tonic-gate	char *p;
1077c478bdstevel@tonic-gate
1087c478bdstevel@tonic-gate	if (arg) {
1097c478bdstevel@tonic-gate		(void) strcpy(space, arg);
1107c478bdstevel@tonic-gate		p = space-1+strlen(space);
1117c478bdstevel@tonic-gate		while ((p >= space) && (*p == '/'))
1127c478bdstevel@tonic-gate			*p-- = '\0';
1137c478bdstevel@tonic-gate	}
1147c478bdstevel@tonic-gate	return (space);
1157c478bdstevel@tonic-gate}
116