#include <u.h>
#include <libc.h>
int
gphrdial(char* host, char* net, char* service)
{
return host ? dial(netmkaddr(host, net, service), 0, 0, 0) : -1;
}
long
gphrget(int fd, char* selector, char** buf)
{
char *newbuf, *rb, wb[512];
long n, nlast;
n = snprint(wb, 512, "%s\r\n", selector ? selector : "");
if(fd < 0 || write(fd, wb, n) < n) return -1;
n = 0;
if((rb = *buf = malloc(512)) == nil) return -1;
do{
nlast = read(fd, rb, 512);
n += nlast;
if((newbuf = realloc(*buf, n+512)) == nil){
free(*buf);
*buf = nil;
return -1;
}
*buf = newbuf;
rb = *buf+n;
}while(nlast);
close(fd);
return n;
}
|