varsym - Add -x option
[dragonfly.git] / bin / varsym / varsym.c
CommitLineData
65b3153c
MD
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 *
5c0b1e71 26 * $DragonFly: src/bin/varsym/varsym.c,v 1.4 2003/12/11 20:33:49 dillon Exp $
65b3153c
MD
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
0a004aa2 36static void dumpvars(char *buf, int bytes);
2f5c5b87
MD
37static int doexec(char **av);
38static void usage(void);
aac72047 39
65b3153c
MD
40int
41main(int ac, char **av)
42{
aac72047
EN
43 int i;
44 int mask = VARSYM_ALL_MASK;
45 int level = VARSYM_USER;
46 int deleteOpt = 0;
47 int verboseOpt = 1;
0a004aa2 48 int allOpt = 0;
2f5c5b87 49 int execok = 0;
65b3153c 50
2f5c5b87 51 while ((i = getopt(ac, av, "adhpqsux")) != -1) {
aac72047 52 switch (i) {
0a004aa2
MD
53 case 'a':
54 allOpt = 1;
55 break;
aac72047
EN
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;
2f5c5b87
MD
74 case 'x':
75 mask = VARSYM_PROC_MASK;
76 level = VARSYM_PROC;
77 execok = 1;
78 break;
aac72047
EN
79 case 'h':
80 default:
81 usage();
82 return(-1);
83 }
65b3153c 84 }
65b3153c 85
0a004aa2
MD
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
aac72047
EN
103 for ( ; optind < ac; optind++) {
104 char *name = av[optind];
105 char *data = strchr(name, '=');
106 int error;
107 char buf[MAXVARSYM_DATA];
65b3153c 108
aac72047
EN
109 if (data)
110 *data++ = 0;
111
2f5c5b87
MD
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) {
aac72047 123 error = varsym_set(level, name, NULL);
2f5c5b87 124 } else if (data) {
aac72047 125 error = varsym_set(level, name, data);
2f5c5b87 126 } else {
aac72047
EN
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 }
5c0b1e71 134 if (error < 0 && verboseOpt)
aac72047 135 fprintf(stderr, "%s: %s\n", name, strerror(errno));
65b3153c 136 }
aac72047
EN
137
138 return(0);
139}
140
0a004aa2
MD
141static void
142dumpvars(char *buf, int bytes)
143{
2f5c5b87
MD
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 }
0a004aa2 160 }
2f5c5b87
MD
161}
162
163static int
164doexec(char **av)
165{
166 int error;
167
168 error = execvp(av[0], av);
169 return (error);
0a004aa2
MD
170}
171
172static void
aac72047
EN
173usage(void)
174{
1072b009 175 fprintf(stderr, "usage: varsym [-adpqsu] [var[=data] ...]\n");
65b3153c
MD
176}
177