IBM Metal C

Examples

Example 7. Print Routine

One problem with using Metal-C is that there are no I/O routines. When writing stand alone programs in Metal-C, I have wanted to at least have the ability to print messages. This could be accomplished by having an Assembler libray of routines for I/O. Not wanting to have to create a separate library of Assembler I/O routines, I came up with the routine below, which works for simple print cases.

Struct DCB

The struct dcb is common to all routines. It contains dcb, storage, and environment information. At program start, it is declared and intialized as in this example:

    int rc;
    int i;
    struct dcb _dcb;   // Allocation
    char message[133];
    
    rc = 0;
    memset(&_dcb, 0, sizeof(struct dcb)); // Initialize to all x'00's
    memcpy(_dcb.ddname, "SYSPRINT", 8);   // DDNAME to Open
    _dcb.lrecl = 133;                     // LRECL
    _dcb.blksize = 1330;                  // BLKSIZE
    
    memset(&message, ' ', sizeof(message));
    sprintf((char*)&message, "The value of i=%i",i);
    _dcb.currentRecord = &message;       // Set address of text to print 

// Routines to Invoke for Printing
    rc = init(&_dcb);    
    rc = open(&_dcb);
    rc = put(&_dcb);
    rc = close(&_dcb);
    rc = term(&_dcb);

 

struct storage
{
    char dcb[96];
} ;

struct dcb
{
    char ddname[8];
    int  lrecl;
    int  blksize;
    void *storage;        // Pointer to z/OS DCB
    int  size;
    void *currentRecord;  // Pointer to current output string
    void *token;
    __csysenv_t _envtkn;  // Metal C Environment Token
} ;

Type Signatures

extern int init(struct dcb *_dcb);
extern int term(struct dcb *_dcb);
extern int open(struct dcb *_dcb);
extern int put(struct dcb *_dcb);
extern int close(struct dcb *_dcb);

Signature Details

init() – Create Metal C Environment
This routine is invoked once per program execution. It initializes the Metal-C runtime environment. This environment is used when calling Metal-C functions that require an environment, such as those related to storage management and other functions, e.g. malloc(), free, sprintf(), etc. Storage for the environment structure is obtained by using the attributes specified in the input csysenv structure. Not all Metal-C functions require this environment to be set up. In this case I am using the sprintf function which requires the environment. To determine which functions require the Metal-C environment to be established see z/OS Metal C Programming Guide and Reference©, Chapter 3. C functions available to Metal C programs.
term() – Terminate Metal C Environment
This routine is invoked once per program execution. It terminates the Metal-C runtime environment.
open() – Open File for Output
This routine is normally invoked once per program execution. It performs an OPEN of the dcb to be used for printing.
close() - Close File
This routine is normally invoked once per program execution. It performs a CLOSE of the dcb that was opened.
put() - Write a Character String to File
This routine is invoked for every desired print operation. It performs a PUT to the output file specified by the dcb.

C Source

References
  1. z/OS Metal C Programming Guide and Reference
  2. z/OS DFSMS Macro Instructions for Data Sets
  3. z/OS DFSMS Using Data Sets
All references copyright© IBM Corporation.