2 * Copyright (C) 2013-2014 Intel Corporation.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 * José Bollo <jose.bollo@open.eurogiciel.org>
20 * Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
21 * Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
28 #include <sys/types.h>
31 #include <tzplatform_config.h>
33 #define basename(x) (x)
35 static const char usage [] = "type '%s --help' to get help.\n";
37 static const char help [] = "\
39 usage: %s [options...] [keys...]\n\
43 -a --all all keys (must not be followed by keys)\n\
44 -n --not not in the keys\n\
45 -e --export prefix with export\n\
46 -l --list prints only the key names, not the values\n\
47 -s --space separate with spaces instead of new-lines\n\
48 -q --query silently check that given variables are existing\n\
49 -c --continue continue to process if error\n\
50 -u --user id set the user using its 'id' (name or numeric)\n\
54 int main(int argc, char **argv)
56 char *progname = *argv++, *user = 0;
57 int all = 0, not = 0, query = 0, export = 0, space = 0, list = 0, cont = 0;
59 enum tzplatform_variable id;
60 struct passwd pwd, *spw;
64 while(*argv && **argv=='-') {
65 char c, *opt = 1+*argv++;
71 case 'a': x = "all"; break;
72 case 'n': x = "not"; break;
73 case 'q': x = "query"; break;
74 case 'e': x = "export"; break;
75 case 's': x = "space"; break;
76 case 'l': x = "list"; break;
77 case 'c': x = "continue"; break;
78 case 'h': x = "help"; break;
79 case 'u': x = "user"; break;
81 if (!x || strcmp(x,opt))
91 case 'a': all = 1; break;
92 case 'n': not = 1; break;
93 case 'q': query = 1; break;
94 case 'e': export = 1; break;
95 case 's': space = 1; break;
96 case 'l': list = 1; break;
97 case 'c': cont = 1; break;
98 case 'u': user = *argv; if (user) argv++; break;
100 fprintf( stdout, help, basename(progname));
103 fprintf( stderr, usage, basename(progname));
113 "error! --all and --query aren't compatibles.\n");
118 "error! --not and --query aren't compatibles.\n");
123 "error! --list and --query aren't compatibles.\n");
128 "error! --continue and --query aren't compatibles.\n");
133 "warning! --space option ignored for queries.\n");
137 "warning! --export option ignored for queries.\n");
143 "error! --all doesn't accept any key.\n");
146 /* all is like not nothing!! */
151 n = tzplatform_getcount();
152 sel = calloc( sizeof(int), n);
154 fprintf( stderr, "error! out of memory!\n");
158 /* get the variables from the list */
160 id = tzplatform_getid( *argv);
161 if (id == _TZPLATFORM_VARIABLES_INVALID_) {
164 fprintf( stderr, "error! %s isn't a variable.\n", *argv);
174 /* finished for queries */
180 for (i=0 ; '0' <= user[i] && user[i] <= '9' ; i++);
182 getpwnam_r(user, &pwd, buf, sizeof(buf), &spw);
184 getpwuid_r(user, &pwd, buf, sizeof(buf), &spw);
186 fprintf( stderr, "error! %s isn't standing for a valid user.\n", user);
190 i = tzplatform_set_user(spw->pw_uid);
192 fprintf( stderr, "error! can't set the valid user %s.\n", user);
199 /* emits the result */
200 for (p = i = 0 ; i < n ; i++) {
203 printf( space ? " " : export ? "\nexport " : "\n");
206 id = (enum tzplatform_variable) i;
207 printf( "%s", tzplatform_getname(id));
209 printf( "=%s", tzplatform_getenv(id));