1 /* setprop.c - Set an Android system property
2 *
3 * Copyright 2015 The Android Open Source Project
4
5 USE_SETPROP(NEWTOY(setprop, "<2>2", TOYFLAG_USR|TOYFLAG_SBIN))
6
7 config SETPROP
8 bool "setprop"
9 default y
10 depends on TOYBOX_ON_ANDROID
11 help
12 usage: setprop NAME VALUE
13
14 Sets an Android system property.
15 */
16
17 #define FOR_setprop
18 #include "toys.h"
19
setprop_main(void)20 void setprop_main(void)
21 {
22 char *name = toys.optargs[0], *value = toys.optargs[1];
23 char *p;
24 size_t name_len = strlen(name), value_len = strlen(value);
25
26 // property_set doesn't tell us why it failed, and actually can't
27 // recognize most failures (because it doesn't wait for init), so
28 // we duplicate all of init's checks here to help the user.
29
30 if (value_len >= PROP_VALUE_MAX && !strncmp(value, "ro.", 3))
31 error_exit("value '%s' too long; try '%.*s'",
32 value, PROP_VALUE_MAX - 1, value);
33
34 if (*name == '.' || name[name_len - 1] == '.')
35 error_exit("property names must not start or end with '.'");
36 if (strstr(name, ".."))
37 error_exit("'..' is not allowed in a property name");
38 for (p = name; *p; ++p)
39 if (!isalnum(*p) && !strchr(":@_.-", *p))
40 error_exit("invalid character '%c' in name '%s'", *p, name);
41
42 if (__system_property_set(name, value))
43 error_msg("failed to set property '%s' to '%s'", name, value);
44 }
45