Cygwin: mmap: allow remapping part of an existing anonymous mapping
[newlib-cygwin.git] / newlib / libc / stdio / tmpfile.c
blob5fe6e3bf1de255a8398177b167f5d0b8dff987c5
1 /*
2 FUNCTION
3 <<tmpfile>>---create a temporary file
5 INDEX
6 tmpfile
7 INDEX
8 _tmpfile_r
10 SYNOPSIS
11 #include <stdio.h>
12 FILE *tmpfile(void);
14 FILE *_tmpfile_r(struct _reent *<[reent]>);
16 DESCRIPTION
17 Create a 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).
23 The alternate function <<_tmpfile_r>> is a reentrant version. The
24 argument <[reent]> is a pointer to a reentrancy structure.
26 RETURNS
27 <<tmpfile>> normally returns a pointer to the temporary file. If no
28 temporary file could be created, the result is NULL, and <<errno>>
29 records the reason for failure.
31 PORTABILITY
32 Both ANSI C and the System V Interface Definition (Issue 2) require
33 <<tmpfile>>.
35 Supporting OS subroutines required: <<close>>, <<fstat>>, <<getpid>>,
36 <<isatty>>, <<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
38 <<tmpfile>> also requires the global pointer <<environ>>.
41 #include <_ansi.h>
42 #include <reent.h>
43 #include <stdio.h>
44 #include <errno.h>
45 #include <fcntl.h>
46 #include <sys/stat.h>
48 #ifndef O_BINARY
49 # define O_BINARY 0
50 #endif
52 FILE *
53 _tmpfile_r (struct _reent *ptr)
55 FILE *fp;
56 int e;
57 char *f;
58 char buf[L_tmpnam];
59 int fd;
63 if ((f = _tmpnam_r (ptr, buf)) == NULL)
64 return NULL;
65 fd = _open_r (ptr, f, O_RDWR | O_CREAT | O_EXCL | O_BINARY,
66 S_IRUSR | S_IWUSR);
68 while (fd < 0 && _REENT_ERRNO(ptr) == EEXIST);
69 if (fd < 0)
70 return NULL;
71 fp = _fdopen_r (ptr, fd, "wb+");
72 e = _REENT_ERRNO(ptr);
73 if (!fp)
74 _close_r (ptr, fd);
75 (void) _remove_r (ptr, f);
76 _REENT_ERRNO(ptr) = e;
77 return fp;
80 #ifndef _REENT_ONLY
82 FILE *
83 tmpfile (void)
85 return _tmpfile_r (_REENT);
88 #endif