Open System Services Programmer's Guide
Example 22 Reading an OSS File With Guardian Procedures
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tal.h>
#include <cextdecs.h(FILE_OPEN_,READX)>
_cc_status CC;
short filenum, bytesread, retcode;
char *pathname;
char buffer[1025]; /* allow for NULL-terminated string */
int main(int argc, char *argv[]) {
int i;
/* If no pathname is supplied, use the default name. */
if (argc > 1)
pathname = argv[1];
else
pathname = "/usr/include/stdio.h";
/* Invoke the Guardian procedure. Note the special value of options. */
retcode = FILE_OPEN_(pathname, /* name of OSS file */
, /* length of pathname not needed */
&filenum, /* file number returned */
1, /* open for read */
, /* shared exclusion */
, /* nowait depth */
, /* sync or receive depth */
040, /* <10>=1 indicates OSS pathname */
);
/* If the open fails, print a message and exit. */
if (retcode != 0) {
fprintf(stderr, "retcode = %d\n", retcode);
fprintf(stderr, "Cannot open %s\n", pathname);
exit(1);
}
/* Read the file with the Guardian READX procedure. */
CC = READX( filenum, /* file number */
buffer, /* buffer address */
1024, /* size of buffer */
&bytesread, /* number of bytes read */
);
/* If the condition code returned is not the one for success,
print a message and exit. */
if(_status_ne(CC)) {
fprintf(stderr, "Read error, bytes read = %d\n", bytesread);
exit(1);
}
printf("Bytes read = %d\n", bytesread);
/* Print out the first ten bytes in octal plus "string". */
printf("First 10 bytes read (in octal): ");
for (i = 0; i < 10; i++)
printf("%o ", buffer[i]);
printf("\n");
buffer[bytesread] = '\000'; /* make string NULL-terminated */
printf("\n%s\n", buffer);
return(0);
}
Accessing Files From the Guardian API 73