ls: Fixed bug about wrong interpeting root directory.

This commit is contained in:
jienfak 2020-05-06 05:57:54 +05:00
parent 194994e813
commit 15e21be18c

View file

@ -4,6 +4,7 @@ import(
"fmt" "fmt"
"flag" "flag"
"strings" "strings"
"regexp"
) )
func IsDir(p string) (bool, error) { func IsDir(p string) (bool, error) {
@ -44,21 +45,27 @@ func ls(p string, fold int) error {
return e return e
} }
pp := strings.TrimRight(p, "/") // Delete repeating slashes.
slash := regexp.MustCompile("/+")
p = slash.ReplaceAllString(p, "/")
if p != "/" { // Do not trim if it is root dir.
p = strings.TrimRight(p, "/")
}
if !isDir { if !isDir {
fmt.Println(pp) fmt.Println(p)
return nil return nil
} }
if fold>0 { if fold>0 {
/* It's a directory and it can be ls-ed. */ /* It's a directory and it can be ls-ed. */
l, e := ReadDir(pp) l, e := ReadDir(p)
if e!=nil { if e!=nil {
return e return e
} }
for _, f := range l { for _, f := range l {
s := pp+"/"+f.Name() s := p+"/"+f.Name()
if b, _ := IsDir(s) ; b && fold!=1 { if b, _ := IsDir(s) ; b && fold!=1 {
fmt.Println(s) fmt.Println(s)
} }
@ -66,7 +73,7 @@ func ls(p string, fold int) error {
} }
} else { } else {
/* It's finish directory. Fold==0 or fold<0. */ /* It's finish directory. Fold==0 or fold<0. */
fmt.Println(pp) fmt.Println(p)
} }
return nil return nil