2000-02-18 03:39:52 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1987 Regents of the University of California.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that: (1) source distributions retain this entire copyright
|
|
|
|
* notice and comment, and (2) distributions including binaries display
|
|
|
|
* the following acknowledgement: ``This product includes software
|
|
|
|
* developed by the University of California, Berkeley and its contributors''
|
2020-01-29 02:54:53 +08:00
|
|
|
* in the documentation or other materials provided with the distribution.
|
|
|
|
* Neither the name of the University nor the names of its
|
2000-02-18 03:39:52 +08:00
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*/
|
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
|
|
|
/* This is file MKTEMP.C */
|
|
|
|
/* This file may have been modified by DJ Delorie (Jan 1991). If so,
|
|
|
|
** these modifications are Copyright (C) 1991 DJ Delorie.
|
|
|
|
*/
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
FUNCTION
|
2010-07-20 02:21:11 +08:00
|
|
|
<<mktemp>>, <<mkstemp>>, <<mkostemp>>, <<mkstemps>>,
|
|
|
|
<<mkostemps>>---generate unused file name
|
2009-07-03 20:03:25 +08:00
|
|
|
<<mkdtemp>>---generate unused directory
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
INDEX
|
|
|
|
mktemp
|
2009-07-03 20:03:25 +08:00
|
|
|
INDEX
|
|
|
|
mkdtemp
|
2000-02-18 03:39:52 +08:00
|
|
|
INDEX
|
|
|
|
mkstemp
|
2009-07-03 20:03:25 +08:00
|
|
|
INDEX
|
|
|
|
mkstemps
|
2010-07-20 02:21:11 +08:00
|
|
|
INDEX
|
|
|
|
mkostemp
|
|
|
|
INDEX
|
|
|
|
mkostemps
|
2000-02-18 03:39:52 +08:00
|
|
|
INDEX
|
|
|
|
_mktemp_r
|
2009-07-03 20:03:25 +08:00
|
|
|
INDEX
|
|
|
|
_mkdtemp_r
|
2000-02-18 03:39:52 +08:00
|
|
|
INDEX
|
|
|
|
_mkstemp_r
|
2009-07-03 20:03:25 +08:00
|
|
|
INDEX
|
|
|
|
_mkstemps_r
|
2010-07-20 02:21:11 +08:00
|
|
|
INDEX
|
|
|
|
_mkostemp_r
|
|
|
|
INDEX
|
|
|
|
_mkostemps_r
|
2000-02-18 03:39:52 +08:00
|
|
|
|
2017-11-30 16:05:02 +08:00
|
|
|
SYNOPSIS
|
2009-07-03 20:03:25 +08:00
|
|
|
#include <stdlib.h>
|
2000-02-18 03:39:52 +08:00
|
|
|
char *mktemp(char *<[path]>);
|
2009-07-03 20:03:25 +08:00
|
|
|
char *mkdtemp(char *<[path]>);
|
2000-02-18 03:39:52 +08:00
|
|
|
int mkstemp(char *<[path]>);
|
2009-07-03 20:03:25 +08:00
|
|
|
int mkstemps(char *<[path]>, int <[suffixlen]>);
|
2010-07-20 02:21:11 +08:00
|
|
|
int mkostemp(char *<[path]>, int <[flags]>);
|
|
|
|
int mkostemps(char *<[path]>, int <[suffixlen]>, int <[flags]>);
|
2000-02-18 03:39:52 +08:00
|
|
|
|
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 *_mktemp_r(struct _reent *<[reent]>, char *<[path]>);
|
2009-07-03 20:03:25 +08:00
|
|
|
char *_mkdtemp_r(struct _reent *<[reent]>, char *<[path]>);
|
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 *_mkstemp_r(struct _reent *<[reent]>, char *<[path]>);
|
2009-07-03 20:03:25 +08:00
|
|
|
int *_mkstemps_r(struct _reent *<[reent]>, char *<[path]>, int <[len]>);
|
2010-07-20 02:21:11 +08:00
|
|
|
int *_mkostemp_r(struct _reent *<[reent]>, char *<[path]>,
|
|
|
|
int <[flags]>);
|
|
|
|
int *_mkostemps_r(struct _reent *<[reent]>, char *<[path]>, int <[len]>,
|
|
|
|
int <[flags]>);
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
DESCRIPTION
|
2009-07-03 20:03:25 +08:00
|
|
|
<<mktemp>>, <<mkstemp>>, and <<mkstemps>> attempt to generate a file name
|
|
|
|
that is not yet in use for any existing file. <<mkstemp>> and <<mkstemps>>
|
|
|
|
create the file and open it for reading and writing; <<mktemp>> simply
|
2010-07-20 02:21:11 +08:00
|
|
|
generates the file name (making <<mktemp>> a security risk). <<mkostemp>>
|
|
|
|
and <<mkostemps>> allow the addition of other <<open>> flags, such
|
|
|
|
as <<O_CLOEXEC>>, <<O_APPEND>>, or <<O_SYNC>>. On platforms with a
|
|
|
|
separate text mode, <<mkstemp>> forces <<O_BINARY>>, while <<mkostemp>>
|
|
|
|
allows the choice between <<O_BINARY>>, <<O_TEXT>>, or 0 for default.
|
|
|
|
<<mkdtemp>> attempts to create a directory instead of a file, with a
|
|
|
|
permissions mask of 0700.
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
You supply a simple pattern for the generated file name, as the string
|
|
|
|
at <[path]>. The pattern should be a valid filename (including path
|
2009-07-03 20:03:25 +08:00
|
|
|
information if you wish) ending with at least six `<<X>>'
|
2000-02-18 03:39:52 +08:00
|
|
|
characters. The generated filename will match the leading part of the
|
|
|
|
name you supply, with the trailing `<<X>>' characters replaced by some
|
2009-07-03 20:03:25 +08:00
|
|
|
combination of digits and letters. With <<mkstemps>>, the `<<X>>'
|
|
|
|
characters end <[suffixlen]> bytes before the end of the string.
|
2000-02-18 03:39:52 +08:00
|
|
|
|
2009-07-03 20:03:25 +08:00
|
|
|
The alternate functions <<_mktemp_r>>, <<_mkdtemp_r>>, <<_mkstemp_r>>,
|
2010-07-20 02:21:11 +08:00
|
|
|
<<_mkostemp_r>>, <<_mkostemps_r>>, and <<_mkstemps_r>> are reentrant
|
|
|
|
versions. The extra argument <[reent]> is a pointer to a reentrancy
|
|
|
|
structure.
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
RETURNS
|
|
|
|
<<mktemp>> returns the pointer <[path]> to the modified string
|
|
|
|
representing an unused filename, unless it could not generate one, or
|
|
|
|
the pattern you provided is not suitable for a filename; in that case,
|
2010-07-20 02:21:11 +08:00
|
|
|
it returns <<NULL>>. Be aware that there is an inherent race between
|
|
|
|
generating the name and attempting to create a file by that name;
|
|
|
|
you are advised to use <<O_EXCL|O_CREAT>>.
|
2000-02-18 03:39:52 +08:00
|
|
|
|
2009-07-03 20:03:25 +08:00
|
|
|
<<mkdtemp>> returns the pointer <[path]> to the modified string if the
|
|
|
|
directory was created, otherwise it returns <<NULL>>.
|
|
|
|
|
2010-07-20 02:21:11 +08:00
|
|
|
<<mkstemp>>, <<mkstemps>>, <<mkostemp>>, and <<mkostemps>> return a file
|
|
|
|
descriptor to the newly created file, unless it could not generate an
|
|
|
|
unused filename, or the pattern you provided is not suitable for a
|
|
|
|
filename; in that case, it returns <<-1>>.
|
2000-02-18 03:39:52 +08:00
|
|
|
|
2009-03-14 20:14:08 +08:00
|
|
|
NOTES
|
|
|
|
Never use <<mktemp>>. The generated filenames are easy to guess and
|
|
|
|
there's a race between the test if the file exists and the creation
|
|
|
|
of the file. In combination this makes <<mktemp>> prone to attacks
|
|
|
|
and using it is a security risk. Whenever possible use <<mkstemp>>
|
|
|
|
instead. It doesn't suffer the race condition.
|
|
|
|
|
2000-02-18 03:39:52 +08:00
|
|
|
PORTABILITY
|
|
|
|
ANSI C does not require either <<mktemp>> or <<mkstemp>>; the System
|
2009-07-03 20:03:25 +08:00
|
|
|
V Interface Definition requires <<mktemp>> as of Issue 2. POSIX 2001
|
2010-07-20 02:21:11 +08:00
|
|
|
requires <<mkstemp>>, and POSIX 2008 requires <<mkdtemp>> while
|
|
|
|
deprecating <<mktemp>>. <<mkstemps>>, <<mkostemp>>, and <<mkostemps>>
|
|
|
|
are not standardized.
|
2000-02-18 03:39:52 +08:00
|
|
|
|
2009-07-03 20:03:25 +08:00
|
|
|
Supporting OS subroutines required: <<getpid>>, <<mkdir>>, <<open>>, <<stat>>.
|
2000-02-18 03:39:52 +08:00
|
|
|
*/
|
|
|
|
|
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 <_ansi.h>
|
2009-08-22 04:25:28 +08:00
|
|
|
#include <stdlib.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 <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
2000-04-18 01:10:18 +08:00
|
|
|
static int
|
2017-12-04 11:43:30 +08:00
|
|
|
_gettemp (struct _reent *ptr,
|
2017-12-04 09:31:41 +08:00
|
|
|
char *path,
|
|
|
|
register int *doopen,
|
|
|
|
int domkdir,
|
|
|
|
size_t suffixlen,
|
2010-07-20 02:21:11 +08:00
|
|
|
int flags)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
register char *start, *trv;
|
2009-07-03 20:03:25 +08:00
|
|
|
char *end;
|
2007-05-02 07:03:36 +08:00
|
|
|
#ifdef __USE_INTERNAL_STAT64
|
|
|
|
struct stat64 sbuf;
|
|
|
|
#else
|
2000-02-18 03:39:52 +08:00
|
|
|
struct stat sbuf;
|
2007-05-02 07:03:36 +08:00
|
|
|
#endif
|
2000-02-18 03:39:52 +08:00
|
|
|
unsigned int pid;
|
|
|
|
|
|
|
|
pid = _getpid_r (ptr);
|
|
|
|
for (trv = path; *trv; ++trv) /* extra X's get set to 0's */
|
|
|
|
continue;
|
2009-07-03 20:03:25 +08:00
|
|
|
if (trv - path < suffixlen)
|
|
|
|
{
|
2022-01-18 17:13:04 +08:00
|
|
|
_REENT_ERRNO(ptr) = EINVAL;
|
2009-07-03 20:03:25 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
trv -= suffixlen;
|
|
|
|
end = trv;
|
|
|
|
while (path < trv && *--trv == 'X')
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
*trv = (pid % 10) + '0';
|
|
|
|
pid /= 10;
|
|
|
|
}
|
2009-07-03 20:03:25 +08:00
|
|
|
if (end - trv < 6)
|
|
|
|
{
|
2022-01-18 17:13:04 +08:00
|
|
|
_REENT_ERRNO(ptr) = EINVAL;
|
2009-07-03 20:03:25 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check the target directory; if you have six X's and it
|
|
|
|
* doesn't exist this runs for a *very* long time.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (start = trv + 1;; --trv)
|
|
|
|
{
|
|
|
|
if (trv <= path)
|
|
|
|
break;
|
|
|
|
if (*trv == '/')
|
|
|
|
{
|
|
|
|
*trv = '\0';
|
2003-05-13 17:19:15 +08:00
|
|
|
#ifdef __USE_INTERNAL_STAT64
|
2003-05-12 19:09:39 +08:00
|
|
|
if (_stat64_r (ptr, path, &sbuf))
|
|
|
|
#else
|
2000-02-18 03:39:52 +08:00
|
|
|
if (_stat_r (ptr, path, &sbuf))
|
2003-05-12 19:09:39 +08:00
|
|
|
#endif
|
2000-02-18 03:39:52 +08:00
|
|
|
return (0);
|
|
|
|
if (!(sbuf.st_mode & S_IFDIR))
|
|
|
|
{
|
2022-01-18 17:13:04 +08:00
|
|
|
_REENT_ERRNO(ptr) = ENOTDIR;
|
2000-02-18 03:39:52 +08:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
*trv = '/';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
2009-07-03 20:03:25 +08:00
|
|
|
#if !defined _ELIX_LEVEL || _ELIX_LEVEL >= 4
|
|
|
|
if (domkdir)
|
|
|
|
{
|
2009-08-13 02:22:38 +08:00
|
|
|
#ifdef HAVE_MKDIR
|
2009-07-03 20:03:25 +08:00
|
|
|
if (_mkdir_r (ptr, path, 0700) == 0)
|
|
|
|
return 1;
|
2022-01-18 17:13:04 +08:00
|
|
|
if (_REENT_ERRNO(ptr) != EEXIST)
|
2009-07-03 20:03:25 +08:00
|
|
|
return 0;
|
2009-08-13 02:22:38 +08:00
|
|
|
#else /* !HAVE_MKDIR */
|
2022-01-18 17:13:04 +08:00
|
|
|
_REENT_ERRNO(ptr) = ENOSYS;
|
2009-08-13 02:22:38 +08:00
|
|
|
return 0;
|
|
|
|
#endif /* !HAVE_MKDIR */
|
2009-07-03 20:03:25 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif /* _ELIX_LEVEL */
|
2000-02-18 03:39:52 +08:00
|
|
|
if (doopen)
|
|
|
|
{
|
2010-07-20 02:21:11 +08:00
|
|
|
if ((*doopen = _open_r (ptr, path, O_CREAT | O_EXCL | O_RDWR | flags,
|
|
|
|
0600)) >= 0)
|
2000-02-18 03:39:52 +08:00
|
|
|
return 1;
|
2022-01-18 17:13:04 +08:00
|
|
|
if (_REENT_ERRNO(ptr) != EEXIST)
|
2000-02-18 03:39:52 +08:00
|
|
|
return 0;
|
|
|
|
}
|
2003-05-13 17:19:15 +08:00
|
|
|
#ifdef __USE_INTERNAL_STAT64
|
2003-05-12 19:09:39 +08:00
|
|
|
else if (_stat64_r (ptr, path, &sbuf))
|
|
|
|
#else
|
2000-02-18 03:39:52 +08:00
|
|
|
else if (_stat_r (ptr, path, &sbuf))
|
2003-05-12 19:09:39 +08:00
|
|
|
#endif
|
2022-01-18 17:13:04 +08:00
|
|
|
return (_REENT_ERRNO(ptr) == ENOENT ? 1 : 0);
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
/* tricky little algorithm for backward compatibility */
|
|
|
|
for (trv = start;;)
|
|
|
|
{
|
2009-07-03 20:03:25 +08:00
|
|
|
if (trv == end)
|
2000-02-18 03:39:52 +08:00
|
|
|
return 0;
|
|
|
|
if (*trv == 'z')
|
|
|
|
*trv++ = 'a';
|
|
|
|
else
|
|
|
|
{
|
2009-07-03 20:03:25 +08:00
|
|
|
/* Safe, since it only encounters 7-bit characters. */
|
2012-08-08 19:04:18 +08:00
|
|
|
if (isdigit ((unsigned char) *trv))
|
2000-02-18 03:39:52 +08:00
|
|
|
*trv = 'a';
|
|
|
|
else
|
|
|
|
++ * trv;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*NOTREACHED*/
|
|
|
|
}
|
|
|
|
|
2010-07-20 02:21:11 +08:00
|
|
|
#ifndef O_BINARY
|
|
|
|
# define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2000-04-18 01:10:18 +08:00
|
|
|
int
|
2017-12-04 11:43:30 +08:00
|
|
|
_mkstemp_r (struct _reent *ptr,
|
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 *path)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2010-07-20 02:21:11 +08:00
|
|
|
return (_gettemp (ptr, path, &fd, 0, 0, O_BINARY) ? fd : -1);
|
2009-07-03 20:03:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#if !defined _ELIX_LEVEL || _ELIX_LEVEL >= 4
|
|
|
|
char *
|
2017-12-04 11:43:30 +08:00
|
|
|
_mkdtemp_r (struct _reent *ptr,
|
2009-07-03 20:03:25 +08:00
|
|
|
char *path)
|
|
|
|
{
|
2010-07-20 02:21:11 +08:00
|
|
|
return (_gettemp (ptr, path, (int *) NULL, 1, 0, 0) ? path : NULL);
|
2009-07-03 20:03:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-12-04 11:43:30 +08:00
|
|
|
_mkstemps_r (struct _reent *ptr,
|
2017-12-04 09:31:41 +08:00
|
|
|
char *path,
|
2009-07-03 20:03:25 +08:00
|
|
|
int len)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2010-07-20 02:21:11 +08:00
|
|
|
return (_gettemp (ptr, path, &fd, 0, len, O_BINARY) ? fd : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-12-04 11:43:30 +08:00
|
|
|
_mkostemp_r (struct _reent *ptr,
|
2017-12-04 09:31:41 +08:00
|
|
|
char *path,
|
2010-07-20 02:21:11 +08:00
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
return (_gettemp (ptr, path, &fd, 0, 0, flags & ~O_ACCMODE) ? fd : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-12-04 11:43:30 +08:00
|
|
|
_mkostemps_r (struct _reent *ptr,
|
2017-12-04 09:31:41 +08:00
|
|
|
char *path,
|
|
|
|
int len,
|
2010-07-20 02:21:11 +08:00
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
return (_gettemp (ptr, path, &fd, 0, len, flags & ~O_ACCMODE) ? fd : -1);
|
2000-02-18 03:39:52 +08:00
|
|
|
}
|
2009-07-03 20:03:25 +08:00
|
|
|
#endif /* _ELIX_LEVEL */
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
char *
|
2017-12-04 11:43:30 +08:00
|
|
|
_mktemp_r (struct _reent *ptr,
|
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 *path)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
2010-07-20 02:21:11 +08:00
|
|
|
return (_gettemp (ptr, path, (int *) NULL, 0, 0, 0) ? path : (char *) NULL);
|
2000-02-18 03:39:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _REENT_ONLY
|
|
|
|
|
2000-04-18 01:10:18 +08:00
|
|
|
int
|
2017-12-04 11:43:30 +08:00
|
|
|
mkstemp (char *path)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2010-07-20 02:21:11 +08:00
|
|
|
return (_gettemp (_REENT, path, &fd, 0, 0, O_BINARY) ? fd : -1);
|
2009-07-03 20:03:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# if !defined _ELIX_LEVEL || _ELIX_LEVEL >= 4
|
|
|
|
char *
|
2017-12-04 11:43:30 +08:00
|
|
|
mkdtemp (char *path)
|
2009-07-03 20:03:25 +08:00
|
|
|
{
|
2010-07-20 02:21:11 +08:00
|
|
|
return (_gettemp (_REENT, path, (int *) NULL, 1, 0, 0) ? path : NULL);
|
2009-07-03 20:03:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-12-04 11:43:30 +08:00
|
|
|
mkstemps (char *path,
|
2009-07-03 20:03:25 +08:00
|
|
|
int len)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2010-07-20 02:21:11 +08:00
|
|
|
return (_gettemp (_REENT, path, &fd, 0, len, O_BINARY) ? fd : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-12-04 11:43:30 +08:00
|
|
|
mkostemp (char *path,
|
2010-07-20 02:21:11 +08:00
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
return (_gettemp (_REENT, path, &fd, 0, 0, flags & ~O_ACCMODE) ? fd : -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-12-04 11:43:30 +08:00
|
|
|
mkostemps (char *path,
|
2017-12-04 09:31:41 +08:00
|
|
|
int len,
|
2010-07-20 02:21:11 +08:00
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
return (_gettemp (_REENT, path, &fd, 0, len, flags & ~O_ACCMODE) ? fd : -1);
|
2000-02-18 03:39:52 +08:00
|
|
|
}
|
2009-07-03 20:03:25 +08:00
|
|
|
# endif /* _ELIX_LEVEL */
|
2000-02-18 03:39:52 +08:00
|
|
|
|
|
|
|
char *
|
2017-12-04 11:43:30 +08:00
|
|
|
mktemp (char *path)
|
2000-02-18 03:39:52 +08:00
|
|
|
{
|
2010-07-20 02:21:11 +08:00
|
|
|
return (_gettemp (_REENT, path, (int *) NULL, 0, 0, 0) ? path : (char *) NULL);
|
2000-02-18 03:39:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* ! defined (_REENT_ONLY) */
|