2000-02-18 03:39:52 +08:00
|
|
|
/*
|
|
|
|
* tmpname.c
|
|
|
|
* Original Author: G. Haley
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
FUNCTION
|
|
|
|
<<tmpnam>>, <<tempnam>>---name for a temporary file
|
|
|
|
|
|
|
|
INDEX
|
|
|
|
tmpnam
|
|
|
|
INDEX
|
|
|
|
tempnam
|
|
|
|
INDEX
|
|
|
|
_tmpnam_r
|
|
|
|
INDEX
|
|
|
|
_tempnam_r
|
|
|
|
|
2017-11-30 16:05:02 +08:00
|
|
|
SYNOPSIS
|
2000-02-18 03:39:52 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
char *tmpnam(char *<[s]>);
|
|
|
|
char *tempnam(char *<[dir]>, char *<[pfx]>);
|
2004-04-23 Artem B. Bityuckiy <abitytsky@softminecorp.com>
* libc/stdio/asprintf.c libc/stdio/clearerr.c,
libc/stdio/fclose.c libc/stdio/fcloseall.c libc/stdio/fdopen.c,
libc/stdio/feof.c libc/stdio/ferror.c libc/stdio/fflush.c,
libc/stdio/fgetc.c libc/stdio/fgetpos.c libc/stdio/fgets.c,
libc/stdio/fileno.c libc/stdio/findfp.c libc/stdio/fiprintf.c,
libc/stdio/flags.c libc/stdio/fopen.c libc/stdio/fprintf.c,
libc/stdio/fputc.c libc/stdio/fputs.c libc/stdio/fread.c,
libc/stdio/freopen.c libc/stdio/fscanf.c libc/stdio/fseek.c,
libc/stdio/fseeko.c libc/stdio/fsetpos.c libc/stdio/ftell.c,
libc/stdio/ftello.c libc/stdio/fvwrite.c libc/stdio/fwalk.c,
libc/stdio/fwrite.c libc/stdio/getc.c libc/stdio/getc_u.c,
libc/stdio/getchar.c libc/stdio/getchar_u.c,
libc/stdio/getdelim.c libc/stdio/getline.c libc/stdio/gets.c,
libc/stdio/getw.c libc/stdio/iprintf.c libc/stdio/local.h,
libc/stdio/makebuf.c libc/stdio/mktemp.c libc/stdio/perror.c,
libc/stdio/printf.c libc/stdio/putc.c libc/stdio/putc_u.c,
libc/stdio/putchar.c libc/stdio/putchar_u.c libc/stdio/puts.c,
libc/stdio/putw.c libc/stdio/refill.c libc/stdio/remove.c,
libc/stdio/rename.c libc/stdio/rewind.c libc/stdio/rget.c,
libc/stdio/scanf.c libc/stdio/setbuf.c libc/stdio/setbuffer.c,
libc/stdio/setlinebuf.c libc/stdio/setvbuf.c,
libc/stdio/siprintf.c libc/stdio/snprintf.c,
libc/stdio/sprintf.c libc/stdio/sscanf.c libc/stdio/stdio.c,
libc/stdio/tmpfile.c libc/stdio/tmpnam.c libc/stdio/ungetc.c,
libc/stdio/vasprintf.c libc/stdio/vfieeefp.h,
libc/stdio/vfprintf.c libc/stdio/vfscanf.c,
libc/stdio/vprintf.c libc/stdio/vscanf.c,
libc/stdio/vsnprintf.c libc/stdio/vsprintf.c,
libc/stdio/vsscanf.c libc/stdio/wbuf.c,
libc/stdio/wsetup.c: Perform minor formatting changes. Move
copyright notices to top of file, ensure that <_ansi.h> is
included, be consistent with open parentheses, use _DEFUN macro,
include "local.h" where needed, and remove various compiler
warnings.
2004-04-24 04:01:55 +08:00
|
|
|
char *_tmpnam_r(struct _reent *<[reent]>, char *<[s]>);
|
|
|
|
char *_tempnam_r(struct _reent *<[reent]>, char *<[dir]>, char *<[pfx]>);
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
DESCRIPTION
|
|
|
|
Use either of these functions to generate a name for a temporary file.
|
|
|
|
The generated name is guaranteed to avoid collision with other files
|
|
|
|
(for up to <<TMP_MAX>> calls of either function).
|
|
|
|
|
|
|
|
<<tmpnam>> generates file names with the value of <<P_tmpdir>>
|
|
|
|
(defined in `<<stdio.h>>') as the leading directory component of the path.
|
|
|
|
|
|
|
|
You can use the <<tmpnam>> argument <[s]> to specify a suitable area
|
|
|
|
of memory for the generated filename; otherwise, you can call
|
|
|
|
<<tmpnam(NULL)>> to use an internal static buffer.
|
|
|
|
|
|
|
|
<<tempnam>> allows you more control over the generated filename: you
|
|
|
|
can use the argument <[dir]> to specify the path to a directory for
|
|
|
|
temporary files, and you can use the argument <[pfx]> to specify a
|
|
|
|
prefix for the base filename.
|
|
|
|
|
|
|
|
If <[dir]> is <<NULL>>, <<tempnam>> will attempt to use the value of
|
|
|
|
environment variable <<TMPDIR>> instead; if there is no such value,
|
|
|
|
<<tempnam>> uses the value of <<P_tmpdir>> (defined in `<<stdio.h>>').
|
|
|
|
|
|
|
|
If you don't need any particular prefix to the basename of temporary
|
|
|
|
files, you can pass <<NULL>> as the <[pfx]> argument to <<tempnam>>.
|
|
|
|
|
|
|
|
<<_tmpnam_r>> and <<_tempnam_r>> are reentrant versions of <<tmpnam>>
|
|
|
|
and <<tempnam>> respectively. The extra argument <[reent]> is a
|
|
|
|
pointer to a reentrancy structure.
|
|
|
|
|
|
|
|
WARNINGS
|
|
|
|
The generated filenames are suitable for temporary files, but do not
|
|
|
|
in themselves make files temporary. Files with these names must still
|
|
|
|
be explicitly removed when you no longer want them.
|
|
|
|
|
|
|
|
If you supply your own data area <[s]> for <<tmpnam>>, you must ensure
|
|
|
|
that it has room for at least <<L_tmpnam>> elements of type <<char>>.
|
|
|
|
|
|
|
|
RETURNS
|
|
|
|
Both <<tmpnam>> and <<tempnam>> return a pointer to the newly
|
|
|
|
generated filename.
|
|
|
|
|
|
|
|
PORTABILITY
|
|
|
|
ANSI C requires <<tmpnam>>, but does not specify the use of
|
|
|
|
<<P_tmpdir>>. The System V Interface Definition (Issue 2) requires
|
|
|
|
both <<tmpnam>> and <<tempnam>>.
|
|
|
|
|
|
|
|
Supporting OS subroutines required: <<close>>, <<fstat>>, <<getpid>>,
|
|
|
|
<<isatty>>, <<lseek>>, <<open>>, <<read>>, <<sbrk>>, <<write>>.
|
|
|
|
|
|
|
|
The global pointer <<environ>> is also required.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <_ansi.h>
|
2004-04-23 Artem B. Bityuckiy <abitytsky@softminecorp.com>
* libc/stdio/asprintf.c libc/stdio/clearerr.c,
libc/stdio/fclose.c libc/stdio/fcloseall.c libc/stdio/fdopen.c,
libc/stdio/feof.c libc/stdio/ferror.c libc/stdio/fflush.c,
libc/stdio/fgetc.c libc/stdio/fgetpos.c libc/stdio/fgets.c,
libc/stdio/fileno.c libc/stdio/findfp.c libc/stdio/fiprintf.c,
libc/stdio/flags.c libc/stdio/fopen.c libc/stdio/fprintf.c,
libc/stdio/fputc.c libc/stdio/fputs.c libc/stdio/fread.c,
libc/stdio/freopen.c libc/stdio/fscanf.c libc/stdio/fseek.c,
libc/stdio/fseeko.c libc/stdio/fsetpos.c libc/stdio/ftell.c,
libc/stdio/ftello.c libc/stdio/fvwrite.c libc/stdio/fwalk.c,
libc/stdio/fwrite.c libc/stdio/getc.c libc/stdio/getc_u.c,
libc/stdio/getchar.c libc/stdio/getchar_u.c,
libc/stdio/getdelim.c libc/stdio/getline.c libc/stdio/gets.c,
libc/stdio/getw.c libc/stdio/iprintf.c libc/stdio/local.h,
libc/stdio/makebuf.c libc/stdio/mktemp.c libc/stdio/perror.c,
libc/stdio/printf.c libc/stdio/putc.c libc/stdio/putc_u.c,
libc/stdio/putchar.c libc/stdio/putchar_u.c libc/stdio/puts.c,
libc/stdio/putw.c libc/stdio/refill.c libc/stdio/remove.c,
libc/stdio/rename.c libc/stdio/rewind.c libc/stdio/rget.c,
libc/stdio/scanf.c libc/stdio/setbuf.c libc/stdio/setbuffer.c,
libc/stdio/setlinebuf.c libc/stdio/setvbuf.c,
libc/stdio/siprintf.c libc/stdio/snprintf.c,
libc/stdio/sprintf.c libc/stdio/sscanf.c libc/stdio/stdio.c,
libc/stdio/tmpfile.c libc/stdio/tmpnam.c libc/stdio/ungetc.c,
libc/stdio/vasprintf.c libc/stdio/vfieeefp.h,
libc/stdio/vfprintf.c libc/stdio/vfscanf.c,
libc/stdio/vprintf.c libc/stdio/vscanf.c,
libc/stdio/vsnprintf.c libc/stdio/vsprintf.c,
libc/stdio/vsscanf.c libc/stdio/wbuf.c,
libc/stdio/wsetup.c: Perform minor formatting changes. Move
copyright notices to top of file, ensure that <_ansi.h> is
included, be consistent with open parentheses, use _DEFUN macro,
include "local.h" where needed, and remove various compiler
warnings.
2004-04-24 04:01:55 +08:00
|
|
|
#include <reent.h>
|
2000-02-18 03:39:52 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <reent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
Add --enable-newlib-reent-thread-local option
By default, Newlib uses a huge object of type struct _reent to store
thread-specific data. This object is returned by __getreent() if the
__DYNAMIC_REENT__ Newlib configuration option is defined.
The reentrancy structure contains for example errno and the standard input,
output, and error file streams. This means that if an application only uses
errno it has a dependency on the file stream support even if it does not use
it. This is an issue for lower end targets and applications which need to
qualify the software according to safety standards (for example ECSS-E-ST-40C,
ECSS-Q-ST-80C, IEC 61508, ISO 26262, DO-178, DO-330, DO-333).
If the new _REENT_THREAD_LOCAL configuration option is enabled, then struct
_reent is replaced by dedicated thread-local objects for each struct _reent
member. The thread-local objects are defined in translation units which use
the corresponding object.
2022-05-16 17:51:54 +08:00
|
|
|
#ifdef _REENT_THREAD_LOCAL
|
|
|
|
_Thread_local int _tls_inc;
|
2023-04-26 14:23:04 +08:00
|
|
|
_Thread_local char _tls_emergency[_REENT_EMERGENCY_SIZE];
|
Add --enable-newlib-reent-thread-local option
By default, Newlib uses a huge object of type struct _reent to store
thread-specific data. This object is returned by __getreent() if the
__DYNAMIC_REENT__ Newlib configuration option is defined.
The reentrancy structure contains for example errno and the standard input,
output, and error file streams. This means that if an application only uses
errno it has a dependency on the file stream support even if it does not use
it. This is an issue for lower end targets and applications which need to
qualify the software according to safety standards (for example ECSS-E-ST-40C,
ECSS-Q-ST-80C, IEC 61508, ISO 26262, DO-178, DO-330, DO-333).
If the new _REENT_THREAD_LOCAL configuration option is enabled, then struct
_reent is replaced by dedicated thread-local objects for each struct _reent
member. The thread-local objects are defined in translation units which use
the corresponding object.
2022-05-16 17:51:54 +08:00
|
|
|
#endif
|
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
/* Try to open the file specified, if it can't be opened then try
|
|
|
|
another one. Return nonzero if successful, otherwise zero. */
|
|
|
|
|
|
|
|
static int
|
2017-12-04 11:43:30 +08:00
|
|
|
worker (struct _reent *ptr,
|
2017-12-04 09:31:41 +08:00
|
|
|
char *result,
|
2017-12-04 10:25:16 +08:00
|
|
|
const char *part1,
|
|
|
|
const char *part2,
|
2017-12-04 09:31:41 +08:00
|
|
|
int part3,
|
2004-04-23 Artem B. Bityuckiy <abitytsky@softminecorp.com>
* libc/stdio/asprintf.c libc/stdio/clearerr.c,
libc/stdio/fclose.c libc/stdio/fcloseall.c libc/stdio/fdopen.c,
libc/stdio/feof.c libc/stdio/ferror.c libc/stdio/fflush.c,
libc/stdio/fgetc.c libc/stdio/fgetpos.c libc/stdio/fgets.c,
libc/stdio/fileno.c libc/stdio/findfp.c libc/stdio/fiprintf.c,
libc/stdio/flags.c libc/stdio/fopen.c libc/stdio/fprintf.c,
libc/stdio/fputc.c libc/stdio/fputs.c libc/stdio/fread.c,
libc/stdio/freopen.c libc/stdio/fscanf.c libc/stdio/fseek.c,
libc/stdio/fseeko.c libc/stdio/fsetpos.c libc/stdio/ftell.c,
libc/stdio/ftello.c libc/stdio/fvwrite.c libc/stdio/fwalk.c,
libc/stdio/fwrite.c libc/stdio/getc.c libc/stdio/getc_u.c,
libc/stdio/getchar.c libc/stdio/getchar_u.c,
libc/stdio/getdelim.c libc/stdio/getline.c libc/stdio/gets.c,
libc/stdio/getw.c libc/stdio/iprintf.c libc/stdio/local.h,
libc/stdio/makebuf.c libc/stdio/mktemp.c libc/stdio/perror.c,
libc/stdio/printf.c libc/stdio/putc.c libc/stdio/putc_u.c,
libc/stdio/putchar.c libc/stdio/putchar_u.c libc/stdio/puts.c,
libc/stdio/putw.c libc/stdio/refill.c libc/stdio/remove.c,
libc/stdio/rename.c libc/stdio/rewind.c libc/stdio/rget.c,
libc/stdio/scanf.c libc/stdio/setbuf.c libc/stdio/setbuffer.c,
libc/stdio/setlinebuf.c libc/stdio/setvbuf.c,
libc/stdio/siprintf.c libc/stdio/snprintf.c,
libc/stdio/sprintf.c libc/stdio/sscanf.c libc/stdio/stdio.c,
libc/stdio/tmpfile.c libc/stdio/tmpnam.c libc/stdio/ungetc.c,
libc/stdio/vasprintf.c libc/stdio/vfieeefp.h,
libc/stdio/vfprintf.c libc/stdio/vfscanf.c,
libc/stdio/vprintf.c libc/stdio/vscanf.c,
libc/stdio/vsnprintf.c libc/stdio/vsprintf.c,
libc/stdio/vsscanf.c libc/stdio/wbuf.c,
libc/stdio/wsetup.c: Perform minor formatting changes. Move
copyright notices to top of file, ensure that <_ansi.h> is
included, be consistent with open parentheses, use _DEFUN macro,
include "local.h" where needed, and remove various compiler
warnings.
2004-04-24 04:01:55 +08:00
|
|
|
int *part4)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
/* Generate the filename and make sure that there isn't one called
|
|
|
|
it already. */
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
int t;
|
|
|
|
_sprintf_r (ptr, result, "%s/%s%x.%x", part1, part2, part3, *part4);
|
|
|
|
(*part4)++;
|
|
|
|
t = _open_r (ptr, result, O_RDONLY, 0);
|
|
|
|
if (t == -1)
|
|
|
|
{
|
2022-01-18 17:13:04 +08:00
|
|
|
if (_REENT_ERRNO(ptr) == ENOSYS)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
result[0] = '\0';
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_close_r (ptr, t);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2017-12-04 11:43:30 +08:00
|
|
|
_tmpnam_r (struct _reent *p,
|
2004-04-23 Artem B. Bityuckiy <abitytsky@softminecorp.com>
* libc/stdio/asprintf.c libc/stdio/clearerr.c,
libc/stdio/fclose.c libc/stdio/fcloseall.c libc/stdio/fdopen.c,
libc/stdio/feof.c libc/stdio/ferror.c libc/stdio/fflush.c,
libc/stdio/fgetc.c libc/stdio/fgetpos.c libc/stdio/fgets.c,
libc/stdio/fileno.c libc/stdio/findfp.c libc/stdio/fiprintf.c,
libc/stdio/flags.c libc/stdio/fopen.c libc/stdio/fprintf.c,
libc/stdio/fputc.c libc/stdio/fputs.c libc/stdio/fread.c,
libc/stdio/freopen.c libc/stdio/fscanf.c libc/stdio/fseek.c,
libc/stdio/fseeko.c libc/stdio/fsetpos.c libc/stdio/ftell.c,
libc/stdio/ftello.c libc/stdio/fvwrite.c libc/stdio/fwalk.c,
libc/stdio/fwrite.c libc/stdio/getc.c libc/stdio/getc_u.c,
libc/stdio/getchar.c libc/stdio/getchar_u.c,
libc/stdio/getdelim.c libc/stdio/getline.c libc/stdio/gets.c,
libc/stdio/getw.c libc/stdio/iprintf.c libc/stdio/local.h,
libc/stdio/makebuf.c libc/stdio/mktemp.c libc/stdio/perror.c,
libc/stdio/printf.c libc/stdio/putc.c libc/stdio/putc_u.c,
libc/stdio/putchar.c libc/stdio/putchar_u.c libc/stdio/puts.c,
libc/stdio/putw.c libc/stdio/refill.c libc/stdio/remove.c,
libc/stdio/rename.c libc/stdio/rewind.c libc/stdio/rget.c,
libc/stdio/scanf.c libc/stdio/setbuf.c libc/stdio/setbuffer.c,
libc/stdio/setlinebuf.c libc/stdio/setvbuf.c,
libc/stdio/siprintf.c libc/stdio/snprintf.c,
libc/stdio/sprintf.c libc/stdio/sscanf.c libc/stdio/stdio.c,
libc/stdio/tmpfile.c libc/stdio/tmpnam.c libc/stdio/ungetc.c,
libc/stdio/vasprintf.c libc/stdio/vfieeefp.h,
libc/stdio/vfprintf.c libc/stdio/vfscanf.c,
libc/stdio/vprintf.c libc/stdio/vscanf.c,
libc/stdio/vsnprintf.c libc/stdio/vsprintf.c,
libc/stdio/vsscanf.c libc/stdio/wbuf.c,
libc/stdio/wsetup.c: Perform minor formatting changes. Move
copyright notices to top of file, ensure that <_ansi.h> is
included, be consistent with open parentheses, use _DEFUN macro,
include "local.h" where needed, and remove various compiler
warnings.
2004-04-24 04:01:55 +08:00
|
|
|
char *s)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
char *result;
|
|
|
|
int pid;
|
|
|
|
|
|
|
|
if (s == NULL)
|
|
|
|
{
|
|
|
|
/* ANSI states we must use an internal static buffer if s is NULL */
|
2002-02-03 17:24:18 +08:00
|
|
|
_REENT_CHECK_EMERGENCY(p);
|
|
|
|
result = _REENT_EMERGENCY(p);
|
2000-02-18 03:39:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = s;
|
|
|
|
}
|
|
|
|
pid = _getpid_r (p);
|
|
|
|
|
2022-02-02 16:49:00 +08:00
|
|
|
if (worker (p, result, P_tmpdir, "t", pid, &_REENT_INC(p)))
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
2022-02-02 16:49:00 +08:00
|
|
|
_REENT_INC(p)++;
|
2000-02-18 03:39:52 +08:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2017-12-04 11:43:30 +08:00
|
|
|
_tempnam_r (struct _reent *p,
|
2017-12-04 10:25:16 +08:00
|
|
|
const char *dir,
|
|
|
|
const char *pfx)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
char *filename;
|
|
|
|
int length;
|
2017-12-04 10:25:16 +08:00
|
|
|
const char *prefix = (pfx) ? pfx : "";
|
2000-02-18 03:39:52 +08:00
|
|
|
if (dir == NULL && (dir = getenv ("TMPDIR")) == NULL)
|
|
|
|
dir = P_tmpdir;
|
|
|
|
|
|
|
|
/* two 8 digit numbers + . / */
|
|
|
|
length = strlen (dir) + strlen (prefix) + (4 * sizeof (int)) + 2 + 1;
|
|
|
|
|
|
|
|
filename = _malloc_r (p, length);
|
|
|
|
if (filename)
|
|
|
|
{
|
|
|
|
if (! worker (p, filename, dir, prefix,
|
2022-02-02 16:49:00 +08:00
|
|
|
_getpid_r (p) ^ (int) (_POINTER_INT) p, &_REENT_INC(p)))
|
2000-02-18 03:39:52 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _REENT_ONLY
|
|
|
|
|
|
|
|
char *
|
2017-12-04 11:43:30 +08:00
|
|
|
tempnam (const char *dir,
|
2017-12-04 10:25:16 +08:00
|
|
|
const char *pfx)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
return _tempnam_r (_REENT, dir, pfx);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
2017-12-04 11:43:30 +08:00
|
|
|
tmpnam (char *s)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
return _tmpnam_r (_REENT, s);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|