summaryrefslogtreecommitdiff
path: root/sys/src/cmd/upas/imap4d/csquery.c
blob: 44a86c893f0020f6a72d7649a79aeef29cacdd6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <u.h>
#include <libc.h>

/*
 *  query the connection server
 */
char*
csquery(char *attr, char *val, char *rattr)
{
	char token[64 + 4], buf[256], *p, *sp;
	int fd, n;

	if(val == nil || val[0] == 0)
		return nil;
	fd = open("/net/cs", ORDWR);
	if(fd < 0)
		return nil;
	fprint(fd, "!%s=%s", attr, val);
	seek(fd, 0, 0);
	snprint(token, sizeof token, "%s=", rattr);
	for(;;){
		n = read(fd, buf, sizeof buf - 1);
		if(n <= 0)
			break;
		buf[n] = 0;
		p = strstr(buf, token);
		if(p != nil && (p == buf || p[-1] == 0)){
			close(fd);
			sp = strchr(p, ' ');
			if(sp)
				*sp = 0;
			p = strchr(p, '=');
			if(p == nil)
				return nil;
			return strdup(p + 1);
		}
	}
	close(fd);
	return nil;
}