/* 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 <fabdef.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 DISKBLOCKSIZE 512

#define FNM "M.DAT"
#define GLBSECNAM "M"

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

int main(int argc, char *argv[])
{
    struct FAB myfab;
    $DESCRIPTOR(name, GLBSECNAM);
    void *range[2];
    unsigned short chan;
    long int stat;
    char dummy[2];
    /* open file */
    myfab = cc$rms_fab;
    myfab.fab$l_fna = FNM;
    myfab.fab$b_fns = strlen(FNM);
    myfab.fab$l_fop = FAB$M_UFO;
    myfab.fab$b_fac = FAB$M_GET | FAB$M_PUT | FAB$M_UPD;
    stat = sys$open(&myfab, 0, 0);
    if((stat & 1) == 0) printf("sys$open stat = %d\n", stat);
    /* create global section */
    range[0] = (char *)&shrmem;
    range[1] = (char *)&shrmem + sizeof(shrmem) - 1;
    chan = myfab.fab$l_stv;
    stat = sys$crmpsc(range, 0, 0, SEC$M_GBL | SEC$M_WRT,
                      &name, 0, 0, chan, sizeof(shrmem) / DISKBLOCKSIZE, 0, 0, 0);
    if((stat & 1) == 0) printf("sys$crmpsc stat = %d\n", stat);
    /* wait for user input */
    printf("Press enter to exit");
    fgets(dummy, sizeof(dummy), stdin);
    sys$close(&myfab, 0, 0);
    return 0;
}
