Skip to content
This repository has been archived by the owner on May 2, 2018. It is now read-only.

Commit

Permalink
gofmt: use os.Stdin instead of opening /dev/stdin
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fhs authored and griesemer committed Dec 1, 2009
1 parent 9e45088 commit db18ce2
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions src/cmd/gofmt/gofmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand All @@ -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 {
Expand All @@ -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
}
}
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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():
Expand Down

0 comments on commit db18ce2

Please sign in to comment.