United States    
COMPAQ STORE | PRODUCTS |
SERVICES | SUPPORT | CONTACT US | SEARCH
Compaq C

Compaq C
Run-Time Library Reference Manual for OpenVMS Systems


Previous Contents Index

1.6.2.1 Accessing RMS Files in Stream Mode

Stream access to RMS files is done with the block I/O facilities of RMS. Stream input is performed from RMS files by passing each byte of the on-disk representation of the file to your program. Stream output to RMS files is done by passing each byte from your program to the file. The Compaq C RTL performs no special processing on the data.

When opening a file in stream mode, the Compaq C RTL allocates a large internal buffer area. Data is read from the file using a single read into the buffer area and then passing the data to your program as needed. Data is written to the file when the internal buffer is full or when the fflush function is called.

1.6.2.2 Accessing RMS Record Files in Record Mode

Record access to record files is done with the record I/O facilities of RMS. The Compaq C RTL emulates a byte stream by translating carriage-control characters during the process of reading and writing records. Random access is allowed to all record files, but positioning (with fseek and lseek ) must be on a record boundary for VFC files, variable record files, or files with non-null carriage control. Positioning a record file causes all buffered input to be discarded and buffered output to be written to the file.

Record input from RMS record files is emulated by the Compaq C RTL in two steps:

  1. The Compaq C RTL reads a logical record from the file.
    If the record format is variable length with fixed control (RFM = VFC), and the record attributes are not print carriage control (RAT is not PRN), then the Compaq C RTL concatenates the fixed-control area to the beginning of the record.
  2. The Compaq C RTL expands the record to simulate a stream of bytes by translating the record's carriage-control information (if any).

In RMS terms, the Compaq C RTL translates the record's carriage-control information using one of the following methods:

As you read from the file, the Compaq C RTL delivers a stream of bytes resulting from the translations. Information that is not read from an expanded record by one function call is delivered on the next input function call.

The Compaq C RTL performs record output to RMS record files in two steps.

The first part of the record output emulation is the formation of a logical record. As you write bytes to a record file, the emulator examines the information being written for record boundaries. The handling of information in the byte stream depends on the attributes of the destination file or device, as follows:

The second part of record output emulation is to write the logical record formed during the first step. The Compaq C RTL forms the output record as follows:

1.6.2.2.1 Accessing Variable-Length or VFC Record Files in Record Mode

When you access a variable-length or VFC record file in record mode, many I/O functions behave differently than they would if they were being used with stream mode. This section describes these differences.

In general, the new-line character (\n) is the record separator for all record modes. On output, when a new-line character is encountered, a record is generated unless you specify an optional argument (such as "ctx=bin" or "ctx=xplct") that affects the interpretation of new lines.

The read and decc$record_read functions always read at most one record. The write and decc$record_write functions always generate at least one record.

decc$record_read and decc$record_write are equivalent, respectively, to read and write , except that they work with file pointers rather than file descriptors.

Unlike the read function which reads at most one record, the fread function can span records. Rather than read number_items records (where number_items is the third parameter to fread ), fread tries to read the number of bytes equal to number_items x size_of_item (where size_of_item is the second parameter to fread ). The value returned by fread is equal to the number of bytes read divided by size_of_item.

However, the fwrite function always generates at least number_items records.

The fgets and gets functions read to either a new-line character or a record boundary.

The fflush function always generates a record if there is unwritten data in the buffer. The same is true of close , fclose , fseek , lseek , rewind , and fsetpos , all of which perform implicit fflush functions.

A record is also generated whenever an attempt is made to write more characters than allowed by the maximum record size.

For more information on these functions, see the Reference Section.

1.6.2.2.2 Accessing Fixed-Length Record Files in Record Mode

When accessing a fixed-length record file in record mode, the I/O functions generally behave as described in Section 1.6.2.2.1.

The write , fwrite , and decc$record_write functions will fail if given a record size that is not an integral multiple of the maximum record size, unless the file was opened with the "ctx=xplct" optional argument specified. All other output functions will generate records at every nth byte, where n is the maximum record size.

If a new record is forced by fflush , the data in the buffer is padded to the maximum record size with null characters.

Note

This padding can cause problems for programs that seek to the end-of-file. For example, if a program were to append data to a file, then seek backwards in the file (causing an fflush to occur), and then seek to the end-of-file again, a zero-filled "hole" will have been created between the previous end-of-file and the new end-of-file if the previous end-of-file was not on a record boundary.

1.6.2.3 Example---Difference Between Stream Mode and Record Mode

Example 1-1 demonstrates the difference between stream mode and record mode access.

Example 1-1 Differences Between Stream Mode and Record Mode

/*      CHAP_1_STREAM_RECORD.C                       */ 
 
/* This program demonstrates the difference between  */ 
/* record mode and stream mode Input/Output.         */ 
 
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
 
void process_records(const char *fspec, FILE * fp); 
 
main() 
{ 
    FILE *fp; 
 
    fp = fopen("example-fixed.dat", "w", "rfm=fix", "mrs=40", "rat=none"); 
    if (fp == NULL) { 
        perror("example-fixed"); 
        exit(EXIT_FAILURE); 
    } 
    printf("Record mode\n"); 
    process_records("example-fixed.dat", fp); 
    fclose(fp); 
 
    printf("\nStream mode\n"); 
    fp = fopen("example-streamlf.dat", "w"); 
    if (fp == NULL) { 
        perror("example-streamlf"); 
        exit(EXIT_FAILURE); 
    } 
    process_records("example-streamlf.dat", fp); 
    fclose(fp); 
} 
 
