android: fix os.create and builtin
parent
c24a1b3786
commit
8053175ead
6
Makefile
6
Makefile
|
@ -1,4 +1,6 @@
|
|||
CC ?= cc
|
||||
CFLAGS ?=
|
||||
LDFLAGS ?=
|
||||
TMPDIR ?= /tmp
|
||||
|
||||
VCFILE := v.c
|
||||
|
@ -39,12 +41,12 @@ endif
|
|||
|
||||
all: latest_vc latest_tcc
|
||||
ifdef WIN32
|
||||
$(CC) -std=c99 -municode -w -o v2.exe $(TMPVC)/$(VCFILE) $(LDFLAGS)
|
||||
$(CC) $(CFLAGS) -std=c99 -municode -w -o v2.exe $(TMPVC)/$(VCFILE) $(LDFLAGS)
|
||||
./v2.exe -o v3.exe v.v
|
||||
./v3.exe -o v.exe -prod v.v
|
||||
rm -f v2.exe v3.exe
|
||||
else
|
||||
$(CC) -std=gnu11 -w -o v $(TMPVC)/$(VCFILE) $(LDFLAGS) -lm
|
||||
$(CC) $(CFLAGS) -std=gnu11 -w -o v $(TMPVC)/$(VCFILE) $(LDFLAGS) -lm
|
||||
ifdef ANDROID
|
||||
chmod 755 v
|
||||
endif
|
||||
|
|
|
@ -22,15 +22,14 @@ fn C.puts(charptr)
|
|||
*/
|
||||
|
||||
pub fn println(s string) {
|
||||
/*
|
||||
$if linux {
|
||||
$if !android {
|
||||
snl := s + '\n'
|
||||
C.syscall(sys_write, stdout_value, snl.str, s.len+1)
|
||||
C.syscall(/* sys_write */ 1, /* stdout_value */ 1, snl.str, s.len+1)
|
||||
return
|
||||
}
|
||||
}
|
||||
$else {
|
||||
*/
|
||||
C.printf('%.*s\n', s.len, s.str)
|
||||
//}
|
||||
}
|
||||
|
||||
fn print_backtrace_skipping_top_frames_msvc(skipframes int) bool {
|
||||
|
|
|
@ -29,11 +29,15 @@ fn (p mut Parser) comp_time() {
|
|||
}
|
||||
|
||||
if not {
|
||||
if name == 'linux_or_macos' {
|
||||
p.genln('#if !defined(__linux__) && !defined(__APPLE__)')
|
||||
} else {
|
||||
p.genln('#ifndef $ifdef_name')
|
||||
}
|
||||
}
|
||||
else {
|
||||
if name == 'linux_or_macos' {
|
||||
p.genln('#if defined(__linux) || defined(__APPLE__)')
|
||||
p.genln('#if defined(__linux__) || defined(__APPLE__)')
|
||||
} else {
|
||||
p.genln('#ifdef $ifdef_name')
|
||||
}
|
||||
|
|
|
@ -789,7 +789,7 @@ pub fn executable() string {
|
|||
$if haiku {}
|
||||
$if netbsd {
|
||||
mut result := calloc(MAX_PATH)
|
||||
count := int(C.readlink('/proc/curproc/exe', result, MAX_PATH))
|
||||
count := C.readlink('/proc/curproc/exe', result, MAX_PATH)
|
||||
if count < 0 {
|
||||
eprintln('os.executable() failed at reading /proc/curproc/exe to get exe path')
|
||||
return os.args[0]
|
||||
|
@ -798,7 +798,7 @@ pub fn executable() string {
|
|||
}
|
||||
$if dragonfly {
|
||||
mut result := calloc(MAX_PATH)
|
||||
count := int(C.readlink('/proc/curproc/file', result, MAX_PATH))
|
||||
count := C.readlink('/proc/curproc/file', result, MAX_PATH)
|
||||
if count < 0 {
|
||||
eprintln('os.executable() failed at reading /proc/curproc/file to get exe path')
|
||||
return os.args[0]
|
||||
|
|
|
@ -14,6 +14,7 @@ const (
|
|||
|
||||
fn C.symlink(charptr, charptr) int
|
||||
|
||||
|
||||
pub fn init_os_args(argc int, argv &byteptr) []string {
|
||||
mut args := []string
|
||||
for i in 0 .. argc {
|
||||
|
@ -67,9 +68,11 @@ pub fn is_dir(path string) bool {
|
|||
}
|
||||
*/
|
||||
|
||||
|
||||
pub fn open(path string) ?File {
|
||||
$if linux {
|
||||
//$if linux_or_macos {
|
||||
mut file := File{}
|
||||
$if linux_or_macos {
|
||||
$if !android {
|
||||
fd := C.syscall(sys_open, path.str, 511)
|
||||
if fd == -1 {
|
||||
return error('failed to open file "$path"')
|
||||
|
@ -79,9 +82,9 @@ pub fn open(path string) ?File {
|
|||
opened: true
|
||||
}
|
||||
}
|
||||
$else {
|
||||
}
|
||||
cpath := path.str
|
||||
file := File{
|
||||
file = File{
|
||||
cfile: C.fopen(charptr(cpath), 'rb')
|
||||
opened: true
|
||||
}
|
||||
|
@ -89,35 +92,34 @@ pub fn open(path string) ?File {
|
|||
return error('failed to open file "$path"')
|
||||
}
|
||||
return file
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// create creates a file at a specified location and returns a writable `File` object.
|
||||
pub fn create(path string) ?File {
|
||||
$if linux {
|
||||
//$if linux_or_macos {
|
||||
mut fd := 0
|
||||
//println('creat SYS')
|
||||
/*
|
||||
mut file := File{}
|
||||
// NB: android/termux/bionic is also a kind of linux,
|
||||
// but linux syscalls there sometimes fail,
|
||||
// while the libc version should work.
|
||||
$if linux_or_macos {
|
||||
$if !android {
|
||||
$if macos {
|
||||
fd = C.syscall(sys_open_nocancel, path.str, 0x601, 0x1b6)
|
||||
fd = C.syscall(398, path.str, 0x601, 0x1b6)
|
||||
}
|
||||
$else {
|
||||
*/
|
||||
$if linux {
|
||||
fd = C.syscall(sys_creat, path.str, 511)
|
||||
//}
|
||||
//println('fd=$fd')
|
||||
}
|
||||
if fd == -1 {
|
||||
return error('failed to create file "$path"')
|
||||
}
|
||||
return File{
|
||||
file = File{
|
||||
fd: fd
|
||||
opened: true
|
||||
}
|
||||
|
||||
return file
|
||||
}
|
||||
mut file := File{
|
||||
}
|
||||
file = File{
|
||||
cfile: C.fopen(charptr(path.str), 'wb')
|
||||
opened: true
|
||||
}
|
||||
|
@ -132,16 +134,17 @@ pub fn (f mut File) fseek(pos, mode int) {
|
|||
}
|
||||
*/
|
||||
|
||||
|
||||
pub fn (f mut File) write(s string) {
|
||||
if !f.opened {
|
||||
return
|
||||
}
|
||||
$if linux {
|
||||
//$if linux_or_macos {
|
||||
$if linux_or_macos {
|
||||
$if !android {
|
||||
C.syscall(sys_write, f.fd, s.str, s.len)
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
C.fputs(s.str, f.cfile)
|
||||
// C.fwrite(s.str, 1, s.len, f.cfile)
|
||||
}
|
||||
|
@ -150,14 +153,13 @@ pub fn (f mut File) writeln(s string) {
|
|||
if !f.opened {
|
||||
return
|
||||
}
|
||||
//$if linux_or_macos {
|
||||
$if linux {
|
||||
$if linux_or_macos {
|
||||
$if !android {
|
||||
snl := s + '\n'
|
||||
C.syscall(sys_write, f.fd, snl.str, snl.len)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
// C.fwrite(s.str, 1, s.len, f.cfile)
|
||||
// ss := s.clone()
|
||||
// TODO perf
|
||||
|
@ -166,20 +168,21 @@ pub fn (f mut File) writeln(s string) {
|
|||
C.fputs('\n', f.cfile)
|
||||
}
|
||||
|
||||
|
||||
// mkdir creates a new directory with the specified path.
|
||||
pub fn mkdir(path string) ?bool {
|
||||
if path == '.' {
|
||||
return true
|
||||
}
|
||||
apath := os.realpath(path)
|
||||
$if linux {
|
||||
$if linux_or_macos {
|
||||
$if !android {
|
||||
ret := C.syscall(sys_mkdir, apath.str, 511)
|
||||
if ret == -1 {
|
||||
return error(get_error_msg(C.errno))
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
r := C.mkdir(apath.str, 511)
|
||||
if r == -1 {
|
||||
return error(get_error_msg(C.errno))
|
||||
|
@ -215,7 +218,9 @@ pub fn exec(cmd string) ?Result {
|
|||
|
||||
pub fn symlink(origin, target string) ?bool {
|
||||
res := C.symlink(origin.str, target.str)
|
||||
if res == 0 { return true }
|
||||
if res == 0 {
|
||||
return true
|
||||
}
|
||||
return error(get_error_msg(C.errno))
|
||||
}
|
||||
|
||||
|
@ -223,11 +228,13 @@ pub fn symlink(origin, target string) ?bool {
|
|||
// for example if we have write(7, 4), "07 00 00 00" gets written
|
||||
// write(0x1234, 2) => "34 12"
|
||||
pub fn (f mut File) write_bytes(data voidptr, size int) {
|
||||
$if linux {
|
||||
$if linux_or_macos {
|
||||
$if !android {
|
||||
C.syscall(sys_write, f.fd, data, 1)
|
||||
} $else {
|
||||
C.fwrite(data, 1, size, f.cfile)
|
||||
return
|
||||
}
|
||||
}
|
||||
C.fwrite(data, 1, size, f.cfile)
|
||||
}
|
||||
|
||||
pub fn (f mut File) close() {
|
||||
|
@ -235,12 +242,12 @@ pub fn (f mut File) close() {
|
|||
return
|
||||
}
|
||||
f.opened = false
|
||||
$if linux {
|
||||
//$if linux_or_macos {
|
||||
$if linux_or_macos {
|
||||
$if !android {
|
||||
C.syscall(sys_close, f.fd)
|
||||
return
|
||||
}
|
||||
}
|
||||
C.fflush(f.cfile)
|
||||
C.fclose(f.cfile)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue