X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?a=blobdiff_plain;f=src%2Fproto.c;fp=src%2Fproto.c;h=7d6466cd0ff0d2d4dcd66e63948391e255a7b683;hb=8f61a4a76806a3788f817d8833ac77a34c76484f;hp=60e22b8fdc5a54f1389fc40c9ffb6c7f4324b7c1;hpb=9aae937f30532ce26191c0380706b9250e0819ca;p=reposerve diff --git a/src/proto.c b/src/proto.c index 60e22b8..7d6466c 100644 --- a/src/proto.c +++ b/src/proto.c @@ -142,3 +142,33 @@ int flist_finalize(struct flist *flist) flist->final_size = newsz; return 0; } + +int read_line(int s, char *buf, int bufsz) +{ + char *ptr = buf; + while(bufsz > 1 && read(s, ptr, 1) > 0 && *ptr++ != '\n'); + *ptr = 0; + return ptr == buf ? -1 : 0; +} + +int read_resp(int s) +{ + int sz, msglen = 0; + char buf[64]; + char *ptr; + + while(msglen < 3 && (sz = read(s, buf + msglen, 3 - msglen)) > 0) { + msglen += sz; + } + if(msglen < 3) return -1; + + if(memcmp(buf, "ERR", 3) == 0) { + return -1; + } + + ptr = buf; + while(read(s, ptr, 1) > 0 && *ptr++ != '\n'); + *ptr = 0; + + return atoi(buf); +}