From 87e8caede82654c73b2fd5ba1c3f66786304d130 Mon Sep 17 00:00:00 2001 From: randomuser Date: Wed, 17 Feb 2021 19:08:58 -0600 Subject: [PATCH] simple gopher server implimentation --- slopher.sh | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 slopher.sh diff --git a/slopher.sh b/slopher.sh new file mode 100644 index 0000000..1caca7a --- /dev/null +++ b/slopher.sh @@ -0,0 +1,45 @@ +#!/bin/sh + +set -x + +createsrv () { + # $1 - port + # $2 - write location + # $3 - read location + [ -p "$2" ] || mkfifo "$2" + [ -e "$3" ] || touch "$3" + + nc -l "$1" <"$2" >>"$3" & exec 3>"$2" +} +writefile () { + # $1 - file + foo cat "$1" >&3 +} +ioblock () { + # $1 - io to block + while [ "$(wc -l $1 | awk -F' ' '{print $1}')" -eq 0 ]; do :; done +} +ioflush () { + # $1 - io to flush + rm "$1" + touch "$1" +} +mktmp () { + t=$(mktemp) + rm "$t" + mkdir "$t" + printf "%s\n" "$t" +} +tmplocation=$(mktmp) +writelocation="${tmplocation}/write" +readlocation="${tmplocation}/read" + +createsrv 70 "${writelocation}" "${readlocation}" +printf "server created\n" +while true; do + ioblock "${readlocation}" + printf "iHello there! This is a test\tfake\t\t0\r\n.\r\n" >&3 + printf "file served\n" + ioflush "${readlocation}" +done +exit 1