1 /* 2 * This file and its contents are supplied under the terms of the 3 * Common Development and Distribution License ("CDDL"), version 1.0. 4 * You may only use this file in accordance with the terms of version 5 * 1.0 of the CDDL. 6 * 7 * A full copy of the text of the CDDL should have accompanied this 8 * source. A copy of the CDDL is also available via the Internet at 9 * http://www.illumos.org/license/CDDL. 10 */ 11 12 /* 13 * Copyright 2022 Oxide Computer Company 14 * Copyright 2022 OmniOS Community Edition (OmniOSce) Association. 15 */ 16 17 /* 18 * Regression test for illumos #15294. A change in isatty(3C) caused printf(3C) 19 * to start setting errno on a successful call if the output is not a TTY. 20 */ 21 22 #include <err.h> 23 #include <errno.h> 24 #include <fcntl.h> 25 #include <stdarg.h> 26 #include <stdbool.h> 27 #include <stdio.h> 28 #include <stdlib.h> 29 #include <string.h> 30 #include <strings.h> 31 #include <unistd.h> 32 #include <sys/debug.h> 33 #include <sys/types.h> 34 35 #include "common/openpty.c" 36 37 static int failures; 38 39 static void 40 fail(const char *fmt, ...) 41 { 42 va_list va; 43 44 va_start(va, fmt); 45 (void) vfprintf(stderr, fmt, va); 46 va_end(va); 47 48 failures++; 49 } 50 51 static void 52 test_file(FILE *fp, char *tag, int e_n, int e_err) 53 { 54 int n, err; 55 56 errno = 0; 57 n = fprintf(fp, "%s", tag); 58 err = errno; 59 60 if (n != e_n) 61 fail("%s return value was %d, expected %d\n", tag, n, e_n); 62 if (err != e_err) 63 fail("%s errno value was %d, expected %d\n", tag, errno, e_err); 64 } 65 66 int 67 main(void) 68 { 69 int mfd, sfd; 70 FILE *fp, *sfp; 71 72 if (!openpty(&mfd, &sfd)) 73 errx(EXIT_FAILURE, "failed to open a pty"); 74 if (isatty(sfd) != 1) 75 errx(EXIT_FAILURE, "subsidiary PTY fd somehow isn't a TTY!"); 76 77 fp = tmpfile(); 78 if (fp == NULL) 79 errx(EXIT_FAILURE, "could not create temporary file"); 80 81 if ((sfp = fdopen(sfd, "w")) == NULL) 82 errx(EXIT_FAILURE, "could not fdopen subsidiary PTY fd"); 83 84 test_file(fp, "test non-PTY", sizeof ("test non-PTY") - 1, 0); 85 test_file(sfp, "test PTY", sizeof ("test PTY") - 1, 0); 86 87 VERIFY0(fclose(fp)); 88 VERIFY0(fclose(sfp)); 89 VERIFY0(close(mfd)); 90 91 return (failures); 92 } 93