xref: /titanic_41/usr/src/lib/libc/port/gen/mkstemp.c (revision 1402f64f9efa57c2c2cba776f3f45c67e6055deb)
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 (c) 2013 OmniTI Computer Consulting, Inc. All rights reserved. */
23 
24 /*
25  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
26  * Use is subject to license terms.
27  */
28 
29 /*
30  * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
31  * All Rights Reserved
32  *
33  * Portions of this source code were derived from Berkeley
34  * 4.3 BSD under license from the regents of the University of
35  * California.
36  */
37 
38 #include <sys/feature_tests.h>
39 
40 #if !defined(_LP64) && _FILE_OFFSET_BITS == 64
41 #define	mkstemp		mkstemp64
42 #define	mkstemps	mkstemps64
43 #define	mkostemp	mkostemp64
44 #define	mkostemps	mkostemps64
45 #define	libc_mkstemps	libc_mkstemps64		/* prefer unique statics */
46 #pragma weak _mkstemp64 = mkstemp64
47 #else
48 #pragma weak _mkstemp = mkstemp
49 #endif
50 
51 #include "lint.h"
52 #include <sys/fcntl.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <errno.h>
56 #include <alloca.h>
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 #include <fcntl.h>
60 
61 extern char *libc_mktemps(char *, int);
62 
63 static int
64 libc_mkstemps(char *as, int slen, int flags)
65 {
66 	int	fd;
67 	int	len;
68 	char	*tstr, *str, *mkret;
69 
70 	if (as == NULL || *as == NULL)
71 		return (-1);
72 
73 	len = (int)strlen(as);
74 	tstr = alloca(len + 1);
75 	(void) strcpy(tstr, as);
76 
77 	if (slen < 0 || slen >= len)
78 		return (-1);
79 
80 	str = tstr + (len - 1 - slen);
81 
82 	/*
83 	 * The following for() loop is doing work.  mktemp() will generate
84 	 * a different name each time through the loop.  So if the first
85 	 * name is used then keep trying until you find a free filename.
86 	 */
87 
88 	for (;;) {
89 		if (*str == 'X') { /* If no trailing X's don't call mktemp. */
90 			mkret = libc_mktemps(as, slen);
91 			if (*mkret == '\0') {
92 				return (-1);
93 			}
94 		}
95 #if _FILE_OFFSET_BITS == 64
96 		if ((fd = open64(as, O_CREAT|O_EXCL|O_RDWR|flags,
97 		    0600)) != -1) {
98 			return (fd);
99 		}
100 #else
101 		if ((fd = open(as, O_CREAT|O_EXCL|O_RDWR|flags,
102 		    0600)) != -1) {
103 			return (fd);
104 		}
105 #endif  /* _FILE_OFFSET_BITS == 64 */
106 
107 		/*
108 		 * If the error condition is other than EEXIST or if the
109 		 * file exists and there are no X's in the string
110 		 * return -1.
111 		 */
112 
113 		if ((errno != EEXIST) || (*str != 'X')) {
114 			return (-1);
115 		}
116 		(void) strcpy(as, tstr);
117 	}
118 }
119 
120 int
121 mkstemp(char *as)
122 {
123 	return (libc_mkstemps(as, 0, 0));
124 }
125 
126 int
127 mkstemps(char *as, int slen)
128 {
129 	return (libc_mkstemps(as, slen, 0));
130 }
131 
132 int
133 mkostemp(char *as, int flags)
134 {
135 	return (libc_mkstemps(as, 0, flags));
136 }
137 
138 int
139 mkostemps(char *as, int slen, int flags)
140 {
141 	return (libc_mkstemps(as, slen, flags));
142 }
143