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.
12 func libc_dup(fd int) int __asm__ ("dup")
13 func libc_opendir(*byte) *syscall.DIR __asm__ ("opendir")
14 func libc_closedir(*syscall.DIR) int __asm__ ("closedir")
16 // FIXME: pathconf returns long, not int.
17 func libc_pathconf(*byte, int) int __asm__ ("pathconf")
19 func clen(n []byte) int {
20 for i := 0; i < len(n); i++ {
30 // Readdirnames reads and returns a slice of names from the directory f.
32 // If n > 0, Readdirnames returns at most n names. In this case, if
33 // Readdirnames returns an empty slice, it will return a non-nil error
34 // explaining why. At the end of a directory, the error is os.EOF.
36 // If n <= 0, Readdirnames returns all the names from the directory in
37 // a single slice. In this case, if Readdirnames succeeds (reads all
38 // the way to the end of the directory), it returns the slice and a
39 // nil os.Error. If it encounters an error before the end of the
40 // directory, Readdirnames returns the names read until that point and
42 func (file *File) Readdirnames(n int) (names []string, err Error) {
44 var dummy syscall.Dirent;
45 elen = (unsafe.Offsetof(dummy.Name) +
46 libc_pathconf(syscall.StringBytePtr(file.name), syscall.PC_NAME_MAX) +
50 if file.dirinfo == nil {
51 file.dirinfo = new(dirInfo)
52 file.dirinfo.buf = make([]byte, elen)
53 file.dirinfo.dir = libc_opendir(syscall.StringBytePtr(file.name))
56 entry_dirent := unsafe.Pointer(&file.dirinfo.buf[0]).(*syscall.Dirent)
64 names = make([]string, 0, size) // Empty with room to grow.
66 dir := file.dirinfo.dir
68 return names, NewSyscallError("opendir", syscall.GetErrno())
72 var result *syscall.Dirent
73 i := libc_readdir_r(dir, entry_dirent, &result)
75 return names, NewSyscallError("readdir_r", i)
80 var name = string(result.Name[0:clen(result.Name[0:])])
81 if name == "." || name == ".." { // Useless names
84 names = append(names, name)
87 if n >= 0 && len(names) == 0 {