Open System Services Porting Guide (G06.29+, H06.06+, J06.03+)
• Precision is lost in shifting. For example, this existing code fragment assumes that off_t is
the same size as int:
off_t offset;
int blkno;
offset = blkno << BLOCK_SHIFT;
In the large file compilation environment, the codes should be as follows:
off_t offset;
int blkno;
offset = (off_t)blkno << BLOCK_SHIFT;
Typecasting blkno forces its promotion to a 64-bit value before the shift takes place. Without
the typecasting, the shift would occur on blkno as an int (32-bits), and then the value would
be promoted to a 64-bit value.
• Types are embedded in generic library calls. For example, this existing printf() call:
printf(“%d\n”,offset);
works for a 32-bit offset, but it does not work for a 64-bit offset. In the large file compilation
environment, offset requires %lld. For portability, use #define for calls that depend on the
value of _FILE_OFFSET_BITS. For example:
/** Define in header file or top of source **/
#ifdef _FILE_OFFSET_BITS ==64
#define offset_format “%lld”
#else
#define offset_format “%ld”
#endif
...
/** Somewhere in program **/
printf(offset_format “\n”, offset);
Conversion Issues for fseek() and ftell()
You must decide how to migrate code that uses the fseek() or ftell() functions. This source
code, even if strict typing is used, can not be converted by using the _FILE_OFFSET_BITS 64 compile
option only.
One option is to change the ftell() and fseek() calls to ftello() and fseeko(),
respectively. You also must ensure that the types for appropriate variables are changed from long
to off_t. For example, this code fragment:
long int offset;
int rslt;
offset = ftell(fd);
if(offset != -1)
rslt = fseek(fd,offset,SEEK_CUR);
Must be changed to this code fragment:
off_t offset;
int rslt;
offset = ftello(fd);
if(offset != -1)
rslt = fseeko(fd,offset,SEEK_CUR);
Another option is to use the available macros to determine what types and interfaces are to be
used. The code fragment in Example 2 (page 194) demonstrates a way to address this situation
and maintain portability. The code fragment works both in environments that include support for
large files and in environments that do not include support for large files.
Conversion Issues for fseek() and ftell() 193