Fix coding rule violation
[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                         } else {
87                                 c = *opt;
88                                 if (!*++opt)
89                                         opt = 0;
90                         }
91                         switch (c) {
92                         case 'a': all = 1; break;
93                         case 'n': not = 1; break;
94                         case 'q': query = 1; break;
95                         case 'e': export = 1; break;
96                         case 's': space = 1; break;
97                         case 'l': list = 1; break;
98                         case 'c': cont = 1; break;
99                         case 'u': user = *argv; if (user) argv++; break;
100                         case 'h':
101                                 fprintf(stdout, help, basename(progname));
102                                 return 0;
103                         default:
104                                 fprintf(stderr, usage, basename(progname));
105                                 return 1;
106                         }
107                 }
108         }
109
110         /* some checks */
111         if (query) {
112                 if (all) {
113                         fprintf(stderr,
114                                 "error! --all and --query aren't compatibles.\n");
115                         return 1;
116                 }
117                 if (not) {
118                         fprintf(stderr,
119                                 "error! --not and --query aren't compatibles.\n");
120                         return 1;
121                 }
122                 if (list) {
123                         fprintf(stderr,
124                                 "error! --list and --query aren't compatibles.\n");
125                         return 1;
126                 }
127                 if (cont) {
128                         fprintf(stderr,
129                                 "error! --continue and --query aren't compatibles.\n");
130                         return 1;
131                 }
132                 if (space) {
133                         fprintf(stderr,
134                                 "warning! --space option ignored for queries.\n");
135                 }
136                 if (export) {
137                         fprintf(stderr,
138                                 "warning! --export option ignored for queries.\n");
139                 }
140         }
141         if (all) {
142                 if (*argv) {
143                         fprintf(stderr,
144                                 "error! --all doesn't accept any key.\n");
145                         return 1;
146                 }
147                 /* all is like not nothing!! */
148                 not = 1;
149         }
150
151         /* process */
152         n = tzplatform_getcount();
153         sel = calloc(sizeof(int), n);
154         if (sel == NULL) {
155                 fprintf(stderr, "error! out of memory!\n");
156                 return 1;
157         }
158
159         /* get the variables from the list */
160         while (*argv) {
161                 id = tzplatform_getid(*argv);
162                 if (id == _TZPLATFORM_VARIABLES_INVALID_) {
163                         if (query) {
164                                 ret = 1;
165                                 goto out;
166                         }
167                         fprintf(stderr, "error! %s isn't a variable.\n", *argv);
168                         if (!cont) {
169                                 ret = 1;
170                                 goto out;
171                         }
172                 } else {
173                         sel[(int)id] = 1;
174                 }
175                 argv++;
176         }
177
178         /* finished for queries */
179         if (query) {
180                 ret = 0;
181                 goto out;
182         }
183
184         /* set the user */
185         if (user) {
186                 for (i = 0 ; '0' <= user[i] && user[i] <= '9' ; i++);
187                 if (user[i])
188                         getpwnam_r(user, &pwd, buf, sizeof(buf), &spw);
189                 else
190                         getpwuid_r((uid_t)atoi(user), &pwd, buf, sizeof(buf), &spw);
191                 if (!spw) {
192                         fprintf(stderr, "error! %s isn't standing for a valid user.\n", user);
193                         if (!cont) {
194                                 ret = 1;
195                                 goto out;
196                         }
197                 } else {
198                         i = tzplatform_set_user(spw->pw_uid);
199                         if (i) {
200                                 fprintf(stderr, "error! can't set the valid user %s.\n", user);
201                                 if (!cont) {
202                                         ret = 1;
203                                         goto out;
204                                 }
205                         }
206                 }
207         }
208
209         /* emits the result */
210         for (p = i = 0 ; i < n ; i++) {
211                 if (sel[i] != not) {
212                         if (p++)
213                                 printf(space ? " " : export ? "\nexport " : "\n");
214                         else if (export)
215                                 printf("export ");
216                         id = (enum tzplatform_variable) i;
217                         printf("%s", tzplatform_getname(id));
218                         if (!list)
219                                 printf("=%s", tzplatform_getenv(id));
220                 }
221         }
222         if (p)
223                 printf("\n");
224
225         /* return value is 0 */
226         ret = 0;
227 out:
228         if (sel)
229                 free(sel);
230
231         return ret;
232 }
233