Board index » cppbuilder » _stat() giving incorrect results

_stat() giving incorrect results


2005-10-19 10:51:00 AM
cppbuilder102
Dear all,
I am porting an application from VC6 to BCB5. An error is encountered when
a call to the C library function _stat(). The code fragment is something
like:
:
#ifdef _WIN32
#include <sys/stat.h>
#include <sys/types.h>
:
long GetFileSize(char *szFName)
{
buf[MAXPATHLEN] = {'\0'};
struct _stat statbuf;
if(_stat(buf, &statbuf))
return 0;
return (long)statbuf.st_size;
}
The contents of statbuf in VC6 and BCB5 are significantly different after
the call to _stat().
In essence in VC6 the statbuf.st_size is 417284, which is correct. In BCB5
the statbuf.st_size is -708050938L (0xD5CC0006), causing a failure in memory
allocation afterward.
Can anybody kindly indicate where the problem is?
Thanks in advance,
Patrick
 
 

Re:_stat() giving incorrect results

"Patrick Wong" < XXXX@XXXXX.COM >wrote in message
Quote
Can anybody kindly indicate where the problem is?
set data alignment to Word or Byte. BCB use QuadWord as default
data alignment, and compiler inserts padding bytes inside _stat structure
Cheers,
Serge
 

Re:_stat() giving incorrect results

Thanks for your response
"Sergiy Kanilo" < XXXX@XXXXX.COM >wrote in message
Quote

set data alignment to Word or Byte.

Would you please show me how to do this programmatically?
Alternatively, when I searched the BCB Help for _stat with no luck, I change
struct stat with struct stati64
and
if(_stat(buf, &statbuf)) with if(_stati64(buf, &statbuf))
This works.
 

{smallsort}

Re:_stat() giving incorrect results

Patrick Wong wrote:
Quote
Dear all,

I am porting an application from VC6 to BCB5. An error is encountered when
a call to the C library function _stat(). The code fragment is something
like:
Correct name of the function is stat, not _stat. That name with
underscore is MS relict. As for packing issue you can use something like
this:
#pragma option push -a8
//code
#pragma option pop
 

Re:_stat() giving incorrect results

Sergiy Kanilo wrote:
Quote
set data alignment to Word or Byte. BCB use QuadWord as default
data alignment, and compiler inserts padding bytes inside _stat structure

Cheers,
Serge


I would be surprised if the compiler-provided headers don't pack the
structures automatically via #pragma option.
I would advise against setting the project-wide alignment option to be
different from 8 bytes due to the possible slower memory access.
.a
 

Re:_stat() giving incorrect results

"Alex Bakaev [TeamB]" < XXXX@XXXXX.COM >wrote in message
Quote
Sergiy Kanilo wrote:

>set data alignment to Word or Byte. BCB use QuadWord as default
>data alignment, and compiler inserts padding bytes inside _stat structure
>
>Cheers,
>Serge
I would be surprised if the compiler-provided headers don't pack the
structures automatically via #pragma option.
RW library (at lest in BCB5) provides such pragmas for POSIX's
struct stat, but not for MS's struct _stat.
Cheers,
Serge
 

Re:_stat() giving incorrect results

Sergiy Kanilo wrote:
Quote
RW library (at lest in BCB5) provides such pragmas for POSIX's
struct stat, but not for MS's struct _stat.
It's a bug, then.
.a
 

Re:_stat() giving incorrect results

Sergiy Kanilo wrote:
Quote
RW library (at lest in BCB5) provides such pragmas for POSIX's
struct stat, but not for MS's struct _stat.
Use fstat, stat and struct stat instead of the _ versions.
The non-underlined versions are the ones documented in K&R, and work
fine.
You might also try this test and inspect Stat as you step through it
in de{*word*81}....In theory, s and t will be the same.
(I can't get it to mess up, and there are no pragmas around the struct
_stat in the headers either)
#include <sys/stat.h>
//#include <assert.h>
union{
struct _stat s;
struct stat t;
}Stat;
int main(void)
{// assert( sizeof( struct _stat) == sizeof( struct stat) );
Stat.s.st_dev =1;
Stat.s.st_ino =2;
Stat.s.st_mode =3;
Stat.s.st_nlink =4;
Stat.s.st_uid =5;
Stat.s.st_gid =6;
Stat.s.st_rdev =7;
Stat.s.st_size =8;
Stat.s.st_atime =9;
Stat.s.st_mtime =10;
Stat.s.st_ctime =11;
return stat( 0, &Stat.t); // stop here and compare s and t
}