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;
63 while(*argv && **argv=='-') {
64 char c, *opt = 1+*argv++;
70 case 'a': x = "all"; break;
71 case 'n': x = "not"; break;
72 case 'q': x = "query"; break;
73 case 'e': x = "export"; break;
74 case 's': x = "space"; break;
75 case 'l': x = "list"; break;
76 case 'c': x = "continue"; break;
77 case 'h': x = "help"; break;
78 case 'u': x = "user"; break;
80 if (!x || strcmp(x,opt))
90 case 'a': all = 1; break;
91 case 'n': not = 1; break;
92 case 'q': query = 1; break;
93 case 'e': export = 1; break;
94 case 's': space = 1; break;
95 case 'l': list = 1; break;
96 case 'c': cont = 1; break;
97 case 'u': user = *argv; if (user) argv++; break;
99 fprintf( stdout, help, basename(progname));
102 fprintf( stderr, usage, basename(progname));
112 "error! --all and --query aren't compatibles.\n");
117 "error! --not and --query aren't compatibles.\n");
122 "error! --list and --query aren't compatibles.\n");
127 "error! --continue and --query aren't compatibles.\n");
132 "warning! --space option ignored for queries.\n");
136 "warning! --export option ignored for queries.\n");
142 "error! --all doesn't accept any key.\n");
145 /* all is like not nothing!! */
150 n = tzplatform_getcount();
151 sel = calloc( sizeof(int), n);
153 fprintf( stderr, "error! out of memory!\n");
157 /* get the variables from the list */
159 id = tzplatform_getid( *argv);
160 if (id == _TZPLATFORM_VARIABLES_INVALID_) {
163 fprintf( stderr, "error! %s isn't a variable.\n", *argv);
173 /* finished for queries */
179 for (i=0 ; '0' <= user[i] && user[i] <= '9' ; i++);
181 spw = getpwnam(user);
183 spw = getpwuid((uid_t)atoi(user));
185 fprintf( stderr, "error! %s isn't standing for a valid user.\n", user);
189 i = tzplatform_set_user(spw->pw_uid);
191 fprintf( stderr, "error! can't set the valid user %s.\n", user);
198 /* emits the result */
199 for (p = i = 0 ; i < n ; i++) {
202 printf( space ? " " : export ? "\nexport " : "\n");
205 id = (enum tzplatform_variable) i;
206 printf( "%s", tzplatform_getname(id));
208 printf( "=%s", tzplatform_getenv(id));