diff options
Diffstat (limited to 'tools/iio/lsiio.c')
-rw-r--r-- | tools/iio/lsiio.c | 55 |
1 files changed, 29 insertions, 26 deletions
diff --git a/tools/iio/lsiio.c b/tools/iio/lsiio.c index b59ee1733924..3d650e668252 100644 --- a/tools/iio/lsiio.c +++ b/tools/iio/lsiio.c | |||
@@ -20,7 +20,6 @@ | |||
20 | #include <sys/dir.h> | 20 | #include <sys/dir.h> |
21 | #include "iio_utils.h" | 21 | #include "iio_utils.h" |
22 | 22 | ||
23 | |||
24 | static enum verbosity { | 23 | static enum verbosity { |
25 | VERBLEVEL_DEFAULT, /* 0 gives lspci behaviour */ | 24 | VERBLEVEL_DEFAULT, /* 0 gives lspci behaviour */ |
26 | VERBLEVEL_SENSORS, /* 1 lists sensors */ | 25 | VERBLEVEL_SENSORS, /* 1 lists sensors */ |
@@ -29,17 +28,16 @@ static enum verbosity { | |||
29 | const char *type_device = "iio:device"; | 28 | const char *type_device = "iio:device"; |
30 | const char *type_trigger = "trigger"; | 29 | const char *type_trigger = "trigger"; |
31 | 30 | ||
32 | |||
33 | static inline int check_prefix(const char *str, const char *prefix) | 31 | static inline int check_prefix(const char *str, const char *prefix) |
34 | { | 32 | { |
35 | return strlen(str) > strlen(prefix) && | 33 | return strlen(str) > strlen(prefix) && |
36 | strncmp(str, prefix, strlen(prefix)) == 0; | 34 | strncmp(str, prefix, strlen(prefix)) == 0; |
37 | } | 35 | } |
38 | 36 | ||
39 | static inline int check_postfix(const char *str, const char *postfix) | 37 | static inline int check_postfix(const char *str, const char *postfix) |
40 | { | 38 | { |
41 | return strlen(str) > strlen(postfix) && | 39 | return strlen(str) > strlen(postfix) && |
42 | strcmp(str + strlen(str) - strlen(postfix), postfix) == 0; | 40 | strcmp(str + strlen(str) - strlen(postfix), postfix) == 0; |
43 | } | 41 | } |
44 | 42 | ||
45 | static int dump_channels(const char *dev_dir_name) | 43 | static int dump_channels(const char *dev_dir_name) |
@@ -48,13 +46,13 @@ static int dump_channels(const char *dev_dir_name) | |||
48 | const struct dirent *ent; | 46 | const struct dirent *ent; |
49 | 47 | ||
50 | dp = opendir(dev_dir_name); | 48 | dp = opendir(dev_dir_name); |
51 | if (dp == NULL) | 49 | if (!dp) |
52 | return -errno; | 50 | return -errno; |
53 | while (ent = readdir(dp), ent != NULL) | 51 | |
52 | while (ent = readdir(dp), ent) | ||
54 | if (check_prefix(ent->d_name, "in_") && | 53 | if (check_prefix(ent->d_name, "in_") && |
55 | check_postfix(ent->d_name, "_raw")) { | 54 | check_postfix(ent->d_name, "_raw")) |
56 | printf(" %-10s\n", ent->d_name); | 55 | printf(" %-10s\n", ent->d_name); |
57 | } | ||
58 | 56 | ||
59 | return (closedir(dp) == -1) ? -errno : 0; | 57 | return (closedir(dp) == -1) ? -errno : 0; |
60 | } | 58 | } |
@@ -63,20 +61,22 @@ static int dump_one_device(const char *dev_dir_name) | |||
63 | { | 61 | { |
64 | char name[IIO_MAX_NAME_LENGTH]; | 62 | char name[IIO_MAX_NAME_LENGTH]; |
65 | int dev_idx; | 63 | int dev_idx; |
66 | int retval; | 64 | int ret; |
67 | 65 | ||
68 | retval = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_device), | 66 | ret = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_device), "%i", |
69 | "%i", &dev_idx); | 67 | &dev_idx); |
70 | if (retval != 1) | 68 | if (ret != 1) |
71 | return -EINVAL; | 69 | return -EINVAL; |
72 | retval = read_sysfs_string("name", dev_dir_name, name); | 70 | |
73 | if (retval) | 71 | ret = read_sysfs_string("name", dev_dir_name, name); |
74 | return retval; | 72 | if (ret < 0) |
73 | return ret; | ||
75 | 74 | ||
76 | printf("Device %03d: %s\n", dev_idx, name); | 75 | printf("Device %03d: %s\n", dev_idx, name); |
77 | 76 | ||
78 | if (verblevel >= VERBLEVEL_SENSORS) | 77 | if (verblevel >= VERBLEVEL_SENSORS) |
79 | return dump_channels(dev_dir_name); | 78 | return dump_channels(dev_dir_name); |
79 | |||
80 | return 0; | 80 | return 0; |
81 | } | 81 | } |
82 | 82 | ||
@@ -84,17 +84,19 @@ static int dump_one_trigger(const char *dev_dir_name) | |||
84 | { | 84 | { |
85 | char name[IIO_MAX_NAME_LENGTH]; | 85 | char name[IIO_MAX_NAME_LENGTH]; |
86 | int dev_idx; | 86 | int dev_idx; |
87 | int retval; | 87 | int ret; |
88 | 88 | ||
89 | retval = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_trigger), | 89 | ret = sscanf(dev_dir_name + strlen(iio_dir) + strlen(type_trigger), |
90 | "%i", &dev_idx); | 90 | "%i", &dev_idx); |
91 | if (retval != 1) | 91 | if (ret != 1) |
92 | return -EINVAL; | 92 | return -EINVAL; |
93 | retval = read_sysfs_string("name", dev_dir_name, name); | 93 | |
94 | if (retval) | 94 | ret = read_sysfs_string("name", dev_dir_name, name); |
95 | return retval; | 95 | if (ret < 0) |
96 | return ret; | ||
96 | 97 | ||
97 | printf("Trigger %03d: %s\n", dev_idx, name); | 98 | printf("Trigger %03d: %s\n", dev_idx, name); |
99 | |||
98 | return 0; | 100 | return 0; |
99 | } | 101 | } |
100 | 102 | ||
@@ -105,12 +107,12 @@ static int dump_devices(void) | |||
105 | DIR *dp; | 107 | DIR *dp; |
106 | 108 | ||
107 | dp = opendir(iio_dir); | 109 | dp = opendir(iio_dir); |
108 | if (dp == NULL) { | 110 | if (!dp) { |
109 | printf("No industrial I/O devices available\n"); | 111 | fprintf(stderr, "No industrial I/O devices available\n"); |
110 | return -ENODEV; | 112 | return -ENODEV; |
111 | } | 113 | } |
112 | 114 | ||
113 | while (ent = readdir(dp), ent != NULL) { | 115 | while (ent = readdir(dp), ent) { |
114 | if (check_prefix(ent->d_name, type_device)) { | 116 | if (check_prefix(ent->d_name, type_device)) { |
115 | char *dev_dir_name; | 117 | char *dev_dir_name; |
116 | 118 | ||
@@ -132,7 +134,7 @@ static int dump_devices(void) | |||
132 | } | 134 | } |
133 | } | 135 | } |
134 | rewinddir(dp); | 136 | rewinddir(dp); |
135 | while (ent = readdir(dp), ent != NULL) { | 137 | while (ent = readdir(dp), ent) { |
136 | if (check_prefix(ent->d_name, type_trigger)) { | 138 | if (check_prefix(ent->d_name, type_trigger)) { |
137 | char *dev_dir_name; | 139 | char *dev_dir_name; |
138 | 140 | ||
@@ -151,6 +153,7 @@ static int dump_devices(void) | |||
151 | free(dev_dir_name); | 153 | free(dev_dir_name); |
152 | } | 154 | } |
153 | } | 155 | } |
156 | |||
154 | return (closedir(dp) == -1) ? -errno : 0; | 157 | return (closedir(dp) == -1) ? -errno : 0; |
155 | 158 | ||
156 | error_close_dir: | 159 | error_close_dir: |