ls: Fixed bug about twice printing finishing directories.

This commit is contained in:
jienfak 2020-05-06 05:36:41 +05:00
parent 3ac9732a26
commit 194994e813

View file

@ -46,19 +46,26 @@ func ls(p string, fold int) error {
pp := strings.TrimRight(p, "/") pp := strings.TrimRight(p, "/")
if isDir && fold>0 { if !isDir {
fmt.Println(pp)
return nil
}
if fold>0 {
/* It's a directory and it can be ls-ed. */
l, e := ReadDir(pp) l, e := ReadDir(pp)
if e!=nil { if e!=nil {
return e return e
} }
for _, f := range l { for _, f := range l {
s := pp+"/"+f.Name() s := pp+"/"+f.Name()
if b, _ := IsDir(s) ; b && fold!=1 {
fmt.Println(s) fmt.Println(s)
if b, _ :=IsDir(s) ; b { }
ls(s, fold-1) ls(s, fold-1)
} }
}
} else { } else {
/* It's finish directory. Fold==0 or fold<0. */
fmt.Println(pp) fmt.Println(pp)
} }
@ -97,15 +104,14 @@ func Run(args []string) int {
} else { } else {
for _, f := range l { for _, f := range l {
isDir, _ := IsDir(f.Name()) isDir, _ := IsDir(f.Name())
if isDir && foldLvl>0 {
fmt.Println(f.Name()) fmt.Println(f.Name())
if isDir && foldLvl>0 {
e := ls(f.Name(), foldLvl) e := ls(f.Name(), foldLvl)
if e!=nil { if e!=nil {
status = 1 status = 1
fmt.Fprintf(os.Stderr, "%s: %s.\n", arg0, e) fmt.Fprintf(os.Stderr, "%s: %s.\n", arg0, e)
} }
} else {
fmt.Println(f.Name())
} }
} }
} }