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

📄 stat.c

📁 C标准库源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
                                               SystemTime.wMinute,
                                               SystemTime.wSecond,
                                               -1 );
            } else
                buf->st_ctime = buf->st_mtime ;

            FindClose(findhandle);
        }

        /* Fill in buf */

        buf->st_mode = __tdtoxmode(findbuf.dwFileAttributes, name);
        buf->st_nlink = 1;

#ifdef _USE_INT64
        buf->st_size = ((__int64)(findbuf.nFileSizeHigh)) * (0x100000000i64) +
                        (__int64)(findbuf.nFileSizeLow);
#else  /* _USE_INT64 */
        buf->st_size = findbuf.nFileSizeLow;
#endif  /* _USE_INT64 */

        /* now set the common fields */

        buf->st_uid = buf->st_gid = buf->st_ino = 0;

        buf->st_rdev = buf->st_dev = (_dev_t)(drive - 1); /* A=0, B=1, etc. */

        return(0);
}


/*
 * IsRootUNCName - returns TRUE if the argument is a UNC name specifying
 *      a root share.  That is, if it is of the form \\server\share\.
 *      This routine will also return true if the argument is of the
 *      form \\server\share (no trailing slash) but Win32 currently
 *      does not like that form.
 *
 *      Forward slashes ('/') may be used instead of backslashes ('\').
 */

static int IsRootUNCName(const _TSCHAR *path)
{
        /*
         * If a root UNC name, path will start with 2 (but not 3) slashes
         */

        if ( ( _tcslen ( path ) >= 5 ) /* minimum string is "//x/y" */
             && ISSLASH(path[0]) && ISSLASH(path[1]))
        {
            const _TSCHAR * p = path + 2 ;

            /*
             * find the slash between the server name and share name
             */
            while ( * ++ p )
                if ( ISSLASH(*p) )
                    break ;

            if ( *p && p[1] )
            {
                /*
                 * is there a further slash?
                 */
                while ( * ++ p )
                    if ( ISSLASH(*p) )
                        break ;

                /*
                 * just final slash (or no final slash)
                 */
                if ( !*p || !p[1])
                    return 1;
            }
        }

        return 0 ;
}


#else  /* _MAC */


#include <cruntime.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <ctype.h>
#include <string.h>
#include <internal.h>
#include <stdlib.h>
#include <direct.h>
#include <macos\osutils.h>
#include <macos\files.h>
#include <macos\errors.h>

#ifdef _MBCS
#include <mbstring.h>
#define STRPBRK _mbspbrk
#else  /* _MBCS */
#define STRPBRK strpbrk
#endif  /* _MBCS */


unsigned short __cdecl _dtoxmode ( CInfoPBPtr pcinfoPB );

/***
*unsigned _dtoxmode(CInfoPBPtr) -
*
*Purpose:
*
*Entry:
*
*Exit:
*
*Exceptions:
*
*******************************************************************************/

unsigned short __cdecl _dtoxmode (
        CInfoPBPtr pcinfoPB
        )
{
        REG1 unsigned short uxmode;
        unsigned char dosmode;
        int fReadOnly;

        dosmode = pcinfoPB->hFileInfo.ioFlAttrib;

        /* file or dir */
        if (dosmode & 0x10)
        {
            uxmode = _S_IFDIR;
            fReadOnly = pcinfoPB->dirInfo.ioDrUsrWds.frFlags == fsRdPerm;
        }
        else
        {
            uxmode = _S_IFREG;
            fReadOnly = pcinfoPB->hFileInfo.ioFlFndrInfo.fdFlags == fsRdPerm;
            /* see if file appears to be executable - check 'APPL'? */
            if (pcinfoPB->hFileInfo.ioFlFndrInfo.fdType == 'APPL')
                uxmode |= _S_IEXEC;
        }


        /* If attribute byte has 0 bit set, file is locked, then read-only */

        uxmode |= (((dosmode & 0x01) || fReadOnly) ? _S_IREAD :
                  (_S_IREAD|_S_IWRITE));


        /* propagate user read/write/execute bits to group/other fields */

        uxmode |= (uxmode & 0700) >> 3;
        uxmode |= (uxmode & 0700) >> 6;

        return(uxmode);
}


/***
*int _stat(name, buf) - get file status info
*
*Purpose:
*       _stat obtains information about the file and stores it in the structure
*       pointed to by buf.
*
*Entry:
*       char *name -    pathname of given file
*       struct _stat *buffer - pointer to buffer to store info in
*
*Exit:
*       fills in structure pointed to by buffer
*       returns 0 if successful
*       returns -1 and sets errno if unsuccessful
*
*Exceptions:
*
*******************************************************************************/

int __cdecl _stat (
        REG1 const char *name,
        REG2 struct _stat *buf
        )
{
        CInfoPBRec cinfoPB;
        OSErr osErr;
        char szBuf[256];
        DateTimeRec dt;
        DateTimeRec dtc;
        HParamBlockRec hparamBlock;


        if (!*name)
        {
            errno = ENOENT;
            return -1;
        }

        strcpy(szBuf, name);
        cinfoPB.hFileInfo.ioNamePtr = _c2pstr(szBuf);
        cinfoPB.hFileInfo.ioFDirIndex = 0;
        cinfoPB.hFileInfo.ioVRefNum = 0;
        cinfoPB.hFileInfo.ioDirID = 0;

        osErr = PBGetCatInfoSync(&cinfoPB);
        switch (osErr)
        {
            case noErr:
                break;
            case bdNamErr:
            case dirNFErr:
            case extFSErr:
            case fnfErr:
            case ioErr:
            case nsvErr:
            case paramErr:
                errno = ENOENT;
                return -1;
            default:
                return -1;
        }


        /* file or dir ? */
        if (cinfoPB.hFileInfo.ioFlAttrib & 0x10)
        {   /*dir*/
            Secs2Date(cinfoPB.dirInfo.ioDrMdDat, &dt);
            Secs2Date(cinfoPB.dirInfo.ioDrCrDat, &dtc);
            buf->st_size = 0;
        }
        else
        {   /*file*/
            Secs2Date(cinfoPB.hFileInfo.ioFlMdDat, &dt);
            Secs2Date(cinfoPB.hFileInfo.ioFlCrDat, &dtc);
            buf->st_size = cinfoPB.hFileInfo.ioFlLgLen +
                           cinfoPB.hFileInfo.ioFlRLgLen;
        }

        buf->st_mtime = _gmtotime_t( dt.year,
                                     dt.month,
                                     dt.day,
                                     dt.hour,
                                     dt.minute,
                                     dt.second );

        buf->st_atime = buf->st_mtime;
        buf->st_ctime = _gmtotime_t( dtc.year,
                                     dtc.month,
                                     dtc.day,
                                     dtc.hour,
                                     dtc.minute,
                                     dtc.second );

        buf->st_nlink = 1;
        buf->st_uid = buf->st_gid = buf->st_ino = 0;

        buf->st_mode = _dtoxmode(&cinfoPB);

        /* get volume number and map it to st_dev/rdev */
        hparamBlock.volumeParam.ioNamePtr = szBuf;
        hparamBlock.volumeParam.ioVRefNum = 0;
        hparamBlock.volumeParam.ioVolIndex = 0;
        osErr = PBHGetVInfoSync(&hparamBlock);
        if (osErr != noErr)
            return -1;

        buf->st_rdev = buf->st_dev = hparamBlock.volumeParam.ioVRefNum;

        return(0);
}

#endif  /* _MAC */

⌨️ 快捷键说明

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