2020-02-27 06:37:41 +03:00
|
|
|
package tac
|
|
|
|
/* Concatenate files in "stdout" reversed. */
|
|
|
|
import(
|
|
|
|
"os"
|
|
|
|
"io"
|
|
|
|
"fmt"
|
|
|
|
"bufio"
|
2024-05-15 21:07:35 +03:00
|
|
|
"surdeus.su/core/cli/mtool"
|
2020-02-27 06:37:41 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func reverse(a []string) chan string {
|
|
|
|
ret := make(chan string)
|
|
|
|
go func() {
|
|
|
|
l := len(a)
|
|
|
|
for i, _ := range a {
|
|
|
|
ret <- a[l-1-i]
|
|
|
|
}
|
|
|
|
close(ret)
|
|
|
|
}()
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func tac(p string) error {
|
|
|
|
f, e := os.Open(p)
|
|
|
|
if e != nil {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
ftac(f)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func ftac(f *os.File) error {
|
2020-03-07 14:14:40 +03:00
|
|
|
r := bufio.NewReader(f)
|
2020-02-27 06:37:41 +03:00
|
|
|
var lines []string
|
|
|
|
for {
|
|
|
|
line, e := r.ReadString('\n')
|
|
|
|
if e == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
lines = append(lines, line)
|
|
|
|
}
|
|
|
|
for l := range reverse(lines) {
|
|
|
|
fmt.Print(l)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-24 16:54:51 +03:00
|
|
|
func Run(flagSet *mtool.Flags) {
|
|
|
|
flagSet.Parse()
|
|
|
|
args := flagSet.Args()
|
2020-02-27 06:37:41 +03:00
|
|
|
if len(args)>0 {
|
|
|
|
for _, p := range args {
|
|
|
|
e := tac(p)
|
|
|
|
if e != nil {
|
2023-03-24 16:54:51 +03:00
|
|
|
fmt.Fprintf(os.Stderr, "%s.\n", e)
|
2020-02-27 06:37:41 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ftac(os.Stdin)
|
|
|
|
}
|
|
|
|
}
|