summaryrefslogtreecommitdiff
path: root/src/net_set.c
diff options
context:
space:
mode:
authorStephen Hemminger <stephen.hemminger@vyatta.com>2010-05-21 14:36:53 -0700
committerStephen Hemminger <stephen.hemminger@vyatta.com>2010-05-21 14:36:53 -0700
commit50b84d48c76b5070410a98f04eb5fcfbe06a233f (patch)
treee93cdd0ca6f7a2c00970ccb6446b6a4dac102ac9 /src/net_set.c
parent708ca20311ea7fb3fb92cb16e29aa582bf9f653d (diff)
downloadvyatta-cfg-50b84d48c76b5070410a98f04eb5fcfbe06a233f.tar.gz
vyatta-cfg-50b84d48c76b5070410a98f04eb5fcfbe06a233f.zip
Rename sysfs to net_set
Less chance of name collision, and useful for /proc as well.
Diffstat (limited to 'src/net_set.c')
-rw-r--r--src/net_set.c79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/net_set.c b/src/net_set.c
new file mode 100644
index 0000000..a84cea4
--- /dev/null
+++ b/src/net_set.c
@@ -0,0 +1,79 @@
+/*
+ * Program to set sysfs value - similar to sysctl commmand
+ */
+
+#include <stdio.h>
+#include <limits.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <string.h>
+
+#define SYS "/sys"
+
+static void get(const char *name)
+{
+ char path[PATH_MAX];
+ char buf[BUFSIZ];
+ FILE *f;
+
+ snprintf(path, PATH_MAX, SYS "/%s", name);
+ f = fopen(path, "r");
+ if (f == NULL) {
+ fprintf(stderr, "%s : %s\n", path, strerror(errno));
+ exit(1);
+ }
+
+ while (fgets(buf, BUFSIZ, f) != NULL)
+ fputs(buf, stdout);
+
+ if (ferror(f)) {
+ fprintf(stderr, "%s : read %s\n", path, strerror(errno));
+ exit(1);
+ }
+ fclose(f);
+}
+
+static void set(const char *name, const char *val)
+{
+ FILE *f;
+ char path[PATH_MAX];
+
+ snprintf(path, PATH_MAX, SYS "/%s", name);
+ f = fopen(path, "w");
+ if (f == NULL) {
+ fprintf(stderr, "%s : %s\n", path, strerror(errno));
+ exit(1);
+ }
+
+ fprintf(f, "%s\n", val);
+ fflush(f);
+
+ if (ferror(f)) {
+ fprintf(stderr, "%s : read %s\n", path, strerror(errno));
+ exit(1);
+ }
+ fclose(f);
+}
+
+int main(int argc, char **argv)
+{
+ if (argc == 1) {
+ fprintf(stderr, "Usage: %s variable\n", argv[0]);
+ fprintf(stderr, " %s variable=value\n", argv[0]);
+ return 1;
+ }
+
+ while (--argc) {
+ char *ep, *arg = *++argv;
+
+ ep = strchr(arg, '=');
+ if (!ep)
+ get(arg);
+ else {
+ *ep++ = '\0';
+ set(arg, ep);
+ }
+ }
+
+ return 0;
+}