/* Collection of code snippets by Arne Vajhøj */
/* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) */
#include <stdio.h>
#include <string.h>

#include <descrip.h>
#include <secdef.h>
#include <starlet.h>

#define BLOCKSIZE 8192
#define BLOCKS 10
#define SHRMEMSIZ BLOCKS * BLOCKSIZE
#define MAXMSGSIZ 100
#define MAXMSGS SHRMEMSIZ / MAXMSGSIZ

#define GLBSECNAM "M"

typedef union
{
   char dummy[SHRMEMSIZ];
   char msg[MAXMSGS][MAXMSGSIZ];
} shrmem_t;

int main(int argc, char *argv[])
{
    shrmem_t *shrmem;
    $DESCRIPTOR(name, GLBSECNAM);
    void *range[2];
    long int stat;
    int i;
    /* map global section */
    range[0] = 0;
    range[1] = 0;
    stat = sys$mgblsc(range, range, 0, SEC$M_EXPREG | SEC$M_WRT,&name, 0, 0);
    if((stat & 1) == 0) printf("sys$mgblsc stat = %d\n", stat);
    shrmem = (shrmem_t *)range[0];
    /* write to global section */
    i = 0;
    while(shrmem->msg[i][0] != 0) i++;
    strcpy(shrmem->msg[i], "Hi from C (alt)!");
    /* dump global section */
    for(i = 0; i < MAXMSGS; i++)
    {
        if(strlen(shrmem->msg[i]) > 0)
        {
            printf("|%s|\n", shrmem->msg[i]);
        }
    }
    return 0;
}
