1 /*
2 * Copyright (c) 1987 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that: (1) source distributions retain this entire copyright
7 * notice and comment, and (2) distributions including binaries display
8 * the following acknowledgement: ``This product includes software
9 * developed by the University of California, Berkeley and its contributors''
10 * in the documentation or other materials provided with the distribution.
11 * Neither the name of the University nor the names of its
12 * contributors may be used to endorse or promote products derived
13 * from this software without specific prior written permission.
14 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17 */
18 /* This is file MKTEMP.C */
19 /* This file may have been modified by DJ Delorie (Jan 1991). If so,
20 ** these modifications are Copyright (C) 1991 DJ Delorie.
21 */
22
23 /*
24 FUNCTION
25 <<mktemp>>, <<mkstemp>>, <<mkstemps>>,
26 <<mkostemps>>---generate unused file name
27
28 INDEX
29 mktemp
30 INDEX
31 mkstemp
32 INDEX
33 mkstemps
34 INDEX
35 mkostemps
36
37 SYNOPSIS
38 #include <stdlib.h>
39 char *mktemp(char *<[path]>);
40 int mkstemp(char *<[path]>);
41 int mkstemps(char *<[path]>, int suffixlen);
42 int mkostemps(char *<[path]>, int suffixlen, int flags);
43
44 DESCRIPTION
45 <<mktemp>>, <<mkstemp>>, <<mkstemps>>, and <<mkostemps>> attempt to
46 generate a file name that is not yet in use for any existing file.
47 <<mkstemp>>, <<mkstemps>> and <mkostemps>> create the file and open it
48 for reading and writing; <<mktemp>> simply generates the file name
49 (making <<mktemp>> a security risk). <<mkostemps>> allow the addition
50 of other <<open>> flags, such as <<O_CLOEXEC>>, <<O_APPEND>>, or
51 <<O_SYNC>>. On platforms with a separate text mode, <<mkstemp>>
52 forces <<O_BINARY>>, while <<mkostemp>> allows the choice between
53 <<O_BINARY>>, <<O_TEXT>>, or 0 for default.
54
55 You supply a simple pattern for the generated file name, as the string
56 at <[path]>. The pattern should be a valid filename (including path
57 information if you wish) ending with at least six `<<X>>' characters.
58 The generated filename will match the leading part of the name you
59 supply, with the trailing `<<X>>' characters replaced by some
60 combination of digits and letters. With <<mkstemps>> and
61 <mkostemps>>, the `<<X>>' characters end <[suffixlen]> bytes before
62 the end of the string.
63
64 RETURNS
65 <<mktemp>> returns the pointer <[path]> to the modified string
66 representing an unused filename, unless it could not generate one, or
67 the pattern you provided is not suitable for a filename; in that case,
68 it returns <<NULL>>. Be aware that there is an inherent race between
69 generating the name and attempting to create a file by that name;
70 you are advised to use <<O_EXCL|O_CREAT>>.
71
72 <<mkstemp>>, <<mkstemps>> and <<mkostemps>> return a file descriptor
73 to the newly created file, unless it could not generate an unused
74 filename, or the pattern you provided is not suitable for a filename;
75 in that case, it returns <<-1>>.
76
77 NOTES
78 Never use <<mktemp>>. The generated filenames are easy to guess and
79 there's a race between the test if the file exists and the creation
80 of the file. In combination this makes <<mktemp>> prone to attacks
81 and using it is a security risk. Whenever possible use <<mkstemp>>
82 instead. It doesn't suffer the race condition.
83
84 PORTABILITY
85 ANSI C does not require either <<mktemp>> or <<mkstemp>>; the System V
86 Interface Definition requires <<mktemp>> as of Issue 2. POSIX 2001
87 requires <<mkstemp>> while deprecating <<mktemp>>. <<mkstemps>>, and
88 <<mkostemps>> are not standardized.
89
90 Supporting OS subroutines required: <<open>>
91 */
92
93 #include "stdio_private.h"
94
95 static int
_gettemp(char * path,int suffixlen,int * doopen,int flags)96 _gettemp (char *path,
97 int suffixlen,
98 int *doopen,
99 int flags)
100 {
101 char *start, *trv;
102 char *end;
103
104 end = path + strlen(path) - suffixlen;
105 trv = end;
106
107 /* Replace 'X' with 'a' */
108 while (path < trv && *--trv == 'X')
109 *trv = 'a';
110
111 /* Make sure we got six Xs */
112 if (end - trv < 6)
113 {
114 errno = EINVAL;
115 return 0;
116 }
117
118 start = trv + 1;
119
120 for (;;)
121 {
122 /*
123 * Use open to check if the file exists to avoid depending on
124 * stat or access. Don't rely on O_EXCL working, although if it
125 * doesn't, this introduces a race condition
126 */
127 int fd = open(path, O_RDONLY);
128 if (fd < 0) {
129 if (errno != EACCES) {
130 if (errno != ENOENT)
131 return 0;
132 if (doopen)
133 {
134 fd = open (path, flags | O_CREAT | O_EXCL | O_RDWR,
135 0600);
136 if (fd >= 0) {
137 *doopen = fd;
138 return 1;
139 }
140 if (errno != EEXIST)
141 return 0;
142 } else {
143 return 1;
144 }
145 }
146 } else
147 close(fd);
148
149 /* Increment the string of letters to generate another name */
150 trv = start;
151 for(;;)
152 {
153 if (trv == end)
154 return 0;
155 if (*trv == 'z')
156 *trv++ = 'a';
157 else {
158 ++ * trv;
159 break;
160 }
161 }
162 }
163 /*NOTREACHED*/
164 }
165
166 int
mkstemp(char * template)167 mkstemp (char *template)
168 {
169 int fd;
170
171 return (_gettemp (template, 0, &fd, 0) ? fd : -1);
172 }
173
174 char *
mktemp(char * template)175 mktemp (char *template)
176 {
177 return (_gettemp (template, 0, (int *) NULL, 0) ? template : (char *) NULL);
178 }
179
180 #ifndef O_BINARY
181 #define O_BINARY 0
182 #endif
183 #ifndef O_TEXT
184 #define O_TEXT 0
185 #endif
186
187 int
mkstemps(char * template,int suffixlen)188 mkstemps(char *template, int suffixlen)
189 {
190 int fd;
191
192 return (_gettemp (template, suffixlen, &fd, O_BINARY) ? fd : -1);
193 }
194
195 int
mkostemps(char * template,int suffixlen,int flags)196 mkostemps(char *template, int suffixlen, int flags)
197 {
198 int fd;
199
200 flags &= (O_APPEND | O_CLOEXEC | O_SYNC | O_BINARY | O_TEXT);
201 return (_gettemp (template, suffixlen, &fd, flags) ? fd : -1);
202 }
203