From 96e37153e08d12f1bea4d02d41daf4cd650b2fc0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Marc=20Andr=C3=A9=20Tanner?= Date: Mon, 24 Feb 2014 17:51:31 +0100 Subject: [PATCH] Sort active session list by creation date --- abduco.c | 34 +++++++++++++++++++++++----------- 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/abduco.c b/abduco.c index 32c4795..d3af780 100644 --- a/abduco.c +++ b/abduco.c @@ -337,25 +337,37 @@ static bool attach_session(const char *name) { return true; } +static int session_filter(const struct dirent *d) { + return d->d_name[0] != '.'; +} + +static int session_comparator(const struct dirent **a, const struct dirent **b) { + struct stat sa, sb; + if (stat((*a)->d_name, &sa) != 0) + return -1; + if (stat((*b)->d_name, &sb) != 0) + return 1; + return sa.st_atime < sb.st_atime ? -1 : 1; +} + static int list_session() { if (create_socket_dir() == -1) return 1; chdir(sockaddr.sun_path); - DIR *d = opendir(sockaddr.sun_path); - if (!d) + struct dirent **namelist; + int n = scandir(sockaddr.sun_path, &namelist, session_filter, session_comparator); + if (n < 0) return 1; puts("Active sessions"); - struct dirent *e; - while ((e = readdir(d))) { - if (e->d_name[0] != '.') { - struct stat sb; char buf[255]; - if (stat(e->d_name, &sb) == 0) { - strftime(buf, sizeof(buf), "%a%t %d.%m.%Y %T", localtime(&sb.st_atime)); - printf(" %s\t%s\n", buf, e->d_name); - } + while (n--) { + struct stat sb; char buf[255]; + if (stat(namelist[n]->d_name, &sb) == 0) { + strftime(buf, sizeof(buf), "%a%t %d.%m.%Y %T", localtime(&sb.st_atime)); + printf(" %s\t%s\n", buf, namelist[n]->d_name); } + free(namelist[n]); } - closedir(d); + free(namelist); return 0; } -- 2.11.4.GIT