#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <sys/debug.h>
#include <sys/types.h>
#include "common/openpty.c"
static int failures;
static void
fail(const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
(void) vfprintf(stderr, fmt, va);
va_end(va);
failures++;
}
static void
test_file(FILE *fp, char *tag, int e_n, int e_err)
{
int n, err;
errno = 0;
n = fprintf(fp, "%s", tag);
err = errno;
if (n != e_n)
fail("%s return value was %d, expected %d\n", tag, n, e_n);
if (err != e_err)
fail("%s errno value was %d, expected %d\n", tag, errno, e_err);
}
int
main(void)
{
int mfd, sfd;
FILE *fp, *sfp;
if (!openpty(&mfd, &sfd))
errx(EXIT_FAILURE, "failed to open a pty");
if (isatty(sfd) != 1)
errx(EXIT_FAILURE, "subsidiary PTY fd somehow isn't a TTY!");
fp = tmpfile();
if (fp == NULL)
errx(EXIT_FAILURE, "could not create temporary file");
if ((sfp = fdopen(sfd, "w")) == NULL)
errx(EXIT_FAILURE, "could not fdopen subsidiary PTY fd");
test_file(fp, "test non-PTY", sizeof ("test non-PTY") - 1, 0);
test_file(sfp, "test PTY", sizeof ("test PTY") - 1, 0);
VERIFY0(fclose(fp));
VERIFY0(fclose(sfp));
VERIFY0(close(mfd));
return (failures);
}