aboutsummaryrefslogtreecommitdiff
path: root/v_windows/v/old/vlib/os/fd.c.v
diff options
context:
space:
mode:
authorIndrajith K L2022-12-03 17:00:20 +0530
committerIndrajith K L2022-12-03 17:00:20 +0530
commitf5c4671bfbad96bf346bd7e9a21fc4317b4959df (patch)
tree2764fc62da58f2ba8da7ed341643fc359873142f /v_windows/v/old/vlib/os/fd.c.v
downloadcli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.tar.gz
cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.tar.bz2
cli-tools-windows-f5c4671bfbad96bf346bd7e9a21fc4317b4959df.zip
Adds most of the toolsHEADmaster
Diffstat (limited to 'v_windows/v/old/vlib/os/fd.c.v')
-rw-r--r--v_windows/v/old/vlib/os/fd.c.v61
1 files changed, 61 insertions, 0 deletions
diff --git a/v_windows/v/old/vlib/os/fd.c.v b/v_windows/v/old/vlib/os/fd.c.v
new file mode 100644
index 0000000..de69e38
--- /dev/null
+++ b/v_windows/v/old/vlib/os/fd.c.v
@@ -0,0 +1,61 @@
+module os
+
+// file descriptor based operations:
+
+// close filedescriptor
+pub fn fd_close(fd int) int {
+ if fd == -1 {
+ return 0
+ }
+ return C.close(fd)
+}
+
+pub fn fd_write(fd int, s string) {
+ if fd == -1 {
+ return
+ }
+ mut sp := s.str
+ mut remaining := s.len
+ for remaining > 0 {
+ written := C.write(fd, sp, remaining)
+ if written < 0 {
+ return
+ }
+ remaining = remaining - written
+ sp = unsafe { sp + written }
+ }
+}
+
+// read from filedescriptor, block until data
+pub fn fd_slurp(fd int) []string {
+ mut res := []string{}
+ if fd == -1 {
+ return res
+ }
+ for {
+ s, b := fd_read(fd, 4096)
+ if b <= 0 {
+ break
+ }
+ res << s
+ }
+ return res
+}
+
+// read from filedescriptor, don't block
+// return [bytestring,nrbytes]
+pub fn fd_read(fd int, maxbytes int) (string, int) {
+ if fd == -1 {
+ return '', 0
+ }
+ unsafe {
+ mut buf := malloc_noscan(maxbytes + 1)
+ nbytes := C.read(fd, buf, maxbytes)
+ if nbytes < 0 {
+ free(buf)
+ return '', nbytes
+ }
+ buf[nbytes] = 0
+ return tos(buf, nbytes), nbytes
+ }
+}