Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdio64 / tmpfile64.c
blob35b035c91576c16a725dafb498fd35d43588e211
1 /*
2 FUNCTION
3 <<tmpfile64>>---create a large temporary file
5 INDEX
6 tmpfile64
7 INDEX
8 _tmpfile64_r
10 SYNOPSIS
11 #include <stdio.h>
12 FILE *tmpfile64(void);
14 FILE *_tmpfile64_r(void *<[reent]>);
16 DESCRIPTION
17 Create a large temporary file (a file which will be deleted automatically),
18 using a name generated by <<tmpnam>>. The temporary file is opened with
19 the mode <<"wb+">>, permitting you to read and write anywhere in it
20 as a binary file (without any data transformations the host system may
21 perform for text files). The file may be larger than 2GB.
23 The alternate function <<_tmpfile64_r>> is a reentrant version. The
24 argument <[reent]> is a pointer to a reentrancy structure.
26 Both <<tmpfile64>> and <<_tmpfile64_r>> are only defined if __LARGE64_FILES
27 is defined.
29 RETURNS
30 <<tmpfile64>> normally returns a pointer to the temporary file. If no
31 temporary file could be created, the result is NULL, and <<errno>>
32 records the reason for failure.
34 PORTABILITY
35 <<tmpfile64>> is a glibc extension.
37 Supporting OS subroutines required: <<close>>, <<fstat>>, <<getpid>>,
38 <<isatty>>, <<lseek64>>, <<open64>>, <<read>>, <<sbrk>>, <<write>>.
40 <<tmpfile64>> also requires the global pointer <<environ>>.
43 #include <stdio.h>
44 #include <reent.h>
45 #include <errno.h>
46 #include <fcntl.h>
47 #include <sys/stat.h>
49 #ifndef O_BINARY
50 # define O_BINARY 0
51 #endif
53 #ifdef __LARGE64_FILES
55 FILE *
56 _tmpfile64_r (struct _reent *ptr)
58 FILE *fp;
59 int e;
60 char *f;
61 char buf[L_tmpnam];
62 int fd;
66 if ((f = _tmpnam_r (ptr, buf)) == NULL)
67 return NULL;
68 fd = _open64_r (ptr, f, O_RDWR | O_CREAT | O_EXCL | O_BINARY,
69 S_IRUSR | S_IWUSR);
71 while (fd < 0 && _REENT_ERRNO(ptr) == EEXIST);
72 if (fd < 0)
73 return NULL;
74 fp = _fdopen64_r (ptr, fd, "wb+");
75 e = _REENT_ERRNO(ptr);
76 if (!fp)
77 _close_r (ptr, fd);
78 (void) _remove_r (ptr, f);
79 _REENT_ERRNO(ptr) = e;
80 return fp;
83 #ifndef _REENT_ONLY
85 FILE *
86 tmpfile64 (void)
88 return _tmpfile64_r (_REENT);
91 #endif
93 #endif /* __LARGE64_FILES */