2009-03-16 22:53:23 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// Copyright 2009 The Go Authors. All rights reserved.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// Use of this source code is governed by a BSD-style
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								// license that can be found in the LICENSE file.
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								package file
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								import (
							 | 
						
					
						
							
								
									
										
										
										
											2009-12-16 10:29:53 +11:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									"os"
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									"syscall"
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-16 22:53:23 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								type File struct {
							 | 
						
					
						
							
								
									
										
										
										
											2010-08-04 08:34:52 +10:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									fd   int    // file descriptor number
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									name string // file name at Open time
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-16 22:53:23 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-01 22:14:39 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								func newFile(fd int, name string) *File {
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-16 22:53:23 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									if fd < 0 {
							 | 
						
					
						
							
								
									
										
										
										
											2009-10-13 12:37:04 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										return nil
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-16 22:53:23 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							
								
									
										
										
										
											2009-10-13 12:37:04 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									return &File{fd, name}
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-16 22:53:23 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								var (
							 | 
						
					
						
							
								
									
										
										
										
											2011-01-18 11:01:47 -08:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									Stdin  = newFile(syscall.Stdin, "/dev/stdin")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									Stdout = newFile(syscall.Stdout, "/dev/stdout")
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
									Stderr = newFile(syscall.Stderr, "/dev/stderr")
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-16 22:53:23 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								)
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2010-08-04 08:34:52 +10:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								func Open(name string, mode int, perm uint32) (file *File, err os.Error) {
							 | 
						
					
						
							
								
									
										
										
										
											2009-12-16 10:29:53 +11:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									r, e := syscall.Open(name, mode, perm)
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-25 20:24:55 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									if e != 0 {
							 | 
						
					
						
							
								
									
										
										
										
											2009-12-16 10:29:53 +11:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										err = os.Errno(e)
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-25 20:24:55 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							
								
									
										
										
										
											2009-10-13 12:37:04 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									return newFile(r, name), err
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-16 22:53:23 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-04-17 00:08:24 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								func (file *File) Close() os.Error {
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-16 22:53:23 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									if file == nil {
							 | 
						
					
						
							
								
									
										
										
										
											2009-10-13 12:37:04 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										return os.EINVAL
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-16 22:53:23 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							
								
									
										
										
										
											2009-12-16 10:29:53 +11:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									e := syscall.Close(file.fd)
							 | 
						
					
						
							
								
									
										
										
										
											2010-08-04 08:34:52 +10:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									file.fd = -1 // so it can't be closed again
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-25 20:24:55 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									if e != 0 {
							 | 
						
					
						
							
								
									
										
										
										
											2009-12-16 10:29:53 +11:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										return os.Errno(e)
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-25 20:24:55 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							
								
									
										
										
										
											2009-10-13 12:37:04 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									return nil
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-16 22:53:23 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-04-17 00:08:24 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								func (file *File) Read(b []byte) (ret int, err os.Error) {
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-16 22:53:23 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									if file == nil {
							 | 
						
					
						
							
								
									
										
										
										
											2009-10-13 12:37:04 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										return -1, os.EINVAL
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-16 22:53:23 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							
								
									
										
										
										
											2009-12-16 10:29:53 +11:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									r, e := syscall.Read(file.fd, b)
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-25 20:24:55 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									if e != 0 {
							 | 
						
					
						
							
								
									
										
										
										
											2009-12-16 10:29:53 +11:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										err = os.Errno(e)
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-25 20:24:55 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							
								
									
										
										
										
											2009-10-13 12:37:04 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									return int(r), err
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-16 22:53:23 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							
								
									
										
										
										
											2009-04-17 00:08:24 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
								func (file *File) Write(b []byte) (ret int, err os.Error) {
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-16 22:53:23 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									if file == nil {
							 | 
						
					
						
							
								
									
										
										
										
											2009-10-13 12:37:04 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										return -1, os.EINVAL
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-16 22:53:23 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							
								
									
										
										
										
											2009-12-16 10:29:53 +11:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									r, e := syscall.Write(file.fd, b)
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-25 20:24:55 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									if e != 0 {
							 | 
						
					
						
							
								
									
										
										
										
											2009-12-16 10:29:53 +11:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
										err = os.Errno(e)
							 | 
						
					
						
							
								
									
										
										
										
											2009-06-25 20:24:55 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									}
							 | 
						
					
						
							
								
									
										
										
										
											2009-10-13 12:37:04 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									return int(r), err
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-16 22:53:23 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								
							 | 
						
					
						
							| 
								
							 | 
							
								
							 | 
							
								
							 | 
							
							
								func (file *File) String() string {
							 | 
						
					
						
							
								
									
										
										
										
											2009-10-13 12:37:04 -07:00
										 
									 
								 
							 | 
							
								
									
										
									
								
							 | 
							
								
							 | 
							
							
									return file.name
							 | 
						
					
						
							
								
									
										
										
										
											2009-03-16 22:53:23 -07:00
										 
									 
								 
							 | 
							
								
							 | 
							
								
							 | 
							
							
								}
							 |