OSDN Git Service

Cleanup getprop, add qstrcmp() to lib for qsort (because posix-2008 broke
authorRob Landley <rob@landley.net>
Mon, 20 Apr 2015 02:50:51 +0000 (21:50 -0500)
committerRob Landley <rob@landley.net>
Mon, 20 Apr 2015 02:50:51 +0000 (21:50 -0500)
alphasort), add compile-time probe for config symbol TOYBOX_ON_ANDROID.

lib/lib.c
lib/lib.h
scripts/genconfig.sh
toys/android/getprop.c

index 99d2ea6..1a60a7f 100644 (file)
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -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);
+}
index 3cda7d9..a4e5808 100644 (file)
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -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);
index ead5b8a..b8dc3c7 100755 (executable)
@@ -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()
index 6653041..9d5398a 100644 (file)
@@ -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