Cross Reference: tempnam.c
xref
: /
osnet-11
/
usr
/
src
/
lib
/
libc
/
port
/
stdio
/
tempnam.c
Home
History
Annotate
Line#
Navigate
Download
Search
only in
./
2
N/A
/*
2
N/A
* CDDL HEADER START
2
N/A
*
2
N/A
* The contents of this file are subject to the terms of the
2
N/A
* Common Development and Distribution License (the "License").
2
N/A
* You may not use this file except in compliance with the License.
2
N/A
*
2
N/A
* You can obtain a copy of the license at
usr
/
src
/
OPENSOLARIS.LICENSE
2
N/A
* or
http://www.opensolaris.org/os/licensing
.
2
N/A
* See the License for the specific language governing permissions
2
N/A
* and limitations under the License.
2
N/A
*
2
N/A
* When distributing Covered Code, include this CDDL HEADER in each
2
N/A
* file and include the License file at
usr
/
src
/
OPENSOLARIS.LICENSE
.
2
N/A
* If applicable, add the following below this CDDL HEADER, with the
2
N/A
* fields enclosed by brackets "[]" replaced with your own identifying
2
N/A
* information: Portions Copyright [yyyy] [name of copyright owner]
2
N/A
*
2
N/A
* CDDL HEADER END
2
N/A
*/
2
N/A
2
N/A
/*
2
N/A
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
2
N/A
* Use is subject to license terms.
2
N/A
*/
2
N/A
2
N/A
/* Copyright (c) 1988 AT&T */
2
N/A
/* All Rights Reserved */
2
N/A
2
N/A
#
pragma
ident
"%Z%%M% %I% %E% SMI"
2
N/A
2
N/A
#
pragma
weak
_tempnam
=
tempnam
2
N/A
2
N/A
#
include
"
lint.h
"
2
N/A
#
include
<
mtlib.h
>
2
N/A
#
include
<
sys
/
types.h
>
2
N/A
#
include
<
stdio.h
>
2
N/A
#
include
<
string.h
>
2
N/A
#
include
<
stdlib.h
>
2
N/A
#
include
<
thread.h
>
2
N/A
#
include
<
synch.h
>
2
N/A
#
include
<
unistd.h
>
2
N/A
#
include
<
sys
/
stat.h
>
2
N/A
2
N/A
#
define
max
(A, B) (((A) < (B))?(B):(A))
2
N/A
2
N/A
static
char
*
pcopy
(
char
*,
const
char
*);
2
N/A
2
N/A
static
char
seed
[] =
"AAA"
;
2
N/A
2
N/A
static
mutex_t
seed_lk
=
DEFAULTMUTEX
;
2
N/A
2
N/A
char
*
2
N/A
tempnam
(
const
char
*
dir
,
/* use this directory please (if non-NULL) */
2
N/A
const
char
*
pfx
)
/* use this (if non-NULL) as filename prefix */
2
N/A
{
2
N/A
char
*p, *q, *
tdir
;
2
N/A
size_t
x = 0, y = 0, z;
2
N/A
struct
stat64
statbuf
;
2
N/A
2
N/A
z =
sizeof
(
P_tmpdir
) -
1
;
2
N/A
if
((
tdir
=
getenv
(
"TMPDIR"
)) !=
NULL
) {
2
N/A
x =
strlen
(
tdir
);
2
N/A
}
2
N/A
if
(
dir
!=
NULL
) {
2
N/A
if
(
stat64
(
dir
, &
statbuf
) == 0 &&
S_ISDIR
(
statbuf
.
st_mode
))
2
N/A
y =
strlen
(
dir
);
2
N/A
}
2
N/A
if
((p =
malloc
(
max
(
max
(x, y), z)+
16
)) ==
NULL
)
2
N/A
return
(
NULL
);
2
N/A
if
(x > 0 &&
access
(
pcopy
(p,
tdir
), (
W_OK
|
X_OK
)) == 0)
2
N/A
goto
OK
;
2
N/A
if
(y > 0 &&
access
(
pcopy
(p,
dir
), (
W_OK
|
X_OK
)) == 0)
2
N/A
goto
OK
;
2
N/A
if
(
access
(
pcopy
(p,
P_tmpdir
), (
W_OK
|
X_OK
)) == 0)
2
N/A
goto
OK
;
2
N/A
if
(
access
(
pcopy
(p,
"/tmp"
), (
W_OK
|
X_OK
)) != 0) {
2
N/A
free
(p);
2
N/A
return
(
NULL
);
2
N/A
}
2
N/A
OK
:
2
N/A
(
void
)
strcat
(p,
"/"
);
2
N/A
if
(
pfx
) {
2
N/A
*(p+
strlen
(p)+
5
) =
'\0'
;
2
N/A
(
void
)
strncat
(p,
pfx
,
5
);
2
N/A
}
2
N/A
lmutex_lock
(&
seed_lk
);
2
N/A
(
void
)
strcat
(p,
seed
);
2
N/A
(
void
)
strcat
(p,
"XXXXXX"
);
2
N/A
q =
seed
;
2
N/A
while
(*q ==
'Z'
)
2
N/A
*q++ =
'A'
;
2
N/A
if
(*q !=
'\0'
)
2
N/A
++*q;
2
N/A
lmutex_unlock
(&
seed_lk
);
2
N/A
if
(*
mktemp
(p) ==
'\0'
) {
2
N/A
free
(p);
2
N/A
return
(
NULL
);
2
N/A
}
2
N/A
return
(p);
2
N/A
}
2
N/A
2
N/A
static
char
*
2
N/A
pcopy
(
char
*
space
,
const
char
*
arg
)
2
N/A
{
2
N/A
char
*p;
2
N/A
2
N/A
if
(
arg
) {
2
N/A
(
void
)
strcpy
(
space
,
arg
);
2
N/A
p =
space
-
1
+
strlen
(
space
);
2
N/A
while
((p >=
space
) && (*p ==
'/'
))
2
N/A
*p-- =
'\0'
;
2
N/A
}
2
N/A
return
(
space
);
2
N/A
}