diff options
author | Aki <please@ignore.pl> | 2020-04-29 23:15:40 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2020-04-29 23:18:09 +0200 |
commit | ab0f572b4cdd4b48aa28129f703e7e6347829e8a (patch) | |
tree | 1cce2063c18ccf8ec8fbf40ae9bffe06da7dd7ac | |
parent | 48868c97a87740b43a16d59bb32adccfd6da379d (diff) | |
download | plop-ab0f572b4cdd4b48aa28129f703e7e6347829e8a.zip plop-ab0f572b4cdd4b48aa28129f703e7e6347829e8a.tar.gz plop-ab0f572b4cdd4b48aa28129f703e7e6347829e8a.tar.bz2 |
Moved out server startup to a function
-rw-r--r-- | plop.c | 29 |
1 files changed, 18 insertions, 11 deletions
@@ -4,7 +4,7 @@ #include <sys/types.h> #include <unistd.h> -int main(int argc, char ** argv) +int make_server(const char * node, const char * service) { struct addrinfo hints = { .ai_family = AF_UNSPEC, @@ -15,14 +15,9 @@ int main(int argc, char ** argv) struct addrinfo * result; struct addrinfo * it; - if (2 != argc) - { - return 4; - } - - if (0 != getaddrinfo(NULL, argv[1], &hints, &result)) + if (0 != getaddrinfo(node, service, &hints, &result)) { - return 1; // TODO: Handle errors properly + return -1; // TODO: Handle errors properly } int server; @@ -42,16 +37,28 @@ int main(int argc, char ** argv) if (it == NULL) { - return 2; // TODO: Handle errors properly + server = -1; // TODO: Handle errors properly } freeaddrinfo(result); - if (-1 == listen(server, 5)) + if (-1 == server || -1 == listen(server, 5)) + { + return -1; // TODO: Handle errors properly + } + + return server; +} + +int main(int argc, char ** argv) +{ + if (2 != argc) { - return 3; // TODO: Handle errors properly + return 4; } + int server = make_server(NULL, argv[1]); + int client; static const char * response = "HTTP/1.1 200 OK\r\n" |