commit fbd469d9810ebf7c33102b501dab1f37ee2dad7a
parent b77fa63e7bb3b920f6d1c50c5dcf59c739723e04
Author: Jacob R. Edwards <n/a>
Date: Sun, 4 Dec 2022 13:24:18 -0600
Add unroot, a dumb script to try and make paths relative
It's dumb in that it does handles some correct paths incorrectly
because of it's simple lexical processing (which lacks support for
the '..' directory).
Diffstat:
1 file changed, 31 insertions(+), 0 deletions(-)
diff --git a/local/bin/bin/unroot b/local/bin/bin/unroot
@@ -0,0 +1,31 @@
+#!/usr/bin/awk -f
+# Copyright 2022 Jacob R. Edwards
+# Unroot names
+#
+# This script tries to make paths relative, but will not do so to
+# those outside of the current working directory's heirarchy.
+# (NOTE: This utility only does minimal lexical processing, and
+# thus handles some correct paths incorrectly. For example,
+# '/dir/../dir' is treated as a different directory than '/dir'.)
+
+BEGIN {
+ if (("pwd" | getline wd) <= 0) {
+ print "Unable to get working directory" > "/dev/stderr"
+ exit 1
+ }
+ close("pwd")
+
+ if (wd == "/")
+ wd = ""
+}
+
+{
+ while (gsub("(/\.?/)+", "/", $0))
+ ;
+ if (index($0, wd) == 1) {
+ rel = substr($0, length(wd) + 2)
+ print rel ? rel : "."
+ } else {
+ print
+ }
+}