OSDN Git Service

5f383c12fdab6f86f332d843b018a36b3d885b39
[pf3gnuchains/gcc-fork.git] / libgo / go / os / dir.go
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.
4
5 package os
6
7 import (
8         "syscall"
9         "unsafe"
10 )
11
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")
15
16 // FIXME: pathconf returns long, not int.
17 func libc_pathconf(*byte, int) int __asm__ ("pathconf")
18
19 func clen(n []byte) int {
20         for i := 0; i < len(n); i++ {
21                 if n[i] == 0 {
22                         return i
23                 }
24         }
25         return len(n)
26 }
27
28 var elen int;
29
30 // Readdirnames reads and returns a slice of names from the directory f.
31 //
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.
35 //
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
41 // a non-nil error.
42 func (file *File) Readdirnames(n int) (names []string, err Error) {
43         if elen == 0 {
44                 var dummy syscall.Dirent;
45                 elen = (unsafe.Offsetof(dummy.Name) +
46                         libc_pathconf(syscall.StringBytePtr(file.name), syscall.PC_NAME_MAX) +
47                         1);
48         }
49
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))
54         }
55
56         entry_dirent := unsafe.Pointer(&file.dirinfo.buf[0]).(*syscall.Dirent)
57
58         size := n
59         if size < 0 {
60                 size = 100
61                 n = -1
62         }
63
64         names = make([]string, 0, size) // Empty with room to grow.
65
66         dir := file.dirinfo.dir
67         if dir == nil {
68                 return names, NewSyscallError("opendir", syscall.GetErrno())
69         }       
70
71         for n != 0 {
72                 var result *syscall.Dirent
73                 i := libc_readdir_r(dir, entry_dirent, &result)
74                 if i != 0 {
75                         return names, NewSyscallError("readdir_r", i)
76                 }
77                 if result == nil {
78                         break // EOF
79                 }
80                 var name = string(result.Name[0:clen(result.Name[0:])])
81                 if name == "." || name == ".." {        // Useless names
82                         continue
83                 }
84                 names = append(names, name)
85                 n--
86         }
87         if n >= 0 && len(names) == 0 {
88                 return names, EOF
89         }
90         return names, nil
91 }