You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

58 lines
893 B

package main
import (
4 years ago
"flag"
"fmt"
"os"
)
const (
success = iota
failure
)
var status = success
func notNil(err error) bool {
if err != nil {
status = failure
fmt.Fprintln(os.Stderr, err)
return true
}
return false
}
func main() {
4 years ago
var bytes int64
flag.Int64Var(&bytes, "c", 0, "output the last NUM bytes")
flag.Parse()
filenames := flag.Args()
for i, filename := range filenames {
file, err := os.Open(filename)
if notNil(err) {
continue
}
4 years ago
defer file.Close()
fileInfo, err := file.Stat()
if notNil(err) {
continue
}
4 years ago
offset := fileInfo.Size() - bytes
if offset < 0 {
offset = 0
}
4 years ago
b := make([]byte, fileInfo.Size()-offset)
_, err = file.ReadAt(b, offset)
if notNil(err) {
continue
}
4 years ago
if len(filenames) > 1 {
if i > 0 {
fmt.Println()
}
4 years ago
fmt.Println("==>", filename, "<==")
}
4 years ago
os.Stdout.Write(b)
}
os.Exit(status)
}