xref: /illumos-gate/usr/src/lib/libc/port/gen/isatty.c (revision cfa8d083)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  * Copyright 2022 Oxide Computer Company
26  */
27 
28 /*	Copyright (c) 1988 AT&T	*/
29 /*	  All Rights Reserved   */
30 
31 #pragma weak _isatty = isatty
32 
33 #include "lint.h"
34 #include <sys/types.h>
35 #include <sys/termio.h>
36 #include <errno.h>
37 #include <unistd.h>
38 
39 /*
40  * Returns 1 iff file is a tty
41  */
42 int
isatty(int f)43 isatty(int f)
44 {
45 	struct termio tty;
46 
47 	if (ioctl(f, TCGETA, &tty) < 0) {
48 		/*
49 		 * POSIX stipulates that systems may return an error here and if
50 		 * they do, it should either be EBADF or ENOTTY. In general, we
51 		 * assume that a driver that receives this ioctl is not going to
52 		 * return EBADF say due to an fd that's not open with the right
53 		 * mode and will instead return something else. It is possible
54 		 * to get many other errors here and we assume anything else
55 		 * that's returned means it's not a TTY and thus transform that.
56 		 *
57 		 * In the past, errno was preserved around this, which was
58 		 * incorrect because that meant that on failure there was no way
59 		 * to know whether it was meaningful or not. As pretty much
60 		 * every other system always returns an errno and there are
61 		 * consumers in the wild which assume they'll get something, we
62 		 * opt to always return an error.
63 		 */
64 		if (errno != EBADF) {
65 			errno = ENOTTY;
66 		}
67 		return (0);
68 	}
69 	return (1);
70 }
71