diff options
author | Rob Landley <rob@landley.net> | 2015-04-19 21:50:51 -0500 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2015-04-19 21:50:51 -0500 |
commit | 5b493dc48db03c7e27c8ce002fe216bcd778fe92 (patch) | |
tree | b7bb7af18aff4d01bab1b635384d455add1aeb33 | |
parent | 3cf9b08cc9ebeddc449638608f7a28c4c4ba20f9 (diff) | |
download | toybox-5b493dc48db03c7e27c8ce002fe216bcd778fe92.tar.gz |
Cleanup getprop, add qstrcmp() to lib for qsort (because posix-2008 broke
alphasort), add compile-time probe for config symbol TOYBOX_ON_ANDROID.
-rw-r--r-- | lib/lib.c | 8 | ||||
-rw-r--r-- | lib/lib.h | 1 | ||||
-rwxr-xr-x | scripts/genconfig.sh | 7 | ||||
-rw-r--r-- | toys/android/getprop.c | 76 |
4 files changed, 29 insertions, 63 deletions
@@ -887,3 +887,11 @@ int human_readable(char *buf, unsigned long long num) return end; } + +// The qsort man page says you can use alphasort, the posix committee +// disagreed, and doubled down: http://austingroupbugs.net/view.php?id=142 +// So just do our own. (The const is entirely to humor the stupid compiler.) +int qstrcmp(const void *a, const void *b) +{ + return strcmp(*(char **)a, *(char **)b); +} @@ -176,6 +176,7 @@ void base64_init(char *p); int terminal_size(unsigned *x, unsigned *y); int yesno(char *prompt, int def); int human_readable(char *buf, unsigned long long num); +int qstrcmp(const void *a, const void *b); // net.c int xsocket(int domain, int type, int protocol); diff --git a/scripts/genconfig.sh b/scripts/genconfig.sh index ead5b8a2..b8dc3c79 100755 --- a/scripts/genconfig.sh +++ b/scripts/genconfig.sh @@ -65,6 +65,13 @@ EOF struct spwd *a = getspnam("root"); return 0; } EOF + + # Some commands are android-specific + probesymbol TOYBOX_ON_ANDROID -c << EOF + #ifndef __ANDROID__ + #error nope + #endif +EOF } genconfig() diff --git a/toys/android/getprop.c b/toys/android/getprop.c index 66530419..9d5398aa 100644 --- a/toys/android/getprop.c +++ b/toys/android/getprop.c @@ -7,6 +7,7 @@ USE_GETPROP(NEWTOY(getprop, ">2", TOYFLAG_USR|TOYFLAG_SBIN)) config GETPROP bool "getprop" default y + depends on TOYBOX_ON_ANDROID help usage: getprop [NAME [DEFAULT]] @@ -16,83 +17,32 @@ config GETPROP #define FOR_getprop #include "toys.h" -#if defined(__ANDROID__) - -#include <cutils/properties.h> +//#include <cutils/properties.h> GLOBALS( size_t size; - size_t capacity; + char **nv; // name/value pairs: even=name, odd=value ) -struct property_info { - char *name; - char *value; -}; - -static struct property_info **properties; - -static void add_property(const char *name, const char *value, void *unused) +static void add_property(char *name, char *value, void *unused) { - struct property_info *new = xmalloc(sizeof(struct property_info)); + if (!(TT.size&31)) TT.nv = xrealloc(TT.nv, (TT.size+32)*2*sizeof(char *)); - if (TT.size >= TT.capacity) { - TT.capacity += 32; - properties = xrealloc(properties, - TT.capacity * sizeof(struct property_info *)); - } - - // TODO: fix xstrdup signature so we can remove these bogus casts. - new->name = xstrdup((char *) name); - new->value = xstrdup((char *) value); - properties[TT.size++] = new; -} - -static void free_properties() -{ - size_t i; - - for (i = 0; i < TT.size; ++i) { - free(properties[i]->name); - free(properties[i]->value); - free(properties[i]); - } - free(properties); -} - -static int property_cmp(const void *a, const void *b) -{ - struct property_info *pa = *((struct property_info **)a); - struct property_info *pb = *((struct property_info **)b); - - return strcmp(pa->name, pb->name); + TT.nv[2*TT.size] = xstrdup(name); + TT.nv[1+2*TT.size++] = xstrdup(value); } void getprop_main(void) { if (*toys.optargs) { - char value[PROPERTY_VALUE_MAX]; - const char *default_value = ""; - - if (toys.optargs[1]) default_value = toys.optargs[1]; - property_get(*toys.optargs, value, default_value); - puts(value); + property_get(*toys.optargs, toybuf, toys.optargs[1] ? toys.optargs[1] : ""); + puts(toybuf); } else { size_t i; - if (property_list(add_property, NULL)) - error_exit("property_list failed"); - qsort(properties, TT.size, sizeof(struct property_info *), property_cmp); - for (i = 0; i < TT.size; ++i) - printf("[%s]: [%s]\n", properties[i]->name, properties[i]->value); - if (CFG_TOYBOX_FREE) free_properties(); + if (property_list((void *)add_property, 0)) perror_exit("property_list"); + qsort(TT.nv, TT.size, 2*sizeof(char *), alphasort); + for (i = 0; i<TT.size; i++) printf("[%s]: [%s]\n", TT.nv[i*2],TT.nv[1+i*2]); + if (CFG_TOYBOX_FREE) free(TT.nv); } } - -#else - -void getprop_main(void) -{ -} - -#endif |