#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#if 0
#include <dmalloc.h>
#endif

const char largeValue[] = "This is a large value.";
const char smallValue[] = "Small value";
const char envVar[] = "PATH";

int main(int argc,
         char **argv)
{
    int iterations = 30;
    int overwrite = 1;
    int settings = 1000000;
    int sleepTime = 1;

    // Test unsetting variable on the stack.
    unsetenv(envVar);

    // Iterate over setting variable.
    for (; iterations; iterations--)
    {
        /*
         * Set variable multiple times between two different sized values to
         * exhibit memory growth.
         */
        for (; settings; settings--)
        {
            // Set large value to variable.
            if (setenv(envVar, largeValue, overwrite))
            {
                perror("setenv(largeValue)");

                exit(EXIT_FAILURE);
            }

            // Set small value to variable.
            if (setenv(envVar, smallValue, overwrite))
            {
                perror("setenv(smallValue)");

                exit(EXIT_FAILURE);
            }
        }

        // Sleep to slow memory exhaustion.
        sleep(sleepTime);
    }

    // Test unsetting variable on the stack.
    unsetenv(envVar);

    exit(EXIT_SUCCESS);
}

