xref: /titanic_41/usr/src/lib/libc/port/gen/mkstemp.c (revision 70025d765b044c6d8594bb965a2247a61e991a99)
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  * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
27  * All Rights Reserved
28  *
29  * Portions of this source code were derived from Berkeley
30  * 4.3 BSD under license from the regents of the University of
31  * California.
32  */
33 
34 #pragma ident	"%Z%%M%	%I%	%E% SMI"
35 
36 #include <sys/feature_tests.h>
37 
38 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
39 #pragma weak mkstemp64 = _mkstemp64
40 #define	_mkstemp	_mkstemp64
41 #else
42 #pragma weak mkstemp = _mkstemp
43 #endif
44 
45 #include "synonyms.h"
46 #include <sys/fcntl.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include <errno.h>
50 #include <alloca.h>
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #include <fcntl.h>
54 
55 
56 int
57 _mkstemp(char *as)
58 {
59 	int	fd;
60 	char	*tstr, *str, *mkret;
61 
62 	if (as == NULL || *as == NULL)
63 		return (-1);
64 
65 	tstr = alloca(strlen(as) + 1);
66 	(void) strcpy(tstr, as);
67 
68 	str = tstr + (strlen(tstr) - 1);
69 
70 	/*
71 	 * The following for() loop is doing work.  mktemp() will generate
72 	 * a different name each time through the loop.  So if the first
73 	 * name is used then keep trying until you find a free filename.
74 	 */
75 
76 	for (; ; ) {
77 		if (*str == 'X') { /* If no trailing X's don't call mktemp. */
78 			mkret = mktemp(as);
79 			if (*mkret == '\0') {
80 				return (-1);
81 			}
82 		}
83 #if _FILE_OFFSET_BITS == 64
84 		if ((fd = open64(as, O_CREAT|O_EXCL|O_RDWR, 0600)) != -1) {
85 			return (fd);
86 		}
87 #else
88 		if ((fd = open(as, O_CREAT|O_EXCL|O_RDWR, 0600)) != -1) {
89 			return (fd);
90 		}
91 #endif  /* _FILE_OFFSET_BITS == 64 */
92 
93 		/*
94 		 * If the error condition is other than EEXIST or if the
95 		 * file exists and there are no X's in the string
96 		 * return -1.
97 		 */
98 
99 		if ((errno != EEXIST) || (*str != 'X')) {
100 			return (-1);
101 		}
102 		(void) strcpy(as, tstr);
103 	}
104 }
105