From e557e5ae0229f096fce9435f891d304f7daa4e4a Mon Sep 17 00:00:00 2001 From: Armaan Bhojwani Date: Wed, 3 Mar 2021 20:41:25 -0500 Subject: [PATCH] fortune: add more bad input checks --- fortune | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) 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 \ -- 2.39.2