/* Collection of code snippets by Arne Vajhøj */
/* (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <process.h>

void bad(const char *a1, const char *a2)
{
    printf("Bad\n");
    char cmd[1000];
    snprintf(cmd, sizeof(cmd), "parmdump %s %s", a1, a2);
    system(cmd);
}

void mystrcat(char *to, const char *from)
{
    int ix = strlen(to);
    for(int i = 0; i <= strlen(from); i++)
    {
        if(from[i] == '"')
        {
            to[ix] = from[i];
            ix++;
            to[ix] = from[i];
            ix++;
        }
        else
        {
            to[ix] = from[i];
            ix++;
        }
    }
}

void hack(const char *a1, const char *a2)
{
    char *pto, *pfrom;
    printf("Hack\n");
    char cmd[1000];
    strcpy(cmd, "parmdump \"");
    mystrcat(cmd, a1);
    strcat(cmd, "\" \"");
    mystrcat(cmd, a2);
    strcat(cmd, "\"");
    system(cmd);
}

void latest_l(const char *a1, const char *a2)
{
    printf("Latest (l)\n");
    _spawnl(_P_WAIT, "parmdump", a1, a2, NULL);
}

void latest_v(const char *a1, const char *a2)
{
    const char *argv[] = { a1, a2, NULL };
    printf("Latest (v)\n");
    _spawnv(_P_WAIT, "parmdump", argv);
}

void test(const char *a1, const char *a2)
{
    bad(a1, a2);
    hack(a1, a2);
    latest_l(a1, a2);
    latest_v(a1, a2);
}

int main()
{
    test("a", "b");
    test("a", "b c d");
    test("a", "b\"c");
    return 0;
}
