Open System Services Porting Guide (G06.29+, H06.06+, J06.03+)

Example 4 Program Converted For a Transitional Compilation Environment
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#define _LARGEFILE64_SOURCE 1
/** You can specify this macro as a comiler option **/
/** instead of including it in the source code. **/
print_byte(name,offset)
char *name;
off64_t offset;
{
int fd;
off64_t ret;
if((fd = open64(name,O_RDONLY)) == -1) {
printf(Open Failed\n);
return(-2);
}
if((ret=lseek64(fd, offset, SEEK_SET)) == -1) {
printf(lseek failed\n);
return(-3);
}
/** Print ret **/
printf(lseek returned %lld\n,ret);
}
save(name)
char *name;
{
struct stat64 statb;
off64_t x;
if(stat64(name,&statb) == -1) {
printf(Stat Failed\n);
return(-1);
}
x = statb.st_size/2;
print_byte(name,x);
}
main()
{
char *name=Fred;
save(name);
}
Program Converted For Large File Compilation Environment
Example 5 (page 197) shows the program as converted in the large file compilation environment.
You might assume that there is nothing to change from the original version in Example 3, because
the existing code already uses the POSIX API, but this assumption is incorrect. Although the program
in Example 3 works when compiled in the regular compilation environment, it doesn’t work when
compiled in the large file compilation environment, because the program implicitly assumes that
st_size can be assigned to an int (x), and that x/2 can be passed to a routine that takes an
int as parameter. In a regular compilation environment, these assumptions are correct (although
it is not a good practice to rely on implicit assumptions). However, in the large file compilation
environment, these assumptions are wrong.
The program in Example 5 (page 197) could be compiled and run in either the regular or the large
file compilation environment if you specify #define _FILE_OFFSET_BITS 64 as a compiler
196 Porting Applications to Support Large OSS Files