tmpnamのヘルプ・マニュアル
日本語 英語
tmpnam --help
man tmpnam
TMPNAM(3) Linux Programmer’s Manual TMPNAM(3)
名前
tmpnam, tmpnam_r - 一時ファイルの名前を作成する
書式
#include
char *tmpnam(char *s);
説明
tmpnam() 関数は、ファイル名に使える文字列へのポインタを返す。ある時点で
は同じ名前を持つファイルが存在しないファイル名が返されるので、幼稚な プ
ロ グラマはこの文字列が一時ファイルのファイル名として適していると考える
かもしれない。引き数 s が NULL なら、この名前は内部の静的バッファに作成
され、次に tmpnam() 関数が呼び出された時に上書きされる。 s が NULL でな
ければ、ファイル名は s が指す (少なくとも L_tmpnam の長さを持つ) 文字配
列にコピーされ、成功した場合は s が返される。
作 成されるパス名は、ディレクトリの部分に P_tmpdir が使われる。 (L_tmp-
nam と P_tmpdir は、以下で説明する TMP_MAX 同様 で定義され て
いる。)
返り値
tmpnam() 関数は一意な一時ファイル名へのポインタを返す。一意なファイル名
が作成できなかった場合は NULL を返す。
エラー
エラーは定義されていない。
準拠
SVr4, 4.3BSD, C89, C99, POSIX.1-2001. POSIX.1-2008 は tmpnam() を廃 止
予定としている。
注意
tmpnam() 関数は最大 TMP_MAX 回まで、呼び出される度に異なる文字列を作成
する。 TMP_MAX 回以上呼び出された場合、その動作は実装依存である。
tmpnam() は推測が難しい名前を生成するが、それにもかかわらず、 tmpnam()
が パス名を返してから、プログラムがそのファイルをオープンするまでの間に
、別のプログラムが同じパス名で、ファイルを open(2) で作成したり、シンボ
リ ックリンクを作成したりする可能性がある。これはセキュリティホールにつ
ながる可能性がある。そのような可能性を回避するためには 、 open(2) の
O_EXCL フ ラ グ を 使ってパス名をオープンすればよい。もっといいのは、
mkstemp(3) や tmpfile(3) を使うことである。
移植性が必要な、スレッドを使ったアプリケーションでは 、 _POSIX_THREADS
か _POSIX_THREAD_SAFE_FUNCTIONS が定義されている場合に、 tmpnam() 関数
を NULL 引き数で呼び出してはならない。
POSIX 草案では、関数 tmpnam_r() を使うことを提案している。この関数は 、
以 下 の ように定義されており、 NULL を使わないようにという警告の意味で
NULL を別扱いしている。
char *
tmpnam_r(char *s)
{
return s ? tmpnam(s) : NULL;
}
数は少ないが、この関数を実装しているシステムもある。この関数の glibc の
プ ロトタイプを使うには、 をインクルードする前に _SVID_SOURCE
か _BSD_SOURCE を定義しておく必要がある。
バグ
決してこの関数を使ってはならない。代わりに mkstemp(3) か tmpfile(3) を
使うこと。
関連項目
mkstemp(3), mktemp(3), tempnam(3), tmpfile(3)
2008-08-06 TMPNAM(3)
TMPNAM(3) Linux Programmer’s Manual TMPNAM(3)
NAME
tmpnam, tmpnam_r - create a name for a temporary file
SYNOPSIS
#include
char *tmpnam(char *s);
DESCRIPTION
The tmpnam() function returns a pointer to a string that is a valid
filename, and such that a file with this name did not exist at some
point in time, so that naive programmers may think it a suitable name
for a temporary file. If the argument s is NULL this name is generated
in an internal static buffer and may be overwritten by the next call to
tmpnam(). If s is not NULL, the name is copied to the character array
(of length at least L_tmpnam) pointed to by s and the value s is
returned in case of success.
The pathname that is created, has a directory prefix P_tmpdir. (Both
L_tmpnam and P_tmpdir are defined in , just like the TMP_MAX
mentioned below.)
RETURN VALUE
The tmpnam() function returns a pointer to a unique temporary filename,
or NULL if a unique name cannot be generated.
ERRORS
No errors are defined.
CONFORMING TO
SVr4, 4.3BSD, C89, C99, POSIX.1-2001. POSIX.1-2008 marks tmpnam() as
obsolete.
NOTES
The tmpnam() function generates a different string each time it is
called, up to TMP_MAX times. If it is called more than TMP_MAX times,
the behavior is implementation defined.
Although tmpnam() generates names that are difficult to guess, it is
nevertheless possible that between the time that tmpnam() returns a
pathname, and the time that the program opens it, another program might
create that pathname using open(2), or create it as a symbolic link.
This can lead to security holes. To avoid such possibilities, use the
open(2) O_EXCL flag to open the pathname. Or better yet, use
mkstemp(3) or tmpfile(3).
Portable applications that use threads cannot call tmpnam() with a NULL
argument if either _POSIX_THREADS or _POSIX_THREAD_SAFE_FUNCTIONS is
defined.
A POSIX draft proposed to use a function tmpnam_r() defined by
char *
tmpnam_r(char *s)
{
return s ? tmpnam(s) : NULL;
}
apparently as a warning not to use NULL. A few systems implement it.
To get a glibc prototype for this function, define _SVID_SOURCE or
_BSD_SOURCE before including .
BUGS
Never use this function. Use mkstemp(3) or tmpfile(3) instead.
SEE ALSO
mkstemp(3), mktemp(3), tempnam(3), tmpfile(3)
COLOPHON
This page is part of release 3.22 of the Linux man-pages project. A
description of the project, and information about reporting bugs, can
be found at http://www.kernel.org/doc/man-pages/.
2008-08-06 TMPNAM(3)