From ba91d30ba0e8d549797cf8d8e1fa45ca038dd36c Mon Sep 17 00:00:00 2001 From: hellerve Date: Mon, 18 Sep 2017 11:06:25 +0200 Subject: [PATCH] make it so --- .gitignore | 1 + Makefile | 15 ++++++ README.md | 8 ++++ main.c | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++++ quotes.txt | 2 + 5 files changed, 159 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 README.md create mode 100644 main.c create mode 100644 quotes.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e660fd9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bin/ diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..1301ca5 --- /dev/null +++ b/Makefile @@ -0,0 +1,15 @@ +TARGET=865 +BUILDDIR=bin/ +PREFIX=/usr/local/bin/ +MAIN=main.c +override CFLAGS+=-Werror -Wall -g -fPIC -O2 -DNDEBUG -ftrapv -Wfloat-equal -Wundef -Wwrite-strings -Wuninitialized -pedantic -std=gnu11 + +all: main.c + mkdir -p $(BUILDDIR) + $(CC) $(MAIN) -o $(BUILDDIR)$(TARGET) $(CFLAGS) + +install: all + install $(BUILDDIR)$(TARGET) $(PREFIX)$(TARGET) + +uninstall: + rm -rf $(PREFIX)$(TARGET) diff --git a/README.md b/README.md new file mode 100644 index 0000000..c4d6139 --- /dev/null +++ b/README.md @@ -0,0 +1,8 @@ +# 865 + +An implementation of [RFC 865](https://tools.ietf.org/html/rfc865), the “quote +of the day” service, for the heck of it. + +## Limitations + +I don’t have many great quotes. diff --git a/main.c b/main.c new file mode 100644 index 0000000..f6dd0df --- /dev/null +++ b/main.c @@ -0,0 +1,133 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +typedef struct { + char* said; + int len; +} quote; + +quote** quotes = NULL; +int n_quotes = 0; + +void die(const char *msg) { + perror(msg); + exit(1); +} + +char* read_quotes_file() { + int size; + char* inp; + FILE* f = fopen("quotes.txt", "r"); + + if (!f) die("error reading quotes file"); + + fseek(f, 0, SEEK_END); + size = ftell(f); + fseek(f, 0, SEEK_SET); + + inp = malloc(size+1); + fread(inp, size, 1, f); + fclose(f); + + inp[size] = '\0'; + + return inp; +} + +void make_quotes(char* contents) { + char* line; + quote* q; + + while ((line = strsep(&contents, "\n"))) { + if (!strcmp(line, "")) continue; + q = malloc(sizeof(quote)); + q->said = line; + q->len = strlen(line); + quotes = realloc(quotes, (++n_quotes)*sizeof(quote*)); + quotes[n_quotes-1] = q; + } +} + +void initialize_quotd() { + char* contents = read_quotes_file(); + + make_quotes(contents); +} + +void cleanup_quotd() { + int i; + + for (i = 0; i < n_quotes; i++) free(quotes[i]); + free(quotes); +} + +int get_random_from_day() { + time_t t; + struct tm* tmp; + t = time(NULL); + tmp = localtime(&t); + + srand(tmp->tm_yday); // hack to provide determinism per day. + + return rand(); +} + +char* get_quotd(int* len) { + quote* q; + char* res; + int n; + n = get_random_from_day() % n_quotes; + q = quotes[n]; + res = malloc(q->len+2); + + snprintf(res, q->len+2, "%s\n", q->said); + *len = q->len+2; + + return res; +} + +int main(int argc, char *argv[]) { + int sockfd; + int newsockfd; + int port = 17; + socklen_t clilen; + struct sockaddr_in serv_addr, cli_addr; + int n; + + initialize_quotd(); + + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd < 0) die("error opening socket."); + + bzero((char *) &serv_addr, sizeof(serv_addr)); + serv_addr.sin_family = AF_INET; + serv_addr.sin_addr.s_addr = INADDR_ANY; + serv_addr.sin_port = htons(port); + if (bind(sockfd, (struct sockaddr *) &serv_addr, + sizeof(serv_addr)) < 0) die("error binding socket."); + + clilen = sizeof(cli_addr); + + while (1) { + listen(sockfd, 5); + + newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen); + if (newsockfd < 0) die("error accepting"); + + char* quotd = get_quotd(&n); + n = write(newsockfd, quotd, n); + free(quotd); + if (n < 0) die("error writing to socket"); + close(newsockfd); + } + + close(sockfd); + cleanup_quotd(); + return 0; +} diff --git a/quotes.txt b/quotes.txt new file mode 100644 index 0000000..14462e9 --- /dev/null +++ b/quotes.txt @@ -0,0 +1,2 @@ +"they're[timezones] a scourge upon the earth. the only thing worse than having timezones is not having timezones." -- Saul Pwanson +"I know this will come as a shock to you, Mr. Goldwyn, but in all history, which has held billions and billions of human beings, not a single one ever had a happy ending." -- Dorothy Parker