0
|
1 |
/*
|
|
2 |
* CDDL HEADER START
|
|
3 |
*
|
|
4 |
* The contents of this file are subject to the terms of the
|
1475
|
5 |
* Common Development and Distribution License (the "License").
|
|
6 |
* You may not use this file except in compliance with the License.
|
0
|
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 |
*/
|
6812
|
21 |
|
0
|
22 |
/*
|
6812
|
23 |
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
|
0
|
24 |
* Use is subject to license terms.
|
6812
|
25 |
*/
|
|
26 |
|
|
27 |
/*
|
0
|
28 |
* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
|
|
29 |
* All Rights Reserved
|
|
30 |
*
|
|
31 |
* Portions of this source code were derived from Berkeley
|
|
32 |
* 4.3 BSD under license from the regents of the University of
|
|
33 |
* California.
|
|
34 |
*/
|
|
35 |
|
|
36 |
#pragma ident "%Z%%M% %I% %E% SMI"
|
|
37 |
|
|
38 |
#include <sys/feature_tests.h>
|
|
39 |
|
|
40 |
#if !defined(_LP64) && _FILE_OFFSET_BITS == 64
|
6812
|
41 |
#define mkstemp mkstemp64
|
|
42 |
#define mkstemps mkstemps64
|
1475
|
43 |
#define libc_mkstemps libc_mkstemps64 /* prefer unique statics */
|
6812
|
44 |
#pragma weak _mkstemp64 = mkstemp64
|
0
|
45 |
#else
|
6812
|
46 |
#pragma weak _mkstemp = mkstemp
|
0
|
47 |
#endif
|
|
48 |
|
6812
|
49 |
#include "lint.h"
|
0
|
50 |
#include <sys/fcntl.h>
|
|
51 |
#include <stdlib.h>
|
|
52 |
#include <string.h>
|
|
53 |
#include <errno.h>
|
|
54 |
#include <alloca.h>
|
|
55 |
#include <sys/types.h>
|
|
56 |
#include <sys/stat.h>
|
|
57 |
#include <fcntl.h>
|
|
58 |
|
1475
|
59 |
extern char *libc_mktemps(char *, int);
|
0
|
60 |
|
1475
|
61 |
static int
|
|
62 |
libc_mkstemps(char *as, int slen)
|
0
|
63 |
{
|
|
64 |
int fd;
|
1475
|
65 |
int len;
|
0
|
66 |
char *tstr, *str, *mkret;
|
|
67 |
|
|
68 |
if (as == NULL || *as == NULL)
|
|
69 |
return (-1);
|
|
70 |
|
1475
|
71 |
len = (int)strlen(as);
|
|
72 |
tstr = alloca(len + 1);
|
0
|
73 |
(void) strcpy(tstr, as);
|
|
74 |
|
1475
|
75 |
if (slen < 0 || slen >= len)
|
|
76 |
return (-1);
|
|
77 |
|
|
78 |
str = tstr + (len - 1 - slen);
|
0
|
79 |
|
|
80 |
/*
|
|
81 |
* The following for() loop is doing work. mktemp() will generate
|
|
82 |
* a different name each time through the loop. So if the first
|
|
83 |
* name is used then keep trying until you find a free filename.
|
|
84 |
*/
|
|
85 |
|
6812
|
86 |
for (;;) {
|
0
|
87 |
if (*str == 'X') { /* If no trailing X's don't call mktemp. */
|
1475
|
88 |
mkret = libc_mktemps(as, slen);
|
0
|
89 |
if (*mkret == '\0') {
|
|
90 |
return (-1);
|
|
91 |
}
|
|
92 |
}
|
|
93 |
#if _FILE_OFFSET_BITS == 64
|
|
94 |
if ((fd = open64(as, O_CREAT|O_EXCL|O_RDWR, 0600)) != -1) {
|
|
95 |
return (fd);
|
|
96 |
}
|
|
97 |
#else
|
|
98 |
if ((fd = open(as, O_CREAT|O_EXCL|O_RDWR, 0600)) != -1) {
|
|
99 |
return (fd);
|
|
100 |
}
|
|
101 |
#endif /* _FILE_OFFSET_BITS == 64 */
|
|
102 |
|
|
103 |
/*
|
|
104 |
* If the error condition is other than EEXIST or if the
|
|
105 |
* file exists and there are no X's in the string
|
|
106 |
* return -1.
|
|
107 |
*/
|
|
108 |
|
|
109 |
if ((errno != EEXIST) || (*str != 'X')) {
|
|
110 |
return (-1);
|
|
111 |
}
|
|
112 |
(void) strcpy(as, tstr);
|
|
113 |
}
|
|
114 |
}
|
1475
|
115 |
|
|
116 |
int
|
6812
|
117 |
mkstemp(char *as)
|
1475
|
118 |
{
|
|
119 |
return (libc_mkstemps(as, 0));
|
|
120 |
}
|
|
121 |
|
|
122 |
int
|
6812
|
123 |
mkstemps(char *as, int slen)
|
1475
|
124 |
{
|
|
125 |
return (libc_mkstemps(as, slen));
|
|
126 |
}
|