void process_records(const char *fspec, FILE * fp) 
{ 
    int i, 
        sts; 
 
    char buffer[40]; 
 
    /* Write records of all 1's, all 2's and all 3's */ 
    for (i = 0; i < 3; i++) { 
        memset(buffer, '1' + i, 40); 
        sts = fwrite(buffer, 40, 1, fp); 
        if (sts != 1) { 
            perror("fwrite"); 
            exit(EXIT_FAILURE); 
        } 
    } 
 
    /* Rewind the file and write 10 characters of A's, then 10 B's, */ 
    /* then 10 C's.                                                 */ 
    /*                                                              */ 
    /* For stream mode, each fwrite call outputs 10 characters      */ 
    /* and advances the file position 10 characters                 */ 
    /* characters.                                                  */ 
    /*                                                              */ 
    /* For record mode, each fwrite merges the 10 characters  into  */ 
    /* the existing 40-character record, updates the record and     */ 
    /* advances the file position 40 characters to the next record. */ 
    rewind(fp); 
    for (i = 0; i < 3; i++) { 
        memset(buffer, 'A' + i, 10); 
        sts = fwrite(buffer, 10, 1, fp); 
        if (sts != 1) { 
            perror("fwrite2"); 
            exit(EXIT_FAILURE); 
        } 
    } 
 
    /* Now reopen the file and output the records. */ 
   
    fclose(fp); 
    fp = fopen(fspec, "r"); 
    for (i = 0; i < 3; i++) { 
        sts = fread(buffer, 40, 1, fp); 
        if (sts != 1) 
            perror("fread"); 
            printf("%.40s\n", buffer); 
    } 
 
    return; 
} 

Running this program produces the following output:


Record Mode 
AAAAAAAAAA111111111111111111111111111111 
BBBBBBBBBB222222222222222222222222222222 
CCCCCCCCCC333333333333333333333333333333 
 
Stream mode 
AAAAAAAAAABBBBBBBBBBCCCCCCCCCC1111111111 
2222222222222222222222222222222222222222 
3333333333333333333333333333333333333333 

1.7 Specific Portability Concerns

One of the last tasks in preparing to use the Compaq C RTL, if you are going to port your source programs across systems, is to be aware of specific differences between the Compaq C RTL and the run-time libraries of other implementations of the C language. This section describes some of the problems that you might encounter when porting programs to and from an OpenVMS system. Although portability is closely tied to the implementation of the Compaq C RTL, this section also contains information on the portability of other Compaq C for OpenVMS constructs.

The Compaq C RTL provides ANSI C defined library functions as well as many commonly available APIs and a few OpenVMS extensions. See Section 1.5 for specific standards, portions of which are implemented by the Compaq C RTL. Attempts have been made to maintain complete portability in functionality whenever possible. Many of the Standard I/O and UNIX I/O functions and macros contained in the Compaq C RTL are functionally equivalent to those of other implementations.

The RTL function and macro descriptions elaborate on issues presented in this section and describe concerns not documented here.

The following list documents issues of concern if you wish to port C programs to the OpenVMS environment:

1.7.1 Reentrancy

The Compaq C RTL supports an improved and enhanced reentrancy. The following types of reentrancy are supported:

The default reentrancy type is TOLERANT.

You can set the reentrancy type by compiling with the /REENTRANCY command-line qualifier or by calling the decc$set_reentrancy function. This function must be called exclusively from non-AST level.

When programming an application using multiple threads or ASTs, you should consider three classes of functions:

Most functions have no internal data at all. For these functions, synchronization is necessary only if the parameter is used by the application in multiple threads or in both AST and non-AST context. For example, although the strcat function is ordinarily safe, the following is an example of unsafe usage:


extern char buffer[100]; 
void routine1(char *data) { 
    strcat( buffer, data ); 
} 

If routine1 executed concurrently in multiple threads, or if routine1 is interrupted by an AST routine that calls it, the results of the strcat call are unpredictable.

The second class of functions are those that have thread-local static data. Typically, these are routines in the library that return a string where the application is not permitted to free the storage for the string. These routines are thread-safe but not AST-reentrant. This means they can safely be called concurrently, and each thread will have its own copy of the data. They cannot be called from AST routines if it is possible that the same routine was executing in non-AST context. The routines in this class are:


asctime        stat 
clock          strerror  
ctermid        strtok 
ctime          times 
cuserid        VAXC$ESTABLISH 
gmtime         the errno variable 
localtime      wcstok 
perror                

All the socket functions are also included in this list if the TCP/IP product in use is thread-safe.

The third class of functions are those that affect process-wide data. These functions are neither thread-safe nor AST-reentrant. For example, sigsetmask establishes the process-wide signal mask. Consider a routine like the following:


void update_data 
base() 
{ 
    int old_mask; 
 
    old_mask = sigsetmask( 1 << (SIGINT - 1)); 
        /* Do work here that should not be aborted. */ 
    sigsetmask( old_mask ); 
} 

If update_database was called concurrently in multiple threads, thread 1 might unblock SIGINT while thread 2 was still performing work that should not be aborted.

The routines in this class are:

Note

Generally speaking, UTC-based time functions can affect in-memory time-zone information, which is process-wide data. However, if the system time zone remains the same during the execution of the application (which is the common case) and the cache of timezone files is enabled (which is the default), then the _r variant of the time functions asctime_r , ctime_r , gmtime_r and localtime_r is both thread-safe and AST-reentrant.

If, however, the system time zone can change during the execution of the application or the cache of timezone files is not enabled, then both variants of the UTC-based time functions belong to the third class of functions, which are neither thread-safe nor AST-reentrant.


Previous Next Contents Index
  

1.800.AT.COMPAQ

privacy and legal statement