]> Cypherpunks repositories - gostls13.git/commitdiff
gofmt: use os.Stdin instead of opening /dev/stdin
authorFazlul Shahriar <fshahriar@gmail.com>
Tue, 1 Dec 2009 17:32:22 +0000 (09:32 -0800)
committerRobert Griesemer <gri@golang.org>
Tue, 1 Dec 2009 17:32:22 +0000 (09:32 -0800)
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

src/cmd/gofmt/gofmt.go

index d7c96dc3ac943d1e8216da67668a73d5df4db041..47d03405e186e055f7ca8259ea67f5f9ee6443c7 100644 (file)
@@ -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():