github.com/gotranspile/cxgo@v0.3.7/runtime/stdio/glob.go (about) 1 package stdio 2 3 import ( 4 "log" 5 "path/filepath" 6 "strings" 7 8 "github.com/gotranspile/cxgo/runtime/libc" 9 ) 10 11 const GlobNoEscape = 1 12 13 type Glob struct { 14 Num int32 15 Paths **byte 16 Reserve int32 17 } 18 19 func isHidden(s string) bool { 20 for _, p := range filepath.SplitList(s) { 21 if strings.HasPrefix(p, ".") { 22 return true 23 } 24 } 25 return false 26 } 27 28 func (g *Glob) Glob(pattern *byte, flags int32, errFunc func(epath *byte, errno int32) int32) int32 { 29 if errFunc != nil { 30 panic("implement me") 31 } 32 glob := libc.GoString(pattern) 33 res, err := filepath.Glob(glob) 34 log.Printf("glob(%q, %x, %p, %p): %d, %v", glob, flags, errFunc, g, len(res), err) 35 if err != nil { 36 libc.SetErr(err) 37 return 1 // TODO 38 } 39 g.Reserve = 0 40 paths := make([]*byte, 0, len(res)+1+2) 41 paths = append(paths, nil) // padding 42 for _, s := range res { 43 if isHidden(s) { 44 continue 45 } 46 paths = append(paths, libc.CString(s)) 47 } 48 g.Num = int32(len(paths) - 1) // - padding 49 if g.Num == 0 { 50 g.Paths = nil 51 return 1 // TODO 52 } 53 paths = append(paths, nil) // null-terminator 54 paths = append(paths, nil) // padding 55 g.Paths = &paths[1] // padding 56 return 0 57 } 58 59 func (g *Glob) Free() { 60 g.Paths = nil 61 g.Num = 0 62 g.Reserve = 0 63 }