summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHarry Cutts <hcutts@google.com>2023-10-09 17:28:55 +0000
committerHarry Cutts <hcutts@google.com>2023-10-09 17:30:35 +0000
commit6acdcbcb9368a3e3df07ed3dc4f03f0cd6f60497 (patch)
tree0a456baf314031f176453d6a32bf919592ea958d
parent13aa59c62df79bac2a75dcd640d5b5724325db6f (diff)
tools: fall back to alphasort when versionsort isn't available
versionsort keeps the device list in proper numerical order (e.g. so that event10 is sorted after event9, not event1), but is only available when compiling with the GNU C library. Signed-off-by: Harry Cutts <hcutts@google.com>
-rw-r--r--tools/find_event_devices.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/tools/find_event_devices.c b/tools/find_event_devices.c
index 3a74096..4cfb0ac 100644
--- a/tools/find_event_devices.c
+++ b/tools/find_event_devices.c
@@ -34,6 +34,14 @@
#include <fcntl.h>
#include <unistd.h>
+#ifdef __GLIBC__
+// versionsort sorts the device names in proper numerical order (e.g. so that
+// event10 is listed after event9, not event1), but is only available in glibc.
+static int (*sort_fn)(const struct dirent **, const struct dirent **) = versionsort;
+#else
+// Fall back to basic alphabetical sorting for other libc implementations.
+static int (*sort_fn)(const struct dirent **, const struct dirent **) = alphasort;
+#endif
#define DEV_INPUT_EVENT "/dev/input"
#define EVENT_DEV_NAME "event"
@@ -50,7 +58,7 @@ char* find_event_devices(void)
int max_device = 0;
int rc;
- ndev = scandir(DEV_INPUT_EVENT, &namelist, is_event_device, versionsort);
+ ndev = scandir(DEV_INPUT_EVENT, &namelist, is_event_device, sort_fn);
if (ndev <= 0)
return NULL;