1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5168404Spjd * Common Development and Distribution License, Version 1.0 only
6168404Spjd * (the "License").  You may not use this file except in compliance
7168404Spjd * with the License.
8168404Spjd *
9168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10168404Spjd * or http://www.opensolaris.org/os/licensing.
11168404Spjd * See the License for the specific language governing permissions
12168404Spjd * and limitations under the License.
13168404Spjd *
14168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
15168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16168404Spjd * If applicable, add the following below this CDDL HEADER, with the
17168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
18168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
19168404Spjd *
20168404Spjd * CDDL HEADER END
21168404Spjd */
22168404Spjd/*
23168404Spjd * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24168404Spjd * Use is subject to license terms.
25168404Spjd */
26168404Spjd
27168404Spjd#pragma ident	"%Z%%M%	%I%	%E% SMI"
28168404Spjd
29168404Spjd#include "libuutil_common.h"
30168404Spjd
31168404Spjd#include <sys/time.h>
32168404Spjd
33168404Spjd#include <errno.h>
34168404Spjd#include <fcntl.h>
35168404Spjd#include <limits.h>
36168404Spjd#include <stdio.h>
37168404Spjd#include <unistd.h>
38168404Spjd
39168404Spjd#ifdef _LP64
40168404Spjd#define	TMPPATHFMT	"%s/uu%ld"
41168404Spjd#else /* _LP64 */
42168404Spjd#define	TMPPATHFMT	"%s/uu%lld"
43168404Spjd#endif /* _LP64 */
44168404Spjd
45168404Spjd/*ARGSUSED*/
46168404Spjdint
47168404Spjduu_open_tmp(const char *dir, uint_t uflags)
48168404Spjd{
49168404Spjd	int f;
50168404Spjd	char *fname = uu_zalloc(PATH_MAX);
51168404Spjd
52168404Spjd	if (fname == NULL)
53168404Spjd		return (-1);
54168404Spjd
55168404Spjd	for (;;) {
56168404Spjd		(void) snprintf(fname, PATH_MAX, "%s/uu%lld", dir, gethrtime());
57168404Spjd
58168404Spjd		f = open(fname, O_CREAT | O_EXCL | O_RDWR, 0600);
59168404Spjd
60168404Spjd		if (f >= 0 || errno != EEXIST)
61168404Spjd			break;
62168404Spjd	}
63168404Spjd
64168404Spjd	if (f >= 0)
65168404Spjd		(void) unlink(fname);
66168404Spjd
67168404Spjd	uu_free(fname);
68168404Spjd
69168404Spjd	return (f);
70168404Spjd}
71