1903fe54bbe252fc632c5ce9b7a6f334791651ab
[platform/core/system/tizen-platform-config.git] / src / tzplatform_get.c
1 /*
2  * Copyright (C) 2013-2014 Intel Corporation.
3  * 
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.
8  *
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.
13  *
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
17  *
18  * Authors:
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>
22  *
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <pwd.h>
30
31 #include <tzplatform_config.h>
32
33 #define basename(x) (x)
34
35 static const char usage [] = "type '%s --help' to get help.\n";
36
37 static const char help [] = "\
38 \n\
39 usage: %s [options...] [keys...]\n\
40 \n\
41 options:\n\
42 \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\
51 \n\
52 ";
53
54 int main(int argc, char **argv)
55 {
56         char *progname = *argv++, *user = 0;
57         int all = 0, not = 0, query = 0, export = 0, space = 0, list = 0, cont = 0;
58         int i, n, p;
59         int *sel = NULL;
60         enum tzplatform_variable id;
61         struct passwd pwd, *spw;
62         char buf[1024];
63         int ret = 0;
64
65         /* parse args */
66         while(*argv && **argv=='-') {
67                 char c, *opt = 1+*argv++;
68                 while(opt) {
69                         if (*opt == '-') {
70                                 char *x = 0;
71                                 c = *++opt;
72                                 switch(c) {
73                                         case 'a': x = "all"; break;
74                                         case 'n': x = "not"; break;
75                                         case 'q': x = "query"; break;
76                                         case 'e': x = "export"; break;
77                                         case 's': x = "space"; break;
78                                         case 'l': x = "list"; break;
79                                         case 'c': x = "continue"; break;
80                                         case 'h': x = "help"; break;
81                                         case 'u': x = "user"; break;
82                                 }
83                                 if (!x || strcmp(x,opt))
84                                         c = 0;
85                                 opt = 0;
86                         }
87                         else {
88                                 c = *opt;
89                                 if (!*++opt)
90                                         opt = 0;
91                         }
92                         switch(c) {
93                         case 'a': all = 1; break;
94                         case 'n': not = 1; break;
95                         case 'q': query = 1; break;
96                         case 'e': export = 1; break;
97                         case 's': space = 1; break;
98                         case 'l': list = 1; break;
99                         case 'c': cont = 1; break;
100                         case 'u': user = *argv; if (user) argv++; break;
101                         case 'h':
102                                 fprintf( stdout, help, basename(progname));
103                                 return 0;
104                         default:
105                                 fprintf( stderr, usage, basename(progname));
106                                 return 1;
107                         }
108                 }
109         }
110
111         /* some checks */
112         if (query) {
113                 if (all) {
114                         fprintf( stderr, 
115                                 "error! --all and --query aren't compatibles.\n");
116                         return 1;
117                 }
118                 if (not) {
119                         fprintf( stderr, 
120                                 "error! --not and --query aren't compatibles.\n");
121                         return 1;
122                 }
123                 if (list) {
124                         fprintf( stderr, 
125                                 "error! --list and --query aren't compatibles.\n");
126                         return 1;
127                 }
128                 if (cont) {
129                         fprintf( stderr, 
130                                 "error! --continue and --query aren't compatibles.\n");
131                         return 1;
132                 }
133                 if (space) {
134                         fprintf( stderr, 
135                                 "warning! --space option ignored for queries.\n");
136                 }
137                 if (export) {
138                         fprintf( stderr, 
139                                 "warning! --export option ignored for queries.\n");
140                 }
141         }
142         if (all) {
143                 if (*argv) {
144                         fprintf( stderr, 
145                                 "error! --all doesn't accept any key.\n");
146                         return 1;
147                 }
148                 /* all is like not nothing!! */
149                 not = 1;
150         }
151
152         /* process */
153         n = tzplatform_getcount();
154         sel = calloc(sizeof(int), n);
155         if (sel == NULL) {
156                 fprintf( stderr, "error! out of memory!\n");
157                 return 1;
158         }
159
160         /* get the variables from the list */
161         while (*argv) {
162                 id = tzplatform_getid( *argv);
163                 if (id == _TZPLATFORM_VARIABLES_INVALID_) {
164                         if (query) {
165                                 ret = 1;
166                                 goto out;
167                         }
168                         fprintf( stderr, "error! %s isn't a variable.\n", *argv);
169                         if (!cont) {
170                                 ret = 1;
171                                 goto out;
172                         }
173                 }
174                 else {
175                         sel[(int)id] = 1;
176                 }
177                 argv++;
178         }
179
180         /* finished for queries */
181         if (query) {
182                 ret = 0;
183                 goto out;
184         }
185
186         /* set the user */
187         if (user) {
188                 for (i=0 ; '0' <= user[i] && user[i] <= '9' ; i++);
189                 if (user[i])
190                         getpwnam_r(user, &pwd, buf, sizeof(buf), &spw);
191                 else
192                         getpwuid_r((uid_t)atoi(user), &pwd, buf, sizeof(buf), &spw);
193                 if (!spw) {
194                         fprintf( stderr, "error! %s isn't standing for a valid user.\n", user);
195                         if (!cont) {
196                                 ret = 1;
197                                 goto out;
198                         }
199                 } else {
200                         i = tzplatform_set_user(spw->pw_uid);
201                         if (i) {
202                                 fprintf( stderr, "error! can't set the valid user %s.\n", user);
203                                 if (!cont) {
204                                         ret = 1;
205                                         goto out;
206                                 }
207                         }
208                 }
209         }
210
211         /* emits the result */
212         for (p = i = 0 ; i < n ; i++) {
213                 if (sel[i] != not) {
214                         if (p++) 
215                                 printf( space ? " " : export ? "\nexport " : "\n");
216                         else if (export)
217                                 printf( "export ");
218                         id = (enum tzplatform_variable) i;
219                         printf( "%s", tzplatform_getname(id));
220                         if (!list)      
221                                 printf( "=%s", tzplatform_getenv(id));
222                 }
223         }
224         if (p)
225                 printf("\n");
226
227         /* return value is 0 */
228         ret = 0;
229 out:
230         if (sel)
231                 free(sel);
232
233         return ret;
234 }
235