insomnia

A frontend for the hii IRC client

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

 1#include <err.h>
 2#include <stdio.h>
 3#include <stdlib.h>
 4#include <string.h>
 5#include <unistd.h>
 6
 7#include <sys/socket.h>
 8#include <sys/types.h>
 9#include <sys/un.h>
10
11static struct sockaddr_un addr;
12
13int
14main(int argc, char **argv)
15{
16	int sfd;
17	ssize_t ret;
18	char *path, buf[BUFSIZ];
19
20#ifdef __OpenBSD__
21	if (pledge("stdio unix", NULL) == -1)
22		err(EXIT_FAILURE, "pledge failed");
23#endif
24
25	if (argc <= 1) {
26		fprintf(stderr, "USAGE: %s PATH\n", argv[0]);
27		return EXIT_FAILURE;
28	}
29
30	path = argv[1];
31	if (strlen(path) >= sizeof(addr.sun_path)) {
32		fprintf(stderr, "The given path is too long\n");
33		return EXIT_FAILURE;
34	}
35
36	if ((sfd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
37		err(EXIT_FAILURE, "socket failed");
38
39	addr.sun_family = AF_UNIX;
40	strncpy(addr.sun_path, path, sizeof(addr.sun_path) - 1);
41
42	if (connect(sfd, (struct sockaddr*)&addr,
43			sizeof(struct sockaddr_un)) == -1)
44		err(EXIT_FAILURE, "connect failed");
45
46	while ((ret = read(sfd, buf, sizeof(buf))) > 0) {
47		if (write(STDOUT_FILENO, buf, ret) == -1)
48			err(EXIT_FAILURE, "write failed");
49	}
50	if (ret == -1)
51		err(EXIT_FAILURE, "read filed");
52
53	return EXIT_SUCCESS;
54}