xref: /illumos-gate/usr/src/lib/libc/port/gen/isatty.c (revision cfa8d083)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57257d1b4Sraf  * Common Development and Distribution License (the "License").
67257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
237257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*cfa8d083SRobert Mustacchi  * Copyright 2022 Oxide Computer Company
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
29*cfa8d083SRobert Mustacchi /*	  All Rights Reserved   */
307c478bd9Sstevel@tonic-gate 
317257d1b4Sraf #pragma weak _isatty = isatty
327c478bd9Sstevel@tonic-gate 
337257d1b4Sraf #include "lint.h"
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/termio.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <unistd.h>
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * Returns 1 iff file is a tty
417c478bd9Sstevel@tonic-gate  */
427c478bd9Sstevel@tonic-gate int
isatty(int f)437c478bd9Sstevel@tonic-gate isatty(int f)
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate 	struct termio tty;
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	if (ioctl(f, TCGETA, &tty) < 0) {
48*cfa8d083SRobert Mustacchi 		/*
49*cfa8d083SRobert Mustacchi 		 * POSIX stipulates that systems may return an error here and if
50*cfa8d083SRobert Mustacchi 		 * they do, it should either be EBADF or ENOTTY. In general, we
51*cfa8d083SRobert Mustacchi 		 * assume that a driver that receives this ioctl is not going to
52*cfa8d083SRobert Mustacchi 		 * return EBADF say due to an fd that's not open with the right
53*cfa8d083SRobert Mustacchi 		 * mode and will instead return something else. It is possible
54*cfa8d083SRobert Mustacchi 		 * to get many other errors here and we assume anything else
55*cfa8d083SRobert Mustacchi 		 * that's returned means it's not a TTY and thus transform that.
56*cfa8d083SRobert Mustacchi 		 *
57*cfa8d083SRobert Mustacchi 		 * In the past, errno was preserved around this, which was
58*cfa8d083SRobert Mustacchi 		 * incorrect because that meant that on failure there was no way
59*cfa8d083SRobert Mustacchi 		 * to know whether it was meaningful or not. As pretty much
60*cfa8d083SRobert Mustacchi 		 * every other system always returns an errno and there are
61*cfa8d083SRobert Mustacchi 		 * consumers in the wild which assume they'll get something, we
62*cfa8d083SRobert Mustacchi 		 * opt to always return an error.
63*cfa8d083SRobert Mustacchi 		 */
64*cfa8d083SRobert Mustacchi 		if (errno != EBADF) {
65*cfa8d083SRobert Mustacchi 			errno = ENOTTY;
66*cfa8d083SRobert Mustacchi 		}
677c478bd9Sstevel@tonic-gate 		return (0);
687c478bd9Sstevel@tonic-gate 	}
697c478bd9Sstevel@tonic-gate 	return (1);
707c478bd9Sstevel@tonic-gate }
71