summaryrefslogblamecommitdiffstats
path: root/private/crt32/stdio/tmpfile.c
blob: da6f15307a28e8d1ef1d98818e9fa07c3ea2e554 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418

































































































































































































































































































































































































































                                                                                
/***
*tmpfile.c - create unique file name or file
*
*	Copyright (c) 1985-1993, Microsoft Corporation. All rights reserved.
*
*Purpose:
*	defines tmpnam() and tmpfile().
*
*Revision History:
*	??-??-??  TC	initial version
*	04-17-86  JMB	tmpnam - brought semantics in line with System V
*			definition as follows: 1) if tmpnam paramter is NULL,
*			store name in static buffer (do NOT use malloc); (2)
*			use P_tmpdir as the directory prefix to the temp file
*			name (do NOT use current working directory)
*	05-26-87  JCR	fixed bug where tmpnam was modifying errno
*	08-10-87  JCR	Added code to support P_tmpdir with or without trailing
*			'\'.
*	11-09-87  JCR	Multi-thread support
*	12-11-87  JCR	Added "_LOAD_DS" to declaration
*	01-22-88  JCR	Added per thread static namebuf area (mthread bug fix)
*	05-27-88  PHG	Merged DLL/normal versions
*	11-14-88  GJF	_openfile() now takes a file sharing flag, also some
*			cleanup (now specific to the 386)
*	06-06-89  JCR	386 mthread support
*	11-28-89  JCR	Added check to _tmpnam so it can't loop forever
*	02-16-90  GJF	Fixed copyright and indents
*	03-19-90  GJF	Replaced _LOAD_DS with _CALLTYPE1, added #include
*			<cruntime.h> and removed #include <register.h>.
*	03-26-90  GJF	Added #include <io.h>.
*	10-03-90  GJF	New-style function declarators.
*	01-21-91  GJF	ANSI naming.
*	07-22-91  GJF	Multi-thread support for Win32 [_WIN32_].
*	03-17-92  GJF	Completely rewrote Win32 version.
*	03-27-92  DJM	POSIX support.
*	05-02-92  SRW	Use _O_TEMPORARY flag for tmpfile routine.
*	05-04-92  GJF	Force cinittmp.obj in for Win32.
*	08-26-92  GJF	Fixed POSIX build.
*	08-28-92  GJF	Oops, forgot about getpid...
*	11-06-92  GJF	Use '/' for POSIX, '\\' otherwise, as the path
*			separator. Also, backed out JHavens' bug fix of 6-14,
*			which was itself a bug (albeit a less serious one).
*	02-26-93  GJF	Put in per-thread buffers, purged Cruiser support.
*
*******************************************************************************/


#include <cruntime.h>
#ifdef	_POSIX_
#include <unistd.h>
#endif
#include <errno.h>
#include <process.h>
#include <fcntl.h>
#include <io.h>
#include <os2dll.h>
#include <share.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <file2.h>
#include <internal.h>

/*
 * Pointers to buffers used by tmpnam() and tmpfile() to build filenames.
 * Each is initialized on first use.
 */
#ifdef	MTHREAD

/*
 * The following macros ASSSUME that ptd is the name local _ptiddata variable
 * in each frame!
 */
#define namebuf0    (ptd->_namebuf0)
#define namebuf1    (ptd->_namebuf1)

#else	/* not MTHREAD */

static char *namebuf0 = NULL;		/* used by tmpnam()  */
static char *namebuf1 = NULL;		/* used by tmpfile() */

#endif	/* MTHREAD */

/*
 * Initializing function for namebuf0 and namebuf1.
 */
static int _CRTAPI3 init_namebuf(int, char **);

/*
 * Generator function that produces temporary filenames
 */
static int _CRTAPI3 genfname(char *);


