#include <sys/types.h>
#include <sys/ioctl.h>
#include <sys/time.h>

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

int
main(int argc, char **argv)
{
	int arg;
	int fd;
	int lastval = -1, val;
	struct timeval tv;

	if (argv[1] == NULL)
		errx(1, "Must give device name");

	if ((fd = open(argv[1], O_RDWR)) == -1)
		errx(1, "Couldn't open %s", argv[1]);

	while (1) {
		if (ioctl(fd, TIOCMGET, &arg) != 0)
			errx(2, "TIOCMGET failed");
		val = (arg & TIOCM_CD) ? 1 : 0;
		if (val != lastval) {
			if (gettimeofday(&tv, NULL) != 0)
				errx(2, "gettimeofday failed");
			printf("%f %d\n", tv.tv_sec+1E-6*tv.tv_usec, val);
			lastval = val;
		}
		usleep(1000);
	}
}
