From 80d7f6d1a30e709fad3d3a4e6cb8673f19d080ae Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Wed, 22 Oct 2025 12:37:52 -0400 Subject: [PATCH] ls is working now. --- ls/.gitignore | 3 +++ ls/Makefile | 4 ++++ ls/go.mod | 3 +++ ls/main.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+) create mode 100644 ls/.gitignore create mode 100644 ls/Makefile create mode 100644 ls/go.mod create mode 100644 ls/main.go diff --git a/ls/.gitignore b/ls/.gitignore new file mode 100644 index 0000000..c2b60aa --- /dev/null +++ b/ls/.gitignore @@ -0,0 +1,3 @@ +ls +ls.exe +*.out diff --git a/ls/Makefile b/ls/Makefile new file mode 100644 index 0000000..bea7546 --- /dev/null +++ b/ls/Makefile @@ -0,0 +1,4 @@ + + +build: + go build . diff --git a/ls/go.mod b/ls/go.mod new file mode 100644 index 0000000..64056b6 --- /dev/null +++ b/ls/go.mod @@ -0,0 +1,3 @@ +module lcthw.dev/go/go-coreutils/ls + +go 1.25.3 diff --git a/ls/main.go b/ls/main.go new file mode 100644 index 0000000..b2fee5c --- /dev/null +++ b/ls/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "fmt" + "io/fs" + "path/filepath" + "flag" +) + +func main() { + var recurse bool + + flag.BoolVar(&recurse, "R", false, "recursive listing") + flag.Parse() + + paths := flag.Args() + + if len(paths) == 0 { + paths = append(paths, ".") + } + + for _, what := range paths { + if flag.NArg() > 1 { + fmt.Printf("\n%s:\n", what) + } + + filepath.WalkDir(what, func (path string, d fs.DirEntry, err error) error { + if path == what { return nil } + + if d.IsDir() && recurse { + fmt.Printf("\n%s:\n", path) + } else if d.IsDir() { + fmt.Printf("%s\n", filepath.Base(path)) + return fs.SkipDir + } else { + fmt.Printf("%s\n", filepath.Base(path)) + } + + return nil + }) + } +}