source: trunk/libs/newlib/src/newlib/libc/stdio/tmpfile.c @ 543

Last change on this file since 543 was 444, checked in by satin@…, 6 years ago

add newlib,libalmos-mkh, restructure shared_syscalls.h and mini-libc

File size: 1.8 KB
Line 
1/*
2FUNCTION
3<<tmpfile>>---create a temporary file
4
5INDEX
6        tmpfile
7INDEX
8        _tmpfile_r
9
10SYNOPSIS
11        #include <stdio.h>
12        FILE *tmpfile(void);
13
14        FILE *_tmpfile_r(struct _reent *<[reent]>);
15
16DESCRIPTION
17Create a temporary file (a file which will be deleted automatically),
18using a name generated by <<tmpnam>>.  The temporary file is opened with
19the mode <<"wb+">>, permitting you to read and write anywhere in it
20as a binary file (without any data transformations the host system may
21perform for text files).
22
23The alternate function <<_tmpfile_r>> is a reentrant version.  The
24argument <[reent]> is a pointer to a reentrancy structure.
25
26RETURNS
27<<tmpfile>> normally returns a pointer to the temporary file.  If no
28temporary file could be created, the result is NULL, and <<errno>>
29records the reason for failure.
30
31PORTABILITY
32Both ANSI C and the System V Interface Definition (Issue 2) require
33<<tmpfile>>.
34
35Supporting OS subroutines required: <<close>>, <<fstat>>, <<getpid>>,
36<<isatty>>, <<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
37
38<<tmpfile>> also requires the global pointer <<environ>>.
39*/
40
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>
47
48#ifndef O_BINARY
49# define O_BINARY 0
50#endif
51
52FILE *
53_tmpfile_r (struct _reent *ptr)
54{
55  FILE *fp;
56  int e;
57  char *f;
58  char buf[L_tmpnam];
59  int fd;
60
61  do
62    {
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);
67    }
68  while (fd < 0 && ptr->_errno == EEXIST);
69  if (fd < 0)
70    return NULL;
71  fp = _fdopen_r (ptr, fd, "wb+");
72  e = ptr->_errno;
73  if (!fp)
74    _close_r (ptr, fd);
75  (void) _remove_r (ptr, f);
76  ptr->_errno = e;
77  return fp;
78}
79
80#ifndef _REENT_ONLY
81
82FILE *
83tmpfile (void)
84{
85  return _tmpfile_r (_REENT);
86}
87
88#endif
Note: See TracBrowser for help on using the repository browser.