⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tmpfile.c

📁 C标准库源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
#else  /* _UNICODE */
        strcpy(p, _P_tmpdir);
#endif  /* _UNICODE */
        q = p + sizeof(_P_tmpdir) - 1;      /* same as p + _tcslen(p) */

        if  ( (*(q - 1) != _T('\\')) && (*(q - 1) != _T('/')) )
                *(q++) = _T('\\');

        /*
         * Append the leading character of the filename.
         */
        if ( flag )
                /* for tmpfile() */
                *(q++) = _T('t');
        else
                /* for tmpnam() */
                *(q++) = _T('s');

        /*
         * Append the process id, encoded in base 32. Note this makes
         * p back into a string again (i.e., terminated by a '\0').
         */
        _ultot((unsigned long)_getpid(), q, 32);
        _tcscat(p, _T("."));
}


/***
*static int genfname(_TSCHAR *fname) -
*
*Purpose:
*
*Entry:
*
*Exit:
*
*Exceptions:
*
*******************************************************************************/

#ifdef _UNICODE
static int __cdecl wgenfname (
#else  /* _UNICODE */
static int __cdecl genfname (
#endif  /* _UNICODE */
        _TSCHAR *fname
        )
{
        _TSCHAR *p;
        _TSCHAR pext[4];
        unsigned long extnum;

        p = _tcsrchr(fname, _T('.'));

        p++;

        if ( (extnum = _tcstoul(p, NULL, 32) + 1) >= (unsigned long)TMP_MAX )
                return -1;

        _tcscpy(p, _ultot(extnum, pext, 32));

        return 0;
}

#if !defined (_UNICODE) && !defined (CRTDLL)

/***
*void __inc_tmpoff(void) - force external reference for _tmpoff
*
*Purpose:
*       Forces an external reference to be generate for _tmpoff, which is
*       is defined in cinittmp.obj. This has the forces cinittmp.obj to be
*       pulled in, making a call to rmtmp part of the termination.
*
*Entry:
*
*Exit:
*
*Exceptions:
*
*******************************************************************************/


extern int _tmpoff;

void __inc_tmpoff(
        void
        )
{
        _tmpoff++;
}

#endif  /* !defined (_UNICODE) && !defined (CRTDLL) */


#else  /* _WIN32 */

#if defined (_M_MPPC) || defined (_M_M68K)


#include <cruntime.h>
#include <stdio.h>
#include <share.h>
#include <stdlib.h>
#include <file2.h>
#include <io.h>
#include <malloc.h>
#include <errno.h>
#include <internal.h>
#include <string.h>
#include <dbgint.h>
#include <macos\osutils.h>
#include <macos\files.h>
#include <macos\errors.h>
#include <macos\gestalte.h>
#include <macos\traps.h>
#include <macos\toolutil.h>
#include <macos\folders.h>

static char namebuf[L_tmpnam];  /* internal static buffer for tmpnam */


/***
*char *tmpnam(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:
*       char *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:
*
*******************************************************************************/

char * __cdecl tmpnam (
        REG1 char *s
        )
{
        int olderrno;
        unsigned int first;
        OSErr osErr;
        short foundVRefNum;
        long foundDirID;
        HParamBlockRec hparamBlock;
        CInfoPBRec cinfoPB;
        char *ptmp;
        Str255 st, st2;
        int fwrapped;

        /* use internal buffer if user didn't provide one */

        if (s == NULL)
                s = namebuf;

        /* construct base name */

        *s = '\0';

        /*decide if we are in system 7.0 or earlier version*/
        /*if system 7.0, put in temp file folder, otherwise*/
        /*put in system folder*/

        hparamBlock.volumeParam.ioNamePtr = &st2[0];
        hparamBlock.volumeParam.ioVRefNum = 0;
        hparamBlock.volumeParam.ioVolIndex = 1;
        osErr = PBHGetVInfoSync(&hparamBlock);
        if (osErr)
                {
                return NULL;
                }

        if (__TrapFromGestalt(gestaltFindFolderAttr, gestaltFindFolderPresent))
                {
                osErr = FindFolder((unsigned short)kOnSystemDisk, (OSType)kTemporaryFolderType, (Boolean)kCreateFolder,
                        &foundVRefNum, &foundDirID);
                if (osErr)
                        {
                        return NULL;
                        }
                }
        else    /* put into system folder */
                {
                foundVRefNum = hparamBlock.volumeParam.ioVRefNum;
                foundDirID = hparamBlock.volumeParam.ioVFndrInfo[0];
                if (!foundDirID)
                        {
                        return NULL;
                        }
                }


        /* get full pathname -- folder name to put tmp file*/
        cinfoPB.dirInfo.ioVRefNum = foundVRefNum;
        cinfoPB.dirInfo.ioDrDirID = foundDirID;
        cinfoPB.dirInfo.ioFDirIndex = -1; /*use ioDirID only*/
        cinfoPB.dirInfo.ioNamePtr = &st[0];
        osErr = PBGetCatInfoSync(&cinfoPB);
        if (osErr)
                {
                return NULL;
                }
        else
                {
                _p2cstr(st);            /* dir name system folder or temp items */
                _p2cstr(st2);           /* volume name */
                strcpy(s, st2);
                strcat(s, ":");
                strcat(s, st);
                }

        /* Loop until a non-existent filename is found */

        strcat(s, ":");
        ptmp = s + strlen(s);

        olderrno = errno;
        first = _tmpoff;

        fwrapped = 0;
        do {
            if (_tmpoff > TMP_MAX )
                        {
                        if (!fwrapped)
                                {
                                _tmpoff = 1;
                                fwrapped = 1;
                                }
                        else
                                {
                                return (NULL);
                                }
                        }
                _itoa( _tmpoff, ptmp, 10 );
                _tmpoff++;
                errno = 0;
        }
        while ( ( _access( s, 0 ) == 0 ) || ( errno == EACCES ) );

        errno = olderrno;

        return( s );
}


/***
*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
        )
{
        char name[ L_tmpnam ];
        char *ptr;
        FILE *stream;
        int tmpnum;
        FILE *retval;

        /* Call tmpnam() to generate the filename. Save the _tmpoff value. */

        ptr = name;
        ptr = tmpnam( ptr );
        tmpnum = _tmpoff - 1;

        /* Now get a free stream and open the file. */

        if ((stream = _getstream()) == NULL)
                return(NULL);

        retval = _openfile(name,"w+b",_SH_DENYNO,stream);

        if (retval != NULL)
#ifdef _DEBUG
                if ( (stream->_tmpfname = _malloc_crt( strlen(ptr) + 1 ))
                     != NULL )
                        strcpy(stream->_tmpfname, ptr);
#else  /* _DEBUG */
                stream->_tmpfname = _strdup(ptr);   /* store _tmpoff value */
#endif  /* _DEBUG */

        return(retval);
}


#endif  /* defined (_M_MPPC) || defined (_M_M68K) */

#endif  /* _WIN32 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -