[an error occurred while processing this directive]

HP OpenVMS Systems Documentation

Content starts here HP C

HP C
Run-Time Library Reference Manual for OpenVMS Systems


Previous Contents Index

1.8.2 Access to RMS Files

RMS sequential files can be opened in record mode or stream mode. By default, STREAM_LF files are opened in stream mode; all other file types are opened in record mode. When opening a file, you can override these defaults by specifying the optional argument "ctx=rec" to force record mode, or "ctx=stm" to force stream mode. RMS relative and indexed files are always opened in record mode. The access mode determines the behavior of various I/O functions in the HP C RTL.

One of the file types defined by RMS is an RMS-11 stream format file, corresponding to a value of FAB$C_STM for the record format. The definition of this format is such that the RMS record operation SYS$GET removes leading null bytes from each record. Because this file type is processed in record mode by the HP C RTL, it is unsuitable as a file format for binary data unless it is explicitly opened with "ctx=stm", in which case the raw bytes of data from the file are returned.

Note

In OpenVMS Version 7.0 the default LRL value on stream files was changed from 0 to 32767. This change caused significant performance degradation on certain file operations such as sort.

This is no longer a problem. The HP C RTL now lets you define the logical DECC$DEFAULT_LRL to change the default record-length value on stream files.

The HP C RTL first looks for this logical. If it is found and it translates to a numeric value between 0 and 32767, that value is used for the default LRL.

To restore the behavior prior to OpenVMS Version 7.0, enter the following command:


$ DEFINE DECC$DEFAULT_LRL 0

1.8.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 HP C RTL performs no special processing on the data.

When opening a file in stream mode, the HP 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.8.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 HP 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 HP C RTL in two steps:

  1. The HP 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 HP C RTL concatenates the fixed-control area to the beginning of the record.
  2. The HP 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 HP C RTL translates the record's carriage-control information using one of the following methods:

  • If the record attribute is implied carriage control (RAT = CR), then the HP C RTL appends a new-line character to the record.
    This new-line character is considered an integral part of the record, which means for example, that it can be obtained by the fgetc function and is considered a line terminator by the fgets function. Since fgets reads the file up to the new-line character, for RAT=CR files this function cannot retrieve a string that crosses the record boundaries.
  • If the record attributes are print carriage control (RAT = PRN), then the HP C RTL expands and concatenates the prefix and postfix carriage controls before and after the record.
    This translation is done according to rules specified by RMS, with one exception: if the prefix character is x01 and the postfix character is x8D, then nothing is attached to the beginning of the record and a single new-line character is attached to the end of it. This is done because this prefix/postfix combination is normally used to represent a line.
  • If the record attributes are Fortran carriage control (RAT = FTN), then the HP C RTL removes the initial control byte and attaches the appropriate carriage-control characters before and after the data as defined by RMS, with the exception of the space and default carriage-control characters. In these cases, which are used to represent a line, the HP C RTL appends a single new-line character to the data.
    The mapping of Fortran carriage-control can be disabled by using "ctx=nocvt".
  • If the record attributes are null (RAT = NONE) and the input is coming from a terminal, then the HP C RTL appends the terminating character to the record. If the terminator is a carriage return or Ctrl/Z, then HP C translates the character to a new-line character (\n).
    If the input is coming from a nonterminal file, then the HP C RTL passes the record unchanged to your program with no additional prefix or postfix characters.

As you read from the file, the HP 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 HP 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:

  • For all files, if the number of output bytes is greater than the internal buffer allocated by the HP C RTL, a record is output.
  • For files with fixed record length (RFM = FIX) or for files opened with "ctx=bin" or "ctx=xplct", a record is output only when the internal buffer is filled or when the flush function is called.
  • For files with STREAM_CR record format (RFM = STMCR), the HP C RTL outputs a record when it encounters a carriage-return character (\r).
  • For files with STREAM record format (RFM = STM) the HP C RTL outputs a record when it encounters a new-line (\n), form feed (\f), or vertical tab (\v) character.
  • For all other file types, the HP C RTL outputs a record when it encounters a new-line (\n) character.

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

  • If the record attribute is carriage control (R AT = CR), and if the logical record ends with a new-line character (\n), the HP C RTL drops the new-line character and writes the logical record with implied carriage control.
  • If the record attribute is print carriage control (RAT = PRN), then the HP C RTL writes the record with print carriage control according to the rules specified by RMS. If the logical record ends with a single new-line character (\n), the HP C RTL maps the new-line character to an x01 prefix and x8D postfix character. This is the reverse of the translation for record input files with print carriage-control attributes.
  • If the record attributes are Fortran carriage control (RAT = FTN), then the HP C RTL removes any prefix and/or postfix carriage-control characters and concatenates a single carriage-control byte to the beginning of the record as defined by RMS, with one exception: If the output record ends in a new-line character (\n), the HP C RTL will remove the new-line character and use the space carriage-control byte. This is the reverse of the translation for record input files with Fortran carriage-control attributes.
    The mapping of Fortran carriage-control can be disabled by using "ctx=nocvt".
  • If the logical record is to be written to a terminal device and the last character of the record is a new-line character (\n) the HP C RTL replaces the new-line character with a carriage-return (\r), and attaches a line-feed character (\n) to the front of the record. The HP C RTL then writes out the record with no carriage control.
  • If the output file record format is variable length with fixed control (RFM = VFC), and the record attributes do not include print carriage control (RAT is not PRN), then the HP C RTL takes the beginning of the logical record to be the fixed-control header, and reduces the number of bytes written out by the length of the header. These bytes are then used to construct the fixed-control header. If there are too few bytes in the logical record, an error is signaled.

1.8.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.8.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.8.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.8.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 Access

/*      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.9 Specific Portability Concerns

One of the last tasks in preparing to use the HP C RTL, if you are going to port your source programs across systems, is to be aware of specific differences between the HP 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 HP C RTL, this section also contains information on the portability of other HP C for OpenVMS constructs.

The HP 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 HP 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 HP 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:

  • HP C for OpenVMS Systems does not implement the global symbols end , edata , and etext .
  • There are differences in how OpenVMS and UNIX systems lay out virtual memory. In some UNIX systems, the address space between 0 and the break address is accessible to your program. In OpenVMS systems, the first page of memory is not accessible.
    For example, if a program tries to reference location 0 on an OpenVMS system, a hardware error (ACCVIO) is returned and the program terminates abnormally. OpenVMS systems reserve the first page of address space to catch incorrect pointer references, such as a reference to a location pointed to by a null pointer. For this reason, some existing programs that run on some UNIX systems may fail and you should modify them, as necessary. (Tru64 UNIX and OpenVMS, however, are compatible in this regard.)
  • Some C programmers code all external declarations in #include files. Then, specific declarations that require initialization are redeclared in the relevant module. This practice causes the HP C compiler to issue a warning message about multiply declared variables in the same compilation. One way to avoid this warning is to make the redeclared symbols extern variables in the #include files.
  • HP C does not support asm calls on OpenVMS VAX systems. They are supported on OpenVMS Alpha systems. See the HP C User's Guide for OpenVMS Systems for more information on intrinsic functions.
  • Some C programs call the counted string functions strcmpn and strcpyn . These names are not used by HP C for OpenVMS Systems. Instead, you can define macros that expand the strcmpn and strcpyn names into the equivalent, ANSI-compliant names strncmp and strncpy .
  • The HP C for OpenVMS compiler does not support the following initialization form:


    int  foo  123;
    

    Programs using this form of initialization must be changed.
  • HP C for OpenVMS Systems predefines several compile-time macros such as __vax , __alpha , __32BITS , __vms , __vaxc , __VMS_VER , __DECC_VER , __D_FLOAT , __G_FLOAT , __IEEE_FLOAT , __X_FLOAT , and others. These predefined macros are useful for programs that must be compatible on other machines and operating systems. For more information, see the predefined macro chapter of the HP C User's Guide for OpenVMS Systems.
  • The ANSI C language does not guarantee any memory order for the variables in a declaration. For example:


    int  a, b, c;
    
  • Depending on the type of external linkage requested, extern variables in a program may be treated differently using HP C on OpenVMS systems than they would on UNIX systems. See the HP C User's Guide for OpenVMS Systems for more information.
  • The dollar sign ($) is a legal character in HP C for OpenVMS identifiers, and can be used as the first character.
  • The ANSI C language does not define any order for evaluating expressions in function parameter lists or for many kinds of expressions. The way in which different C compilers evaluate an expression is only important when the expression has side effects. Consider the following examples:


    a[i]  =  i++;
    


    x = func_y() + func_z();
    


    f(p++, p++)
    

    Neither HP C nor any other C compiler can guarantee that such expressions evaluate in the same order on all C compilers.
  • The size of a HP C variable of type int is 32 bits on OpenVMS systems. You will have to modify programs that are written for other machines and that assume a different size for a variable of type int . A variable of type long is the same size (32 bits) as a variable of type int .
  • The C language defines structure alignment to be dependent on the machine for which the compiler is designed. On OpenVMS VAX systems, HP C aligns structure members on byte boundaries, unless #pragma member_alignment is specified. On OpenVMS Alpha systems, HP C aligns structure members on natural boundaries, unless #pragma nomember_alignment is specified. Other implementations may align structure members differently.
  • References to structure members in HP C cannot be vague. For more information, see the HP C Language Reference Manual.
  • Registers are allocated based upon how often a variable is used, but the register keyword gives the compiler a strong hint that you want to place a particular variable into a register. Whenever possible, the variable is placed into a register. Any scalar variable with the storage class auto or register can be allocated to a register as long as the variable's address is not taken with the ampersand operator (&) and it is not a member of a structure or union.


Previous Next Contents Index