Beispiel #1
0
func main() {
	flag.Parse()

	paths := flag.Args()
	switch len(paths) {
	case 0:
		msg.Errln("mv: missing file operand")
	case 1:
		msg.Errf("mv: missing destination operand after %q\n", paths[0])
	case 2:
		src, dest := paths[0], paths[1]

		err := shared.Move(src, dest)
		if err != nil {
			msg.Errf("mv: %s\n", err)
			os.Exit(1)
		}
	// More than 2 arguments.
	default:
		srcs, dest := paths[:len(paths)-1], paths[len(paths)-1]

		var exitcode int
		for _, src := range srcs {
			err := shared.Move(src, dest)
			if err != nil {
				msg.Errf("mv: %s\n", err)
				exitcode++
			}
		}
		os.Exit(exitcode)
	}
}
Beispiel #2
0
func main() {
	flag.Parse()

	if recursive {
		remove = os.RemoveAll
	}

	paths := flag.Args()
	if len(paths) < 1 {
		msg.Errln("rm: no files given")
	}

	for _, p := range paths {
		for p != "." {
			err := remove(p)
			if err != nil {
				msg.Errf("rm: %s\n", err)
			}
			if !parents {
				break
			}
			p = path.Dir(p)
		}
	}
}
Beispiel #3
0
func main() {
	var (
		// Filenames
		paths []string
		path  string
		// Raw content of a file
		raw []byte
		// The file to read
		file io.ReadCloser
		err  error
	)
	flag.Parse()
	paths = flag.Args()
	// Read from stdin when called without arguments.
	if len(paths) < 1 {
		paths = append(paths, "-")
	}
	for _, path = range paths {
		if path == "-" {
			file = os.Stdin
		} else {
			file, err = os.Open(path)
			if err != nil {
				msg.Errf("cat: %s\n", err)
				continue
			}
			defer file.Close()
		}

		raw, err = ioutil.ReadAll(file)
		if err != nil {
			msg.Errf("cat: %v\n", err)
		} else {
			fmt.Printf("%s", string(raw))
		}
	}
}
Beispiel #4
0
func main() {
	flag.Parse()

	files := flag.Args()
	if len(files) != 2 {
		msg.Errln("Need 2 arguments.")
		os.Exit(1)
	}
	src, dest := files[0], files[1]

	err := shared.Copy(src, dest)
	if err != nil {
		msg.Errf("cp: %s\n", err)
		os.Exit(1)
	}
}
Beispiel #5
0
func main() {
	var (
		bytes, total_bytes int
		chars, total_chars int
		words, total_words int
		lines, total_lines int
		// The file to read
		file io.ReadCloser
		err  error
	)
	flag.Parse()

	// This is only true if all flags are unset or false.
	print_all := !(print_bytes || print_chars || print_lines || print_words)

	paths := flag.Args()
	// Read from stdin when called without arguments.
	if len(paths) < 1 {
		paths = append(paths, "-")
	}
	// TODO: this is duplicate code with cat and probably every program that
	// will read from stdin when no input files are given. Move it to shared.
	for _, path := range paths {
		if path == "-" {
			file = os.Stdin
		} else {
			file, err = os.Open(path)
			if err != nil {
				msg.Errf("cat: %s\n", err)
				continue
			}
			defer file.Close()
		}
		raw, err := ioutil.ReadAll(file)
		if err != nil {
			msg.Errf("wc: %v\n", err)
			continue
		}
		//str := string(raw)
		//runes := []rune(str)

		// TODO: This is quick, dirty, and inefficient, and should be done in a
		// linear way.
		//bytes = len(raw)
		//chars = len(runes)
		//words = len(strings.Split(str, ""))
		//lines = len(strings.Split(str, "\n"))
		bytes, chars, words, lines = Count(raw)

		total_bytes += bytes
		total_chars += chars
		total_words += words
		total_lines += lines

		if print_all {
			fmt.Printf("%d %d %d %d %s\n", lines, words, chars, bytes, path)
			continue
		}
		if print_lines {
			fmt.Printf("%d ", lines)
		}
		if print_words {
			fmt.Printf("%d ", words)
		}
		if print_chars {
			fmt.Printf("%d ", chars)
		}
		if print_bytes {
			fmt.Printf("%d ", bytes)
		}
		fmt.Println(path)
	}
	if len(paths) > 1 {
		if print_all {
			fmt.Printf("%d %d %d %d total\n",
				total_lines, total_words, total_chars, total_bytes)
			return
		}
		if print_lines {
			fmt.Printf("%d ", total_lines)
		}
		if print_words {
			fmt.Printf("%d ", total_words)
		}
		if print_chars {
			fmt.Printf("%d ", total_chars)
		}
		if print_bytes {
			fmt.Printf("%d ", total_bytes)
		}
		fmt.Println("total")
	}

}