fstat.c 3.8 KB
Newer Older
S
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
/*****************************************************************************\
*                                                                             *
*   Filename	    fstat.c						      *
*									      *
*   Description:    Redefinitions of standard C library's fstat()	      *
*                                                                             *
*   Notes:	    							      *
*                                                                             *
*   History:								      *
*    2014-06-24 JFL Created this module.				      *
*                                                                             *
*         ?Copyright 2016 Hewlett Packard Enterprise Development LP          *
* Licensed under the Apache 2.0 license - www.apache.org/licenses/LICENSE-2.0 *
\*****************************************************************************/

#define _CRT_SECURE_NO_WARNINGS 1 /* Avoid Visual C++ security warnings */

#define _UTF8_SOURCE /* Generate the UTF-8 version of routines */

/* Microsoft C libraries include files */
#include <errno.h>
#include <stdio.h>
#include <string.h>
/* MsvcLibX library extensions */
#include <sys/stat.h>
#include "debugm.h"
#include <stdint.h>


#if defined(_MSDOS)

/* Nothing needs to be redefined */

#endif /* defined(_MSDOS) */


#if defined(_WIN32)

/* ------------ Display the *stat* macro values at compile time ------------ */

#pragma message(MACRODEF(_MSVC_stat))
#pragma message(MACRODEF(_MSVC_fstat))
#pragma message(MACRODEF(_MSVC_lstat))
#pragma message(MACRODEF(_MSVC_stat64))

#if _MSVCLIBX_STAT_DEFINED
  #pragma message(MACRODEF(_LIBX_stat))
  #pragma message(MACRODEF(_LIBX_stat64))
#endif

#pragma message(MACRODEF(stat))
#pragma message(MACRODEF(fstat))
#pragma message(MACRODEF(lstat))

#if defined(_LARGEFILE_SOURCE64)
  #pragma message(MACRODEF(stat64))
  #pragma message(MACRODEF(fstat64))
  #pragma message(MACRODEF(lstat64))
#endif

#include <windows.h>

/*---------------------------------------------------------------------------*\
*                                                                             *
|   Function:	    fstat						      |
|									      |
|   Description:    Redefine the standard _fstatXY() functions                |
|									      |
|   Parameters:     int nFile			The file handle		      |
|		    struct stat *buf		Output buffer		      |
|									      |
|   Returns:	    0 = Success, -1 = Failure				      |
|									      |
|   Notes:	    See sys/stat.h for a description of how the stat and fstat|
|		    macros work.					      |
|									      |
|   History:								      |
|    2014-06-24 JFL Created this routine                               	      |
*									      *
\*---------------------------------------------------------------------------*/

int fstat(int nFile, struct stat *pStat) {
  int iErr;
  struct _MSVC_stat msStat;
  DEBUG_CODE(
  struct tm *pTime;
  char szTime[100];
  )

  DEBUG_ENTER((STRINGIZE(fstat) "(%d, 0x%p);\n", nFile, pStat));

  iErr = _MSVC_fstat(nFile, &msStat);
  if (!iErr) {
    ZeroMemory(pStat, sizeof(struct stat));
    pStat->st_mode = msStat.st_mode;
    pStat->st_size = msStat.st_size;
    pStat->st_ctime = 
#undef st_ctime
    			msStat.st_ctime;
    pStat->st_mtime = 
#undef st_mtime
    			msStat.st_mtime;
    pStat->st_atime = 
#undef st_atime
    			msStat.st_atime;
    DEBUG_CODE(
      pTime = LocalFileTime(&(msStat.st_mtime));
      strftime(szTime, sizeof(szTime), "%Y-%m-%d %H:%M:%S", pTime);
    )
    RETURN_INT_COMMENT(0, ("%s  mode = 0x%04X  size = %I64d bytes\n", szTime, pStat->st_mode, (__int64)(pStat->st_size)));
  }
  /* TO DO: Get the nanosecond time resolution using Windows functions */

  RETURN_INT_COMMENT(iErr, ("%s\n", strerror(errno)));
}

#endif /* _WIN32 */