1 /*- 2 * Copyright (c) 2007 Sean C. Farley <scf@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer, 10 * without modification, immediately at the beginning of the file. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 #include <errno.h> 27 #include <libgen.h> 28 #include <stdbool.h> 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <string.h> 32 #include <unistd.h> 33 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 39 extern char **environ; 40 41 42 /* 43 * Print entire environ array. 44 */ 45 static void 46 dump_environ(void) 47 { 48 char **environPtr; 49 50 for (environPtr = environ; *environPtr != NULL; environPtr++) 51 printf("%s\n", *environPtr); 52 53 return; 54 } 55 56 57 /* 58 * Print usage. 59 */ 60 static void 61 usage(const char *program) 62 { 63 fprintf(stderr, "Usage: %s [-CDGUchrt] [-gu name] [-p name=value] " 64 "[(-S|-s name) value overwrite]\n\n" 65 "Options:\n" 66 " -C\t\t\t\tClear environ variable with NULL pointer\n" 67 " -D\t\t\t\tDump environ\n" 68 " -G name\t\t\tgetenv(NULL)\n" 69 " -S value overwrite\t\tsetenv(NULL, value, overwrite)\n" 70 " -U\t\t\t\tunsetenv(NULL)\n" 71 " -c\t\t\t\tClear environ variable with calloc()'d memory\n" 72 " -g name\t\t\tgetenv(name)\n" 73 " -h\t\t\t\tHelp\n" 74 " -p name=value\t\t\tputenv(name=value)\n" 75 " -r\t\t\t\treplace environ with { \"FOO=bar\", NULL }\n" 76 " -s name value overwrite\tsetenv(name, value, overwrite)\n" 77 " -t\t\t\t\tOutput is suitable for testing (no newlines)\n" 78 " -u name\t\t\tunsetenv(name)\n", 79 basename(program)); 80 81 return; 82 } 83 84 85 /* 86 * Print the return value of a call along with errno upon error else zero. 87 * Also, use the eol string based upon whether running in test mode or not. 88 */ 89 static void 90 print_rtrn_errno(int rtrnVal, const char *eol) 91 { 92 printf("%d %d%s", rtrnVal, rtrnVal != 0 ? errno : 0, eol); 93 94 return; 95 } 96 97 98 int 99 main(int argc, char **argv) 100 { 101 char *staticEnv[] = { "FOO=bar", NULL }; 102 char arg; 103 const char *eol = "\n"; 104 const char *value; 105 106 if (argc == 1) { 107 usage(argv[0]); 108 exit(EXIT_FAILURE); 109 } 110 111 /* The entire program is basically executed from this loop. */ 112 while ((arg = getopt(argc, argv, "CDGS:Ucg:hp:rs:tu:")) != -1) { 113 switch (arg) { 114 case 'C': 115 environ = NULL; 116 break; 117 118 case 'c': 119 environ = calloc(1, sizeof(*environ)); 120 break; 121 122 case 'D': 123 dump_environ(); 124 break; 125 126 case 'G': 127 case 'g': 128 value = getenv(arg == 'g' ? optarg : NULL); 129 printf("%s%s", value == NULL ? "" : value, eol); 130 break; 131 132 case 'p': 133 print_rtrn_errno(putenv(optarg), eol); 134 break; 135 136 case 'r': 137 environ = staticEnv; 138 break; 139 140 case 'S': 141 print_rtrn_errno(setenv(NULL, optarg, 142 atoi(argv[optind])), eol); 143 optind += 1; 144 break; 145 146 case 's': 147 print_rtrn_errno(setenv(optarg, argv[optind], 148 atoi(argv[optind + 1])), eol); 149 optind += 2; 150 break; 151 152 case 't': 153 eol = " "; 154 break; 155 156 case 'U': 157 case 'u': 158 print_rtrn_errno(unsetenv(arg == 'u' ? optarg : 159 NULL), eol); 160 break; 161 162 case 'h': 163 default: 164 usage(argv[0]); 165 exit(EXIT_FAILURE); 166 } 167 } 168 169 /* Output a closing newline in test mode. */ 170 if (eol[0] == ' ') 171 printf("\n"); 172 173 return (EXIT_SUCCESS); 174 } 175