Skip to content

Commit

Permalink
add wc
Browse files Browse the repository at this point in the history
  • Loading branch information
shirou committed Apr 19, 2018
1 parent c0d68d9 commit 11bbc21
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ toybox
#*
*~
*.a
dist
vendor
testdata/busybox
11 changes: 8 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
VERSION=0.0.1

.PHONY: build
build:
go build
Expand All @@ -6,10 +8,13 @@ build:
test:
go test ./...

init:
go get github.com/Songmu/goxz/cmd/goxz

.PHONE: release
release:
CGO_ENABLED=0 go build -a -ldflags='-extldflags "-static" -s -w' -installsuffix netgo
release: init
CGO_ENABLED=0 goxz -pv=$(VERSION) -os=freebsd,darwin,linux -arch=amd64 -d=dist -build-ldflags '-extldflags "-static" -s -w'

.PHONE: build_docker
build_docker: release
build_docker:
docker build -t "toybox" .
1 change: 1 addition & 0 deletions STATUS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ name, status, test
- rm, completed, no
- rmdir, completed, no
- which, completed, no
- wc, completed, no
- yes, completed, no
- sleep, completed, no
- seq, completed, no
Expand Down
2 changes: 2 additions & 0 deletions applets.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/shirou/toybox/applets/sha512sum"
"github.com/shirou/toybox/applets/sleep"
"github.com/shirou/toybox/applets/true"
"github.com/shirou/toybox/applets/wc"
"github.com/shirou/toybox/applets/which"
"github.com/shirou/toybox/applets/yes"
)
Expand Down Expand Up @@ -63,6 +64,7 @@ func init() {
"rm": rm.Main,
"rmdir": rmdir.Main,
"yes": yes.Main,
"wc": wc.Main,
"which": which.Main,

"sh": goash.Main,
Expand Down
111 changes: 111 additions & 0 deletions applets/wc/wc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package wc

import (
"bufio"
"bytes"
"flag"
"fmt"
"io"
"os"
"strconv"
"strings"
)

const binaryName = "wc"

type Option struct {
helpFlag bool
charFlag bool
lineFlag bool
wordFlag bool
}

func NewFlagSet() (*flag.FlagSet, *Option) {
ret := flag.NewFlagSet(binaryName, flag.ExitOnError)

ret.Usage = func() {
fmt.Println("wc [-c|-m] [-lw] [file...]")
ret.PrintDefaults()
}

var opt Option

ret.BoolVar(&opt.helpFlag, "help", false, "show this message")
ret.BoolVar(&opt.charFlag, "c", false, "Write to the standard output the number of bytes in each input file.")
ret.BoolVar(&opt.lineFlag, "l", false, "Write to the standard output the number of <newline> characters in each input file.")
ret.BoolVar(&opt.wordFlag, "w", false, "Write to the standard output the number of words in each input file.")

return ret, &opt
}

func Main(stdout io.Writer, args []string) (err error) {
flagSet, opt := NewFlagSet()
flagSet.Parse(args)

as := flagSet.Args()
if opt.helpFlag {
flagSet.Usage()
return nil
}

var f *os.File
if len(as) == 0 || as[0] == "-" {
f = os.Stdin
return wc(stdout, "", f, opt)
}

for _, path := range flagSet.Args() {
f, err = os.Open(os.ExpandEnv(path))
if err != nil {
return err
}
defer f.Close()

if err := wc(stdout, path, f, opt); err != nil {
return err
}
}

return nil
}

type result struct {
lines int
words int
bytes int
maxLength int
}

func wc(w io.Writer, path string, f io.Reader, opt *Option) error {
var ret result
reader := bufio.NewReaderSize(f, 4096)
for {
line, _, err := reader.ReadLine()
if err == io.EOF {
break
}
ret.lines++
ret.bytes += len(line) + 1 // +1 means NewLine
ret.words += len(bytes.Fields(line))
}
var out []string
if opt.charFlag {
out = append(out, strconv.Itoa(ret.bytes))
}
if opt.lineFlag {
out = append(out, strconv.Itoa(ret.lines))
}
if opt.wordFlag {
out = append(out, strconv.Itoa(ret.words))
}
if len(out) == 0 { // no flag set
out = append(out, strconv.Itoa(ret.lines))
out = append(out, strconv.Itoa(ret.words))
out = append(out, strconv.Itoa(ret.bytes))
}
out = append(out, path)

fmt.Fprintln(w, strings.Join(out, " "))

return nil
}

0 comments on commit 11bbc21

Please sign in to comment.