[cs631apue] Blocksize considerations

Sean McCoy smccoy at stevens.edu
Fri Oct 21 22:14:32 EDT 2011

I'm having some trouble understanding how to handle the -s flag 
properly. For reference, -s is defined as follows:

Display the number of file system blocks actually used by each file, in 
units of 512 bytes or BLOCKSIZE (see  ENVIRONMENT) where partial units 
are rounded up to the next integer value."

There are two relevant pieces of information that I see in the man page 
for stat(2):

"The st_size field gives the size of the file (if it is a regular file 
or a symbolic link) in bytes.
The st_blocks field indicates the number of blocks allocated to the 
file, 512-byte units. (This may be smaller than st_size/512, for 
example, when the file has holes.) "

Given the requirement to use the BLOCKSIZE environment variable, each of 
these options presents a problem:
1) If I choose to use st_blocks as the basis for my blocksize output, I 
may introduce errors based on partially used blocks.
2) If I choose to use st_size as the basis for my blocksize output, I 
may have inaccurate (too large) results for sparse files.

Which type of error is preferable for this assignment? Or is there a way 
to avoid it?

-Sean McCoy

More information about the cs631apue mailing list