From 1db36f45d308db09912fbc52c7e4dfe840a69f10 Mon Sep 17 00:00:00 2001 From: dtluna Date: Tue, 26 Mar 2019 14:46:28 +0300 Subject: [PATCH] Start making pwd --- pwd/pwd.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 pwd/pwd.go diff --git a/pwd/pwd.go b/pwd/pwd.go new file mode 100644 index 0000000..9d58438 --- /dev/null +++ b/pwd/pwd.go @@ -0,0 +1,55 @@ +package main + +import ( + "fmt" + "os" +) + +func logicalPWD() { + dir, err := os.Getwd() + if err != nil { + fmt.Printf("%v error: %v", os.Args[0], err) + os.Exit(1) + } + fmt.Println(dir) +} + +func physicalPWD() { + fmt.Println("Using physical WD") +} + +func usage() { + fmt.Printf("usage: %v [-LP]\n", os.Args[0]) +} + +// if there's bs in os.Args[2], it ignores everything else +func parseArgs() []string { + flags := []string{} + return flags +} + +// usePhysical parses command-line arguments and determines which mode should be used. +// If both -L and -P are specified, the last one shall apply. +// If neither -L nor -P is specified, the pwd utility shall behave as if -L had been specified. +func usePhysical() bool { + if len(os.Args) > 1 { + flags := parseArgs() + flagsLen := len(flags) + lastFlag := flags[flagsLen] + if lastFlag == "L" { + return false + } + if lastFlag == "P" { + return true + } + } + return false +} + +func main() { + if usePhysical() { + physicalPWD() + } else { + logicalPWD() + } +}