/***
*char *tmpnam(char *s) - generate temp file name
*
*Purpose:
*	Creates a file name that is unique in the directory specified by
*	_P_tmpdir in stdio.h.  Places file name in string passed by user or
*	in static mem if pass NULL.
*
*Entry:
*	char *s - ptr to place to put temp name
*
*Exit:
*	returns pointer to constructed file name (s or address of static mem)
*	returns NULL if fails
*
*Exceptions:
*
*******************************************************************************/

char * _CRTAPI1 tmpnam (
	char *s
	)
{
#ifdef	MTHREAD
	_ptiddata ptd = _getptd();
#endif

	_mlock(_TMPNAM_LOCK);

	/*
	 * Initialize namebuf0, if needed. Otherwise, call genfname() to
	 * generate the next filename.
	 */
	if ( namebuf0 == NULL ) {
		if ( init_namebuf(0, &namebuf0) )
			goto tmpnam_err;
	}
	else if ( genfname(namebuf0) )
		goto tmpnam_err;

	/*
	 * Generate a filename that doesn't already exist.
	 */
	while ( access(namebuf0, 0) == 0 )
		if ( genfname(namebuf0) )
			goto tmpnam_err;

	/*
	 * Filename has successfully been generated.
	 */
	if ( s == NULL )
		s = namebuf0;
	else
		strcpy(s, namebuf0);

	_munlock(_TMPNAM_LOCK);
	return s;

	/*
	 * Error return path. All errors exit through here.
	 */
tmpnam_err:
	_munlock(_TMPNAM_LOCK);
	return NULL;
}


/***
*FILE *tmpfile() - create a temporary file
*
*Purpose:
*	Creates a temporary file with the file mode "w+b".  The file
*	will be automatically deleted when closed or the program terminates
*	normally.
*
*Entry:
*	None.
*
*Exit:
*	Returns stream pointer to opened file.
*	Returns NULL if fails
*
*Exceptions:
*
*******************************************************************************/

FILE * _CRTAPI1 tmpfile (
	void
	)
{
	FILE *stream;
	int fh;
#ifdef	MTHREAD
	_ptiddata ptd = _getptd();
#endif

	_mlock(_TMPNAM_LOCK);

	/*
	 * Initialize namebuf1, if needed. Otherwise, call genfname() to
	 * generate the next filename.
	 */
	if ( namebuf1 == NULL ) {
		if ( init_namebuf(1, &namebuf1) )
			goto tmpfile_err0;
	}
	else if ( genfname(namebuf1) )
		goto tmpfile_err0;

	/*
	 * Get a free stream.
	 *
	 * Note: In multi-thread models, the stream obtained below is locked!
	 */
	if ( (stream = _getstream()) == NULL )
		goto tmpfile_err0;

	/*
	 * Create a temporary file.
	 *
	 * Note: The loop below will only create a new file. It will NOT
	 * open and truncate an existing file. Either behavior is probably
	 * legal under ANSI (4.9.4.3 says tmpfile "creates" the file, but
	 * also says it is opened with mode "wb+"). However, the behavior
	 * implemented below is compatible with prior versions of MS-C and
	 * makes error checking easier.
	 */
#ifdef	_POSIX_
	while ( ((fh = open(namebuf1,
			     O_CREAT | O_EXCL | O_RDWR,
			     S_IRUSR | S_IWUSR
			     ))
	    == -1) && (errno == EEXIST) )
#else
	while ( ((fh = _sopen(namebuf1,
			      _O_CREAT | _O_EXCL | _O_RDWR | _O_BINARY |
				_O_TEMPORARY,
			      _SH_DENYNO,
			      _S_IREAD | _S_IWRITE
			     ))
	    == -1) && (errno == EEXIST) )
#endif
		if ( genfname(namebuf1) )
			break;

	/*
	 * Check that the loop above did indeed create a temporary
	 * file.
	 */
	if ( fh == -1 )
		goto tmpfile_err1;

	/*
	 * Initialize stream
	 */
	if ( (stream->_tmpfname = _strdup(namebuf1)) == NULL ) {
		/* close the file, then branch to error handling */
#ifdef	_POSIX_
		close(fh);
#else
		_close(fh);
#endif
		goto tmpfile_err1;
	}
	stream->_cnt = 0;
	stream->_base = stream->_ptr = NULL;
	stream->_flag = _commode | _IORW;
	stream->_file = fh;

	_unlock_str(_iob_index(stream));
	_munlock(_TMPNAM_LOCK);
	return stream;

	/*
	 * Error return. All errors paths branch to one of the two
	 * labels below.
	 */
tmpfile_err1:
	_unlock_str(_iob_index(stream));
tmpfile_err0:
	_munlock(_TMPNAM_LOCK);
	return NULL;
}


