OSDN Git Service

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