Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

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