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 --- 19 unchanged lines hidden (view full) --- 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: head/tools/regression/environ/envctl.c 171525 2007-07-20 23:30:13Z scf $"); |
37 38 39extern char **environ; 40 41 42static void 43dump_environ(void) 44{ --- 4 unchanged lines hidden (view full) --- 49 50 return; 51} 52 53 54static void 55usage(const char *program) 56{ |
57 fprintf(stderr, "Usage: %s [-CDGUchrt] [-gu name] [-p name=value] " |
58 "[(-S|-s name) value overwrite]\n\n" 59 "Options:\n" |
60 " -C\t\t\t\tClear environ variable with NULL pointer\n" |
61 " -D\t\t\t\tDump environ\n" 62 " -G name\t\t\tgetenv(NULL)\n" 63 " -S value overwrite\t\tsetenv(NULL, value, overwrite)\n" 64 " -U\t\t\t\tunsetenv(NULL)\n" |
65 " -c\t\t\t\tClear environ variable with calloc()'d memory\n" |
66 " -g name\t\t\tgetenv(name)\n" 67 " -h\t\t\t\tHelp\n" 68 " -p name=value\t\t\tputenv(name=value)\n" |
69 " -r\t\t\t\treplace environ with { \"FOO=bar\", NULL }\n" |
70 " -s name value overwrite\tsetenv(name, value, overwrite)\n" 71 " -t\t\t\t\tOutput is suitable for testing (no newlines)\n" 72 " -u name\t\t\tunsetenv(name)\n", 73 basename(program)); 74 75 return; 76} 77 78 79int 80main(int argc, char **argv) 81{ |
82 char *staticEnv[] = { "FOO=bar", NULL }; |
83 char arg; 84 const char *eol = "\n"; 85 const char *value; 86 87 if (argc == 1) { 88 usage(argv[0]); 89 exit(EXIT_FAILURE); 90 } 91 |
92 while ((arg = getopt(argc, argv, "CDGS:Ucg:hp:rs:tu:")) != -1) { |
93 switch (arg) { |
94 case 'C': 95 environ = NULL; |
96 break; 97 98 case 'c': |
99 environ = calloc(1, sizeof(*environ)); |
100 break; 101 |
102 case 'D': 103 errno = 0; 104 dump_environ(); 105 break; 106 |
107 case 'G': 108 value = getenv(NULL); 109 printf("%s%s", value == NULL ? "" : value, eol); 110 break; 111 112 case 'g': 113 value = getenv(optarg); 114 printf("%s%s", value == NULL ? "" : value, eol); 115 break; 116 117 case 'p': 118 errno = 0; 119 printf("%d %d%s", putenv(optarg), errno, eol); 120 break; 121 |
122 case 'r': 123 environ = staticEnv; 124 break; 125 |
126 case 'S': 127 errno = 0; 128 printf("%d %d%s", setenv(NULL, optarg, 129 atoi(argv[optind])), errno, eol); 130 optind += 1; 131 break; 132 133 case 's': --- 31 unchanged lines hidden --- |