diff --git a/vlib/builtin/cfns.c.v b/vlib/builtin/cfns.c.v index c6c50daec6..f3bd1fe833 100644 --- a/vlib/builtin/cfns.c.v +++ b/vlib/builtin/cfns.c.v @@ -411,3 +411,6 @@ fn C.pthread_mutex_init(voidptr, voidptr) int fn C.pthread_mutex_lock(voidptr) int fn C.pthread_mutex_unlock(voidptr) int +fn C.read(fd int, buf voidptr, count size_t) int +fn C.write(fd int, buf voidptr, count size_t) int +fn C.close(fd int) int diff --git a/vlib/net/socket.v b/vlib/net/socket.v index baccbc7f0f..c5c4325800 100644 --- a/vlib/net/socket.v +++ b/vlib/net/socket.v @@ -62,12 +62,8 @@ fn C.send() int fn C.recv() int -fn C.read() int - fn C.shutdown() int -fn C.close() int - fn C.ntohs() int fn C.getsockname() int diff --git a/vlib/net/websocket/io.v b/vlib/net/websocket/io.v index e4c6b5494b..4aaac8d934 100644 --- a/vlib/net/websocket/io.v +++ b/vlib/net/websocket/io.v @@ -1,7 +1,5 @@ module websocket -fn C.write() int - fn (mut ws Client) write_to_server(buf voidptr, len int) int { mut bytes_written := 0 ws.write_lock.lock() diff --git a/vlib/os/os.v b/vlib/os/os.v index ac20785688..f8c1e1b319 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -207,7 +207,6 @@ pub fn mv(old, new string) { } fn C.CopyFile(&u32, &u32, int) int -// TODO implement actual cp for linux pub fn cp(old, new string) ?bool { $if windows { w_old := old.replace('/', '\\') @@ -221,8 +220,34 @@ pub fn cp(old, new string) ?bool { return error_with_code('failed to copy $old to $new', int(result)) } } $else { - os.system('cp "$old" "$new"') - return true // TODO make it return true or error when cp for linux is implemented + fp_from := C.open(old.str, C.O_RDONLY) + if fp_from < 0 { // Check if file opened + return error_with_code('cp: failed to open $old', int(fp_from)) + } + fp_to := C.open(new.str, C.O_WRONLY | C.O_CREAT | C.O_TRUNC) + if fp_to < 0 { // Check if file opened (permissions problems ...) + C.close(fp_from) + return error_with_code('cp: failed to write to $new', int(fp_to)) + } + mut buf := [1024]byte + mut count := 0 + for { + // FIXME: use sizeof, bug: 'os__buf' undeclared + //count = C.read(fp_from, buf, sizeof(buf)) + count = C.read(fp_from, buf, 1024) + if count == 0 { + break + } + if C.write(fp_to, buf, count) < 0 { + return error_with_code('cp: failed to write to $new', int(-1)) + } + } + from_attr := C.stat{} + C.stat(old.str, &from_attr) + if C.chmod(new.str, from_attr.st_mode) < 0 { + return error_with_code('failed to set permissions for $new', int(-1)) + } + return true } }