1 /*
2 Copyright (c) 1990 The 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 the above copyright notice and this paragraph are
7 duplicated in all such forms and that any documentation,
8 and/or other materials related to such
9 distribution and use acknowledge that the software was developed
10 by the University of California, Berkeley.  The name of the
11 University may not be used to endorse or promote products derived
12 from this software without specific prior written permission.
13 THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 /*
18 FUNCTION
19 <<tmpfile64>>---create a large temporary file
20 
21 INDEX
22 	tmpfile64
23 INDEX
24 	_tmpfile64_r
25 
26 SYNOPSIS
27 	#include <stdio.h>
28 	FILE *tmpfile64(void);
29 
30 	FILE *_tmpfile64_r(void *<[reent]>);
31 
32 DESCRIPTION
33 Create a large temporary file (a file which will be deleted automatically),
34 using a name generated by <<tmpnam>>.  The temporary file is opened with
35 the mode <<"wb+">>, permitting you to read and write anywhere in it
36 as a binary file (without any data transformations the host system may
37 perform for text files).  The file may be larger than 2GB.
38 
39 The alternate function <<_tmpfile64_r>> is a reentrant version.  The
40 argument <[reent]> is a pointer to a reentrancy structure.
41 
42 Both <<tmpfile64>> and <<_tmpfile64_r>> are only defined if __LARGE64_FILES
43 is defined.
44 
45 RETURNS
46 <<tmpfile64>> normally returns a pointer to the temporary file.  If no
47 temporary file could be created, the result is NULL, and <<errno>>
48 records the reason for failure.
49 
50 PORTABILITY
51 <<tmpfile64>> is a glibc extension.
52 
53 Supporting OS subroutines required: <<close>>, <<fstat>>, <<getpid>>,
54 <<isatty>>, <<lseek64>>, <<open64>>, <<read>>, <<sbrk>>, <<write>>.
55 
56 <<tmpfile64>> also requires the global pointer <<environ>>.
57 */
58 
59 #include <stdio.h>
60 #include <errno.h>
61 #include <fcntl.h>
62 #include <unistd.h>
63 #include <sys/stat.h>
64 
65 #ifndef O_BINARY
66 # define O_BINARY 0
67 #endif
68 
69 #ifdef __LARGE64_FILES
70 
71 FILE *
tmpfile64(void)72 tmpfile64 (void)
73 {
74   FILE *fp;
75   int e;
76   char *f;
77   char buf[L_tmpnam];
78   int fd;
79 
80   do
81   {
82      if ((f = tmpnam (buf)) == NULL)
83 	return NULL;
84       fd = open64 (f, O_RDWR | O_CREAT | O_EXCL | O_BINARY,
85 		      S_IRUSR | S_IWUSR);
86   }
87   while (fd < 0 && _REENT_ERRNO(ptr) == EEXIST);
88   if (fd < 0)
89     return NULL;
90   fp = fdopen64 (fd, "wb+");
91   e = _REENT_ERRNO(ptr);
92   if (!fp)
93     close (fd);
94   (void) remove (f);
95   _REENT_ERRNO(ptr) = e;
96   return fp;
97 }
98 
99 #endif /* __LARGE64_FILES */
100