Open System Services Programmer's Guide

Example 19 Reading a Guardian File With an OSS Function Call
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
char buffer[1026]; /* allow for NULL termination of string in buffer */
char *filename; /* filename pointer */
int fd; /* file descriptor */
int nbytes; /* number of bytes read */
int main(int argc, char *argv[]) {
/* Check for the proper number of arguments; if no filename
is supplied, open the default file. */
if(argc > 1)
filename = argv[1];
else
filename = "/G/system/system/stdioh"; /* this file is opened if
no name is supplied */
fd = open(filename, O_RDONLY); /* open Guardian file for read */
/* Check for the validity of the file descriptor; if it is not valid,
print a message and exit. */
if(fd < 0) {
fprintf(stderr,"Can't open %s\n", filename);
exit(1);
}
/* If the read is unsuccessful, print an error message and exit. */
nbytes = read(fd, buffer, 1024);
if (nbytes < 0) {
perror("File read error: ");
exit(1);
}
/* Print the number of bytes read and the first part of the file. */
printf("Number of bytes read = %d\n", nbytes);
buffer[nbytes] = 0; /* make string NULL-terminated */
printf("%s\n", buffer);
return(0);
}
Using Functions With Environment-Specific Parameters
There are several file system functions that allow you to operate on a file in one environment from
a process running in the other environment. These functions are freopen(), remove(),
rename(), tmpfile(), and tmpnam(). Each of these functions has environment-specific variants
with suffixes to indicate which type of file you want to operate on. For example,
fopen_guardian() opens a Guardian file from an OSS process and fopen_oss() opens an
OSS file from a Guardian native process.
NOTE: If a TNS module calls any of the environment-specific variants, the feature test macro
_INTEROPERABLE should be specified when the module is compiled. Specifying _INTEROPERABLE
allows subsequent I/O function calls to know which type of I/O stream they are operating on.
Example 20 opens a Guardian file using fopen_guardian() in an OSS module and opens an
OSS file using fopen().
Accessing Files From the OSS API 67