mirror of
https://github.com/tio/tio.git
synced 2026-05-01 14:57:59 +02:00
Improve --list feature on non-linux platform
This commit is contained in:
parent
5ec33f5d4d
commit
ec8f63f06d
2 changed files with 132 additions and 39 deletions
46
src/fs.c
46
src/fs.c
|
|
@ -178,21 +178,11 @@ char* fs_search_directory(const char *dir_path, const char *dirname)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(__linux__)
|
||||||
|
|
||||||
// Function to return creation time of file
|
// Function to return creation time of file
|
||||||
double fs_get_creation_time(const char *path)
|
double fs_get_creation_time(const char *path)
|
||||||
{
|
{
|
||||||
|
|
||||||
#if defined(__APPLE__) || defined(__MACH__)
|
|
||||||
// Use stat on macOS to access creation time
|
|
||||||
struct stat st;
|
|
||||||
|
|
||||||
if (stat(path, &st) != 0)
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
return st.st_birthtimespec.tv_sec + st.st_birthtimespec.tv_nsec / 1e9;
|
|
||||||
#else
|
|
||||||
struct statx stx;
|
struct statx stx;
|
||||||
int fd = open(path, O_RDONLY);
|
int fd = open(path, O_RDONLY);
|
||||||
if (fd == -1)
|
if (fd == -1)
|
||||||
|
|
@ -210,5 +200,35 @@ double fs_get_creation_time(const char *path)
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
return stx.stx_btime.tv_sec + stx.stx_btime.tv_nsec / 1e9;
|
return stx.stx_btime.tv_sec + stx.stx_btime.tv_nsec / 1e9;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#elif defined(__APPLE__) || defined(__MACH__)
|
||||||
|
|
||||||
|
double fs_get_creation_time(const char *path)
|
||||||
|
{
|
||||||
|
// Use stat on macOS to access creation time
|
||||||
|
struct stat st;
|
||||||
|
|
||||||
|
if (stat(path, &st) != 0)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return st.st_birthtimespec.tv_sec + st.st_birthtimespec.tv_nsec / 1e9;
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
double fs_get_creation_time(const char *path)
|
||||||
|
{
|
||||||
|
struct stat st;
|
||||||
|
|
||||||
|
if (stat(path, &st) != 0)
|
||||||
|
{
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (double) st.st_ctime;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
|
||||||
125
src/tty.c
125
src/tty.c
|
|
@ -68,24 +68,19 @@
|
||||||
/* tty device listing configuration */
|
/* tty device listing configuration */
|
||||||
|
|
||||||
#if defined(__linux__)
|
#if defined(__linux__)
|
||||||
#define PATH_SERIAL_DEVICES "/dev/serial/by-id/"
|
#define PATH_SERIAL_DEVICES "/dev"
|
||||||
#define PATH_SERIAL_DEVICES_BY_PATH "/dev/serial/by-path/"
|
#define PATH_SERIAL_DEVICES_BY_ID "/dev/serial/by-id"
|
||||||
#define PREFIX_TTY_DEVICES ""
|
#define PATH_SERIAL_DEVICES_BY_PATH "/dev/serial/by-path"
|
||||||
#elif defined(__FreeBSD__)
|
#elif defined(__FreeBSD__)
|
||||||
#define PATH_SERIAL_DEVICES "/dev/"
|
#define PATH_SERIAL_DEVICES "/dev"
|
||||||
#define PREFIX_TTY_DEVICES "cua"
|
|
||||||
#elif defined(__APPLE__)
|
#elif defined(__APPLE__)
|
||||||
#define PATH_SERIAL_DEVICES "/dev/"
|
#define PATH_SERIAL_DEVICES "/dev"
|
||||||
#define PREFIX_TTY_DEVICES "tty."
|
|
||||||
#elif defined(__CYGWIN__)
|
#elif defined(__CYGWIN__)
|
||||||
#define PATH_SERIAL_DEVICES "/dev/"
|
#define PATH_SERIAL_DEVICES "/dev"
|
||||||
#define PREFIX_TTY_DEVICES "ttyS"
|
|
||||||
#elif defined(__HAIKU__)
|
#elif defined(__HAIKU__)
|
||||||
#define PATH_SERIAL_DEVICES "/dev/ports/"
|
#define PATH_SERIAL_DEVICES "/dev/ports"
|
||||||
#define PREFIX_TTY_DEVICES ""
|
|
||||||
#else
|
#else
|
||||||
#define PATH_SERIAL_DEVICES "/dev/"
|
#define PATH_SERIAL_DEVICES "/dev"
|
||||||
#define PREFIX_TTY_DEVICES "tty"
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef CMSPAR
|
#ifndef CMSPAR
|
||||||
|
|
@ -1375,7 +1370,8 @@ error:
|
||||||
|
|
||||||
static void list_serial_devices_by_id(void)
|
static void list_serial_devices_by_id(void)
|
||||||
{
|
{
|
||||||
DIR *d = opendir(PATH_SERIAL_DEVICES);
|
#ifdef PATH_SERIAL_DEVICES_BY_ID
|
||||||
|
DIR *d = opendir(PATH_SERIAL_DEVICES_BY_ID);
|
||||||
if (d)
|
if (d)
|
||||||
{
|
{
|
||||||
struct dirent *dir;
|
struct dirent *dir;
|
||||||
|
|
@ -1387,23 +1383,20 @@ static void list_serial_devices_by_id(void)
|
||||||
{
|
{
|
||||||
if ((strcmp(dir->d_name, ".")) && (strcmp(dir->d_name, "..")))
|
if ((strcmp(dir->d_name, ".")) && (strcmp(dir->d_name, "..")))
|
||||||
{
|
{
|
||||||
if (!strncmp(dir->d_name, PREFIX_TTY_DEVICES, sizeof(PREFIX_TTY_DEVICES) - 1))
|
if (is_serial_device("%s/%s", PATH_SERIAL_DEVICES_BY_ID, dir->d_name))
|
||||||
{
|
{
|
||||||
if (is_serial_device("%s%s", PATH_SERIAL_DEVICES, dir->d_name))
|
printf("%s/%s\n", PATH_SERIAL_DEVICES_BY_ID, dir->d_name);
|
||||||
{
|
|
||||||
printf("%s%s\n", PATH_SERIAL_DEVICES, dir->d_name);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
closedir(d);
|
closedir(d);
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static void list_serial_devices_by_path(void)
|
static void list_serial_devices_by_path(void)
|
||||||
{
|
{
|
||||||
#ifdef PATH_SERIAL_DEVICES_BY_PATH
|
#ifdef PATH_SERIAL_DEVICES_BY_PATH
|
||||||
|
|
||||||
DIR *d = opendir(PATH_SERIAL_DEVICES_BY_PATH);
|
DIR *d = opendir(PATH_SERIAL_DEVICES_BY_PATH);
|
||||||
if (d)
|
if (d)
|
||||||
{
|
{
|
||||||
|
|
@ -1416,12 +1409,9 @@ static void list_serial_devices_by_path(void)
|
||||||
{
|
{
|
||||||
if ((strcmp(dir->d_name, ".")) && (strcmp(dir->d_name, "..")))
|
if ((strcmp(dir->d_name, ".")) && (strcmp(dir->d_name, "..")))
|
||||||
{
|
{
|
||||||
if (!strncmp(dir->d_name, "", sizeof("") - 1))
|
if (is_serial_device("%s/%s", PATH_SERIAL_DEVICES_BY_PATH, dir->d_name))
|
||||||
{
|
{
|
||||||
if (is_serial_device("%s%s", PATH_SERIAL_DEVICES_BY_PATH, dir->d_name))
|
printf("%s/%s\n", PATH_SERIAL_DEVICES_BY_PATH, dir->d_name);
|
||||||
{
|
|
||||||
printf("%s%s\n", PATH_SERIAL_DEVICES_BY_PATH, dir->d_name);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -1551,6 +1541,8 @@ static void search_reset(void)
|
||||||
device_list = NULL;
|
device_list = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(__linux__)
|
||||||
|
|
||||||
GList *tty_search_for_serial_devices(void)
|
GList *tty_search_for_serial_devices(void)
|
||||||
{
|
{
|
||||||
DIR *dir;
|
DIR *dir;
|
||||||
|
|
@ -1566,7 +1558,6 @@ GList *tty_search_for_serial_devices(void)
|
||||||
dir = opendir("/sys/class/tty");
|
dir = opendir("/sys/class/tty");
|
||||||
if (!dir)
|
if (!dir)
|
||||||
{
|
{
|
||||||
// Error
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1730,6 +1721,88 @@ GList *tty_search_for_serial_devices(void)
|
||||||
return device_list;
|
return device_list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
GList *tty_search_for_serial_devices(void)
|
||||||
|
{
|
||||||
|
DIR *dir;
|
||||||
|
char path[PATH_MAX] = {};
|
||||||
|
double current_time, creation_time;
|
||||||
|
|
||||||
|
search_reset();
|
||||||
|
|
||||||
|
// Open the directory containing serial devices
|
||||||
|
dir = opendir(PATH_SERIAL_DEVICES);
|
||||||
|
if (!dir)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
current_time = get_current_time();
|
||||||
|
|
||||||
|
// Iterate through each device in the subsystem directory
|
||||||
|
struct dirent *entry;
|
||||||
|
while ((entry = readdir(dir)) != NULL)
|
||||||
|
{
|
||||||
|
// Skip . and .. entries
|
||||||
|
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Construct the path to the TTY device file
|
||||||
|
snprintf(path, sizeof(path), PATH_SERIAL_DEVICES "/%s", entry->d_name);
|
||||||
|
|
||||||
|
// Skip non serial devices
|
||||||
|
if (is_serial_device(path) == false)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Calculate uptime
|
||||||
|
creation_time = fs_get_creation_time(path);
|
||||||
|
double uptime = current_time - creation_time;
|
||||||
|
|
||||||
|
// Do not add devices excluded by exclude patterns
|
||||||
|
if (match_patterns(path, option.exclude_devices))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allocate new device item for device list
|
||||||
|
device_t *device = g_new0(device_t, 1);
|
||||||
|
if (device == NULL)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fill in device information
|
||||||
|
device->path = g_strdup(path);
|
||||||
|
device->tid = "";
|
||||||
|
device->uptime = uptime;
|
||||||
|
device->driver = "";
|
||||||
|
device->description = "";
|
||||||
|
|
||||||
|
// Add device information to device list
|
||||||
|
device_list = g_list_append(device_list, device);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (g_list_length(device_list) == 0)
|
||||||
|
{
|
||||||
|
// Return NULL if no serial devices found
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort device list device with respect to uptime
|
||||||
|
device_list = g_list_sort(device_list, compare_uptime);
|
||||||
|
|
||||||
|
closedir(dir);
|
||||||
|
|
||||||
|
return device_list;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
void list_serial_devices(void)
|
void list_serial_devices(void)
|
||||||
{
|
{
|
||||||
tty_search_for_serial_devices();
|
tty_search_for_serial_devices();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue