/* 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 <starlet.h>

#define C2SMBX "C2SMBX"
#define S2CMBX "S2CMBX"

int main(int argc, char *argv[])
{
    FILE *fc2s, *fs2c;
    long int stat;
    short int c2s, s2c;
    $DESCRIPTOR(dc2smbx, C2SMBX);   
    $DESCRIPTOR(ds2cmbx, S2CMBX);   
    char inmsg[100], outmsg[10000];
    /* create mailboxes */
    stat = sys$crembx(0, &c2s, 1000, 10000, 0, 0, &dc2smbx, 0, 0);
    if((stat & 1) == 0) printf("sys$crembx stat = %d\n", stat);
    stat = sys$crembx(0, &s2c, 1000, 10000, 0, 0, &ds2cmbx, 0, 0);
    if((stat & 1) == 0) printf("sys$crembx stat = %d\n", stat);
    /* open mailboxes */
    fc2s = fopen(C2SMBX ":", "r"); 
    if(fc2s == NULL) printf("fopen %s failed\n", C2SMBX);
    fs2c = fopen(S2CMBX ":", "w"); 
    if(fs2c == NULL) printf("fopen %s failed\n", S2CMBX);
    /* read and echo */
    strcpy(outmsg, "");
    for(;;)
    {
        fgets(inmsg, sizeof(inmsg), fc2s);
printf("inmsg=%s", inmsg);
        strcat(outmsg, inmsg);
printf("outmsg=%s", outmsg);
        fputs(outmsg, fs2c);
        fputs("*\r\n", fs2c);
    }
    return 0;
}
