Autoselect the device when there is only one

Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
This commit is contained in:
Adrien Gallouët
2018-02-27 15:36:35 +00:00
parent 4fa56178cf
commit da44a9f55b
3 changed files with 44 additions and 19 deletions

View File

@@ -4,6 +4,7 @@
#include <stdio.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/un.h>
@@ -125,11 +126,35 @@ ctl_create(const char *dir, const char *file)
int
ctl_connect(int fd, const char *dir, const char *file)
{
if (fd < 0 || str_empty(dir) || str_empty(file)) {
if (fd < 0 || str_empty(dir)) {
errno = EINVAL;
return -1;
}
if (!file) {
DIR *dp = opendir(dir);
if (!dp)
return -1;
struct dirent *d = NULL;
while (d = readdir(dp), d) {
if (d->d_name[0] == '.')
continue;
if (file) {
closedir(dp);
errno = ENOENT;
return -1;
}
file = &d->d_name[0];
}
closedir(dp);
}
struct sockaddr_un sun;
if (ctl_setsun(&sun, dir, file))