xref: /titanic_41/usr/src/lib/libc/port/gen/mkstemp.c (revision 685482d6dae8b0c167f468d6313a738ae17c592c)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
26  * All Rights Reserved
27  *
28  * Portions of this source code were derived from Berkeley
29  * 4.3 BSD under license from the regents of the University of
30  * California.
31  */
32 
33 #pragma ident	"%Z%%M%	%I%	%E% SMI"
34 
35 #include <sys/feature_tests.h>
36 
37 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
38 #pragma weak mkstemp64 = _mkstemp64
39 #pragma weak mkstemps64 = _mkstemps64
40 #define	_mkstemp	_mkstemp64
41 #define	_mkstemps	_mkstemps64
42 #define	libc_mkstemps	libc_mkstemps64		/* prefer unique statics */
43 #else
44 #pragma weak mkstemp = _mkstemp
45 #pragma weak mkstemps = _mkstemps
46 #endif
47 
48 #include "synonyms.h"
49 #include <sys/fcntl.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <errno.h>
53 #include <alloca.h>
54 #include <sys/types.h>
55 #include <sys/stat.h>
56 #include <fcntl.h>
57 
58 extern char *libc_mktemps(char *, int);
59 
60 static int
61 libc_mkstemps(char *as, int slen)
62 {
63 	int	fd;
64 	int	len;
65 	char	*tstr, *str, *mkret;
66 
67 	if (as == NULL || *as == NULL)
68 		return (-1);
69 
70 	len = (int)strlen(as);
71 	tstr = alloca(len + 1);
72 	(void) strcpy(tstr, as);
73 
74 	if (slen < 0 || slen >= len)
75 		return (-1);
76 
77 	str = tstr + (len - 1 - slen);
78 
79 	/*
80 	 * The following for() loop is doing work.  mktemp() will generate
81 	 * a different name each time through the loop.  So if the first
82 	 * name is used then keep trying until you find a free filename.
83 	 */
84 
85 	for (; ; ) {
86 		if (*str == 'X') { /* If no trailing X's don't call mktemp. */
87 			mkret = libc_mktemps(as, slen);
88 			if (*mkret == '\0') {
89 				return (-1);
90 			}
91 		}
92 #if _FILE_OFFSET_BITS == 64
93 		if ((fd = open64(as, O_CREAT|O_EXCL|O_RDWR, 0600)) != -1) {
94 			return (fd);
95 		}
96 #else
97 		if ((fd = open(as, O_CREAT|O_EXCL|O_RDWR, 0600)) != -1) {
98 			return (fd);
99 		}
100 #endif  /* _FILE_OFFSET_BITS == 64 */
101 
102 		/*
103 		 * If the error condition is other than EEXIST or if the
104 		 * file exists and there are no X's in the string
105 		 * return -1.
106 		 */
107 
108 		if ((errno != EEXIST) || (*str != 'X')) {
109 			return (-1);
110 		}
111 		(void) strcpy(as, tstr);
112 	}
113 }
114 
115 int
116 _mkstemp(char *as)
117 {
118 	return (libc_mkstemps(as, 0));
119 }
120 
121 int
122 _mkstemps(char *as, int slen)
123 {
124 	return (libc_mkstemps(as, slen));
125 }
126