package dup

import (
	"fmt"
	"io/ioutil"
	"os"
	"strings"
)

// cf. TGPL p. 12 (read file at once)
func Dup3() {
	counts := make(map[string]int)
	for _, fname := range os.Args[1:] {
		data, err := ioutil.ReadFile(fname)
		if err != nil {
			fmt.Fprint(os.Stderr, "Dup3: %v\n", err)
			continue
		}
		for _, line := range strings.Split(string(data), "\n") {
			counts[line]++
		}
	}

	for line, cnt := range counts {
		if cnt > 1 {
			fmt.Printf("%d duplicates of\t%q\n", cnt, line)
		}
	}
}
