/*
 * df5063_mknod.c — mknod(2) + open(2) probe for DF-5063.
 * The DragonFly guest has no mknod(1) binary, so call the syscall.
 *
 * usage: df5063_mknod <path> <major> <minor>   -- create node
 *        df5063_mknod -o <path>                -- open O_RDONLY|O_NONBLOCK,
 *                                                 read 64 bytes, print them
 */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main(int argc, char **argv)
{
	if (argc == 4) {
		dev_t dev = makedev(atoi(argv[2]), atoi(argv[3]));
		if (mknod(argv[1], S_IFCHR | 0644, dev) < 0) {
			perror("mknod");
			return 1;
		}
		printf("created %s major=%s minor=%s (0x%x)\n",
		    argv[1], argv[2], argv[3], (unsigned)atoi(argv[3]));
		return 0;
	}
	if (argc == 3 && strcmp(argv[1], "-o") == 0) {
		char buf[64];
		int fd, n;
		fd = open(argv[2], O_RDONLY | O_NONBLOCK);
		if (fd < 0) {
			perror("open");
			return 1;
		}
		n = read(fd, buf, sizeof(buf));
		if (n < 0) {
			perror("read");
			close(fd);
			return 2;
		}
		printf("open+read OK, %d bytes: [%.*s]\n", n, n, buf);
		close(fd);
		return 0;
	}
	fprintf(stderr, "usage: %s path major minor | -o path\n", argv[0]);
	return 3;
}
