android: fix os.create and builtin

pull/3338/head
Delyan Angelov 2020-01-05 21:13:35 +02:00 committed by Alexander Medvednikov
parent c24a1b3786
commit 8053175ead
5 changed files with 100 additions and 88 deletions

View File

@ -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

View File

@ -22,15 +22,14 @@ fn C.puts(charptr)
*/
pub fn println(s string) {
/*
$if linux {
snl := s + '\n'
C.syscall(sys_write, stdout_value, snl.str, s.len+1)
$if !android {
snl := s + '\n'
C.syscall(/* sys_write */ 1, /* stdout_value */ 1, snl.str, s.len+1)
return
}
}
$else {
*/
C.printf('%.*s\n', s.len, s.str)
//}
C.printf('%.*s\n', s.len, s.str)
}
fn print_backtrace_skipping_top_frames_msvc(skipframes int) bool {

View File

@ -29,11 +29,15 @@ fn (p mut Parser) comp_time() {
}
if not {
p.genln('#ifndef $ifdef_name')
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')
}

View File

@ -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]

View File

@ -9,11 +9,12 @@ pub const (
const (
stdin_value = 0
stdout_value = 1
stderr_value = 2
stderr_value = 2
)
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,57 +68,58 @@ pub fn is_dir(path string) bool {
}
*/
pub fn open(path string) ?File {
$if linux {
//$if linux_or_macos {
fd := C.syscall(sys_open, path.str, 511)
if fd == -1 {
return error('failed to open file "$path"')
}
return File{
fd: fd
opened: true
}
}
$else {
cpath := path.str
file := File{
cfile: C.fopen(charptr(cpath), 'rb')
opened: true
}
if isnil(file.cfile) {
return error('failed to open file "$path"')
}
return file
}
}
pub fn open(path string) ?File {
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"')
}
return File{
fd: fd
opened: true
}
}
}
cpath := path.str
file = File{
cfile: C.fopen(charptr(cpath), 'rb')
opened: true
}
if isnil(file.cfile) {
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')
/*
$if macos {
fd = C.syscall(sys_open_nocancel, path.str, 0x601, 0x1b6)
mut fd := 0
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(398, path.str, 0x601, 0x1b6)
}
$if linux {
fd = C.syscall(sys_creat, path.str, 511)
}
if fd == -1 {
return error('failed to create file "$path"')
}
file = File{
fd: fd
opened: true
}
return file
}
$else {
*/
fd = C.syscall(sys_creat, path.str, 511)
//}
//println('fd=$fd')
if fd == -1 {
return error('failed to create file "$path"')
}
return File{
fd: fd
opened: true
}
}
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 {
C.syscall(sys_write, f.fd, s.str, s.len)
return
$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 {
snl := s + '\n'
C.syscall(sys_write, f.fd, snl.str, snl.len)
return
$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,19 +168,20 @@ 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 {
ret := C.syscall(sys_mkdir, apath.str, 511)
if ret == -1 {
return error(get_error_msg(C.errno))
$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
}
return true
}
r := C.mkdir(apath.str, 511)
if r == -1 {
@ -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 {
C.syscall(sys_write, f.fd, data, 1)
} $else {
C.fwrite(data, 1, size, f.cfile)
$if linux_or_macos {
$if !android {
C.syscall(sys_write, f.fd, data, 1)
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 {
C.syscall(sys_close, f.fd)
return
$if linux_or_macos {
$if !android {
C.syscall(sys_close, f.fd)
return
}
}
C.fflush(f.cfile)
C.fclose(f.cfile)
}