Add path-fixer to remove duplicate lines from PATH
authorColin P. Mccabe <colin@cmccabe.xyz>
Mon, 22 Jul 2019 18:06:44 +0000 (11:06 -0700)
committerColin P. Mccabe <colin@cmccabe.xyz>
Mon, 22 Jul 2019 18:06:44 +0000 (11:06 -0700)
.gitignore
Makefile
path-fixer.go [new file with mode: 0644]

index da93917..b17d6aa 100644 (file)
@@ -6,6 +6,7 @@
 
 # binaries
 bytor
+path-fixer
 errno_speak
 simple_time
 vimstart
index db04831..a4d7136 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
 CFLAGS=-Wall -O2
 
-all:           audiobooker bytor errno_speak show_default_sockopts random_word simple_time vimstart hexconv
+all:           audiobooker bytor path-fixer errno_speak show_default_sockopts random_word simple_time vimstart hexconv
 
 audiobooker:
        go build audiobooker.go
@@ -8,6 +8,9 @@ audiobooker:
 bytor:
        go build bytor.go
 
+path-fixer:
+       go build path-fixer.go
+
 errno_speak:   errno_speak.o
 
 show_default_sockopts: show_default_sockopts.o
@@ -21,4 +24,4 @@ hexconv:      hexconv.o
 random_word:   random_word.o
 
 clean:
-       rm -rf bytor errno_speak show_default_sockopts simple_time vimstart hexconv *.o
+       rm -rf bytor path-fixer errno_speak show_default_sockopts simple_time vimstart hexconv *.o
diff --git a/path-fixer.go b/path-fixer.go
new file mode 100644 (file)
index 0000000..e43a471
--- /dev/null
@@ -0,0 +1,35 @@
+package main
+
+import (
+       "fmt"
+       "os"
+       "strings"
+)
+
+func main() {
+       path := os.Getenv("PATH")
+       entries := make(map[string]bool)
+       var bld strings.Builder
+       prefix := ""
+       for {
+               index := strings.Index(path, ":")
+               var entry string
+               if index >= 0 {
+                       entry = path[:index]
+               } else {
+                       entry = path
+               }
+               if !entries[entry] {
+                       entries[entry] = true
+                       bld.WriteString(prefix)
+                       prefix = ":"
+                       bld.WriteString(entry)
+               }
+               if index < 0 {
+                       break
+               } else {
+                       path = path[index+1:]
+               }
+       }
+       fmt.Printf("%s", bld.String())
+}