[444] | 1 | /* |
---|
| 2 | FUNCTION |
---|
| 3 | <<tmpfile>>---create a temporary file |
---|
| 4 | |
---|
| 5 | INDEX |
---|
| 6 | tmpfile |
---|
| 7 | INDEX |
---|
| 8 | _tmpfile_r |
---|
| 9 | |
---|
| 10 | SYNOPSIS |
---|
| 11 | #include <stdio.h> |
---|
| 12 | FILE *tmpfile(void); |
---|
| 13 | |
---|
| 14 | FILE *_tmpfile_r(struct _reent *<[reent]>); |
---|
| 15 | |
---|
| 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). |
---|
| 22 | |
---|
| 23 | The alternate function <<_tmpfile_r>> is a reentrant version. The |
---|
| 24 | argument <[reent]> is a pointer to a reentrancy structure. |
---|
| 25 | |
---|
| 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. |
---|
| 30 | |
---|
| 31 | PORTABILITY |
---|
| 32 | Both ANSI C and the System V Interface Definition (Issue 2) require |
---|
| 33 | <<tmpfile>>. |
---|
| 34 | |
---|
| 35 | Supporting 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 | |
---|
| 52 | FILE * |
---|
| 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 | |
---|
| 82 | FILE * |
---|
| 83 | tmpfile (void) |
---|
| 84 | { |
---|
| 85 | return _tmpfile_r (_REENT); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | #endif |
---|