diff --git a/go/tests/prog/ztail_prog/main.go b/go/tests/prog/ztail_prog/main.go index 74415fc0..a9866e0b 100644 --- a/go/tests/prog/ztail_prog/main.go +++ b/go/tests/prog/ztail_prog/main.go @@ -6,10 +6,20 @@ import ( "os" ) -func check(err error) { +const ( + success = iota + failure +) + +var status = success + +func notNil(err error) bool { if err != nil { - panic(err) + status = failure + fmt.Fprintln(os.Stderr, err) + return true } + return false } func main() { var bytes int64 @@ -18,23 +28,30 @@ func main() { filenames := flag.Args() for i, filename := range filenames { file, err := os.Open(filename) - check(err) + if notNil(err) { + continue + } defer file.Close() fileInfo, err := file.Stat() - check(err) + if notNil(err) { + continue + } offset := fileInfo.Size() - bytes if offset < 0 { offset = 0 } b := make([]byte, fileInfo.Size()-offset) _, err = file.ReadAt(b, offset) - check(err) + if notNil(err) { + continue + } if len(filenames) > 1 { + if i > 0 { + fmt.Println() + } fmt.Println("==>", filename, "<==") } os.Stdout.Write(b) - if i < len(filenames)-1 { - fmt.Println() - } } + os.Exit(status) } diff --git a/subjects/ztail/README.md b/subjects/ztail/README.md index 78b1630c..b5adef70 100644 --- a/subjects/ztail/README.md +++ b/subjects/ztail/README.md @@ -4,8 +4,48 @@ Write a program that behaves like a simplified `tail` command but which takes at least one file as argument. -- The only option to be handled is `-c`. This option will be used in all the tests as the first argument, with positive values. +The only option to be handled is `-c` and will be used in all the tests as the first argument, with positive values. -- For this program the `os` package can be used. +For this program the `os` package can be used. -- Handle the errors by returning a non-zero exit status. +Handle the errors by returning a non-zero exit status but process all the files. + +If several files are given, print a new line and the file name between each one of them (see below). + +### Usage + +If `file1.txt` & `file2.txt` contains : + +``` +abcdefghijklmnopqrstuvwxyz +``` + +Normal cases : + +``` +$ ./ztail -c 4 file1.txt +xyz +$ ./ztail -c 4 file1.txt file2.txt +==> file1.txt <== +xyz + +==> file2.txt <== +xyz +$ +``` + +Error cases : + +``` +$ ./ztail -c 4 file1.txt nonexisting1.txt file2.txt nonexisting2.txt +==> file1.txt <== +xyz +open nonexisting1.txt: no such file or directory + +==> file2.txt <== +xyz +open nonexisting2.txt: no such file or directory +$ echo $? +1 +$ +```