From: Fazlul Shahriar Date: Tue, 1 Dec 2009 17:32:22 +0000 (-0800) Subject: gofmt: use os.Stdin instead of opening /dev/stdin X-Git-Tag: weekly.2009-12-07~92 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=db18ce2f53f0fc79b7bf1182fca2d33dc7331303;p=gostls13.git gofmt: use os.Stdin instead of opening /dev/stdin Opening /dev/stdin can sometimes fail. For example, in the acme editor, executing "Edit ,|gofmt" fails with: open /dev/stdin: no such device or address Executing "Edit ,|ls -l /dev/stdin /proc/self/fd/0" gives: lrwxrwxrwx 1 root root 15 2009-09-07 02:17 /dev/stdin -> /proc/self/fd/0 lrwx------ 1 fhs users 64 2009-11-26 22:05 /proc/self/fd/0 -> socket:[5528230] (This is my first change, and I've signed the individual contributor license agreement.) R=rsc, gri CC=golang-dev https://golang.org/cl/162041 --- diff --git a/src/cmd/gofmt/gofmt.go b/src/cmd/gofmt/gofmt.go index d7c96dc3ac..47d03405e1 100644 --- a/src/cmd/gofmt/gofmt.go +++ b/src/cmd/gofmt/gofmt.go @@ -79,13 +79,13 @@ func isGoFile(d *os.Dir) bool { } -func processFile(filename string) os.Error { - src, err := io.ReadFile(filename); +func processFile(f *os.File) os.Error { + src, err := io.ReadAll(f); if err != nil { return err } - file, err := parser.ParseFile(filename, src, parserMode()); + file, err := parser.ParseFile(f.Name(), src, parserMode()); if err != nil { return err } @@ -103,10 +103,10 @@ func processFile(filename string) os.Error { if bytes.Compare(src, res.Bytes()) != 0 { // formatting has changed if *list { - fmt.Fprintln(os.Stdout, filename) + fmt.Fprintln(os.Stdout, f.Name()) } if *write { - err = io.WriteFile(filename, res.Bytes(), 0); + err = io.WriteFile(f.Name(), res.Bytes(), 0); if err != nil { return err } @@ -121,6 +121,16 @@ func processFile(filename string) os.Error { } +func processFileByName(filename string) (err os.Error) { + file, err := os.Open(filename, os.O_RDONLY, 0); + if err != nil { + return + } + defer file.Close(); + return processFile(file); +} + + type fileVisitor chan os.Error func (v fileVisitor) VisitDir(path string, d *os.Dir) bool { @@ -131,7 +141,7 @@ func (v fileVisitor) VisitDir(path string, d *os.Dir) bool { func (v fileVisitor) VisitFile(path string, d *os.Dir) { if isGoFile(d) { v <- nil; // synchronize error handler - if err := processFile(path); err != nil { + if err := processFileByName(path); err != nil { v <- err } } @@ -165,7 +175,7 @@ func main() { initRewrite(); if flag.NArg() == 0 { - if err := processFile("/dev/stdin"); err != nil { + if err := processFile(os.Stdin); err != nil { report(err) } } @@ -176,7 +186,7 @@ func main() { case err != nil: report(err) case dir.IsRegular(): - if err := processFile(path); err != nil { + if err := processFileByName(path); err != nil { report(err) } case dir.IsDirectory():