Home | History | Annotate | Download | only in time
      1 // Copyright 2011 The Go Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style
      3 // license that can be found in the LICENSE file.
      4 
      5 package time
      6 
      7 import (
      8 	"errors"
      9 	"syscall"
     10 )
     11 
     12 // for testing: whatever interrupts a sleep
     13 func interrupt() {
     14 }
     15 
     16 // readFile reads and returns the content of the named file.
     17 // It is a trivial implementation of ioutil.ReadFile, reimplemented
     18 // here to avoid depending on io/ioutil or os.
     19 func readFile(name string) ([]byte, error) {
     20 	f, err := syscall.Open(name, syscall.O_RDONLY, 0)
     21 	if err != nil {
     22 		return nil, err
     23 	}
     24 	defer syscall.Close(f)
     25 	var (
     26 		buf [4096]byte
     27 		ret []byte
     28 		n   int
     29 	)
     30 	for {
     31 		n, err = syscall.Read(f, buf[:])
     32 		if n > 0 {
     33 			ret = append(ret, buf[:n]...)
     34 		}
     35 		if n == 0 || err != nil {
     36 			break
     37 		}
     38 	}
     39 	return ret, err
     40 }
     41 
     42 func open(name string) (uintptr, error) {
     43 	fd, err := syscall.Open(name, syscall.O_RDONLY, 0)
     44 	if err != nil {
     45 		return 0, err
     46 	}
     47 	return uintptr(fd), nil
     48 }
     49 
     50 func closefd(fd uintptr) {
     51 	syscall.Close(syscall.Handle(fd))
     52 }
     53 
     54 func preadn(fd uintptr, buf []byte, off int) error {
     55 	whence := seekStart
     56 	if off < 0 {
     57 		whence = seekEnd
     58 	}
     59 	if _, err := syscall.Seek(syscall.Handle(fd), int64(off), whence); err != nil {
     60 		return err
     61 	}
     62 	for len(buf) > 0 {
     63 		m, err := syscall.Read(syscall.Handle(fd), buf)
     64 		if m <= 0 {
     65 			if err == nil {
     66 				return errors.New("short read")
     67 			}
     68 			return err
     69 		}
     70 		buf = buf[m:]
     71 	}
     72 	return nil
     73 }
     74