abduco

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit 96e37153e08d12f1bea4d02d41daf4cd650b2fc0
parent 4bd951c4ae9e0b2a53f3f0a4120efc58de814c26
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Mon, 24 Feb 2014 17:51:31 +0100

Sort active session list by creation date

Diffstat:
abduco.c | 34+++++++++++++++++++++++-----------
1 file changed, 23 insertions(+), 11 deletions(-)

diff --git 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; }