cat : preallocate by os.Stat() on every file first
This commit is contained in:
parent
83c52e3e70
commit
83ada09fb4
1 changed files with 12 additions and 4 deletions
16
cat/cat.go
16
cat/cat.go
|
|
@ -30,9 +30,17 @@ func initOpts() options {
|
||||||
}
|
}
|
||||||
|
|
||||||
func cat(files []string) (string, error) {
|
func cat(files []string) (string, error) {
|
||||||
// strings.Builder ?
|
var total int64
|
||||||
// can we estimate needed size? do an ls -S call?
|
for _, f := range files {
|
||||||
ff := []string{}
|
stat, err := os.Stat(f)
|
||||||
|
if err != nil {
|
||||||
|
return "", fmt.Errorf("stat failed: %w", err)
|
||||||
|
}
|
||||||
|
if !stat.IsDir() && stat.Mode().IsRegular() {
|
||||||
|
total += stat.Size()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ff := make([]string, 0, total)
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
dat, err := os.ReadFile(f)
|
dat, err := os.ReadFile(f)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -54,5 +62,5 @@ func Cat(args []string) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("um %s: %s", CMD, err)
|
log.Fatalf("um %s: %s", CMD, err)
|
||||||
}
|
}
|
||||||
fmt.Println(s)
|
fmt.Print(s)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue