1 /* 2 * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com> 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 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 * 26 * $DragonFly: src/bin/varsym/varsym.c,v 1.4 2003/12/11 20:33:49 dillon Exp $ 27 */ 28 29 #include <stdio.h> 30 #include <stdlib.h> 31 #include <string.h> 32 #include <errno.h> 33 #include <unistd.h> 34 #include <sys/varsym.h> 35 36 static void dumpvars(char *buf, int bytes); 37 static int doexec(char **av); 38 static void usage(void); 39 40 int 41 main(int ac, char **av) 42 { 43 int i; 44 int mask = VARSYM_ALL_MASK; 45 int level = VARSYM_USER; 46 int deleteOpt = 0; 47 int verboseOpt = 1; 48 int allOpt = 0; 49 int execok = 0; 50 51 while ((i = getopt(ac, av, "adhpqsux")) != -1) { 52 switch (i) { 53 case 'a': 54 allOpt = 1; 55 break; 56 case 'd': 57 deleteOpt = 1; 58 break; 59 case 'p': 60 mask = VARSYM_PROC_MASK; 61 level = VARSYM_PROC; 62 break; 63 case 'q': 64 verboseOpt = 0; 65 break; 66 case 's': 67 mask = VARSYM_SYS_MASK; 68 level = VARSYM_SYS; 69 break; 70 case 'u': 71 mask = VARSYM_USER_MASK; 72 level = VARSYM_USER; 73 break; 74 case 'x': 75 mask = VARSYM_PROC_MASK; 76 level = VARSYM_PROC; 77 execok = 1; 78 break; 79 case 'h': 80 default: 81 usage(); 82 return(-1); 83 } 84 } 85 86 if (allOpt) { 87 char buf[1024]; 88 int marker = 0; 89 int bytes; 90 91 for (;;) { 92 bytes = varsym_list(level, buf, sizeof(buf), &marker); 93 if (bytes < 0) /* error occured */ 94 break; 95 dumpvars(buf, bytes); 96 if (marker < 0) /* no more vars */ 97 break; 98 } 99 if (bytes < 0) 100 fprintf(stderr, "varsym_list(): %s\n", strerror(errno)); 101 } 102 103 for ( ; optind < ac; optind++) { 104 char *name = av[optind]; 105 char *data = strchr(name, '='); 106 int error; 107 char buf[MAXVARSYM_DATA]; 108 109 if (data) 110 *data++ = 0; 111 112 if (execok) { 113 if (deleteOpt) { 114 usage(); 115 exit(1); 116 } 117 if (data) { 118 error = varsym_set(level, name, data); 119 } else { 120 error = doexec(av + optind); 121 } 122 } else if (deleteOpt) { 123 error = varsym_set(level, name, NULL); 124 } else if (data) { 125 error = varsym_set(level, name, data); 126 } else { 127 error = varsym_get(mask, name, buf, sizeof(buf)); 128 if (error >= 0 && error <= (int)sizeof(buf)) { 129 if (verboseOpt) 130 printf("%s=", name); 131 printf("%s\n", buf); 132 } 133 } 134 if (error < 0 && verboseOpt) 135 fprintf(stderr, "%s: %s\n", name, strerror(errno)); 136 } 137 138 return(0); 139 } 140 141 static void 142 dumpvars(char *buf, int bytes) 143 { 144 int b; 145 int i; 146 char *vname = NULL; 147 char *vdata = NULL; 148 149 for (b = i = 0; i < bytes; ++i) { 150 if (buf[i] == 0) { 151 if (vname == NULL) { 152 vname = buf + b; 153 } else { 154 vdata = buf + b; 155 printf("%s=%s\n", vname, vdata); 156 vname = vdata = NULL; 157 } 158 b = i + 1; 159 } 160 } 161 } 162 163 static int 164 doexec(char **av) 165 { 166 int error; 167 168 error = execvp(av[0], av); 169 return (error); 170 } 171 172 static void 173 usage(void) 174 { 175 fprintf(stderr, "usage: varsym [-adpqsu] [var[=data] ...]\n"); 176 } 177 178