diff options
| author | Cliff Click <cliffc@acm.org> | 2026-08-10 11:11:18 -0700 |
|---|---|---|
| committer | Cliff Click <cliffc@acm.org> | 2026-08-10 15:21:17 -0700 |
| commit | 28209703c72fb7e9955961dbdb84e24a0f871acd (patch) | |
| tree | 8e4df2c1a5e6811696b454bbc4d7cd1cc30e42e7 /src/main/smp/sys/io.smp | |
| parent | e57bdd315a7918a0d130e1c95648ca4a1b4f08ed (diff) | |
| download | Simple-linear-chapter25.tar.gz Simple-linear-chapter25.zip | |
Chapter 25: Moduleslinear-chapter25
Diffstat (limited to 'src/main/smp/sys/io.smp')
| -rw-r--r-- | src/main/smp/sys/io.smp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/main/smp/sys/io.smp b/src/main/smp/sys/io.smp new file mode 100644 index 00000000..b94e89fa --- /dev/null +++ b/src/main/smp/sys/io.smp @@ -0,0 +1,32 @@ +// -*- mode: java; -*- + +// Simple I/O Library + +// Write byte array to stdout +val p = { u8[~] str -> + i64 ptr = str; // cast array base to i64 + return libc.write(1,ptr,str#); +}; + +val p_noInline = { u8[~] str -> + i64 ptr = str; // cast array base to i64 + return libc.write(1,ptr,str#); +}; + +// Return stdin as a u8[]. +val stdin = { -> + var buf = new u8[8*1024]; + i64 ptr = buf; + int len = 0; + while( true ) { + int rez = libc.read(0,ptr,buf#); + if( rez < 0 ) libc._exit(libc.errno); + len += rez; + if( rez < buf# ) + break; + libc._exit(-2); // Need to implement for more than 8K input + } + // Compress + var dst = new u8[len]; + return Ary.copy(dst,0,buf,0,len); +}; |
