ninenano

Client implementation of the 9P protocol for constrained devices

git clone https://git.8pit.net/ninenano.git

 1/**
 2 * Retrieve value of environment variable using getenv(3) and return
 3 * `EXIT_FAILURE` if the result was a NULL pointer.
 4 *
 5 * @param VAR Name of the variable to store result in.
 6 * @param ENV Name of the environment variable.
 7 */
 8#define GETENV(VAR, ENV) \
 9	do { if (!(VAR = getenv(ENV))) { \
10		fprintf(stderr, "%s is not set or empty\n", ENV); \
11		return EXIT_FAILURE; } \
12	} while (0)
13
14/**
15 * Global socket for 9P protocol connection.
16 */
17static sock_tcp_t psock;
18
19/**
20 * Function used to receive data from a TCP sock connection. This
21 * function is intended to be used as an ::iofunc for ::_9pinit.
22 */
23static ssize_t
24recvfn(void *buf, size_t count)
25{
26	return sock_tcp_read(&psock, buf, count, SOCK_NO_TIMEOUT);
27}
28
29/**
30 * Function used to send data to a TCP server. This function is intended
31 * to be used as an ::iofunc for ::_9pinit.
32 */
33static ssize_t
34sendfn(void *buf, size_t count)
35{
36	return sock_tcp_write(&psock, buf, count);
37}
38
39/**
40 * Returns the amount of currently opened fids.
41 *
42 * @param fids Pointer to the fid table.
43 * @return Amount of open fids in the fid table.
44 */
45static size_t
46cntfids(_9pfid *fids)
47{
48	size_t i, n;
49
50	for (i = n = 0; i < _9P_MAXFIDS; i++)
51		if (fids[i].fid) n++;
52
53	return n;
54}