github.com/llvm-mirror/llgo@v0.0.0-20190322182713-bf6f0a60fce1/third_party/gotools/go/ssa/interp/external_unix.go (about)

     1  // Copyright 2013 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  // +build !windows,!plan9
     6  
     7  package interp
     8  
     9  import "syscall"
    10  
    11  func fillStat(st *syscall.Stat_t, stat structure) {
    12  	stat[0] = st.Dev
    13  	stat[1] = st.Ino
    14  	stat[2] = st.Nlink
    15  	stat[3] = st.Mode
    16  	stat[4] = st.Uid
    17  	stat[5] = st.Gid
    18  
    19  	stat[7] = st.Rdev
    20  	stat[8] = st.Size
    21  	stat[9] = st.Blksize
    22  	stat[10] = st.Blocks
    23  	// TODO(adonovan): fix: copy Timespecs.
    24  	// stat[11] = st.Atim
    25  	// stat[12] = st.Mtim
    26  	// stat[13] = st.Ctim
    27  }
    28  
    29  func ext۰syscall۰Close(fr *frame, args []value) value {
    30  	// func Close(fd int) (err error)
    31  	return wrapError(syscall.Close(args[0].(int)))
    32  }
    33  
    34  func ext۰syscall۰Fstat(fr *frame, args []value) value {
    35  	// func Fstat(fd int, stat *Stat_t) (err error)
    36  	fd := args[0].(int)
    37  	stat := (*args[1].(*value)).(structure)
    38  
    39  	var st syscall.Stat_t
    40  	err := syscall.Fstat(fd, &st)
    41  	fillStat(&st, stat)
    42  	return wrapError(err)
    43  }
    44  
    45  func ext۰syscall۰ReadDirent(fr *frame, args []value) value {
    46  	// func ReadDirent(fd int, buf []byte) (n int, err error)
    47  	fd := args[0].(int)
    48  	p := args[1].([]value)
    49  	b := make([]byte, len(p))
    50  	n, err := syscall.ReadDirent(fd, b)
    51  	for i := 0; i < n; i++ {
    52  		p[i] = b[i]
    53  	}
    54  	return tuple{n, wrapError(err)}
    55  }
    56  
    57  func ext۰syscall۰Kill(fr *frame, args []value) value {
    58  	// func Kill(pid int, sig Signal) (err error)
    59  	return wrapError(syscall.Kill(args[0].(int), syscall.Signal(args[1].(int))))
    60  }
    61  
    62  func ext۰syscall۰Lstat(fr *frame, args []value) value {
    63  	// func Lstat(name string, stat *Stat_t) (err error)
    64  	name := args[0].(string)
    65  	stat := (*args[1].(*value)).(structure)
    66  
    67  	var st syscall.Stat_t
    68  	err := syscall.Lstat(name, &st)
    69  	fillStat(&st, stat)
    70  	return wrapError(err)
    71  }
    72  
    73  func ext۰syscall۰Open(fr *frame, args []value) value {
    74  	// func Open(path string, mode int, perm uint32) (fd int, err error) {
    75  	path := args[0].(string)
    76  	mode := args[1].(int)
    77  	perm := args[2].(uint32)
    78  	fd, err := syscall.Open(path, mode, perm)
    79  	return tuple{fd, wrapError(err)}
    80  }
    81  
    82  func ext۰syscall۰ParseDirent(fr *frame, args []value) value {
    83  	// func ParseDirent(buf []byte, max int, names []string) (consumed int, count int, newnames []string)
    84  	max := args[1].(int)
    85  	var names []string
    86  	for _, iname := range args[2].([]value) {
    87  		names = append(names, iname.(string))
    88  	}
    89  	consumed, count, newnames := syscall.ParseDirent(valueToBytes(args[0]), max, names)
    90  	var inewnames []value
    91  	for _, newname := range newnames {
    92  		inewnames = append(inewnames, newname)
    93  	}
    94  	return tuple{consumed, count, inewnames}
    95  }
    96  
    97  func ext۰syscall۰Read(fr *frame, args []value) value {
    98  	// func Read(fd int, p []byte) (n int, err error)
    99  	fd := args[0].(int)
   100  	p := args[1].([]value)
   101  	b := make([]byte, len(p))
   102  	n, err := syscall.Read(fd, b)
   103  	for i := 0; i < n; i++ {
   104  		p[i] = b[i]
   105  	}
   106  	return tuple{n, wrapError(err)}
   107  }
   108  
   109  func ext۰syscall۰Stat(fr *frame, args []value) value {
   110  	// func Stat(name string, stat *Stat_t) (err error)
   111  	name := args[0].(string)
   112  	stat := (*args[1].(*value)).(structure)
   113  
   114  	var st syscall.Stat_t
   115  	err := syscall.Stat(name, &st)
   116  	fillStat(&st, stat)
   117  	return wrapError(err)
   118  }
   119  
   120  func ext۰syscall۰Write(fr *frame, args []value) value {
   121  	// func Write(fd int, p []byte) (n int, err error)
   122  	n, err := write(args[0].(int), valueToBytes(args[1]))
   123  	return tuple{n, wrapError(err)}
   124  }
   125  
   126  func ext۰syscall۰RawSyscall(fr *frame, args []value) value {
   127  	return tuple{uintptr(0), uintptr(0), uintptr(syscall.ENOSYS)}
   128  }
   129  
   130  func syswrite(fd int, b []byte) (int, error) {
   131  	return syscall.Write(fd, b)
   132  }