ls: Fixed bug about wrong interpeting root directory.
This commit is contained in:
parent
194994e813
commit
15e21be18c
1 changed files with 12 additions and 5 deletions
17
ls/ls.go
17
ls/ls.go
|
@ -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
|
||||||
|
|
Loading…
Reference in a new issue