bool IsOlder (const char* FileName1, const char* FileName2) { struct _stat buf1; struct _stat buf2; FILE* fp1 = fopen (FileName1, "r"); FILE* fp2 = fopen (FileName2, "r"); _fstat( fileno(fp1), &buf1 ); _fstat( fileno(fp2), &buf2 ); fclose (fp1); fclose (fp2); return buf1.st_mtime < buf2.st_mtime; };
inline size32_t readFile(const char* name,StringBuffer& buf) { size32_t sz = 0; #ifdef _WIN32 int fd = open(name,O_RDONLY|O_BINARY); if(fd) { struct _stat st; if(!_fstat(fd,&st)) { void * data = buf.reserve((size32_t)st.st_size); sz = read(fd, data, st.st_size); } close(fd); } #else int fd = open(name,O_RDONLY); if(fd) { struct stat st; if(!fstat(fd,&st)) { void * data = buf.reserve((size32_t)st.st_size); sz = read(fd, data, st.st_size); } close(fd); } #endif return sz; }
bool ON::GetFileStats( FILE* fp, size_t* filesize, time_t* create_time, time_t* lastmodify_time ) { bool rc = false; if (filesize) *filesize = 0; if (create_time) *create_time = 0; if (lastmodify_time) *lastmodify_time = 0; if ( fp ) { #if defined(ON_COMPILER_MSC) // Microsoft compilers int fd = _fileno(fp); #if (_MSC_VER >= 1400) // VC 8 (2005) // works for file sizes > 4GB // when size_t is a 64 bit integer struct _stat64 sb; memset(&sb,0,sizeof(sb)); int fstat_rc = _fstat64(fd, &sb); #else // VC6 compiler // works on most compilers struct _stat sb; memset(&sb,0,sizeof(sb)); int fstat_rc = _fstat(fd, &sb); #endif #else // works on most compilers int fd = fileno(fp); struct stat sb; memset(&sb,0,sizeof(sb)); int fstat_rc = fstat(fd, &sb); #endif if (0 == fstat_rc) { if (filesize) *filesize = (size_t)sb.st_size; if (create_time) *create_time = (time_t)sb.st_ctime; if (lastmodify_time) *lastmodify_time = (time_t)sb.st_mtime; rc = true; } } return rc; }
/* * get exclusive lock to whole file. * lock gets removed when we close the file * * returns 0 on success */ int rrd_lock( rrd_file_t *rrd_file) { #ifdef DISABLE_FLOCK (void)rrd_file; return 0; #else int rcstat; rrd_simple_file_t *rrd_simple_file; rrd_simple_file = (rrd_simple_file_t *)rrd_file->pvt; { #if defined(_WIN32) && !defined(__CYGWIN__) && !defined(__CYGWIN32__) struct _stat st; if (_fstat(rrd_simple_file->fd, &st) == 0) { rcstat = _locking(rrd_simple_file->fd, _LK_NBLCK, st.st_size); } else { rcstat = -1; } #else struct flock lock; lock.l_type = F_WRLCK; /* exclusive write lock */ lock.l_len = 0; /* whole file */ lock.l_start = 0; /* start of file */ lock.l_whence = SEEK_SET; /* end of file */ rcstat = fcntl(rrd_simple_file->fd, F_SETLK, &lock); #endif } return (rcstat); #endif }
_WCRTLINK int _fstati64( int handle, struct _stati64 *buf ) { struct _stat buf32; int rc; INT_TYPE tmp; /*** Get the info using non-64bit version ***/ rc = _fstat( handle, &buf32 ); if( rc == -1 ) return( -1 ); /*** Convert the info to 64-bit equivalent ***/ buf->st_dev = buf32.st_dev; buf->st_ino = buf32.st_ino; buf->st_mode = buf32.st_mode; buf->st_nlink = buf32.st_nlink; buf->st_uid = buf32.st_uid; buf->st_gid = buf32.st_gid; buf->st_rdev = buf32.st_rdev; _clib_U32ToU64( buf32.st_size, tmp ); buf->st_size = GET_REALINT64(tmp); buf->st_atime = buf32.st_atime; buf->st_mtime = buf32.st_mtime; buf->st_ctime = buf32.st_ctime; buf->st_btime = buf32.st_btime; buf->st_attr = buf32.st_attr; buf->st_archivedID = buf32.st_archivedID; buf->st_updatedID = buf32.st_updatedID; buf->st_inheritedRightsMask = buf32.st_inheritedRightsMask; buf->st_originatingNameSpace = buf32.st_originatingNameSpace; return( rc ); }
inline std::size_t get_size(int fd) { struct _stat s; if(0 != _fstat(fd, &s)) return 0u; return (std::size_t)s.st_size; }
/* * ptsname(): return the pathname of the slave pseudo-terminal device * associated with the specified master. */ char* ptsname(int fildes) { static char slave[] = _PATH_DEV PTS_PREFIX "XY"; static char new_slave[] = _PATH_DEV NEWPTS_PREFIX "4294967295"; char* retval; struct stat sbuf; retval = NULL; if (_fstat(fildes, &sbuf) == 0) { if (!ISPTM(sbuf)) { errno = EINVAL; } else { if (!is_pts(fildes)) { (void)snprintf(slave, sizeof(slave), _PATH_DEV PTS_PREFIX "%s", devname(sbuf.st_rdev, S_IFCHR) + strlen(PTM_PREFIX)); retval = slave; } else { (void)snprintf(new_slave, sizeof(new_slave), _PATH_DEV NEWPTS_PREFIX "%s", devname(sbuf.st_rdev, S_IFCHR) + strlen(PTM_PREFIX)); retval = new_slave; } } } return (retval); }
HRESULT CDX9IncludeManager::Open(D3DXINCLUDE_TYPE IncludeType, LPCSTR pName, LPCVOID pParentData, LPCVOID *ppData, UINT *pBytes ) { StdString szPath = pName; struct _stat filestat; int fd; fd = _open( szPath, _O_RDONLY | _O_BINARY ); if( fd == -1 )//error { return E_FAIL; } _fstat( fd, &filestat ); UINT size = filestat.st_size; BYTE* pData = new BYTE[size + 1]; if( pData == NULL ) return E_OUTOFMEMORY; _read( fd, (void*)pData, size ); pData[ size ] = NULL; *ppData = pData; *pBytes = size; _close( fd ); return S_OK; }
/* * Change to dir specified by fd or p->fts_accpath without getting * tricked by someone changing the world out from underneath us. * Assumes p->fts_dev and p->fts_ino are filled in. */ static int fts_safe_changedir(FTS *sp, FTSENT *p, int fd, char *path) { int ret, oerrno, newfd; struct stat sb; newfd = fd; if (ISSET(FTS_NOCHDIR)) return (0); if (fd < 0 && (newfd = _open(path, O_RDONLY | O_DIRECTORY | O_CLOEXEC, 0)) < 0) return (-1); if (_fstat(newfd, &sb)) { ret = -1; goto bail; } if (p->fts_dev != sb.st_dev || p->fts_ino != sb.st_ino) { errno = ENOENT; /* disinformation */ ret = -1; goto bail; } ret = fchdir(newfd); bail: oerrno = errno; if (fd < 0) (void)_close(newfd); errno = oerrno; return (ret); }
int PrivateFileInfoHandler::get_file_time(LPCSTR file_name, FileInfo& file_info) { int fd = -1; fd = _open(file_name, _O_RDONLY); if (fd != -1) { struct _stat file_stat; int result = _fstat(fd, &file_stat); if (result != 0) { // failed to get file status return result; } else { file_info.file_size = file_stat.st_size; file_info.last_modify = file_stat.st_mtime; } _close(fd); fd = -1; } else { //failed to open file } return 0; }
/* * Internal routine to determine `proper' buffering for a file. */ int __swhatbuf(FILE *fp, size_t *bufsize, int *couldbetty) { struct stat st; if (fp->_file < 0 || _fstat(fp->_file, &st) < 0) { *couldbetty = 0; *bufsize = BUFSIZ; return (__SNPT); } /* could be a tty iff it is a character device */ *couldbetty = (st.st_mode & S_IFMT) == S_IFCHR; if (st.st_blksize <= 0) { *bufsize = BUFSIZ; return (__SNPT); } /* * Optimise fseek() only if it is a regular file. (The test for * __sseek is mainly paranoia.) It is safe to set _blksize * unconditionally; it will only be used if __SOPT is also set. */ *bufsize = st.st_blksize; fp->_blksize = st.st_blksize; return ((st.st_mode & S_IFMT) == S_IFREG && fp->_seek == __sseek ? __SOPT : __SNPT); }
int fstat(int fd, struct stat *st) { struct _uzistat s; if (_fstat(fd, &s) != 0) return -1; return statfix(st, &s); }
FILE *watch_fopen(char *filename, time_t *pfile_modify_time) { FILE *fp; char fname[256]; get_gcin_user_or_sys_fname(filename, fname); if ((fp=fopen(fname, "rb"))==NULL) { #if UNIX strcat(strcat(strcpy(fname, TableDir), "/"), filename); #else strcat(strcat(strcpy(fname, TableDir), "\\"), filename); #endif if ((fp=fopen(fname, "rb"))==NULL) return NULL; } #if UNIX struct stat st; fstat(fileno(fp), &st); #else struct _stat st; _fstat(fileno(fp), &st); #endif if (st.st_mtime == *pfile_modify_time) { fclose(fp); return NULL; } *pfile_modify_time = st.st_mtime; return fp; }
static FILE * futx_open(const char *file) { FILE *fp; struct stat sb; int fd; fd = _open(file, O_CREAT|O_RDWR|O_EXLOCK, 0644); if (fd < 0) return (NULL); /* Safety check: never use broken files. */ if (_fstat(fd, &sb) != -1 && sb.st_size % sizeof(struct futx) != 0) { _close(fd); errno = EFTYPE; return (NULL); } fp = fdopen(fd, "r+"); if (fp == NULL) { _close(fd); return (NULL); } return (fp); }
BOOL fsUploadMgr::Zip_AddFile(void *z, LPCSTR pszFile, LPCSTR pszNameInZip) { zipFile zip = (zipFile) z; bool result = false; if (pszNameInZip == NULL) { pszNameInZip = strrchr (pszFile, '\\'); if (pszNameInZip) pszNameInZip++; if (pszNameInZip == NULL) pszNameInZip = pszFile; } zip_fileinfo zfileinfo = {0}; struct _stat file_stat = {0}; FILE* file = fopen (pszFile, "rb"); _fstat (_fileno (file), &file_stat); struct tm* file_time = localtime (&file_stat.st_mtime); tm_zip* zip_time = &zfileinfo.tmz_date; memcpy (zip_time, file_time, sizeof (tm_zip)); const int READ_BUFFER_SIZE = 65535; char read_buf[READ_BUFFER_SIZE]; wchar_t wsz [30000]; MultiByteToWideChar (CP_ACP, 0, pszNameInZip, -1, wsz, 30000); char szNameInZip [30000]; WideCharToMultiByte (CP_OEMCP, 0, wsz, -1, szNameInZip, 30000, NULL, NULL); if (ZIP_OK == zipOpenNewFileInZip (zip, szNameInZip, &zfileinfo, NULL, 0, NULL, 0, NULL, Z_DEFLATED, m_pkg.m_iZipCompressMethod)) { while (!feof (file)) { result = false; size_t count = fread (read_buf, sizeof(char), READ_BUFFER_SIZE, file); if (!ferror (file)) { if (ZIP_OK == zipWriteInFileInZip (zip, read_buf, count)) { result = true; continue; } else break; } else break; } result = result && (Z_OK == zipCloseFileInZip(zip)); } result = result && (0 == fclose (file)); return result; }
inline bool is_normal_file(int fd) { if(_isatty(fd)) return false; struct _stat s; if(0 != _fstat(fd, &s)) return false; return 0 != (s.st_mode & _S_IFREG); }
bool ethash_file_size(FILE* f, size_t* ret_size) { struct _stat st; int fd; if ((fd = _fileno(f)) == -1 || _fstat(fd, &st) != 0) { return false; } *ret_size = st.st_size; return true; }
CTipDlg::CTipDlg(CWnd* pParent /*=NULL*/) : CDialog(IDD_TIP, pParent) { //{{AFX_DATA_INIT(CTipDlg) m_bStartup = TRUE; //}}AFX_DATA_INIT // We need to find out what the startup and file position parameters are // If startup does not exist, we assume that the Tips on startup is checked TRUE. CHexEditApp *aa = dynamic_cast<CHexEditApp *>(AfxGetApp()); m_bStartup = aa->tipofday_; UINT iFilePos = aa->GetProfileInt(szSection, szIntFilePos, 0); const char *tip_name = "HexEdit.tip"; // Now try to open the tips file in the current directory m_pStream = fopen(tip_name, "r"); if (m_pStream == NULL) { // AP: This bit added by me ----------------------------------------------------------- // Not found so try to open from the .exe dir if ((m_pStream = fopen(GetExePath() + tip_name, "r")) == NULL) // ------------------------------------------------------------------------------------ { VERIFY(m_strTip.LoadString(CG_IDS_FILE_ABSENT)); return; } } // If the timestamp in the INI file is different from the timestamp of // the tips file, then we know that the tips file has been modified // Reset the file position to 0 and write the latest timestamp to the // ini file struct _stat buf; _fstat(_fileno(m_pStream), &buf); CString strCurrentTime = ctime(&buf.st_ctime); strCurrentTime.TrimRight(); CString strStoredTime = aa->GetProfileString(szSection, szTimeStamp, NULL); if (strCurrentTime != strStoredTime) { iFilePos = 0; aa->WriteProfileString(szSection, szTimeStamp, strCurrentTime); } if (fseek(m_pStream, iFilePos, SEEK_SET) != 0) { AfxMessageBox(CG_IDP_FILE_CORRUPT); } else { GetNextTipString(m_strTip); } }
/** * FS only need to support MIDLets to quiry the size of the file. * Check the File size by file handle */ long pcsl_file_sizeofopenfile(void *handle) { struct _stat stat_buf; if (_fstat((int)handle, &stat_buf) < 0) { return -1; } return stat_buf.st_size; }
BOOL Stat(PCHAR Filename, struct _stat &Dest) { int client = 0; errno_t err = _sopen_s(&client, Filename, _O_RDONLY,_SH_DENYNO, _S_IREAD | _S_IWRITE); if (err) { return FALSE; } _fstat(client,&Dest); _close(client); return TRUE; }
/* * Allocate a file buffer, or switch to unbuffered I/O. * Per the ANSI C standard, ALL tty devices default to line buffered. * * As a side effect, we set __SOPT or __SNPT (en/dis-able fseek * optimisation) right after the _fstat() that finds the buffer size. */ void __smakebuf( register FILE *fp ) { register size_t size, couldbetty; register void *p; struct stat st; #if 1 if (fp->_flags & __SNBF) { fp->_bf._base = fp->_p = fp->_nbuf; fp->_bf._size = 1; return; } if (fp->_file < 0 || _fstat(fp->_file, &st) < 0) { couldbetty = 0; size = BUFSIZ; /* do not try to optimise fseek() */ fp->_flags |= __SNPT; } else { couldbetty = (st.st_mode & S_IFMT) == S_IFCHR; size = st.st_blksize <= 0 ? BUFSIZ : st.st_blksize; /* * Optimise fseek() only if it is a regular file. * (The test for __sseek is mainly paranoia.) */ if ((st.st_mode & S_IFMT) == S_IFREG && fp->_seek == __sseek) { fp->_flags |= __SOPT; fp->_blksize = st.st_blksize; } else fp->_flags |= __SNPT; } #else couldbetty = 0; size = BUFSIZ; fp->_flags |= __SNPT; #endif if ((p = malloc(size)) == NULL) { fp->_flags |= __SNBF; fp->_bf._base = fp->_p = fp->_nbuf; fp->_bf._size = 1; } else { __cleanup = _cleanup; fp->_flags |= __SMBF; fp->_bf._base = fp->_p = p; fp->_bf._size = size; if (couldbetty && _isatty(fp->_file)) fp->_flags |= __SLBF; } }
static int check_fsid (uid_t uid, gid_t gid) { int fd; char path[] = "/tmp/testfsuidsupp.XXXXXX"; struct stat sb; fd = _mkstemp (path); _fstat (fd, &sb); _unlink (path); return (sb.st_uid == uid && sb.st_gid == gid); }
BOOL CExecCommand::wait( BOOL bCapture) { int nResult = -1; DWORD dwExitCode, dwTime = 0; char bBuffer[1024]; int nLength; struct _stat fsOut; m_csOutput = ""; // If capturing stdout/stderr enabled, and timeout is not reached while (bCapture && (dwTime < m_dwTimeout)) { // Each 200 millisecond, store stdout and stderr dwTime += 200; if (WaitForSingleObject( m_hProcessHandle, 200) == WAIT_FAILED) { m_csOutput.Format( "WaitForSingleObject Error: %s", GetAnsiFromUnicode( LookupError( GetLastError()))); m_nExitValue = -1; return -1; } // Read console output if ((_fstat( m_fdStdOut, &fsOut) == 0 ) && (fsOut.st_size > 0)) { // There is something to read nLength = _read( m_fdStdOut, bBuffer, 1023); bBuffer[nLength] = 0; m_csOutput.AppendFormat( "%s", bBuffer); } // Check if process still active if (!GetExitCodeProcess( m_hProcessHandle, &dwExitCode) || (dwExitCode != STILL_ACTIVE)) { // Process not active, exit loop break; } } // Wait for process ending (not capturing output or error capturing output) if (GetExitCodeProcess( m_hProcessHandle, &dwExitCode)) { nResult = dwExitCode; } else { m_csOutput.Format( "GetExitCode Error: %s", GetAnsiFromUnicode( LookupError( GetLastError()))); nResult = -1; } m_nExitValue = nResult; return (nResult >= 0); }
int my_fstat( int fd, struct my_stat *st ) { DISABLE_ERRORS; int result = _fstat( fd, (struct _stat *)st ); if(result < 0) { my_errno = errno; } else { my_errno = 0; } D(bug("fstat(%d) = %d\n", fd, result)); if(result >= 0) dump_stat( st ); RESTORE_ERRORS; return result; }
/* Make a file seekable, using temporary files if necessary */ FILE *seekable(FILE *fp) { #ifndef MSDOS FILE *ft; long r, w ; #endif char *p; char buffer[BUFSIZ] ; #if defined(WINNT) || defined(WIN32) struct _stat fs ; #else long fpos; #endif #if defined(WINNT) || defined(WIN32) if (_fstat(fileno(fp), &fs) == 0 && (fs.st_mode&_S_IFREG) != 0) return (fp); #else if ((fpos = ftell(fp)) >= 0) if (!fseek(fp, 0L, SEEK_END) && !fseek(fp, fpos, SEEK_SET)) return (fp); #endif #if defined(MSDOS) message(FATAL, "input is not seekable\n"); return (NULL) ; #else if ((ft = tmpfile()) == NULL) return (NULL); while ((r = fread(p = buffer, sizeof(char), BUFSIZ, fp)) > 0) { do { if ((w = fwrite(p, sizeof(char), r, ft)) == 0) return (NULL) ; p += w ; r -= w ; } while (r > 0) ; } if (!feof(fp)) return (NULL) ; /* discard the input file, and rewind the temporary */ (void) fclose(fp); if (fseek(ft, 0L, SEEK_SET) != 0) return (NULL) ; return (ft); #endif }
/* Stat a file descriptor ================================================== */ int Downhill_File_StatFd(int file_Fd,struct Downhill_File_Stat* file_Info) { struct _stat file_InfoReal; /* Get the fstat information */ if (_fstat(file_Fd,&file_InfoReal) != 0) { return -1; } /* Fill in the structure */ downhill_Stat_Create(file_Info,&file_InfoReal); return 0; }
int VisItFstat(int fd, VisItStat_t *buf) { #if defined(_WIN32) return _fstat(fd, buf); #else #if SIZEOF_OFF64_T > 4 return fstat64(fd, buf); #else return fstat(fd, buf); #endif #endif }
/* * Open a directory with existing file descriptor. */ DIR * fdopendir(int fd) { struct stat statb; /* Check that fd is associated with a directory. */ if (_fstat(fd, &statb) != 0) return (NULL); if (!S_ISDIR(statb.st_mode)) { errno = ENOTDIR; return (NULL); } if (_fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) return (NULL); return (__opendir_common(fd, NULL, DTF_HIDEW|DTF_NODUP)); }
CTipDlg::CTipDlg(CWnd* pParent /*=NULL*/) : CDialog(IDD_TIP, pParent) { //{{AFX_DATA_INIT(CTipDlg) m_bStartup = TRUE; //}}AFX_DATA_INIT // We need to find out what the startup and file position parameters are // If startup does not exist, we assume that the Tips on startup is checked TRUE. CWinApp* pApp = AfxGetApp(); m_bStartup = !pApp->GetProfileInt(szSection, szIntStartup, 0); UINT iFilePos = pApp->GetProfileInt(szSection, szIntFilePos, 0); // Now try to open the tips file m_pStream = fopen("Tips.txt", "r"); if (m_pStream == NULL) { m_strTip.LoadString(CG_IDS_FILE_ABSENT); return; } // If the timestamp in the INI file is different from the timestamp of // the tips file, then we know that the tips file has been modified // Reset the file position to 0 and write the latest timestamp to the // ini file struct _stat buf; _fstat(_fileno(m_pStream), &buf); CString strCurrentTime = ctime(&buf.st_ctime); strCurrentTime.TrimRight(); CString strStoredTime = pApp->GetProfileString(szSection, szTimeStamp, NULL); if (strCurrentTime != strStoredTime) { iFilePos = 0; pApp->WriteProfileString(szSection, szTimeStamp, strCurrentTime); } if (fseek(m_pStream, iFilePos, SEEK_SET) != 0) { AfxMessageBox(CG_IDP_FILE_CORRUPT); } else { GetNextTipString(m_strTip); } }
int setutxdb(int db, const char *file) { struct stat sb; switch (db) { case UTXDB_ACTIVE: if (file == NULL) file = _PATH_UTX_ACTIVE; break; case UTXDB_LASTLOGIN: if (file == NULL) file = _PATH_UTX_LASTLOGIN; break; case UTXDB_LOG: if (file == NULL) file = _PATH_UTX_LOG; break; default: errno = EINVAL; return (-1); } if (uf != NULL) fclose(uf); uf = fopen(file, "r"); if (uf == NULL) return (-1); if (db != UTXDB_LOG) { /* Safety check: never use broken files. */ if (_fstat(fileno(uf), &sb) != -1 && sb.st_size % sizeof(struct futx) != 0) { fclose(uf); uf = NULL; errno = EFTYPE; return (-1); } /* Prevent reading of partial records. */ (void)setvbuf(uf, NULL, _IOFBF, rounddown(BUFSIZ, sizeof(struct futx))); } udb = db; return (0); }