quicker path sanitizing
authorPaul Hänsch <paul@plutz.net>
Tue, 2 Jun 2020 18:00:04 +0000 (20:00 +0200)
committerPaul Hänsch <paul@plutz.net>
Tue, 2 Jun 2020 18:00:04 +0000 (20:00 +0200)
cgilite.sh

index 384ec19..ddad1d4 100755 (executable)
@@ -27,18 +27,21 @@ BR='
 '
 cgilite_timeout=2
 
-PATH(){
-  { [ $# -eq 0 ] && cat || printf %s "$*"; } \
-  | sed -E 's;^.*$;/&/;; s;/+;/;g;
-            :X;
-            s;^/\.\./;/;; s;/\./;/;g;
-            tX;
-            s;/[^/]+/\.\./;/;;
-            tX;
-            s;^(/.*)/$;\1;'
+PATH(){ 
+  local str seg out
+  [ $# -eq 0 ] && str="$(cat)" || str="$*"
+  while [ "$str" ]; do
+    seg=${str%%/*}; str="${str#*/}"
+    case $seg in
+      ..) out="${out%/}"; out="${out%/*}/";;
+    .|'') out="${out%/}/";;
+       *) out="${out%/}/${seg}";;
+    esac;
+    [ "$seg" = "$str" ] && break
+  done
+  [ "${str}" -a "${out}" ] && printf %s "$out" || printf %s/ "${out%/}"
 }
 
-
 HEX_DECODE='
   s;\\;\\\\;g; :HEXDECODE_X; s;%([^0-9A-F]);\\045\1;g; tHEXDECODE_X;
   # Hexadecimal { %00 - %FF } will be transformed to octal { \000 - \377 } for posix printf