From: Colin P. Mccabe Date: Mon, 22 Jul 2019 18:06:44 +0000 (-0700) Subject: Add path-fixer to remove duplicate lines from PATH X-Git-Url: http://club.cc.cmu.edu/~cmccabe/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=41bfed00f6e4c53d1be5031218c0b8614a16a463;p=cmccabe-bin Add path-fixer to remove duplicate lines from PATH --- diff --git a/.gitignore b/.gitignore index da93917..b17d6aa 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,7 @@ # binaries bytor +path-fixer errno_speak simple_time vimstart diff --git a/Makefile b/Makefile index db04831..a4d7136 100644 --- 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 index 0000000..e43a471 --- /dev/null +++ b/path-fixer.go @@ -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()) +}