[PATCH] I2C bus fake enumeration applet lsi2c
souf
souf_oued at yahoo.fr
Fri Jan 3 20:09:18 UTC 2014
happy New Year for all,
Le 03/01/2014 06:53, alison at she-devel.com a écrit :
> From: Alison Chaiken <alison_chaiken at mentor.com>
>
> Signed-off-by: Alison Chaiken <alison_chaiken at mentor.com>
> ---
> util-linux/lsi2c.c | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++
> 1 file changed, 83 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..4cc2191
> --- /dev/null
> +++ b/util-linux/lsi2c.c
> @@ -0,0 +1,83 @@
> +/*
> + * 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);
> +
Why you are using readdir() and recursive_action()? recursive_action
does it for you.
Can you give an example of "nextfile" ?
> + 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);
you can not free fileName, GCC message:
warning: attempt to free a non-heap object [-Wfree-nonheap-object]
> + free(fileName);
> + closedir(dir);
> + }
> +
> + return EXIT_SUCCESS;
> +}
>
More information about the busybox
mailing list