mirror of https://github.com/01-edu/public.git
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.
59 lines
894 B
59 lines
894 B
5 years ago
|
package main
|
||
|
|
||
|
import (
|
||
4 years ago
|
"flag"
|
||
5 years ago
|
"fmt"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
4 years ago
|
const (
|
||
|
success = iota
|
||
|
failure
|
||
|
)
|
||
|
|
||
|
var status = success
|
||
|
|
||
|
func notNil(err error) bool {
|
||
5 years ago
|
if err != nil {
|
||
4 years ago
|
status = failure
|
||
|
fmt.Fprintln(os.Stderr, err)
|
||
|
return true
|
||
5 years ago
|
}
|
||
4 years ago
|
return false
|
||
5 years ago
|
}
|
||
4 years ago
|
|
||
5 years ago
|
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)
|
||
4 years ago
|
if notNil(err) {
|
||
|
continue
|
||
|
}
|
||
4 years ago
|
defer file.Close()
|
||
|
fileInfo, err := file.Stat()
|
||
4 years ago
|
if notNil(err) {
|
||
|
continue
|
||
|
}
|
||
4 years ago
|
offset := fileInfo.Size() - bytes
|
||
|
if offset < 0 {
|
||
|
offset = 0
|
||
5 years ago
|
}
|
||
4 years ago
|
b := make([]byte, fileInfo.Size()-offset)
|
||
|
_, err = file.ReadAt(b, offset)
|
||
4 years ago
|
if notNil(err) {
|
||
|
continue
|
||
|
}
|
||
4 years ago
|
if len(filenames) > 1 {
|
||
4 years ago
|
if i > 0 {
|
||
|
fmt.Println()
|
||
|
}
|
||
4 years ago
|
fmt.Println("==>", filename, "<==")
|
||
5 years ago
|
}
|
||
4 years ago
|
os.Stdout.Write(b)
|
||
5 years ago
|
}
|
||
4 years ago
|
os.Exit(status)
|
||
5 years ago
|
}
|