[PATCH] I2C bus fake enumeration applet lsi2c

alison at she-devel.com alison at she-devel.com
Sun Jan 5 05:30:54 UTC 2014


From: Alison Chaiken <alison_chaiken at mentor.com>

Signed-off-by: Alison Chaiken <alison_chaiken at mentor.com>
---
 util-linux/lsi2c.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 82 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..b5ff9a9
--- /dev/null
+++ b/util-linux/lsi2c.c
@@ -0,0 +1,82 @@
+/*
+ * 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 256
+
+/*  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)
+		bb_error_msg_and_die("Pathname too long to process: %s.\n", instring);
+
+	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 *nextFile = NULL;
+	const char *fileName = "/sys/bus/i2c/drivers";
+	DIR *dir;
+	struct dirent *next;
+
+	dir = xopendir(fileName);
+
+	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 */);
+	}
+
+	if (ENABLE_FEATURE_CLEAN_UP) {
+		free(nextFile);
+		closedir(dir);
+	}
+
+	return EXIT_SUCCESS;
+}
-- 
1.8.5.2



More information about the busybox mailing list