Browse Source

Add condition to subject & implementation

content-update
Xavier Petit 4 years ago
parent
commit
1950440884
No known key found for this signature in database
GPG Key ID: CA3F2B17E25ABD26
  1. 33
      go/tests/prog/ztail_prog/main.go
  2. 46
      subjects/ztail/README.md

33
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)
}

46
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
$
```

Loading…
Cancel
Save