?? tmpfile.c
字號:
/***
*tmpfile.c - create unique file name or file
*
* Copyright (c) 1985-1997, Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines tmpnam() and tmpfile().
*
*******************************************************************************/
#ifdef _WIN32
#include <cruntime.h>
#include <errno.h>
#include <process.h>
#include <fcntl.h>
#include <io.h>
#include <mtdll.h>
#include <share.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <file2.h>
#include <internal.h>
#include <tchar.h>
#include <dbgint.h>
/*
* Buffers used by tmpnam() and tmpfile() to build filenames.
*/
static _TSCHAR namebuf0[L_tmpnam] = { 0 }; /* used by tmpnam() */
static _TSCHAR namebuf1[L_tmpnam] = { 0 }; /* used by tmpfile() */
/*
* Initializing function for namebuf0 and namebuf1.
*/
#ifdef _UNICODE
static void __cdecl winit_namebuf(int);
#else /* _UNICODE */
static void __cdecl init_namebuf(int);
#endif /* _UNICODE */
/*
* Generator function that produces temporary filenames
*/
#ifdef _UNICODE
static int __cdecl wgenfname(wchar_t *);
#else /* _UNICODE */
static int __cdecl genfname(char *);
#endif /* _UNICODE */
/***
*_TSCHAR *tmpnam(_TSCHAR *s) - generate temp file name
*
*Purpose:
* Creates a file name that is unique in the directory specified by
* _P_tmpdir in stdio.h. Places file name in string passed by user or
* in static mem if pass NULL.
*
*Entry:
* _TSCHAR *s - ptr to place to put temp name
*
*Exit:
* returns pointer to constructed file name (s or address of static mem)
* returns NULL if fails
*
*Exceptions:
*
*******************************************************************************/
_TSCHAR * __cdecl _ttmpnam (
_TSCHAR *s
)
{
#ifdef _MT
_ptiddata ptd;
#endif /* _MT */
_mlock(_TMPNAM_LOCK);
/*
* Initialize namebuf0, if needed. Otherwise, call genfname() to
* generate the next filename.
*/
if ( *namebuf0 == 0 ) {
#ifdef _UNICODE
winit_namebuf(0);
#else /* _UNICODE */
init_namebuf(0);
#endif /* _UNICODE */
}
#ifdef _UNICODE
else if ( wgenfname(namebuf0) )
#else /* _UNICODE */
else if ( genfname(namebuf0) )
#endif /* _UNICODE */
goto tmpnam_err;
/*
* Generate a filename that doesn't already exist.
*/
while ( _taccess(namebuf0, 0) == 0 )
#ifdef _UNICODE
if ( wgenfname(namebuf0) )
#else /* _UNICODE */
if ( genfname(namebuf0) )
#endif /* _UNICODE */
goto tmpnam_err;
/*
* Filename has successfully been generated.
*/
if ( s == NULL )
#ifdef _MT
{
/*
* Use a per-thread buffer to hold the generated file name.
* If there isn't one, and one cannot be created, just use
* namebuf0.
*/
ptd = _getptd();
#ifdef _UNICODE
if ( (ptd->_wnamebuf0 != NULL) || ((ptd->_wnamebuf0 =
_malloc_crt(L_tmpnam * sizeof(wchar_t))) != NULL) )
{
s = ptd->_wnamebuf0;
wcscpy(s, namebuf0);
}
#else /* _UNICODE */
if ( (ptd->_namebuf0 != NULL) || ((ptd->_namebuf0 =
_malloc_crt(L_tmpnam)) != NULL) )
{
s = ptd->_namebuf0;
strcpy(s, namebuf0);
}
#endif /* _UNICODE */
else
s = namebuf0;
}
#else /* _MT */
s = namebuf0;
#endif /* _MT */
else
_tcscpy(s, namebuf0);
_munlock(_TMPNAM_LOCK);
return s;
/*
* Error return path. All errors exit through here.
*/
tmpnam_err:
_munlock(_TMPNAM_LOCK);
return NULL;
}
#ifndef _UNICODE
/***
*FILE *tmpfile() - create a temporary file
*
*Purpose:
* Creates a temporary file with the file mode "w+b". The file
* will be automatically deleted when closed or the program terminates
* normally.
*
*Entry:
* None.
*
*Exit:
* Returns stream pointer to opened file.
* Returns NULL if fails
*
*Exceptions:
*
*******************************************************************************/
FILE * __cdecl tmpfile (
void
)
{
FILE *stream;
int fh;
_mlock(_TMPNAM_LOCK);
/*
* Initialize namebuf1, if needed. Otherwise, call genfname() to
* generate the next filename.
*/
if ( *namebuf1 == 0 ) {
init_namebuf(1);
}
else if ( genfname(namebuf1) )
goto tmpfile_err0;
/*
* Get a free stream.
*
* Note: In multi-thread models, the stream obtained below is locked!
*/
if ( (stream = _getstream()) == NULL )
goto tmpfile_err0;
/*
* Create a temporary file.
*
* Note: The loop below will only create a new file. It will NOT
* open and truncate an existing file. Either behavior is probably
* legal under ANSI (4.9.4.3 says tmpfile "creates" the file, but
* also says it is opened with mode "wb+"). However, the behavior
* implemented below is compatible with prior versions of MS-C and
* makes error checking easier.
*/
while ( ((fh = _sopen(namebuf1,
_O_CREAT | _O_EXCL | _O_RDWR | _O_BINARY |
_O_TEMPORARY,
_SH_DENYNO,
_S_IREAD | _S_IWRITE
))
== -1) && (errno == EEXIST) )
if ( genfname(namebuf1) )
break;
/*
* Check that the loop above did indeed create a temporary
* file.
*/
if ( fh == -1 )
goto tmpfile_err1;
/*
* Initialize stream
*/
#ifdef _DEBUG
if ( (stream->_tmpfname = _malloc_crt( (_tcslen( namebuf1 ) + 1) *
sizeof(_TSCHAR) )) == NULL )
#else /* _DEBUG */
if ( (stream->_tmpfname = _tcsdup( namebuf1 )) == NULL )
#endif /* _DEBUG */
{
/* close the file, then branch to error handling */
_close(fh);
goto tmpfile_err1;
}
#ifdef _DEBUG
_tcscpy( stream->_tmpfname, namebuf1 );
#endif /* _DEBUG */
stream->_cnt = 0;
stream->_base = stream->_ptr = NULL;
stream->_flag = _commode | _IORW;
stream->_file = fh;
_unlock_str(stream);
_munlock(_TMPNAM_LOCK);
return stream;
/*
* Error return. All errors paths branch to one of the two
* labels below.
*/
tmpfile_err1:
_unlock_str(stream);
tmpfile_err0:
_munlock(_TMPNAM_LOCK);
return NULL;
}
#endif /* _UNICODE */
/***
*static void init_namebuf(flag) - initializes the namebuf arrays
*
*Purpose:
* Called once each for namebuf0 and namebuf1, to initialize
* them.
*
*Entry:
* int flag - flag set to 0 if namebuf0 is to be initialized,
* non-zero (1) if namebuf1 is to be initialized.
*Exit:
*
*Exceptions:
*
*******************************************************************************/
#ifdef _UNICODE
static void __cdecl winit_namebuf(
#else /* _UNICODE */
static void __cdecl init_namebuf(
#endif /* _UNICODE */
int flag
)
{
_TSCHAR *p, *q;
if ( flag == 0 )
p = namebuf0;
else
p = namebuf1;
/*
* Put in the path prefix. Make sure it ends with a slash or
* backslash character.
*/
#ifdef _UNICODE
wcscpy(p, _wP_tmpdir);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -