2019-04-24 08:54:53 +00:00
|
|
|
// sponge - Soaks up all input from stdin and writes it to a file or stdout
|
|
|
|
// https://git.sr.ht/~tslocum/sponge
|
|
|
|
//
|
|
|
|
// Originally written in C by Tollef Fog Heen
|
|
|
|
// https://joeyh.name/code/moreutils
|
|
|
|
//
|
|
|
|
// Ported by Trevor Slocum
|
|
|
|
// https://rocketnine.space
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2019-06-18 16:54:21 +00:00
|
|
|
"log"
|
2019-04-24 08:54:53 +00:00
|
|
|
"os"
|
2019-06-18 16:54:21 +00:00
|
|
|
"os/exec"
|
2019-04-24 08:54:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const BufferSize = 64 * 1024 // 64 KiB
|
|
|
|
|
|
|
|
func CheckError(err error) {
|
|
|
|
if err != nil {
|
2019-06-18 16:54:21 +00:00
|
|
|
log.Fatal(err)
|
2019-04-24 08:54:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 16:54:21 +00:00
|
|
|
func init() {
|
|
|
|
log.SetFlags(0)
|
|
|
|
log.SetPrefix("sponge: ")
|
|
|
|
}
|
2019-04-24 08:54:53 +00:00
|
|
|
|
2019-06-18 16:54:21 +00:00
|
|
|
func main() {
|
|
|
|
appendFile := flag.Bool("a", false, "append")
|
2019-04-24 08:54:53 +00:00
|
|
|
flag.Usage = func() {
|
|
|
|
_, _ = fmt.Fprintf(os.Stderr, "%s [-a] [file]: soak up all input from stdin and write it to [file] or stdout\n", os.Args[0])
|
|
|
|
}
|
|
|
|
flag.Parse()
|
|
|
|
|
2019-06-18 16:54:21 +00:00
|
|
|
writeFileName := flag.Arg(0)
|
2019-04-24 08:54:53 +00:00
|
|
|
|
2019-06-18 16:54:21 +00:00
|
|
|
tmpFile, err := ioutil.TempFile("", "sponge-*.tmp")
|
|
|
|
CheckError(err)
|
|
|
|
defer func(tmpFile *os.File) {
|
|
|
|
if tmpFile == nil {
|
|
|
|
return // File was closed elsewhere
|
|
|
|
}
|
2019-04-24 08:54:53 +00:00
|
|
|
|
2019-06-18 16:54:21 +00:00
|
|
|
tmpFileName := tmpFile.Name()
|
|
|
|
_ = tmpFile.Close()
|
2019-04-24 08:54:53 +00:00
|
|
|
_ = os.Remove(tmpFileName)
|
2019-06-18 16:54:21 +00:00
|
|
|
}(tmpFile)
|
2019-04-24 08:54:53 +00:00
|
|
|
|
2019-06-18 16:54:21 +00:00
|
|
|
buf := make([]byte, BufferSize)
|
2019-04-24 08:54:53 +00:00
|
|
|
|
2019-06-18 16:54:21 +00:00
|
|
|
_, err = io.CopyBuffer(tmpFile, os.Stdin, buf)
|
|
|
|
CheckError(err)
|
2019-04-24 08:54:53 +00:00
|
|
|
|
2019-06-18 16:54:21 +00:00
|
|
|
_, err = tmpFile.Seek(io.SeekStart, 0)
|
|
|
|
CheckError(err)
|
2019-04-24 08:54:53 +00:00
|
|
|
|
2019-06-18 16:54:21 +00:00
|
|
|
var out io.Writer
|
|
|
|
if writeFileName != "" {
|
|
|
|
regularFile := true
|
|
|
|
fileInfo, err := os.Stat(writeFileName)
|
|
|
|
if err != nil {
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
CheckError(err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
regularFile = fileInfo.Mode().IsRegular()
|
|
|
|
}
|
|
|
|
|
|
|
|
if regularFile && !*appendFile {
|
|
|
|
tmpFileName := tmpFile.Name()
|
|
|
|
tmpFile.Close()
|
|
|
|
tmpFile = nil
|
|
|
|
|
|
|
|
err = os.Rename(tmpFileName, writeFileName)
|
|
|
|
if err != nil {
|
|
|
|
// Fall back to mv
|
|
|
|
cmd := exec.Command("mv", tmpFileName, writeFileName)
|
|
|
|
err = cmd.Run()
|
|
|
|
}
|
|
|
|
CheckError(err)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
openFlags := os.O_WRONLY
|
|
|
|
if regularFile {
|
|
|
|
openFlags |= os.O_CREATE
|
|
|
|
}
|
|
|
|
if *appendFile {
|
|
|
|
openFlags |= os.O_APPEND
|
|
|
|
}
|
|
|
|
|
|
|
|
writeFile, err := os.OpenFile(writeFileName, openFlags, 0644)
|
2019-04-24 08:54:53 +00:00
|
|
|
CheckError(err)
|
2019-06-18 16:54:21 +00:00
|
|
|
defer writeFile.Close()
|
2019-04-24 08:54:53 +00:00
|
|
|
|
2019-06-18 16:54:21 +00:00
|
|
|
out = writeFile
|
|
|
|
} else {
|
|
|
|
out = os.Stdout
|
2019-04-24 08:54:53 +00:00
|
|
|
}
|
|
|
|
|
2019-06-18 16:54:21 +00:00
|
|
|
_, err = io.CopyBuffer(out, tmpFile, buf)
|
2019-04-24 08:54:53 +00:00
|
|
|
CheckError(err)
|
|
|
|
}
|