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 // +build darwin freebsd linux openbsd
18 // Readdirnames reads and returns a slice of names from the directory f.
20 // If n > 0, Readdirnames returns at most n names. In this case, if
21 // Readdirnames returns an empty slice, it will return a non-nil error
22 // explaining why. At the end of a directory, the error is io.EOF.
24 // If n <= 0, Readdirnames returns all the names from the directory in
25 // a single slice. In this case, if Readdirnames succeeds (reads all
26 // the way to the end of the directory), it returns the slice and a
27 // nil error. If it encounters an error before the end of the
28 // directory, Readdirnames returns the names read until that point and
30 func (f *File) Readdirnames(n int) (names []string, err error) {
31 // If this file has no dirinfo, create one.
33 f.dirinfo = new(dirInfo)
34 // The buffer must be at least a block long.
35 f.dirinfo.buf = make([]byte, blockSize)
45 names = make([]string, 0, size) // Empty with room to grow.
47 // Refill the buffer if necessary
51 d.nbuf, errno = syscall.ReadDirent(f.fd, d.buf)
53 return names, NewSyscallError("readdirent", errno)
62 nb, nc, names = syscall.ParseDirent(d.buf[d.bufp:d.nbuf], n, names)
66 if n >= 0 && len(names) == 0 {