summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorStephen Hemminger <stephen.hemminger@vyatta.com>2010-05-21 14:18:37 -0700
committerStephen Hemminger <stephen.hemminger@vyatta.com>2010-05-21 14:18:37 -0700
commit1ed9d5c0937a9aaadc65db5071cf939cd8bef75a (patch)
treecdcbecc030c29e2d4119aab2db6e4795b7219f8f /src
parentbb6651f074d6f7f666b4e9a8fbed353a1c80df10 (diff)
parent7d70cc23db917a04561e390ca039142ec1a0a6ff (diff)
downloadvyatta-cfg-1ed9d5c0937a9aaadc65db5071cf939cd8bef75a.tar.gz
vyatta-cfg-1ed9d5c0937a9aaadc65db5071cf939cd8bef75a.zip
Merge branch 'larkspur' of vm:git/vyatta-cfg into larkspur
Diffstat (limited to 'src')
-rw-r--r--src/sysfs.c79
1 files changed, 79 insertions, 0 deletions
diff --git a/src/sysfs.c b/src/sysfs.c
new file mode 100644
index 0000000..a84cea4
--- /dev/null
+++ b/src/sysfs.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;
+}