1 /* getprop.c - Get an Android system property
2  *
3  * Copyright 2015 The Android Open Source Project
4 
5 USE_GETPROP(NEWTOY(getprop, ">2", TOYFLAG_USR|TOYFLAG_SBIN))
6 
7 config GETPROP
8   bool "getprop"
9   default y
10   depends on TOYBOX_ON_ANDROID
11   help
12     usage: getprop [NAME [DEFAULT]]
13 
14     Gets an Android system property, or lists them all.
15 */
16 
17 #define FOR_getprop
18 #include "toys.h"
19 
20 #if defined(__ANDROID__)
21 
22 #include <cutils/properties.h>
23 
GLOBALS(size_t size;char ** nv;)24 GLOBALS(
25   size_t size;
26   char **nv; // name/value pairs: even=name, odd=value
27 )
28 
29 static void add_property(char *name, char *value, void *unused)
30 {
31   if (!(TT.size&31)) TT.nv = xrealloc(TT.nv, (TT.size+32)*2*sizeof(char *));
32 
33   TT.nv[2*TT.size] = xstrdup(name);
34   TT.nv[1+2*TT.size++] = xstrdup(value);
35 }
36 
getprop_main(void)37 void getprop_main(void)
38 {
39   if (*toys.optargs) {
40     property_get(*toys.optargs, toybuf, toys.optargs[1] ? toys.optargs[1] : "");
41     puts(toybuf);
42   } else {
43     size_t i;
44 
45     if (property_list((void *)add_property, 0)) error_exit("property_list");
46     qsort(TT.nv, TT.size, 2*sizeof(char *), qstrcmp);
47     for (i = 0; i<TT.size; i++) printf("[%s]: [%s]\n", TT.nv[i*2],TT.nv[1+i*2]);
48     if (CFG_TOYBOX_FREE) free(TT.nv);
49   }
50 }
51 
52 #else
53 
getprop_main(void)54 void getprop_main(void)
55 {
56 }
57 
58 #endif
59