// _stattest.cpp : compare MS implementation of functions stat() and fstat() // rudif@bluemail.ch 1 Jul 2001 #include #include #include #include #include #include void main( void ) { const char *filename = "c:\\perl\\bin\\perl.exe"; printf( "File name : %s\n", filename ); { static struct stat buf; // initializes to 0 if ( stat( filename, &buf ) != 0 ) perror( "Problem with stat()" ); else { printf( "\nFrom MSDN page on _stat():\n" " Get status information on a file.\n" " st_mode\n" " Bit mask for file-mode information.\n" " The _S_IFDIR bit is set if path specifies a directory;\n" " the _S_IFREG bit is set if path specifies an ordinary file or a device.\n" " User read/write bits are set according to the file’s permission mode;\n" " user execute bits are set according to the filename extension.\n" ); printf( "Mode (oct) : 0%06o\n", (unsigned short)buf.st_mode ); printf( "Mode (hex) : 0x%04x\n", (unsigned short)buf.st_mode ); } } { int fh; static struct stat buf; // initializes to 0 if ( (fh = open(filename, _O_RDONLY)) == -1 ) perror( "Problem with open()" ); else if ( fstat( fh, &buf ) != 0 ) perror( "Problem with fstat()" ); else { printf( "\nFrom MSDN page on _fstat():\n" " Get information about an open file.\n" " st_mode\n" " Bit mask for file-mode information.\n" " The _S_IFCHR bit is set if handle refers to a device.\n" " The _S_IFREG bit is set if handle refers to an ordinary file.\n" " The read/write bits are set according to the file's permission mode.\n" " _S_IFCHR and other constants are defined in SYS\\STAT.H.\n" ); printf( "Mode (oct) : 0%06o\n", (unsigned short)buf.st_mode ); printf( "Mode (hex) : 0x%04x\n", (unsigned short)buf.st_mode ); } } }