/***
*static int init_namebuf(flag, pnamebuf) - initializes the namebuf
*	pointers
*
*Purpose:
*	Called once each for namebuf0 and namebuf1, to initialize
*	them.
*
*Entry:
*	int flag	    - flag set to 0 if namebuf0 is to be initialized,
*			      non-zero (1) if namebuf1 is to be initialized.
*	char **pnamebuf     - pointer to namebuf0 iff flag is 0,
*			      pointer to namebuf1 iff flag is 1
*
*Exit:
*	Returns 0 if successful, nonzero (-1) otherwise.
*
*Exceptions:
*
*******************************************************************************/

static int _CRTAPI3 init_namebuf(
	int flag,
	char **pnamebuf
	)
{
	char *p, *q;

	/*
	 * Allocate space to hold the filename stem
	 */
	if ( (p = malloc(L_tmpnam)) == NULL )
		return -1;

	/*
	 * Put in the path prefix. Make sure it ends with a slash or
	 * backslash character.
	 */
	strcpy(p, _P_tmpdir);
	q = p + sizeof(_P_tmpdir) - 1;	    /* same as p + strlen(p) */

#ifdef _POSIX_
	if  ( *(q - 1) != '/' )
		*(q++) = '/';
#else
	if  ( (*(q - 1) != '\\') && (*(q - 1) != '/') )
		*(q++) = '\\';
#endif

	/*
	 * Append the leading character of the filename.
	 */
	if ( flag )
		/* for tmpfile() */
		*(q++) = 't';
	else
		/* for tmpnam() */
		*(q++) = 's';

	/*
	 * Append the process id, encoded in base 32. Note this makes
	 * p back into a string again (i.e., terminated by a '\0').
	 */
#ifdef	_POSIX_
	_ultoa((unsigned long)getpid(), q, 32);
#else
	_ultoa((unsigned long)_getpid(), q, 32);
#endif
	strcat(p, ".");

	*pnamebuf = p;

	return(0);
}


/***
*static int genfname(char *fname) -
*
*Purpose:
*
*Entry:
*
*Exit:
*
*Exceptions:
*
*******************************************************************************/

static int _CRTAPI3 genfname (
	char *fname
	)
{
	char *p;
	char pext[4];
	unsigned long extnum;

	p = strrchr(fname, '.');

	p++;

	if ( (extnum = strtoul(p, NULL, 32) + 1) >= (unsigned long)TMP_MAX )
		return -1;

	strcpy(p, _ultoa(extnum, pext, 32));

	return 0;
}


/***
*void __inc_tmpoff(void) - force external reference for _tmpoff
*
*Purpose:
*	Forces an external reference to be generate for _tmpoff, which is
*	is defined in cinittmp.obj. This has the forces cinittmp.obj to be
*	pulled in, making a call to rmtmp part of the termination.
*
*	Yes, yes, I KNOW this is a hack...but it's quick and it works.
*
*Entry:
*
*Exit:
*
*Exceptions:
*
*******************************************************************************/


extern int _tmpoff;

void __inc_tmpoff(
	void
	)
{
	_tmpoff++;
}