[PATCH 4/4] I2C bus fake enumeration applet lsi2c
alison at she-devel.com
alison at she-devel.com
Thu Jan 2 07:25:20 UTC 2014
From: Alison Chaiken <alison_chaiken at mentor.com>
Signed-off-by: Alison Chaiken <alison_chaiken at mentor.com>
---
util-linux/lsi2c.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 84 insertions(+)
create mode 100644 util-linux/lsi2c.c
diff --git a/util-linux/lsi2c.c b/util-linux/lsi2c.c
new file mode 100644
index 0000000..ea1e3b5
--- /dev/null
+++ b/util-linux/lsi2c.c
@@ -0,0 +1,84 @@
+/*
+ * lsi2c implementation for busybox
+ *
+ * Copyright (C) 2013 Alison Chaiken alison at she-devel.com
+ *
+ * Licensed under GPLv2 or later, see file LICENSE in this source tree.
+ */
+
+//usage:#define lsi2c_trivial_usage NOUSAGE_STR
+//usage:#define lsi2c_full_usage ""
+
+#include <stdio.h>
+#include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
+#include <dirent.h>
+#include "libbb.h"
+
+#define MAXSTRING 2456
+
+/* int FAST_FUNC (*fileAction)(const char *fileName, struct stat *statbuf,
+ void* userData, int depth), */
+static int FAST_FUNC fileAction(
+ const char *instring,
+ struct stat *statbuf UNUSED_PARAM,
+ void *userData UNUSED_PARAM,
+ int depth UNUSED_PARAM)
+{
+ char sysfs_node[MAXSTRING];
+ char savestr[MAXSTRING];
+ char drivername[MAXSTRING];
+ char *dashpos;
+ const char *delim = "-";
+
+ if (strlen(instring) > MAXSTRING) {
+ fprintf(stderr, "Pathname too long to process: %s.\n", instring);
+ exit(-1);
+ }
+ strcpy(drivername, instring);
+
+/* see comments about basename in libbb.h */
+ strcpy(sysfs_node, bb_basename(instring));
+ strcpy(savestr, sysfs_node);
+ dashpos = strtok(savestr, delim);
+ dashpos = strtok(NULL, delim);
+ strcpy(drivername, bb_basename(dirname(drivername)));
+
+ if ((dashpos) && isdigit(*sysfs_node))
+ printf("Controller %c for driver %s at address 0x%s.\n", *sysfs_node,
+ drivername, dashpos);
+
+ return TRUE;
+}
+
+int lsi2c_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int lsi2c_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
+{
+ char fileName[MAXSTRING];
+ char *nextFile = NULL;
+ DIR *dir;
+ struct dirent *next;
+
+ strcpy(fileName, "/sys/bus/i2c/drivers");
+ dir = opendir(fileName);
+ if (!dir) {
+ fprintf(stderr, "Can't open sysfs.\n");
+ exit(-1);
+ }
+ while ((next = readdir(dir)) != NULL) {
+ nextFile = concat_subpath_file(fileName, next->d_name);
+ if (nextFile == NULL)
+ continue;
+ recursive_action(nextFile,
+ ACTION_RECURSE,
+ fileAction,
+ NULL, /* dirAction */
+ NULL, /* userData */
+ 0 /* depth */);
+ }
+
+ free(nextFile);
+ closedir(dir);
+ return EXIT_SUCCESS;
+}
--
1.7.9.5
More information about the busybox
mailing list