diff options
author | Erlang/OTP <[email protected]> | 2009-11-20 14:54:40 +0000 |
---|---|---|
committer | Erlang/OTP <[email protected]> | 2009-11-20 14:54:40 +0000 |
commit | 84adefa331c4159d432d22840663c38f155cd4c1 (patch) | |
tree | bff9a9c66adda4df2106dfd0e5c053ab182a12bd /system/doc/tutorial/erl_comm.c | |
download | otp-84adefa331c4159d432d22840663c38f155cd4c1.tar.gz otp-84adefa331c4159d432d22840663c38f155cd4c1.tar.bz2 otp-84adefa331c4159d432d22840663c38f155cd4c1.zip |
The R13B03 release.OTP_R13B03
Diffstat (limited to 'system/doc/tutorial/erl_comm.c')
-rw-r--r-- | system/doc/tutorial/erl_comm.c | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/system/doc/tutorial/erl_comm.c b/system/doc/tutorial/erl_comm.c new file mode 100644 index 0000000000..303c6dc170 --- /dev/null +++ b/system/doc/tutorial/erl_comm.c @@ -0,0 +1,52 @@ +/* erl_comm.c */ + +typedef unsigned char byte; + +read_cmd(byte *buf) +{ + int len; + + if (read_exact(buf, 2) != 2) + return(-1); + len = (buf[0] << 8) | buf[1]; + return read_exact(buf, len); +} + +write_cmd(byte *buf, int len) +{ + byte li; + + li = (len >> 8) & 0xff; + write_exact(&li, 1); + + li = len & 0xff; + write_exact(&li, 1); + + return write_exact(buf, len); +} + +read_exact(byte *buf, int len) +{ + int i, got=0; + + do { + if ((i = read(0, buf+got, len-got)) <= 0) + return(i); + got += i; + } while (got<len); + + return(len); +} + +write_exact(byte *buf, int len) +{ + int i, wrote = 0; + + do { + if ((i = write(1, buf+wrote, len-wrote)) <= 0) + return (i); + wrote += i; + } while (wrote<len); + + return (len); +} |