From fadbc96146f49936a14248cab2115af4f194f213 Mon Sep 17 00:00:00 2001 From: Dm1tr1y147 Date: Tue, 28 Jul 2020 12:14:28 +0500 Subject: [PATCH] Added getting port from environment variable --- bin/detect | 2 +- src/server.c | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/bin/detect b/bin/detect index 1f0d7a0..9308bfd 100644 --- a/bin/detect +++ b/bin/detect @@ -1,7 +1,7 @@ #!/usr/bin/env bash # bin/detect -if [[ -f $1/Makefile || -f $1/configure ]]; then +if [[ -f $1/Makefile ]]; then echo "C" && exit 0 else echo "no" && exit 1 diff --git a/src/server.c b/src/server.c index 8a0c475..58c3a27 100644 --- a/src/server.c +++ b/src/server.c @@ -67,14 +67,20 @@ int main(int argc, char *argv[]) struct sockaddr_storage cli_addr; char s[INET6_ADDRSTRLEN]; - if (argc != 2) + char *port; + + if (argc == 2) + port = argv[1]; + else + port = getenv("PORT"); + + if (port == NULL) { - err_msg("Usage: \n"); + perror("Port must be specified either as an argument or environment variable \"PORT\""); exit(1); } - char *port = argv[1]; - int listenfd = get_listener_socket(port); + int listenfd = get_listener_socket(port); if (listenfd < 0) { err_msg("!fatal! couldn't create listening socket");