/* 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 <stdlib.h>
#include <string.h>
#include <time.h>

//--
// in integer arguments
//--
int nadd(int a, int b)
{
    int c = a + b;
    printf("C says : %d + %d = %d\n", a, b, c);
    return 1;
}

//--
// in character arguments
//--
int nconc(char *sa, char *sb)
{
    char *sc = malloc(strlen(sa) + strlen(sb) + 1);
    strcpy(sc, sa);
    strcat(sc, sb);
    printf("C says : %s + %s = %s\n", sa, sb, sc);
    free(sc);
    return 1;
}

//--
// in and out integer arguments
//--
int naddret(int a, int b, int *c)
{
    *c = a + b;
    return 1;
}

//--
// in and out character arguments
//--
int nconcret(char *sa, char *sb, char *sc)
{
    strcpy(sc, sa);
    strcat(sc, sa);
    return 1;
}

//--
// inout integer argument
//--
int nadd1(int *v)
{
    *v = *v + 1;
    return 1;
}

//--
// inout character argument
//--
int nconcx(char *s)
{
    char *p = s + strlen(s) - 1;
    while(*p == ' ')
    {
        *p = 0;
        p--;
    }
    strcat(s, "X");
    return 1;
}

//--
// inout record argument
//--
struct myrec
{
    char b1;
    char b2;
    short w;
    int lw;
    time_t t;
    char s[8];
};

int nrecmod(struct myrec *r)
{
    r->b1++;
    r->b2++;
    r->w++;
    r->lw++;
    r->t = time(NULL);
    strcat(r->s, "X");
    return 1;
}

