commit 32223c96bdee8f94980d3a1877a643a4d59f897f
parent 33def953e9d216036ad921ac26052a5b0fa790dc
Author: Laslo Hunhold <dev@frign.de>
Date: Thu, 30 May 2019 23:15:47 +0200
Use compound literals and explicit initialization
I didn't really like the use of a "yes"-variable for setsockopt().
A better way is to use compound literals (part of C99).
Another point are the structs. Instead of memsetting to zero we make
use of the standard which guarantees that "unmentioned" fields
are set to zero anyways. Just to note it here: The use of memset()
also sets padding to zero, which is not guaranteed with the method
of "unmentioned" fields.
Signed-off-by: Laslo Hunhold <dev@frign.de>
Diffstat:
2 files changed, 16 insertions(+), 18 deletions(-)
diff --git a/main.c b/main.c
@@ -78,12 +78,11 @@ sigcleanup(int sig)
static void
handlesignals(void(*hdl)(int))
{
- struct sigaction sa;
+ struct sigaction sa = {
+ .sa_handler = hdl,
+ };
- memset(&sa, 0, sizeof(sa));
sigemptyset(&sa.sa_mask);
- sa.sa_handler = hdl;
-
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
diff --git a/sock.c b/sock.c
@@ -18,25 +18,25 @@
int
sock_get_ips(const char *host, const char* port)
{
- struct addrinfo hints, *ai, *p;
- int ret, insock = 0, yes;
-
- memset(&hints, 0, sizeof(hints));
- hints.ai_flags = AI_NUMERICSERV;
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
+ struct addrinfo hints = {
+ .ai_flags = AI_NUMERICSERV,
+ .ai_family = AF_UNSPEC,
+ .ai_socktype = SOCK_STREAM,
+ };
+ struct addrinfo *ai, *p;
+ int ret, insock = 0;
if ((ret = getaddrinfo(host, port, &hints, &ai))) {
die("getaddrinfo: %s", gai_strerror(ret));
}
- for (yes = 1, p = ai; p; p = p->ai_next) {
+ for (p = ai; p; p = p->ai_next) {
if ((insock = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) < 0) {
continue;
}
- if (setsockopt(insock, SOL_SOCKET, SO_REUSEADDR, &yes,
- sizeof(int)) < 0) {
+ if (setsockopt(insock, SOL_SOCKET, SO_REUSEADDR,
+ &(int){1}, sizeof(int)) < 0) {
die("setsockopt:");
}
if (bind(insock, p->ai_addr, p->ai_addrlen) < 0) {
@@ -70,7 +70,9 @@ sock_rem_uds(const char *udsname)
int
sock_get_uds(const char *udsname, uid_t uid, gid_t gid)
{
- struct sockaddr_un addr;
+ struct sockaddr_un addr = {
+ .sun_family = AF_UNIX,
+ };
size_t udsnamelen;
int insock, sockmode = S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH;
@@ -78,9 +80,6 @@ sock_get_uds(const char *udsname, uid_t uid, gid_t gid)
die("socket:");
}
- memset(&addr, 0, sizeof(addr));
- addr.sun_family = AF_UNIX;
-
if ((udsnamelen = strlen(udsname)) > sizeof(addr.sun_path) - 1) {
die("UNIX-domain socket name truncated");
}