]> git.plutz.net Git - cgilite/commitdiff
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 384ec1907f5bd7aa31dc41caec103896a1aaba4b..ddad1d4811c7635c8032c08620666fec9a9b2232 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