From: Armaan Bhojwani Date: Thu, 4 Mar 2021 01:41:25 +0000 (-0500) Subject: fortune: add more bad input checks X-Git-Tag: v0.0.1~30 X-Git-Url: https://git.armaanb.net/?p=bin.git;a=commitdiff_plain;h=e557e5ae0229f096fce9435f891d304f7daa4e4a fortune: add more bad input checks --- diff --git a/fortune b/fortune index a8589a5..cbf1984 100755 --- a/fortune +++ b/fortune @@ -16,8 +16,14 @@ a simple POSIX sh/awk implementation of fortune Exit codes: 0 success - 1 argument error - 2 parse error' + 1 generic error + 2 argument parsing error' +} + +parse_err() { + echo "$1" + $2 + exit 2 } DIR=${FORTUNEDIR:-"/usr/share/fortune/"} @@ -45,22 +51,22 @@ while [ "$1" != "" ]; do exit ;; *) - echo "ERROR: unknown option \"$1\"" - usage - exit 1 + parse_err "ERROR: unknown option \"$1\"" usage ;; esac shift 2 done [ "$MAXLENGTH" -lt "$MINLENGTH" ] && { - echo "ERROR: maximum length is less than minimum length" - exit 2 + parse_err "ERROR: maximum length is less than minimum length" } [ -d "$FILE" ] && { - echo "ERROR: directory given, please provide a file or glob of files" - exit 2 + parse_err "ERROR: directory given, please provide a file or glob of files" +} + +[ "$(find $FILE -maxdepth 1 -print -quit 2> /dev/null)" ] || { + parse_err "ERROR: no such file or directory" } awk -v maxlen=$MAXLENGTH -v minlen=$MINLENGTH \