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
32*6812Sraf #pragma weak _tempnam = tempnam
330Sstevel@tonic-gate
34*6812Sraf #include "lint.h"
350Sstevel@tonic-gate #include <mtlib.h>
360Sstevel@tonic-gate #include <sys/types.h>
370Sstevel@tonic-gate #include <stdio.h>
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate #include <stdlib.h>
400Sstevel@tonic-gate #include <thread.h>
410Sstevel@tonic-gate #include <synch.h>
420Sstevel@tonic-gate #include <unistd.h>
430Sstevel@tonic-gate #include <sys/stat.h>
440Sstevel@tonic-gate
450Sstevel@tonic-gate #define max(A, B) (((A) < (B))?(B):(A))
460Sstevel@tonic-gate
470Sstevel@tonic-gate static char *pcopy(char *, const char *);
480Sstevel@tonic-gate
490Sstevel@tonic-gate static char seed[] = "AAA";
500Sstevel@tonic-gate
510Sstevel@tonic-gate static mutex_t seed_lk = DEFAULTMUTEX;
520Sstevel@tonic-gate
530Sstevel@tonic-gate char *
tempnam(const char * dir,const char * pfx)540Sstevel@tonic-gate tempnam(const char *dir, /* use this directory please (if non-NULL) */
550Sstevel@tonic-gate const char *pfx) /* use this (if non-NULL) as filename prefix */
560Sstevel@tonic-gate {
570Sstevel@tonic-gate char *p, *q, *tdir;
580Sstevel@tonic-gate size_t x = 0, y = 0, z;
590Sstevel@tonic-gate struct stat64 statbuf;
600Sstevel@tonic-gate
610Sstevel@tonic-gate z = sizeof (P_tmpdir) - 1;
620Sstevel@tonic-gate if ((tdir = getenv("TMPDIR")) != NULL) {
630Sstevel@tonic-gate x = strlen(tdir);
640Sstevel@tonic-gate }
650Sstevel@tonic-gate if (dir != NULL) {
660Sstevel@tonic-gate if (stat64(dir, &statbuf) == 0 && S_ISDIR(statbuf.st_mode))
670Sstevel@tonic-gate y = strlen(dir);
680Sstevel@tonic-gate }
690Sstevel@tonic-gate if ((p = malloc(max(max(x, y), z)+16)) == NULL)
700Sstevel@tonic-gate return (NULL);
710Sstevel@tonic-gate if (x > 0 && access(pcopy(p, tdir), (W_OK | X_OK)) == 0)
720Sstevel@tonic-gate goto OK;
730Sstevel@tonic-gate if (y > 0 && access(pcopy(p, dir), (W_OK | X_OK)) == 0)
740Sstevel@tonic-gate goto OK;
750Sstevel@tonic-gate if (access(pcopy(p, P_tmpdir), (W_OK | X_OK)) == 0)
760Sstevel@tonic-gate goto OK;
770Sstevel@tonic-gate if (access(pcopy(p, "/tmp"), (W_OK | X_OK)) != 0) {
780Sstevel@tonic-gate free(p);
790Sstevel@tonic-gate return (NULL);
800Sstevel@tonic-gate }
810Sstevel@tonic-gate OK:
820Sstevel@tonic-gate (void) strcat(p, "/");
830Sstevel@tonic-gate if (pfx) {
840Sstevel@tonic-gate *(p+strlen(p)+5) = '\0';
850Sstevel@tonic-gate (void) strncat(p, pfx, 5);
860Sstevel@tonic-gate }
870Sstevel@tonic-gate lmutex_lock(&seed_lk);
880Sstevel@tonic-gate (void) strcat(p, seed);
890Sstevel@tonic-gate (void) strcat(p, "XXXXXX");
900Sstevel@tonic-gate q = seed;
910Sstevel@tonic-gate while (*q == 'Z')
920Sstevel@tonic-gate *q++ = 'A';
930Sstevel@tonic-gate if (*q != '\0')
940Sstevel@tonic-gate ++*q;
950Sstevel@tonic-gate lmutex_unlock(&seed_lk);
960Sstevel@tonic-gate if (*mktemp(p) == '\0') {
970Sstevel@tonic-gate free(p);
980Sstevel@tonic-gate return (NULL);
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate return (p);
1010Sstevel@tonic-gate }
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate static char *
pcopy(char * space,const char * arg)1040Sstevel@tonic-gate pcopy(char *space, const char *arg)
1050Sstevel@tonic-gate {
1060Sstevel@tonic-gate char *p;
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate if (arg) {
1090Sstevel@tonic-gate (void) strcpy(space, arg);
1100Sstevel@tonic-gate p = space-1+strlen(space);
1110Sstevel@tonic-gate while ((p >= space) && (*p == '/'))
1120Sstevel@tonic-gate *p-- = '\0';
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate return (space);
1150Sstevel@tonic-gate }
116