From 143d18cafcca1b488d657fe2c85a736e24301b6b Mon Sep 17 00:00:00 2001 From: Sean Farley Date: Sat, 1 Mar 2008 00:02:12 +0000 Subject: [PATCH] Remove a dereference. It was unintended and a no-op. Use the correct value of errno. Although the errno value passed into printf() follows the *env() call, it is not guaranteed to be the errno from that call. When I wrote the regression tester, the environment I used did pass the errno from the call. Consolidate the print for the return code and errno into a function in the process of fixing this. Approved by: wes (mentor) --- tools/regression/environ/envctl.c | 50 ++++++++++++++++++------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/tools/regression/environ/envctl.c b/tools/regression/environ/envctl.c index e077e42fa407..a63270d11be8 100644 --- a/tools/regression/environ/envctl.c +++ b/tools/regression/environ/envctl.c @@ -39,18 +39,24 @@ __FBSDID("$FreeBSD$"); extern char **environ; +/* + * Print entire environ array. + */ static void dump_environ(void) { char **environPtr; - for (environPtr = environ; *environPtr != NULL; *environPtr++) + for (environPtr = environ; *environPtr != NULL; environPtr++) printf("%s\n", *environPtr); return; } +/* + * Print usage. + */ static void usage(const char *program) { @@ -76,6 +82,19 @@ usage(const char *program) } +/* + * Print the return value of a call along with errno upon error else zero. + * Also, use the eol string based upon whether running in test mode or not. + */ +static void +print_rtrn_errno(int rtrnVal, const char *eol) +{ + printf("%d %d%s", rtrnVal, rtrnVal != 0 ? errno : 0, eol); + + return; +} + + int main(int argc, char **argv) { @@ -89,6 +108,7 @@ main(int argc, char **argv) exit(EXIT_FAILURE); } + /* The entire program is basically executed from this loop. */ while ((arg = getopt(argc, argv, "CDGS:Ucg:hp:rs:tu:")) != -1) { switch (arg) { case 'C': @@ -100,23 +120,17 @@ main(int argc, char **argv) break; case 'D': - errno = 0; dump_environ(); break; case 'G': - value = getenv(NULL); - printf("%s%s", value == NULL ? "" : value, eol); - break; - case 'g': - value = getenv(optarg); + value = getenv(arg == 'g' ? optarg : NULL); printf("%s%s", value == NULL ? "" : value, eol); break; case 'p': - errno = 0; - printf("%d %d%s", putenv(optarg), errno, eol); + print_rtrn_errno(putenv(optarg), eol); break; case 'r': @@ -124,16 +138,14 @@ main(int argc, char **argv) break; case 'S': - errno = 0; - printf("%d %d%s", setenv(NULL, optarg, - atoi(argv[optind])), errno, eol); + print_rtrn_errno(setenv(NULL, optarg, + atoi(argv[optind])), eol); optind += 1; break; case 's': - errno = 0; - printf("%d %d%s", setenv(optarg, argv[optind], - atoi(argv[optind + 1])), errno, eol); + print_rtrn_errno(setenv(optarg, argv[optind], + atoi(argv[optind + 1])), eol); optind += 2; break; @@ -142,11 +154,9 @@ main(int argc, char **argv) break; case 'U': - printf("%d %d%s", unsetenv(NULL), errno, eol); - break; - case 'u': - printf("%d %d%s", unsetenv(optarg), errno, eol); + print_rtrn_errno(unsetenv(arg == 'u' ? optarg : + NULL), eol); break; case 'h': @@ -156,7 +166,7 @@ main(int argc, char **argv) } } - // Output a closing newline in test mode. + /* Output a closing newline in test mode. */ if (eol[0] == ' ') printf("\n");