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
20 #if defined(__ANDROID__)
21
22 #include <cutils/properties.h>
23
setprop_main(void)24 void setprop_main(void)
25 {
26 char *name = toys.optargs[0], *value = toys.optargs[1];
27 char *p;
28 size_t name_len = strlen(name), value_len = strlen(value);
29
30 // property_set doesn't tell us why it failed, and actually can't
31 // recognize most failures (because it doesn't wait for init), so
32 // we duplicate all of init's checks here to help the user.
33
34 if (name_len >= PROP_NAME_MAX)
35 error_exit("name '%s' too long; try '%.*s'",
36 name, PROP_NAME_MAX - 1, name);
37 if (value_len >= PROP_VALUE_MAX)
38 error_exit("value '%s' too long; try '%.*s'",
39 value, PROP_VALUE_MAX - 1, value);
40
41 if (*name == '.' || name[name_len - 1] == '.')
42 error_exit("property names must not start or end with '.'");
43 if (strstr(name, ".."))
44 error_exit("'..' is not allowed in a property name");
45 for (p = name; *p; ++p)
46 if (!isalnum(*p) && !strchr("_.-", *p))
47 error_exit("invalid character '%c' in name '%s'", *p, name);
48
49 if (property_set(name, value))
50 error_msg("failed to set property '%s' to '%s'", name, value);
51 }
52
53 #else
54
setprop_main(void)55 void setprop_main(void)
56 {
57 }
58
59 #endif
60