Simplify str.h

Signed-off-by: Adrien Gallouët <adrien@gallouet.fr>
This commit is contained in:
Adrien Gallouët
2018-01-17 16:11:22 +00:00
parent 660a6ace2b
commit 975bb1610d
2 changed files with 46 additions and 58 deletions

View File

@@ -18,27 +18,23 @@ ctl_init(const char *dir, const char *file)
if (mkdir(dir, 0700) == -1 && errno != EEXIST)
return -1;
const char *strs[] = {dir, "/", file};
char *path = str_cat(strs, 3);
if (!path)
return -1;
int fd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (fd == -1) {
int err = errno;
free(path);
errno = err;
return -1;
}
struct sockaddr_un sun = {
.sun_family = AF_UNIX,
};
str_cpy(sun.sun_path, path, sizeof(sun.sun_path) - 1);
free(path);
const char *path[] = {dir, "/", file};
const size_t len = sizeof(sun.sun_path) - 1;
if (str_cat(sun.sun_path, path, COUNT(path), len) > len) {
errno = EINVAL;
return -1;
}
int fd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (fd == -1)
return -1;
unlink(sun.sun_path);
if (bind(fd, (struct sockaddr *)&sun, sizeof(sun)) == -1) {
@@ -59,18 +55,17 @@ ctl_connect(int fd, const char *dir, const char *file)
return -1;
}
const char *strs[] = {dir, "/", file};
char *path = str_cat(strs, 3);
if (!path)
return -1;
struct sockaddr_un sun = {
.sun_family = AF_UNIX,
};
str_cpy(sun.sun_path, path, sizeof(sun.sun_path) - 1);
free(path);
const char *path[] = {dir, "/", file};
const size_t len = sizeof(sun.sun_path) - 1;
if (str_cat(sun.sun_path, path, COUNT(path), len) > len) {
errno = EINVAL;
return -1;
}
return connect(fd, (struct sockaddr *)&sun, sizeof(sun));
}