commit e9611a0779a74b6e1289fd5db1606d4a501edf2f
parent 4462d9db5e2f921341694910c43c870463dff1ee
Author: Mario Rosell R. Martinez <mario@mariorosell.es>
Date: Fri, 27 Mar 2026 20:12:25 +0100
all (misc, chore): Format
Diffstat:
| M | cli.c | | | 11 | ++++++----- |
| M | s-httpd.c | | | 3 | +-- |
| M | socket.c | | | 64 | ++++++++++++++++++++++++++++++++++++---------------------------- |
3 files changed, 43 insertions(+), 35 deletions(-)
diff --git a/cli.c b/cli.c
@@ -11,13 +11,14 @@
const char* opts = "hvc:";
-void
-cli(int argc, char** argv)
+void cli(int argc, char** argv)
{
int c;
- while ((c = getopt(argc, argv, opts)) != -1) {
- switch (c) {
+ while ((c = getopt(argc, argv, opts)) != -1)
+ {
+ switch (c)
+ {
case 'h':
printf("%s: usage: %s [-h] [-v] [-c file]\n", argv[0], argv[0]);
puts("See the s-httpd(8) manual page for more.");
@@ -30,7 +31,7 @@ cli(int argc, char** argv)
conffile[sizeof(conffile) - 1] = '\0'; /* null-terminate */
break;
- /* getopt() handles the rest :) */
+ /* getopt() handles the rest :) */
}
}
}
diff --git a/s-httpd.c b/s-httpd.c
@@ -7,8 +7,7 @@
#include "cli.h"
-int
-main(int argc, char** argv)
+int main(int argc, char** argv)
{
cli(argc, argv);
diff --git a/socket.c b/socket.c
@@ -2,24 +2,25 @@
* socket.c -- low-level socket code
*/
+#include <arpa/inet.h>
#include <stdio.h>
#include <string.h>
-#include <unistd.h>
-#include <arpa/inet.h>
-#include <sys/types.h>
#include <sys/socket.h>
+#include <sys/types.h>
+#include <unistd.h>
-int
-sock_create(const char* ip, int port)
+int sock_create(const char* ip, int port)
{
int srvfd = socket(AF_INET, SOCK_STREAM, 0);
- if (srvfd < 0) {
+ if (srvfd < 0)
+ {
perror("socket");
return -1;
}
int opt = 1;
- if (setsockopt(srvfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
+ if (setsockopt(srvfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0)
+ {
perror("setsockopt");
close(srvfd);
return -1;
@@ -31,23 +32,29 @@ sock_create(const char* ip, int port)
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
- if (ip == NULL) {
+ if (ip == NULL)
+ {
addr.sin_addr.s_addr = INADDR_ANY; /* accept requests from anywhere */
- } else {
- if (inet_pton(AF_INET, ip, &addr.sin_addr) <= 0) {
+ }
+ else
+ {
+ if (inet_pton(AF_INET, ip, &addr.sin_addr) <= 0)
+ {
perror("inet_pton");
close(srvfd);
return -1;
}
}
- if (bind(srvfd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
+ if (bind(srvfd, (struct sockaddr*)&addr, sizeof(addr)) < 0)
+ {
perror("bind");
close(srvfd);
return -1;
}
- if (listen(srvfd, 128) < 0) {
+ if (listen(srvfd, 128) < 0)
+ {
perror("listen");
close(srvfd);
return -1;
@@ -56,14 +63,14 @@ sock_create(const char* ip, int port)
return srvfd;
}
-int
-sock_accept(int fd)
+int sock_accept(int fd)
{
struct sockaddr_in client;
socklen_t len = sizeof(client);
int cfd = accept(fd, (struct sockaddr*)&client, &len);
- if (cfd < 0) {
+ if (cfd < 0)
+ {
perror("accept");
return -1;
}
@@ -71,17 +78,16 @@ sock_accept(int fd)
return cfd;
}
-void
-sock_close(int fd)
+void sock_close(int fd)
{
close(fd);
}
-int
-sock_connect(const char* ip, int port)
+int sock_connect(const char* ip, int port)
{
int fd = socket(AF_INET, SOCK_STREAM, 0);
- if (fd < 0) {
+ if (fd < 0)
+ {
perror("socket");
return -1;
}
@@ -92,13 +98,15 @@ sock_connect(const char* ip, int port)
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
- if (inet_pton(AF_INET, ip, &addr.sin_addr) <= 0) {
+ if (inet_pton(AF_INET, ip, &addr.sin_addr) <= 0)
+ {
perror("inet_pton");
close(fd);
return -1;
}
- if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
+ if (connect(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0)
+ {
perror("connect");
close(fd);
return -1;
@@ -107,11 +115,11 @@ sock_connect(const char* ip, int port)
return fd;
}
-int
-sock_send(int fd, const char* data, size_t len)
+int sock_send(int fd, const char* data, size_t len)
{
ssize_t sent = send(fd, data, len, 0);
- if (sent < 0) {
+ if (sent < 0)
+ {
perror("send");
return -1;
}
@@ -119,11 +127,11 @@ sock_send(int fd, const char* data, size_t len)
return (int)sent;
}
-int
-sock_recv(int fd, char* buf, size_t len)
+int sock_recv(int fd, char* buf, size_t len)
{
ssize_t recvd = recv(fd, buf, len, 0);
- if (recvd < 0) {
+ if (recvd < 0)
+ {
perror("recv");
return -1;
}