tzplatform_get: fix to support numeric user id
[platform/core/system/tizen-platform-wrapper.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, *sel, p;
59         enum tzplatform_variable id;
60         struct passwd pwd, *spw;
61         char buf[1024];
62
63         /* parse args */
64         while(*argv && **argv=='-') {
65                 char c, *opt = 1+*argv++;
66                 while(opt) {
67                         if (*opt == '-') {
68                                 char *x = 0;
69                                 c = *++opt;
70                                 switch(c) {
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;
80                                 }
81                                 if (!x || strcmp(x,opt))
82                                         c = 0;
83                                 opt = 0;
84                         }
85                         else {
86                                 c = *opt;
87                                 if (!*++opt)
88                                         opt = 0;
89                         }
90                         switch(c) {
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;
99                         case 'h':
100                                 fprintf( stdout, help, basename(progname));
101                                 return 0;
102                         default:
103                                 fprintf( stderr, usage, basename(progname));
104                                 return 1;
105                         }
106                 }
107         }
108
109         /* some checks */
110         if (query) {
111                 if (all) {
112                         fprintf( stderr, 
113                                 "error! --all and --query aren't compatibles.\n");
114                         return 1;
115                 }
116                 if (not) {
117                         fprintf( stderr, 
118                                 "error! --not and --query aren't compatibles.\n");
119                         return 1;
120                 }
121                 if (list) {
122                         fprintf( stderr, 
123                                 "error! --list and --query aren't compatibles.\n");
124                         return 1;
125                 }
126                 if (cont) {
127                         fprintf( stderr, 
128                                 "error! --continue and --query aren't compatibles.\n");
129                         return 1;
130                 }
131                 if (space) {
132                         fprintf( stderr, 
133                                 "warning! --space option ignored for queries.\n");
134                 }
135                 if (export) {
136                         fprintf( stderr, 
137                                 "warning! --export option ignored for queries.\n");
138                 }
139         }
140         if (all) {
141                 if (*argv) {
142                         fprintf( stderr, 
143                                 "error! --all doesn't accept any key.\n");
144                         return 1;
145                 }
146                 /* all is like not nothing!! */
147                 not = 1;
148         }
149
150         /* process */
151         n = tzplatform_getcount();
152         sel = calloc( sizeof(int), n);
153         if (sel == NULL) {
154                 fprintf( stderr, "error! out of memory!\n");
155                 return 1;
156         }
157
158         /* get the variables from the list */
159         while (*argv) {
160                 id = tzplatform_getid( *argv);
161                 if (id == _TZPLATFORM_VARIABLES_INVALID_) {
162                         if (query)
163                                 return 1;
164                         fprintf( stderr, "error! %s isn't a variable.\n", *argv);
165                         if (!cont)
166                                 return 1;
167                 }
168                 else {
169                         sel[(int)id] = 1;
170                 }
171                 argv++;
172         }
173
174         /* finished for queries */
175         if (query) 
176                 return 0;
177
178         /* set the user */
179         if (user) {
180                 for (i=0 ; '0' <= user[i] && user[i] <= '9' ; i++);
181                 if (user[i])
182                         getpwnam_r(user, &pwd, buf, sizeof(buf), &spw);
183                 else
184                         getpwuid_r((uid_t)atoi(user), &pwd, buf, sizeof(buf), &spw);
185                 if (!spw) {
186                         fprintf( stderr, "error! %s isn't standing for a valid user.\n", user);
187                         if (!cont)
188                                 return 1;
189                 } else {
190                         i = tzplatform_set_user(spw->pw_uid);
191                         if (i) {
192                                 fprintf( stderr, "error! can't set the valid user %s.\n", user);
193                                 if (!cont)
194                                         return 1;
195                         }
196                 }
197         }
198
199         /* emits the result */
200         for (p = i = 0 ; i < n ; i++) {
201                 if (sel[i] != not) {
202                         if (p++) 
203                                 printf( space ? " " : export ? "\nexport " : "\n");
204                         else if (export)
205                                 printf( "export ");
206                         id = (enum tzplatform_variable) i;
207                         printf( "%s", tzplatform_getname(id));
208                         if (!list)      
209                                 printf( "=%s", tzplatform_getenv(id));
210                 }
211         }
212         if (p)
213                 printf("\n");
214         return 0;
215 }
216