summaryrefslogtreecommitdiffstats
path: root/tools/regression
diff options
context:
space:
mode:
authorscf <scf@FreeBSD.org>2007-07-04 00:00:41 +0000
committerscf <scf@FreeBSD.org>2007-07-04 00:00:41 +0000
commit196b6346ba4e13a3f7679e2de3317b6aa65983df (patch)
tree423c7d016f87f6541b9ef8231a14f8b267bc5d5e /tools/regression
parentaf5bbfbc7b6a610f0c44a56a7b2d81b96be9b1b5 (diff)
downloadFreeBSD-src-196b6346ba4e13a3f7679e2de3317b6aa65983df.zip
FreeBSD-src-196b6346ba4e13a3f7679e2de3317b6aa65983df.tar.gz
Significantly reduce the memory leak as noted in BUGS section for
setenv(3) by tracking the size of the memory allocated instead of using strlen() on the current value. Convert all calls to POSIX from historic BSD API: - unsetenv returns an int. - putenv takes a char * instead of const char *. - putenv no longer makes a copy of the input string. - errno is set appropriately for POSIX. Exceptions involve bad environ variable and internal initialization code. These both set errno to EFAULT. Several patches to base utilities to handle the POSIX changes from Andrey Chernov's previous commit. A few I re-wrote to use setenv() instead of putenv(). New regression module for tools/regression/environ to test these functions. It also can be used to test the performance. Bump __FreeBSD_version to 700050 due to API change. PR: kern/99826 Approved by: wes Approved by: re (kensmith)
Diffstat (limited to 'tools/regression')
-rw-r--r--tools/regression/environ/Makefile14
-rw-r--r--tools/regression/environ/Makefile.envctl16
-rw-r--r--tools/regression/environ/Makefile.retention16
-rw-r--r--tools/regression/environ/Makefile.timings16
-rw-r--r--tools/regression/environ/envctl.c154
-rw-r--r--tools/regression/environ/envtest.t182
-rw-r--r--tools/regression/environ/retention.c109
-rw-r--r--tools/regression/environ/timings.c195
8 files changed, 702 insertions, 0 deletions
diff --git a/tools/regression/environ/Makefile b/tools/regression/environ/Makefile
new file mode 100644
index 0000000..62720d2
--- /dev/null
+++ b/tools/regression/environ/Makefile
@@ -0,0 +1,14 @@
+#
+# $FreeBSD$
+#
+PROGS= envctl retention timings
+
+all clean test:
+.for target in ${.TARGET}
+.for prog in ${PROGS}
+ @${MAKE} -f Makefile.${prog} ${target}
+.endfor
+.if make(clean)
+ rm -f *~
+.endif
+.endfor
diff --git a/tools/regression/environ/Makefile.envctl b/tools/regression/environ/Makefile.envctl
new file mode 100644
index 0000000..6298a20
--- /dev/null
+++ b/tools/regression/environ/Makefile.envctl
@@ -0,0 +1,16 @@
+#
+# $FreeBSD$
+#
+SRCS= envctl.c
+PROG= envctl
+
+CFLAGS+=-Wall -I../../../include
+
+CLEANFILES= ${PROG}.core
+
+NO_MAN= yes
+
+.include <bsd.prog.mk>
+
+test: ${PROG}
+ @sh envtest.t
diff --git a/tools/regression/environ/Makefile.retention b/tools/regression/environ/Makefile.retention
new file mode 100644
index 0000000..9e0687f
--- /dev/null
+++ b/tools/regression/environ/Makefile.retention
@@ -0,0 +1,16 @@
+#
+# $FreeBSD$
+#
+SRCS= retention.c
+PROG= retention
+
+CFLAGS+=-Wall -I../../../include
+
+CLEANFILES= *~ ${PROG}.core
+
+NO_MAN= yes
+
+.include <bsd.prog.mk>
+
+test: ${PROG}
+ @./${PROG}
diff --git a/tools/regression/environ/Makefile.timings b/tools/regression/environ/Makefile.timings
new file mode 100644
index 0000000..88c4f7c
--- /dev/null
+++ b/tools/regression/environ/Makefile.timings
@@ -0,0 +1,16 @@
+#
+# $FreeBSD$
+#
+SRCS= timings.c
+PROG= timings
+
+CFLAGS+=-Wall -I../../../include
+
+CLEANFILES= ${PROG}.core
+
+NO_MAN= yes
+
+.include <bsd.prog.mk>
+
+test: ${PROG}
+ @./timings
diff --git a/tools/regression/environ/envctl.c b/tools/regression/environ/envctl.c
new file mode 100644
index 0000000..2681741
--- /dev/null
+++ b/tools/regression/environ/envctl.c
@@ -0,0 +1,154 @@
+/*-
+ * Copyright (c) 2007 Sean C. Farley <scf@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer,
+ * without modification, immediately at the beginning of the file.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <errno.h>
+#include <libgen.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+
+extern char **environ;
+
+
+static void
+dump_environ(void)
+{
+ char **environPtr;
+
+ for (environPtr = environ; *environPtr != NULL; *environPtr++)
+ printf("%s\n", *environPtr);
+
+ return;
+}
+
+
+static void
+usage(const char *program)
+{
+ fprintf(stderr, "Usage: %s [-DGUcht] [-gu name] [-p name=value] "
+ "[(-S|-s name) value overwrite]\n\n"
+ "Options:\n"
+ " -D\t\t\t\tDump environ\n"
+ " -G name\t\t\tgetenv(NULL)\n"
+ " -S value overwrite\t\tsetenv(NULL, value, overwrite)\n"
+ " -U\t\t\t\tunsetenv(NULL)\n"
+ " -c\t\t\t\tClear environ variable\n"
+ " -g name\t\t\tgetenv(name)\n"
+ " -h\t\t\t\tHelp\n"
+ " -p name=value\t\t\tputenv(name=value)\n"
+ " -s name value overwrite\tsetenv(name, value, overwrite)\n"
+ " -t\t\t\t\tOutput is suitable for testing (no newlines)\n"
+ " -u name\t\t\tunsetenv(name)\n",
+ basename(program));
+
+ return;
+}
+
+
+int
+main(int argc, char **argv)
+{
+ char *cleanEnv[] = { NULL };
+ char arg;
+ const char *eol = "\n";
+ const char *value;
+
+ if (argc == 1) {
+ usage(argv[0]);
+ exit(EXIT_FAILURE);
+ }
+
+ while ((arg = getopt(argc, argv, "DGS:Ucg:hp:s:tu:")) != -1) {
+ switch (arg) {
+ case 'D':
+ errno = 0;
+ dump_environ();
+ break;
+
+ case 'c':
+ environ = cleanEnv;
+ break;
+
+ case 'G':
+ value = getenv(NULL);
+ printf("%s%s", value == NULL ? "" : value, eol);
+ break;
+
+ case 'g':
+ value = getenv(optarg);
+ printf("%s%s", value == NULL ? "" : value, eol);
+ break;
+
+ case 'p':
+ errno = 0;
+ printf("%d %d%s", putenv(optarg), errno, eol);
+ break;
+
+ case 'S':
+ errno = 0;
+ printf("%d %d%s", setenv(NULL, optarg,
+ atoi(argv[optind])), errno, eol);
+ optind += 1;
+ break;
+
+ case 's':
+ errno = 0;
+ printf("%d %d%s", setenv(optarg, argv[optind],
+ atoi(argv[optind + 1])), errno, eol);
+ optind += 2;
+ break;
+
+ case 't':
+ eol = " ";
+ break;
+
+ case 'U':
+ printf("%d %d%s", unsetenv(NULL), errno, eol);
+ break;
+
+ case 'u':
+ printf("%d %d%s", unsetenv(optarg), errno, eol);
+ break;
+
+ case 'h':
+ default:
+ usage(argv[0]);
+ exit(EXIT_FAILURE);
+ }
+ }
+
+ // Output a closing newline in test mode.
+ if (eol[0] == ' ')
+ printf("\n");
+
+ return (EXIT_SUCCESS);
+}
diff --git a/tools/regression/environ/envtest.t b/tools/regression/environ/envtest.t
new file mode 100644
index 0000000..e3aca95
--- /dev/null
+++ b/tools/regression/environ/envtest.t
@@ -0,0 +1,182 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Sean C. Farley <scf@FreeBSD.org>
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1. Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer,
+# without modification, immediately at the beginning of the file.
+# 2. Redistributions in binary form must reproduce the above copyright
+# notice, this list of conditions and the following disclaimer in the
+# documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+# $FreeBSD$
+
+
+# Initialization.
+testndx=0
+
+
+# Testing function.
+run_test()
+{
+ lasttest="${@}"
+ result=`./envctl -t "${@}"`
+
+ if [ ${?} -ne 0 ]
+ then
+ echo "Test program failed" >&2
+ exit 1
+ fi
+
+ return
+}
+
+
+# Perform test on results.
+check_result()
+{
+ testndx=$((testndx + 1))
+
+ echo "${result}" | sed 's/[ \t]*$//' | grep -q "^${@}$"
+ if [ ${?} -eq 0 ]
+ then
+ echo "ok ${testndx}"
+ else
+ echo "not ok ${testndx} - '${lasttest}'"
+ fi
+
+ return
+}
+
+
+#
+# Regression tests
+#
+
+# Setup environment for tests.
+readonly BAR="bar"
+readonly NEWBAR="newbar"
+export FOO=${BAR}
+
+
+# Gets from environ.
+run_test -g FOO
+check_result "${FOO}"
+
+run_test -c -g FOO
+check_result ""
+
+run_test -g FOOBAR
+check_result ""
+
+run_test -c -g FOOBAR
+check_result ""
+
+run_test -G
+check_result ""
+
+
+# Sets.
+run_test -s FOO ${NEWBAR} 0 -g FOO
+check_result "0 0 ${BAR}"
+
+run_test -s FOO ${NEWBAR} 1 -g FOO
+check_result "0 0 ${NEWBAR}"
+
+run_test -c -s FOO ${NEWBAR} 0 -g FOO
+check_result "0 0 ${NEWBAR}"
+
+run_test -c -s FOO ${NEWBAR} 1 -g FOO
+check_result "0 0 ${NEWBAR}"
+
+run_test -s "FOO=" ${NEWBAR} 1 -g FOO
+check_result "-1 22 ${BAR}"
+
+run_test -s "=FOO" ${NEWBAR} 1
+check_result "-1 22"
+
+run_test -s "=" ${NEWBAR} 1
+check_result "-1 22"
+
+run_test -s "" ${NEWBAR} 1
+check_result "-1 22"
+
+run_test -S ${NEWBAR} 1
+check_result "-1 22"
+
+run_test -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1 -g FOO
+check_result "0 0 0 0 ${BAR}"
+
+run_test -c -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1 -g FOO
+check_result "0 0 0 0 ${BAR}"
+
+run_test -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1 -s FOO ${NEWBAR} 1 -g FOO
+check_result "0 0 0 0 0 0 ${NEWBAR}"
+
+run_test -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1 -s FOO ${NEWBAR} 1 -s FOO ${BAR} 1\
+ -g FOO
+check_result "0 0 0 0 0 0 0 0 ${BAR}"
+
+
+# Unsets.
+run_test -u FOO -g FOO
+check_result "0 0"
+
+run_test -c -u FOO -g FOO
+check_result "0 0"
+
+run_test -U
+check_result "-1 22"
+
+run_test -u ""
+check_result "-1 22"
+
+run_test -u "=${BAR}"
+check_result "-1 22"
+
+run_test -c -s FOO ${NEWBAR} 1 -g FOO -u FOO -g FOO
+check_result "0 0 ${NEWBAR} 0 0"
+
+
+# Puts.
+run_test -p FOO=${NEWBAR} -g FOO
+check_result "0 0 ${NEWBAR}"
+
+run_test -c -p FOO=${NEWBAR} -g FOO
+check_result "0 0 ${NEWBAR}"
+
+run_test -p FOO -g FOO
+check_result "-1 22 ${BAR}"
+
+run_test -p FOO=${BAR} -p FOO=${NEWBAR} -g FOO
+check_result "0 0 0 0 ${NEWBAR}"
+
+run_test -p FOO=${BAR} -s FOO ${NEWBAR} 1 -g FOO
+check_result "0 0 0 0 ${NEWBAR}"
+
+run_test -s FOO ${NEWBAR} 1 -p FOO=${BAR} -g FOO
+check_result "0 0 0 0 ${BAR}"
+
+run_test -p FOO=${BAR} -u FOO
+check_result "0 0 0 0"
+
+run_test -p FOO=${BAR} -s FOO ${NEWBAR} 1 -u FOO
+check_result "0 0 0 0 0 0"
+
+run_test -s FOO ${NEWBAR} 1 -p FOO=${BAR} -u FOO
+check_result "0 0 0 0 0 0"
diff --git a/tools/regression/environ/retention.c b/tools/regression/environ/retention.c
new file mode 100644
index 0000000..7f75cd1
--- /dev/null
+++ b/tools/regression/environ/retention.c
@@ -0,0 +1,109 @@
+/*-
+ * Copyright (c) 2007 Sean C. Farley <scf@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer,
+ * without modification, immediately at the beginning of the file.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
+ * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+
+extern char **environ;
+const char *envName = "FOOBAR";
+const char *envValSmall = "Hi";
+const char *envValLarge = "Hi, again";
+const char *envValAny = "Any value";
+
+
+int
+main(int argc, char **argv)
+{
+ const char *env1 = NULL;
+ const char *env2 = NULL;
+ const char *env3 = NULL;
+ const char *env4 = NULL;
+ const char *env5 = NULL;
+ int testNdx;
+
+ /* Clean slate. */
+ environ = NULL;
+ testNdx = 0;
+
+ /* Initial value of variable. */
+ if (getenv(envName) != NULL)
+ printf("not ");
+ printf("ok %d - getenv(\"%s\")\n", ++testNdx, envName);
+
+ /* Set value of variable to smaller value and get value. */
+ if ((setenv(envName, envValSmall, 1) != 0) ||
+ ((env1 = getenv(envName)) == NULL) ||
+ (strcmp(env1, envValSmall) != 0))
+ printf("not ");
+ printf("ok %d - setenv(\"%s\", \"%s\", 1)\n", ++testNdx, envName,
+ envValSmall);
+
+ /* Unset variable. */
+ if ((unsetenv(envName) == -1) || ((env2 = getenv(envName)) != NULL))
+ printf("not ");
+ printf("ok %d - unsetenv(\"%s\")\n", ++testNdx, envName);
+
+ /* Set variable to bigger value and get value. */
+ if ((setenv(envName, envValLarge, 1) != 0) ||
+ ((env3 = getenv(envName)) == NULL) ||
+ (strcmp(env3, envValLarge) != 0))
+ printf("not ");
+ printf("ok %d - setenv(\"%s\", \"%s\", 1)\n", ++testNdx, envName,
+ envValLarge);
+
+ /* Set variable to smaller value and get value. */
+ if ((setenv(envName, envValSmall, 1) != 0) ||
+ ((env4 = getenv(envName)) == NULL) ||
+ (strcmp(env4, envValSmall) != 0))
+ printf("not ");
+ printf("ok %d - setenv(\"%s\", \"%s\", 1)\n", ++testNdx, envName,
+ envValSmall);
+
+ /* Set variable to any value without overwrite and get value. */
+ if ((setenv(envName, envValAny, 0) != 0) ||
+ ((env5 = getenv(envName)) == NULL) ||
+ (strcmp(env5, envValAny) == 0))
+ printf("not ");
+ printf("ok %d - setenv(\"%s\", \"%s\", 0)\n", ++testNdx, envName,
+ envValAny);
+
+ /*
+ * Verify FreeBSD-ism about allowing a program to keep old pointers without
+ * risk of segfaulting.
+ */
+ if ((strcmp(env1, envValSmall) != 0) ||
+ (strcmp(env3, envValSmall) != 0) ||
+ (strcmp(env4, envValSmall) != 0))
+ printf("not ");
+ printf("ok %d - old variables point to valid memory\n", ++testNdx);
+
+ exit(EXIT_SUCCESS);
+}
diff --git a/tools/regression/environ/timings.c b/tools/regression/environ/timings.c
new file mode 100644
index 0000000..1bf3c91
--- /dev/null
+++ b/tools/regression/environ/timings.c
@@ -0,0 +1,195 @@
+/*-
+ * Copyright (c) 2007 Sean C. Farley <scf@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+#include <sys/time.h>
+#include <err.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+
+const char value1[] = "Large ------------------ value";
+const char value2[] = "Small -- value";
+char nameValuePair[] = "less=more";
+const char name[] = "PATH";
+const char name2[] = "SHELL";
+const int MaxIterations = 1000000;
+const char Tabs[] = "\t\t\t";
+
+
+static int
+report_time(const char *action, struct timeval *startTime,
+ struct timeval *endTime)
+{
+ int actionLen;
+ int numTabs;
+
+ actionLen = strlen(action);
+ numTabs = 3 - actionLen / 8;
+
+ return (printf("Time spent executing %s:%.*s%f\n", action, numTabs, Tabs,
+ (endTime->tv_sec - startTime->tv_sec) +
+ (double)(endTime->tv_usec - startTime->tv_usec) / 1000000));
+}
+
+
+int
+main(int argc, char **argv)
+{
+ int iterations;
+ struct timeval endTime;
+ struct timeval startTime;
+
+ /*
+ * getenv() on the existing environment.
+ */
+ gettimeofday(&startTime, NULL);
+
+ /* Iterate over setting variable. */
+ for (iterations = 0; iterations < MaxIterations; iterations++)
+ if (getenv(name) == NULL)
+ err(EXIT_FAILURE, "getenv(name)");
+
+ gettimeofday(&endTime, NULL);
+
+ report_time("getenv(name)", &startTime, &endTime);
+
+
+ /*
+ * setenv() a variable with a large value.
+ */
+ gettimeofday(&startTime, NULL);
+
+ /* Iterate over setting variable. */
+ for (iterations = 0; iterations < MaxIterations; iterations++)
+ if (setenv(name, value1, 1) == -1)
+ err(EXIT_FAILURE, "setenv(name, value1, 1)");
+
+ gettimeofday(&endTime, NULL);
+
+ report_time("setenv(name, value1, 1)", &startTime, &endTime);
+
+
+ /*
+ * getenv() the new variable on the new environment.
+ */
+ gettimeofday(&startTime, NULL);
+
+ /* Iterate over setting variable. */
+ for (iterations = 0; iterations < MaxIterations; iterations++)
+ /* Set large value to variable. */
+ if (getenv(name) == NULL)
+ err(EXIT_FAILURE, "getenv(name)");
+
+ gettimeofday(&endTime, NULL);
+
+ report_time("getenv(name)", &startTime, &endTime);
+
+
+ /*
+ * getenv() a different variable on the new environment.
+ */
+ gettimeofday(&startTime, NULL);
+
+ /* Iterate over setting variable. */
+ for (iterations = 0; iterations < MaxIterations; iterations++)
+ /* Set large value to variable. */
+ if (getenv(name2) == NULL)
+ err(EXIT_FAILURE, "getenv(name2)");
+
+ gettimeofday(&endTime, NULL);
+
+ report_time("getenv(name2)", &startTime, &endTime);
+
+
+ /*
+ * setenv() a variable with a small value.
+ */
+ gettimeofday(&startTime, NULL);
+
+ /* Iterate over setting variable. */
+ for (iterations = 0; iterations < MaxIterations; iterations++)
+ if (setenv(name, value2, 1) == -1)
+ err(EXIT_FAILURE, "setenv(name, value2, 1)");
+
+ gettimeofday(&endTime, NULL);
+
+ report_time("setenv(name, value2, 1)", &startTime, &endTime);
+
+
+ /*
+ * getenv() a different variable on the new environment.
+ */
+ gettimeofday(&startTime, NULL);
+
+ /* Iterate over setting variable. */
+ for (iterations = 0; iterations < MaxIterations; iterations++)
+ /* Set large value to variable. */
+ if (getenv(name2) == NULL)
+ err(EXIT_FAILURE, "getenv(name)");
+
+ gettimeofday(&endTime, NULL);
+
+ report_time("getenv(name)", &startTime, &endTime);
+
+
+ /*
+ * getenv() a different variable on the new environment.
+ */
+ gettimeofday(&startTime, NULL);
+
+ /* Iterate over setting variable. */
+ for (iterations = 0; iterations < MaxIterations; iterations++)
+ /* Set large value to variable. */
+ if (getenv(name2) == NULL)
+ err(EXIT_FAILURE, "getenv(name2)");
+
+ gettimeofday(&endTime, NULL);
+
+ report_time("getenv(name2)", &startTime, &endTime);
+
+
+ /*
+ * putenv() a variable with a small value.
+ */
+ gettimeofday(&startTime, NULL);
+
+ /* Iterate over setting variable. */
+ for (iterations = 0; iterations < MaxIterations; iterations++)
+ if (putenv(nameValuePair) == -1)
+ err(EXIT_FAILURE, "putenv(nameValuePair)");
+
+ gettimeofday(&endTime, NULL);
+
+ report_time("putenv(nameValuePair)", &startTime, &endTime);
+
+
+ exit(EXIT_SUCCESS);
+}
OpenPOWER on IntegriCloud