]> git.armaanb.net Git - bin.git/commitdiff
fortune: add more bad input checks
authorArmaan Bhojwani <me@armaanb.net>
Thu, 4 Mar 2021 01:41:25 +0000 (20:41 -0500)
committerArmaan Bhojwani <me@armaanb.net>
Thu, 4 Mar 2021 01:41:25 +0000 (20:41 -0500)
fortune

diff --git a/fortune b/fortune
index a8589a5f9e6a5ba8c4af5d12501becaf16c7270e..cbf19848130a0f74ad677e2fc98f0ae98cf901b1 100755 (executable)
--- 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 \