|
root / base / usr / src / test / libc-tests / tests / printf-15294.c
printf-15294.c C 93 lines 2.0 KB
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * This file and its contents are supplied under the terms of the
 * Common Development and Distribution License ("CDDL"), version 1.0.
 * You may only use this file in accordance with the terms of version
 * 1.0 of the CDDL.
 *
 * A full copy of the text of the CDDL should have accompanied this
 * source.  A copy of the CDDL is also available via the Internet at
 * http://www.illumos.org/license/CDDL.
 */

/*
 * Copyright 2022 Oxide Computer Company
 * Copyright 2022 OmniOS Community Edition (OmniOSce) Association.
 */

/*
 * Regression test for illumos #15294. A change in isatty(3C) caused printf(3C)
 * to start setting errno on a successful call if the output is not a TTY.
 */

#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);
}