func RunFile(filename string, start StartToken, globals, locals Object) (Object, error) { name := C.CString(filename) defer C.free(unsafe.Pointer(name)) mode := C.CString("r") defer C.free(unsafe.Pointer(mode)) var token C.int switch start { case EvalInput: token = C.Py_eval_input case FileInput: token = C.Py_file_input case SingleInput: token = C.Py_single_input } file, err := C.fopen(name, mode) if file == nil { return nil, err } defer C.fclose(file) obj := C.PyRun_FileExFlags(file, name, token, c(globals), c(locals), 0, nil) if obj == nil { return nil, exception() } return newObject(obj), nil }
// file2py opens a stdC file from a Go os.File. Note the returned file has // been newly opened: the caller must close it with C.fclose(retval). func file2py(f *os.File, mode string) *C.FILE { cmode := C.CString(mode) defer C.free(unsafe.Pointer(cmode)) name := C.CString(file.Name()) defer C.free(unsafe.Pointer(name)) file := C.fopen(name, cmode) return file }
func CreateFromGif(infile string) *Image { file := C.fopen(C.CString(infile), C.CString("rb")) if file != nil { defer C.fclose(file) return img(C.gdImageCreateFromGif(file)) } panic(errors.New("Error occurred while opening file.")) }
func (p *Image) Wbmp(out string, foreground Color) { file := C.fopen(C.CString(out), C.CString("wb")) if file != nil { defer C.fclose(file) C.gdImageWBMP(p.img, C.int(foreground), file) } else { panic(errors.New("Error occurred while opening file for writing.")) } }
func (p *Image) Gif(out string) { file := C.fopen(C.CString(out), C.CString("wb")) if file != nil { defer C.fclose(file) C.gdImageGif(p.img, file) } else { panic(errors.New("Error occurred while opening file for writing.")) } }
func (p *Image) Jpeg(out string, quality int) { file := C.fopen(C.CString(out), C.CString("wb")) if file != nil { defer C.fclose(file) C.gdImageJpeg(p.img, file, C.int(quality)) } else { panic(errors.New("Error occurred while opening file for writing.")) } }
func testErrno(t *testing.T) { p := C.CString("no-such-file") m := C.CString("r") f, err := C.fopen(p, m) C.free(unsafe.Pointer(p)) C.free(unsafe.Pointer(m)) if err == nil { C.fclose(f) t.Fatalf("C.fopen: should fail") } if err != syscall.ENOENT { t.Fatalf("C.fopen: unexpected error: %v", err) } }
func Init() { filename := C.CString("/etc/sensors3.conf") defer C.free(unsafe.Pointer(filename)) mode := C.CString("r") defer C.free(unsafe.Pointer(mode)) fp, err := C.fopen(filename, mode) defer C.fclose(fp) if fp == nil { log.Fatal(err) } C.sensors_init(fp) }
// Parses a file and returns a CMarkNode // Returns an error if the file can't be opened func ParseFile(filename string, options int) (*CMarkNode, error) { fname := C.CString(filename) access := C.CString("r") defer C.free(unsafe.Pointer(fname)) defer C.free(unsafe.Pointer(access)) file := C.fopen(fname, access) if file == nil { return nil, errors.New("Unable to open file with name: " + filename) } defer C.fclose(file) n := &CMarkNode{ node: C.cmark_parse_file(file, C.int(options)), } runtime.SetFinalizer(n, (*CMarkNode).Free) return n, nil }
// PyRun_SimpleFile executes the given python script synchronously. Note that // unlike the corresponding C API, this will internally open and close the file // for you. func PyRun_SimpleFile(filename string) error { cfname := C.CString(filename) defer C.free(unsafe.Pointer(cfname)) cronly := C.CString("r") defer C.free(unsafe.Pointer(cronly)) cfile, err := C.fopen(cfname, cronly) if err != nil || cfile == nil { return fmt.Errorf("python: could not open %s: %v", filename, err) } retcode := C.PyRun_SimpleFileExFlags(cfile, cfname, 0, nil) if retcode != 0 { return fmt.Errorf("error %d executing script %s", int(retcode), filename) } return nil }
func (c *Compiler) AddFile(ns, path string) error { cpath := C.CString(path) cmode := C.CString("r") cns := C.CString(ns) defer C.free(unsafe.Pointer(cpath)) defer C.free(unsafe.Pointer(cmode)) defer C.free(unsafe.Pointer(cns)) fd := C.fopen(cpath, cmode) if fd == nil { return fmt.Errorf("libyara: failed to open %q", path) } defer C.fclose(fd) errors := C.yr_compiler_add_file(c.handle, fd, nil, cpath) if errors > 0 { return fmt.Errorf("libyara: failed to compile %q", path) } return nil }
func parseMountTable() ([]*Info, error) { mnttab := C.fopen(C.CString(C.MNTTAB), C.CString("r")) if mnttab == nil { return nil, fmt.Errorf("Failed to open %s", C.MNTTAB) } var out []*Info var mp C.struct_mnttab ret := C.getmntent(mnttab, &mp) for ret == 0 { var mountinfo Info mountinfo.Mountpoint = C.GoString(mp.mnt_mountp) mountinfo.Source = C.GoString(mp.mnt_special) mountinfo.Fstype = C.GoString(mp.mnt_fstype) mountinfo.Opts = C.GoString(mp.mnt_mntopts) out = append(out, &mountinfo) ret = C.getmntent(mnttab, &mp) } C.fclose(mnttab) return out, nil }
func setStream(f *os.File, cstream **C.FILE, mode string, def int) error { fd := def if f != nil { fd = int(f.Fd()) } cfd := def if *cstream != nil { cfd = int(C.fileno(*cstream)) } if fd == cfd { // TODO Validate return nil } cname := C.CString(f.Name()) cmode := C.CString(mode) cf, err := C.fopen(cname, cmode) // TODO How to close? C.free(unsafe.Pointer(cname)) C.free(unsafe.Pointer(cmode)) if err != nil { return err } *cstream = cf return nil }
func main() { _ = C.fopen() // ERROR HERE }