aboutsummaryrefslogtreecommitdiff
path: root/toys/android
diff options
context:
space:
mode:
authorRob Landley <rob@landley.net>2015-04-18 20:28:15 -0500
committerRob Landley <rob@landley.net>2015-04-18 20:28:15 -0500
commitd5566b3bf443b19ec511d702b4f03e352afafc86 (patch)
treee1431626df5a5c0a3206f1cd2e2027c3690adcbb /toys/android
parent58c326901827e1b54a7d82fb38633eafc448874d (diff)
downloadtoybox-d5566b3bf443b19ec511d702b4f03e352afafc86.tar.gz
Android getprop and setprop from Elliott Hughes.
Diffstat (limited to 'toys/android')
-rw-r--r--toys/android/getprop.c98
-rw-r--r--toys/android/setprop.c52
2 files changed, 150 insertions, 0 deletions
diff --git a/toys/android/getprop.c b/toys/android/getprop.c
new file mode 100644
index 00000000..66530419
--- /dev/null
+++ b/toys/android/getprop.c
@@ -0,0 +1,98 @@
+/* getprop.c - Get an Android system property
+ *
+ * Copyright 2015 The Android Open Source Project
+
+USE_GETPROP(NEWTOY(getprop, ">2", TOYFLAG_USR|TOYFLAG_SBIN))
+
+config GETPROP
+ bool "getprop"
+ default y
+ help
+ usage: getprop [NAME [DEFAULT]]
+
+ Gets an Android system property, or lists them all.
+*/
+
+#define FOR_getprop
+#include "toys.h"
+
+#if defined(__ANDROID__)
+
+#include <cutils/properties.h>
+
+GLOBALS(
+ size_t size;
+ size_t capacity;
+)
+
+struct property_info {
+ char *name;
+ char *value;
+};
+
+static struct property_info **properties;
+
+static void add_property(const char *name, const char *value, void *unused)
+{
+ struct property_info *new = xmalloc(sizeof(struct property_info));
+
+ 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);
+}
+
+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);
+ } 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();
+ }
+}
+
+#else
+
+void getprop_main(void)
+{
+}
+
+#endif
diff --git a/toys/android/setprop.c b/toys/android/setprop.c
new file mode 100644
index 00000000..ef24c9ad
--- /dev/null
+++ b/toys/android/setprop.c
@@ -0,0 +1,52 @@
+/* setprop.c - Set an Android system property
+ *
+ * Copyright 2015 The Android Open Source Project
+
+USE_SETPROP(NEWTOY(setprop, "<2>2", TOYFLAG_USR|TOYFLAG_SBIN))
+
+config SETPROP
+ bool "setprop"
+ default y
+ help
+ usage: setprop NAME VALUE
+
+ Sets an Android system property.
+*/
+
+#define FOR_setprop
+#include "toys.h"
+
+#if defined(__ANDROID__)
+#include <cutils/properties.h>
+#endif
+
+void setprop_main(void)
+{
+#if defined(__ANDROID__)
+ char *name = toys.optargs[0], *value = toys.optargs[1];
+ char *p;
+ size_t name_len = strlen(name), value_len = strlen(value);
+
+ // property_set doesn't tell us why it failed, and actually can't
+ // recognize most failures (because it doesn't wait for init), so
+ // we duplicate all of init's checks here to help the user.
+
+ if (name_len >= PROP_NAME_MAX)
+ error_exit("name '%s' too long; try '%.*s'",
+ name, PROP_NAME_MAX - 1, name);
+ if (value_len >= PROP_VALUE_MAX)
+ error_exit("value '%s' too long; try '%.*s'",
+ value, PROP_VALUE_MAX - 1, value);
+
+ if (*name == '.' || name[name_len - 1] == '.')
+ error_exit("property names must not start or end with '.'");
+ if (strstr(name, ".."))
+ error_exit("'..' is not allowed in a property name");
+ for (p = name; *p; ++p)
+ if (!isalnum(*p) && !strchr("_.-", *p))
+ error_exit("invalid character '%c' in name '%s'", *p, name);
+
+ if (property_set(name, value))
+ error_msg("failed to set property '%s' to '%s'", name, value);
+#endif
+}