Merge tag 'trace-v4.1-2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[firefly-linux-kernel-4.4.55.git] / tools / iio / iio_utils.c
index aea9282101871e9d53b52c02340e842fde9292bc..6f6452167b670a833ecc50b6eb136743a9fafd05 100644 (file)
@@ -6,6 +6,8 @@
  * under the terms of the GNU General Public License version 2 as published by
  * the Free Software Foundation.
  */
+#ifndef _IIO_UTILS_H
+#define _IIO_UTILS_H
 
 #include <string.h>
 #include <stdlib.h>
 
 const char *iio_dir = "/sys/bus/iio/devices/";
 
+static char * const iio_direction[] = {
+       "in",
+       "out",
+};
+
 /**
  * iioutils_break_up_name() - extract generic name from full channel name
  * @full_name: the full channel name
@@ -28,10 +35,19 @@ int iioutils_break_up_name(const char *full_name,
 {
        char *current;
        char *w, *r;
-       char *working;
+       char *working, *prefix = "";
+       int i;
+
+       for (i = 0; i < sizeof(iio_direction) / sizeof(iio_direction[0]); i++)
+               if (!strncmp(full_name, iio_direction[i],
+                            strlen(iio_direction[i]))) {
+                       prefix = iio_direction[i];
+                       break;
+               }
 
-       current = strdup(full_name);
+       current = strdup(full_name + strlen(prefix) + 1);
        working = strtok(current, "_\0");
+
        w = working;
        r = working;
 
@@ -43,7 +59,7 @@ int iioutils_break_up_name(const char *full_name,
                r++;
        }
        *w = '\0';
-       *generic_name = strdup(working);
+       asprintf(generic_name, "%s_%s", prefix, working);
        free(current);
 
        return 0;
@@ -649,3 +665,5 @@ error_free:
        free(temp);
        return ret;
 }
+
+#endif /* _IIO_UTILS_H */