CodeCogs
HomeForumsCart
Login



Forgotten Password?

Or login with:

  • Facebookhttp://facebook.com/
  • Googlehttps://www.google.com/accounts/o8/id
  • Yahoohttps://me.yahoo.com
ComputingCStdio.h

tmpfile

Temporary file routines

Interface

#include <stdio.h>
FILE tmpfile (void)
char tmpnam (char *str)
char tempnam (const char *tmpdir, const char *prefix)

Description

The tmpfile function returns a pointer to a stream associated with a file descriptor returned by the routine mkstemp. The created file is unlinked before tmpfile returns, causing the file to be automatically deleted when the last reference to it is closed. The file is opened with the access value '<span class="Qlq">w+</span>'. The file is created in the directory determined by the environment variable <span class="Ev">TMPDIR</span> if set. The default location if <span class="Ev">TMPDIR</span> is not set is <span class="Pa">/tmp</span>.

Example 1

#include <stdio.h>
 
int main()
{
  // create a temporary binary file
  // using the "wb+" access parameters
  FILE *f = tmpfile();
 
  // ...use the temporary file...
 
  // close and delete the temporary file
  fclose(f);
  return 0;
}

The tmpnam function returns a pointer to a file name, in the <span class="Dv">P_tmpdir</span> directory, which did not reference an existing file at some indeterminate point in the past. <span class="Dv">P_tmpdir</span> is defined in the include file #include <stdio.h> If the argument \c str is non- <span class="Dv">NULL</span>, the file name is copied to the buffer it references. Otherwise, the file name is copied to a static buffer. In either case, tmpnam returns a pointer to the file name. \n \n The buffer referenced by \c str is expected to be at least <span class="Dv">L_tmpnam</span> bytes in length. <span class="Dv">L_tmpnam</span> is defined in the include file #include <stdio.h> \n \n The tempnam function is similar to tmpnam , but provides the ability to specify the directory which will contain the temporary file and the file name prefix. \n \n The environment variable <span class="Ev">TMPDIR</span> (if set), the argument \c tmpdir (if non- <span class="Dv">NULL</span>), the directory <span class="Dv">P_tmpdir</span>, and the directory <span class="Pa">/tmp</span> are tried, in the listed order, as directories in which to store the temporary file. \n \n The argument \c prefix, if non- <span class="Dv">NULL</span>, is used to specify a file name prefix, which will be the first part of the created file name. The tempnam function allocates memory in which to store the file name; the returned pointer may be used as a subsequent argument to free.

Return Values

The tmpfile function returns a pointer to an open file stream on success, and a <span class="Dv">NULL</span> pointer on error. \n \n The tmpnam and tempfile functions return a pointer to a file name on success, and a <span class="Dv">NULL</span> pointer on error.

Errors

The tmpfile function may fail and set the global variable <span class="Va">errno</span> for any of the errors specified for the library functions or mkstemp. \n \n The tmpnam function may fail and set <span class="Va">errno</span> for any of the errors specified for the library function mktemp. \n \n The tempnam function may fail and set <span class="Va">errno</span> for any of the errors specified for the library functions reference:malloc or mktemp.

Security Considerations

The tmpnam and tempnam functions are susceptible to a race condition occurring between the selection of the file name and the creation of the file, which allows malicious users to potentially overwrite arbitrary files in the system, depending on the level of privilege of the running program. Additionally, there is no means by which file permissions may be specified. It is strongly suggested that mkstemp be used in place of these functions. (See the FSA.)

Compatibility

These interfaces are provided from System V and <span class="Tn">ANSI</span> compatibility only. \n \n Most historic implementations of these functions provide only a limited number of possible temporary file names (usually 26) before file names will start being recycled. System V implementations of these functions (and of mktemp) use the access system call to determine whether or not the temporary file may be created. This has obvious ramifications for setuid or setgid programs, complicating the portable use of these interfaces in such programs. \n \n The tmpfile interface should not be used in software expected to be used on other systems if there is any possibility that the user does not wish the temporary file to be publicly readable and writable.

Last Modified: 18 Dec 11 @ 13:15     Page Rendered: 2022-03-14 15:46:39