/* 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"

static union
{
   char dummy[SHRMEMSIZ];
   char msg[MAXMSGS][MAXMSGSIZ];
} __align(13) shrmem;

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