// Wait for a Sysfs GPIO interrupt, using the poll(2) library call.
// Requires a set-up GPIO according to https://www.kernel.org/doc/Documentation/gpio/sysfs.txt
// Usage: wait_int /sys/class/gpio/gpioN/value

#include <err.h>
#include <fcntl.h>
#include <poll.h>
#include <unistd.h>

int main(int argc, char *argv[])
{
	int fd, ret;
	struct pollfd pollfd;

	if (argc != 2)
		errx(1, "invalid argument");

	fd = open(argv[1], O_RDONLY);

	if (fd < 0) err(1, "open");
	
	ret = lseek(fd, 0, SEEK_END);
	if (ret < 0) err(1, "lseek");

	{
		char buf[1];
		ret = read(fd, buf, 1);
		if (ret < 0) err(1, "read");
	}

	pollfd.fd = fd;
	pollfd.events = POLLPRI|POLLERR;
	
	ret = poll(&pollfd, 1, -1);

	if (ret < 0) err(1, "poll");

	return 0;
}