gitlab.com/apertussolutions/u-root@v7.0.0+incompatible/cmds/core/cat/cat.go (about)

     1  // Copyright 2012-2017 the u-root Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // cat concatenates files and prints them to stdout.
     6  //
     7  // Synopsis:
     8  //     cat [-u] [FILES]...
     9  //
    10  // Description:
    11  //     If no files are specified, read from stdin.
    12  //
    13  // Options:
    14  //     -u: ignored flag
    15  package main
    16  
    17  import (
    18  	"flag"
    19  	"io"
    20  	"log"
    21  	"os"
    22  )
    23  
    24  var (
    25  	_ = flag.Bool("u", false, "ignored")
    26  )
    27  
    28  func catFile(w io.Writer, file string) error {
    29  	f, err := os.Open(file)
    30  	if err != nil {
    31  		return err
    32  	}
    33  	defer f.Close()
    34  
    35  	_, err = io.Copy(w, f)
    36  	return err
    37  }
    38  
    39  func cat(w io.Writer, files []string) error {
    40  	for _, name := range files {
    41  		if err := catFile(w, name); err != nil {
    42  			return err
    43  		}
    44  	}
    45  	return nil
    46  }
    47  
    48  func main() {
    49  	flag.Parse()
    50  
    51  	if flag.NArg() == 0 {
    52  		if _, err := io.Copy(os.Stdout, os.Stdin); err != nil {
    53  			log.Fatalf("error concatenating stdin to stdout: %v", err)
    54  		}
    55  	}
    56  
    57  	if err := cat(os.Stdout, flag.Args()); err != nil {
    58  		log.Fatalf("cat: %v", err)
    59  	}
    60  }