113675Sdyson/*-
213675Sdyson * Copyright (c) 1991, 1993, 1994
313675Sdyson *	The Regents of the University of California.  All rights reserved.
413675Sdyson *
513675Sdyson * Redistribution and use in source and binary forms, with or without
613675Sdyson * modification, are permitted provided that the following conditions
713675Sdyson * are met:
813675Sdyson * 1. Redistributions of source code must retain the above copyright
913675Sdyson *    notice, this list of conditions and the following disclaimer.
1013675Sdyson * 2. Redistributions in binary form must reproduce the above copyright
1113675Sdyson *    notice, this list of conditions and the following disclaimer in the
1213675Sdyson *    documentation and/or other materials provided with the distribution.
1313675Sdyson * 4. Neither the name of the University nor the names of its contributors
1413675Sdyson *    may be used to endorse or promote products derived from this software
1513675Sdyson *    without specific prior written permission.
1614037Sdyson *
1713675Sdyson * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1813675Sdyson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1950477Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2013675Sdyson * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2113675Sdyson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2213675Sdyson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2313675Sdyson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2413675Sdyson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2513675Sdyson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2613675Sdyson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2713675Sdyson * SUCH DAMAGE.
2813675Sdyson */
2913907Sdyson
3013907Sdyson#ifndef lint
3113907Sdyson#if 0
3213907Sdysonstatic char sccsid[] = "@(#)util.c	8.3 (Berkeley) 4/2/94";
3313907Sdyson#endif
3413907Sdyson#endif /* not lint */
3513907Sdyson#include <sys/cdefs.h>
3613907Sdyson__FBSDID("$FreeBSD$");
3713907Sdyson
3813907Sdyson#include <sys/types.h>
3913913Sdyson#include <sys/stat.h>
4013907Sdyson
4113907Sdyson#include <err.h>
4213907Sdyson#include <stdio.h>
4313907Sdyson#include <stdlib.h>
4413907Sdyson#include <unistd.h>
4513907Sdyson
4613907Sdyson#include "stty.h"
4713907Sdyson#include "extern.h"
4813913Sdyson
4913907Sdyson/*
5013907Sdyson * Gross, but since we're changing the control descriptor from 1 to 0, most
5113907Sdyson * users will be probably be doing "stty > /dev/sometty" by accident.  If 1
5213675Sdyson * and 2 are both ttys, but not the same, assume that 1 was incorrectly
5313675Sdyson * redirected.
5424131Sbde */
5513675Sdysonvoid
5613675Sdysoncheckredirect(void)
5724206Sbde{
5891372Salfred	struct stat sb1, sb2;
5976166Smarkm
6076827Salfred	if (isatty(STDOUT_FILENO) && isatty(STDERR_FILENO) &&
6124206Sbde	    !fstat(STDOUT_FILENO, &sb1) && !fstat(STDERR_FILENO, &sb2) &&
6213675Sdyson	    (sb1.st_rdev != sb2.st_rdev))
6391968Salfredwarnx("stdout appears redirected, but stdin is the control descriptor");
6429356Speter}
6570834Swollman