| 1 | // Copyright (c) 2020-2024 Joe Conigliaro. All rights reserved. |
| 2 | // Use of this source code is governed by an MIT license |
| 3 | // that can be found in the LICENSE file. |
| 4 | module builder |
| 5 | |
| 6 | #include <dirent.h> |
| 7 | |
| 8 | fn C.opendir(path &char) &C.DIR |
| 9 | fn C.readdir(dir &C.DIR) &C.dirent |
| 10 | fn C.closedir(dir &C.DIR) int |
| 11 | |
| 12 | // list_dir_entries_nix returns one-level directory entries without using |
| 13 | // result-or blocks, so SSA/C bootstrap does not depend on option guard lowering. |
| 14 | @[manualfree] |
| 15 | fn list_dir_entries_nix(path string) []string { |
| 16 | if path.len == 0 { |
| 17 | return []string{} |
| 18 | } |
| 19 | mut res := []string{cap: 50} |
| 20 | dir := unsafe { C.opendir(path.str) } |
| 21 | if isnil(dir) { |
| 22 | return res |
| 23 | } |
| 24 | for { |
| 25 | ent := C.readdir(dir) |
| 26 | if isnil(ent) { |
| 27 | break |
| 28 | } |
| 29 | unsafe { |
| 30 | bptr := &u8(&ent.d_name[0]) |
| 31 | if bptr[0] == 0 || (bptr[0] == `.` && bptr[1] == 0) |
| 32 | || (bptr[0] == `.` && bptr[1] == `.` && bptr[2] == 0) { |
| 33 | continue |
| 34 | } |
| 35 | res << tos_clone(bptr) |
| 36 | } |
| 37 | } |
| 38 | C.closedir(dir) |
| 39 | return res |
| 40 | } |
| 41 | |