From beff90faa89a310aca71afed90951e9b30081107 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sun, 20 Oct 2019 16:34:38 +0100 Subject: [PATCH] Add conditional for udev presence in Linux. --- CMakeLists.txt | 11 +++++---- linux/device.c | 57 +++++++++++++++++++++++++------------------- linux/list_devices.c | 36 ++++++++++++++++++---------- 3 files changed, 61 insertions(+), 43 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ba6c80d..02f129c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,13 +5,14 @@ set(CMAKE_C_STANDARD 90) set(MAIN_SOURCES main.c list_devices.c device.c scsi.c hex2bin.c usb.c ieee1394.c pcmcia.c ata.c sdhci.c) -if("${CMAKE_SYSTEM}" MATCHES "Linux") +if ("${CMAKE_SYSTEM}" MATCHES "Linux") set(PLATFORM_SOURCES linux/list_devices.c linux/linux.h linux/device.c linux/scsi.c linux/usb.c linux/ieee1394.c linux/pcmcia.c linux/ata.c linux/sdhci.c) -endif() + find_library(HAS_UDEV NAMES udev) +endif () add_executable(dicremote ${MAIN_SOURCES} ${PLATFORM_SOURCES}) -# TODO: Properly check udev exists -if("${CMAKE_SYSTEM}" MATCHES "Linux") +if (HAS_UDEV) target_link_libraries(dicremote udev) -endif() \ No newline at end of file + add_definitions(-DHAS_UDEV) +endif () \ No newline at end of file diff --git a/linux/device.c b/linux/device.c index 062c368..82e9ee2 100644 --- a/linux/device.c +++ b/linux/device.c @@ -19,12 +19,15 @@ #include #include -#include #include #include #include #include +#ifdef HAS_UDEV +#include +#endif + int LinuxOpenDevice(const char* device_path) { int fd; @@ -38,6 +41,9 @@ int LinuxOpenDevice(const char* device_path) int32_t LinuxGetDeviceType(const char* device_path) { +#ifndef HAS_UDEV + return DICMOTE_DEVICE_TYPE_UNKNOWN; +#else struct udev* udev; struct udev_device* udev_device; const char* tmp_string; @@ -120,26 +126,26 @@ int32_t LinuxGetDeviceType(const char* device_path) } int32_t LinuxGetSdhciRegisters(const char* device_path, - char** csd, - char** cid, - char** ocr, - char** scr, - uint32_t* csd_len, - uint32_t* cid_len, - uint32_t* ocr_len, - uint32_t* scr_len) + char** csd, + char** cid, + char** ocr, + char** scr, + uint32_t* csd_len, + uint32_t* cid_len, + uint32_t* ocr_len, + uint32_t* scr_len) { - char* tmp_string; - char* sysfs_path_csd; - char* sysfs_path_cid; - char* sysfs_path_scr; - char* sysfs_path_ocr; + char* tmp_string; + char* sysfs_path_csd; + char* sysfs_path_cid; + char* sysfs_path_scr; + char* sysfs_path_ocr; size_t len; - FILE* file; - *csd = NULL; - *cid = NULL; - *ocr = NULL; - *scr = NULL; + FILE* file; + *csd = NULL; + *cid = NULL; + *ocr = NULL; + *scr = NULL; *csd_len = 0; *cid_len = 0; *ocr_len = 0; @@ -148,12 +154,12 @@ int32_t LinuxGetSdhciRegisters(const char* device_path, if(strncmp(device_path, "/dev/mmcblk", 11) != 0) return 0; - len = strlen(device_path) + 19; + len = strlen(device_path) + 19; sysfs_path_csd = malloc(len); sysfs_path_cid = malloc(len); sysfs_path_scr = malloc(len); sysfs_path_ocr = malloc(len); - tmp_string = malloc(1024); + tmp_string = malloc(1024); if(!sysfs_path_csd || !sysfs_path_cid || !sysfs_path_scr || !sysfs_path_ocr || !tmp_string) { @@ -191,7 +197,7 @@ int32_t LinuxGetSdhciRegisters(const char* device_path, if(*csd_len <= 0) { *csd_len = 0; - *csd = NULL; + *csd = NULL; } } @@ -213,7 +219,7 @@ int32_t LinuxGetSdhciRegisters(const char* device_path, if(*cid_len <= 0) { *cid_len = 0; - *cid = NULL; + *cid = NULL; } } @@ -235,7 +241,7 @@ int32_t LinuxGetSdhciRegisters(const char* device_path, if(*scr_len <= 0) { *scr_len = 0; - *scr = NULL; + *scr = NULL; } } @@ -257,7 +263,7 @@ int32_t LinuxGetSdhciRegisters(const char* device_path, if(*ocr_len <= 0) { *ocr_len = 0; - *ocr = NULL; + *ocr = NULL; } } @@ -272,4 +278,5 @@ int32_t LinuxGetSdhciRegisters(const char* device_path, free(tmp_string); return csd_len != 0 || cid_len != 0 || scr_len != 0 || ocr_len != 0; +#endif } \ No newline at end of file diff --git a/linux/list_devices.c b/linux/list_devices.c index 04008eb..f6f9e9d 100644 --- a/linux/list_devices.c +++ b/linux/list_devices.c @@ -20,7 +20,6 @@ #include #include -#include #include #include #include @@ -28,23 +27,29 @@ #include #include +#ifdef HAS_UDEV +#include +#endif + DeviceInfoList* LinuxListDevices() { - DIR* dir; - struct dirent* dirent; + DIR* dir; + struct dirent* dirent; + int i; + DeviceInfoList *list_start = NULL, *list_current = NULL, *list_next = NULL; + const char* tmp_string; + FILE* file; + char* line_str; + size_t n, ret; + char* chrptr; +#ifdef HAS_UDEV + int has_udev; struct udev* udev; - int has_udev = false, i; - DeviceInfoList * list_start = NULL, *list_current = NULL, *list_next = NULL; struct udev_device* udev_device; - const char* tmp_string; - FILE* file; - char* line_str; - size_t n, ret; - char* chrptr; - - udev = udev_new(); + udev = udev_new(); has_udev = udev != 0; +#endif dir = opendir(PATH_SYS_DEVBLOCK); if(!dir) return NULL; @@ -66,8 +71,9 @@ DeviceInfoList* LinuxListDevices() { closedir(dir); +#ifdef HAS_UDEV if(has_udev) udev_unref(udev); - +#endif return list_start; } @@ -77,6 +83,7 @@ DeviceInfoList* LinuxListDevices() snprintf(list_next->this.path, 1024, "/dev/%s", dirent->d_name); +#ifdef HAS_UDEV if(has_udev) { udev_device = udev_device_new_from_subsystem_sysname(udev, "block", dirent->d_name); @@ -127,6 +134,7 @@ DeviceInfoList* LinuxListDevices() } } } +#endif tmp_string = malloc(1024); memset((void*)tmp_string, 0, 1024); @@ -299,7 +307,9 @@ DeviceInfoList* LinuxListDevices() closedir(dir); +#ifdef HAS_UDEV if(has_udev) udev_unref(udev); +#endif return list_start; } \ No newline at end of file