var-tracking.c (vt_add_function_parameter): Adjust for VEC changes.
[official-gcc.git] / libgo / go / os / dir.go
blobc77560fc08c5f4214cd541a1b553448a3dbf5e4d
1 // Copyright 2009 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.
5 package os
7 import (
8 "io"
9 "syscall"
10 "unsafe"
13 //extern opendir
14 func libc_opendir(*byte) *syscall.DIR
16 //extern closedir
17 func libc_closedir(*syscall.DIR) int
19 // FIXME: pathconf returns long, not int.
20 //extern pathconf
21 func libc_pathconf(*byte, int) int
23 func clen(n []byte) int {
24 for i := 0; i < len(n); i++ {
25 if n[i] == 0 {
26 return i
29 return len(n)
32 var elen int
34 func (file *File) readdirnames(n int) (names []string, err error) {
35 if elen == 0 {
36 var dummy syscall.Dirent
37 elen = (int(unsafe.Offsetof(dummy.Name)) +
38 libc_pathconf(syscall.StringBytePtr(file.name), syscall.PC_NAME_MAX) +
42 if file.dirinfo == nil {
43 file.dirinfo = new(dirInfo)
44 file.dirinfo.buf = make([]byte, elen)
45 p := syscall.StringBytePtr(file.name)
46 syscall.Entersyscall()
47 r := libc_opendir(p)
48 syscall.Exitsyscall()
49 file.dirinfo.dir = r
52 entry_dirent := (*syscall.Dirent)(unsafe.Pointer(&file.dirinfo.buf[0]))
54 size := n
55 if size < 0 {
56 size = 100
57 n = -1
60 names = make([]string, 0, size) // Empty with room to grow.
62 dir := file.dirinfo.dir
63 if dir == nil {
64 return names, NewSyscallError("opendir", syscall.GetErrno())
67 for n != 0 {
68 var result *syscall.Dirent
69 pr := &result
70 syscall.Entersyscall()
71 i := libc_readdir_r(dir, entry_dirent, pr)
72 syscall.Exitsyscall()
73 if i != 0 {
74 return names, NewSyscallError("readdir_r", i)
76 if result == nil {
77 break // EOF
79 var name = string(result.Name[0:clen(result.Name[0:])])
80 if name == "." || name == ".." { // Useless names
81 continue
83 names = append(names, name)
84 n--
86 if n >= 0 && len(names) == 0 {
87 return names, io.EOF
89 return names, nil