forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hexdump.go
50 lines (42 loc) · 933 Bytes
/
hexdump.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright 2017 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// hexdump prints file content in hexadecimal.
//
// Synopsis:
// hexdump [FILES]...
//
// Description:
// Concatenate the input files into a single hexdump. If there are no
// arguments, stdin is read.
package main
import (
"encoding/hex"
"flag"
"io"
"log"
"os"
)
func main() {
flag.Parse()
var readers []io.Reader
if flag.NArg() == 0 {
readers = []io.Reader{os.Stdin}
} else {
readers = make([]io.Reader, 0, flag.NArg())
for _, filename := range flag.Args() {
f, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer f.Close()
readers = append(readers, f)
}
}
r := io.MultiReader(readers...)
w := hex.Dumper(os.Stdout)
defer w.Close()
if _, err := io.Copy(w, r); err != nil {
log.Fatal(err)
}
}