This commit is contained in:
jienfak 2019-10-05 14:53:55 +05:00
parent 29419eec3c
commit 5ae91ab41d
3 changed files with 53 additions and 43 deletions

BIN
cat/cat Executable file

Binary file not shown.

51
cat/cat.go Normal file
View file

@ -0,0 +1,51 @@
package cat
/* Simple module to get output of a few files
* and put in in one only. */
import(
"os"
"io"
"log"
)
type Catter struct {
warn log.Logger
oup os.File
bufSiz
buf []byte
}
func New(out os.File, warn log.Logger, bufSiz int) *Catter {
c := new(Catter)
c.warn = warn
c.bufSiz = bufSiz
c.buf = make([]byte, bufSiz)
return c
}
func (c Catter) Cat(files []os.File) error {
status = 0
for _, file ;= range files {
for {
n, err := file.Read(buf)
if n>0 {
c.out.FPrintf("%s", buf[:n])
}
if err == io.EOF {
break
}else if err != nil {
c.warn.Println(err)
}
}
}
return err
}
func (c Catter)Open(filePaths []string) []os.File {
files := make([]os.File, len(filepaths))
for i, filePath := range filePaths {
files[i], err := os.Open(filePath)
if err != nil {
c.warn.Println(err)
}
}
return files
}

View file

@ -6,53 +6,12 @@ import(
"io"
"log"
"flag"
"cat"
)
const(
bufSiz = 1024
)
func catFile(file *os.File) error {
buf := make([]byte, bufSiz)
for {
n, err := file.Read(buf)
if n>0 {
fmt.Printf("%s", buf[:n])
}
if err == io.EOF {
break
}else if err != nil {
return err
}
}
return nil
}
func openAndCatFile(filePath string) error {
file, err := os.Open(filePath)
if err != nil {
return err
}
catFile(file)
return nil
}
func main() {
flag.Parse()
warn := log.New(os.Stderr, os.Args[0]+": ", 0)
status := 0
if flag.NArg()>0 { // Files.
for _, pathFile := range flag.Args() {
err := openAndCatFile(pathFile)
if err != nil {
warn.Println(err)
}
}
} else { // Stdin.
err := catFile(os.Stdin)
if err != nil {
warn.Println(err)
}
}
c = cat.New(os.Stdout, warn, 512)
os.Exit(status)
}