# The door to the OS (design/ffi.md). The oracle cannot link C; it emulates# write, exit, getenv, strlen, malloc, free so this runs.extern.Cfnwrite['fd':int32,'buf':ptr[uint8],'len':uint64]->int64;extern.Cfnstrlen['s':ptr[uint8]]->uint64;extern.Cfngetenv['name':ptr[uint8]]->ptr[uint8];extern.Cfnmalloc['bytes':uint64]->ptr.mut[uint8];extern.Cfnfree['p':ptr.mut[uint8]];layout.CstructTimeval{sec:int64,usec:int64}fnsay[lent'text':str.utf8]->fail[io]{no_checklet'n':=write[1,lend'text',strlen[lend'text']];# a lend is the pointer; the call is a no_check siteif'n'<0{failio;}}MAIN{say["hello from write"++newln]otherwisestd::exit[1];say["hello again"++newln]otherwisestd::exit[1];let'key':="HOME";# the lent text ends where C expects a 0no_checklet'home':=getenv[lend'key'];ifptr.is_null['home']{std::print.stored["no HOME",newln];}else{no_checkifstrlen['home']>0{std::print.stored["HOME is set",newln];}}no_checklet'buf':=malloc[4];# bytes nobody ownsno_checkptr.write['buf',value72];no_checkptr.write[ptr.at['buf',by1],value105];no_checkstd::print.useful[ptr.read['buf']," ",ptr.read[ptr.at['buf',by1]],newln];no_checkfree['buf'];std::print.useful[size.of[int32]," ",size.of[Timeval]," ",align.of[int64]," ",size.of[ptr[uint8]],newln];}
Output
hello from write
hello again
HOME is set
72 105
4 16 8 8