stat() and "st_blocks".

This is what I had to do to a UNIX code to compile on QNX4:

if (stat(file, &sb) < 0)
return (-1);
+#ifndef QNX
/* For sparse files, return the size based on number of blocks
used. */
if (sb.st_size / DEV_BSIZE > sb.st_blocks)
return (sb.st_blocks * DEV_BSIZE);
else
+#endif
return (sb.st_size);

First I did try to #include <machine/param.h> to have DEV_BSIZE defined,
then I removed it for I got the error of not having the “st_blocks” in the
union…

Is it OK?
Is there a cleaner work-around?

Tony.

Tony wrote:

This is what I had to do to a UNIX code to compile on QNX4:
if (stat(file, &sb) < 0)
return (-1);
+#ifndef QNX
/* For sparse files, return the size based on number of blocks
used. */
if (sb.st_size / DEV_BSIZE > sb.st_blocks)
return (sb.st_blocks * DEV_BSIZE);
else
+#endif
return (sb.st_size);
First I did try to #include <machine/param.h> to have DEV_BSIZE
defined, then I removed it for I got the error of not having the
“st_blocks” in the union…

Is it OK?
Is there a cleaner work-around?

QNX4 does not have ‘st_blocks’ (but then neither does it have sparse
files). However, the meaning of the ‘st_size’ field is based on
S_ISBLK(st_mode) or not. If it is a block device, then st_size is
the number of 512-byte blocks; otherwise it is the size in bytes.
This is done for overflow reasons, so if you multiple it out (to
always return bytes) be wary of >4GB disk. If you’re only ever
using this code for regular files you are OK to just use ‘st_size’.

On Thu, 12 May 2005 23:40:49 +0400, John Garvey <jgarvey@qnx.com> wrote:

…snip…the meaning of the ‘st_size’ field is based on
S_ISBLK(st_mode) or not. If it is a block device, then st_size is the
number of 512-byte blocks; otherwise it is the size in bytes.
OK.

What is special about the disks larger than 4GB?
I thought “INT_MAX 512-byte blocks” is far more then 4GB… And 4GB is
just 8388608 (decimal) 512-byte blocks…

Now, should I #include <machine/param.h> to get DEV_BSIZE (is this it,
BTW?) or I may allways assume “512_bytes_per_block”?

Tony.

Tony wrote:

On Thu, 12 May 2005 23:40:49 +0400, John Garvey <> jgarvey@qnx.com> > wrote:
…snip…the meaning of the ‘st_size’ field is based on
S_ISBLK(st_mode) or not. If it is a block device, then st_size is the
number of 512-byte blocks; otherwise it is the size in bytes.
OK.
What is special about the disks larger than 4GB?
I thought “INT_MAX 512-byte blocks” is far more then 4GB… And 4GB is
just 8388608 (decimal) 512-byte blocks…

But INT_MAX 512-byte blocks as bytes (if you do the multiplication
from that code snippet) … ! Dealing in block units saves you 9 bits.

Now, should I #include <machine/param.h> to get DEV_BSIZE (is this it,
BTW?) or I may allways assume “512_bytes_per_block”?

In QNX4 all disk-related blocks are 512-byte units, always.