/* Collection of code snippets by Arne Vajhøj */
/* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) */
#include <string.h>
#include <stdlib.h>

#include "demolib.h"

/* some very simple functions */

int add(int a, int b)
{
    return a + b;
}

int sum(int n, int *a)
{
    int i, sum;
    sum = 0;
    for(i = 0; i < n; i++)
    {
        sum += a[i];
    }
    return sum;
}

char *dup(char *s)
{
    char *s2;
    s2 = malloc(2 * strlen(s) + 1);
    strcpy(s2, s);
    strcat(s2, s);
    return s2;
}

/* functions simulating real work */

static int IV = 123;
static char *SV = "ABC";

void get_out(int *iv, char **sv)
{
    *iv = IV;
    *sv = malloc(strlen(SV) + 1);
    strcpy(*sv, SV);
}

struct Data1 *get_ret()
{
    struct Data1 *d;
    d = malloc(sizeof(struct Data1));
    d->iv = IV;
    strcpy(d->sv, SV);
    return d;
}

void chg(struct Data2 *d)
{
    d->iv2 = d->iv1 + 1;
    strcpy(d->sv2, d->sv1);
    strcat(d->sv2, "X");
}
