1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include "libuutil_common.h"
28 
29 #include <sys/time.h>
30 
31 #include <errno.h>
32 #include <fcntl.h>
33 #include <limits.h>
34 #include <stdio.h>
35 #include <unistd.h>
36 
37 #ifdef _LP64
38 #define	TMPPATHFMT	"%s/uu%ld"
39 #else /* _LP64 */
40 #define	TMPPATHFMT	"%s/uu%lld"
41 #endif /* _LP64 */
42 
43 /*ARGSUSED*/
44 int
uu_open_tmp(const char * dir,uint_t uflags)45 uu_open_tmp(const char *dir, uint_t uflags)
46 {
47 	int f;
48 	char *fname = uu_zalloc(PATH_MAX);
49 
50 	if (fname == NULL)
51 		return (-1);
52 
53 	for (;;) {
54 		(void) snprintf(fname, PATH_MAX, "%s/uu%lld", dir, gethrtime());
55 
56 		f = open(fname, O_CREAT | O_EXCL | O_RDWR, 0600);
57 
58 		if (f >= 0 || errno != EEXIST)
59 			break;
60 	}
61 
62 	if (f >= 0)
63 		(void) unlink(fname);
64 
65 	uu_free(fname);
66 
67 	return (f);
68 }
69