/* probe: how many ptmx fds can we hold; do /dev/ptm/N nodes exist; writev 20-iov behavior */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/uio.h>

int main(void) {
	int fds[1024];
	int i, n = 0, max = 500;
	for (i = 0; i < max; i++) {
		int fd = open("/dev/ptmx", O_RDWR | O_NOCTTY);
		if (fd < 0) { printf("open %d failed: %s\n", i, strerror(0)); break; }
		fds[n++] = fd;
	}
	printf("held %d ptmx fds\n", n);
	struct stat st;
	if (stat("/dev/pts/0", &st) == 0)
		printf("/dev/pts/0: mode=%o rdev=%#lx\n", st.st_mode, (unsigned long)st.st_rdev);
	if (stat("/dev/ptm/0", &st) == 0)
		printf("/dev/ptm/0: mode=%o rdev=%#lx\n", st.st_mode, (unsigned long)st.st_rdev);
	/* writev spray self-test: 20 iovs, all zero length, fd to /dev/null */
	int wfd = open("/dev/null", O_WRONLY);
	struct iovec iov[20];
	unsigned char pat[320];
	memset(pat, 0xA5, sizeof(pat));
	memcpy((void *)iov, pat, sizeof(iov));
	ssize_t r = writev(wfd, iov, 20);
	printf("writev 20 zero-len iovs -> %zd\n", r);
	for (i = 0; i < n; i++) close(fds[i]);
	printf("closed all\n");
	return 0;
}
