xref: /original-bsd/bin/stty/util.c (revision c1946e27)
1 /*-
2  * Copyright (c) 1991 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)util.c	5.1 (Berkeley) 05/02/91";
10 #endif /* not lint */
11 
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <sys/ioctl.h>
15 #include <termios.h>
16 #include <unistd.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include "stty.h"
20 #include "extern.h"
21 
22 /*
23  * Gross, but since we're changing the control descriptor from 1 to 0, most
24  * users will be probably be doing "stty > /dev/sometty" by accident.  If 1
25  * and 2 are both ttys, but not the same, assume that 1 was incorrectly
26  * redirected.
27  */
28 void
29 checkredirect()
30 {
31 	struct stat sb1, sb2;
32 
33 	if (isatty(STDOUT_FILENO) && isatty(STDERR_FILENO) &&
34 	    !fstat(STDOUT_FILENO, &sb1) && !fstat(STDERR_FILENO, &sb2) &&
35 	    (sb1.st_rdev != sb2.st_rdev))
36 warn("stdout appears redirected, but stdin is the control descriptor");
37 }
38 
39 #if __STDC__
40 #include <stdarg.h>
41 #else
42 #include <varargs.h>
43 #endif
44 
45 void
46 #if __STDC__
47 err(const char *fmt, ...)
48 #else
49 err(fmt, va_alist)
50 	char *fmt;
51         va_dcl
52 #endif
53 {
54 	va_list ap;
55 #if __STDC__
56 	va_start(ap, fmt);
57 #else
58 	va_start(ap);
59 #endif
60 	(void)fprintf(stderr, "stty: ");
61 	(void)vfprintf(stderr, fmt, ap);
62 	va_end(ap);
63 	(void)fprintf(stderr, "\n");
64 	exit(1);
65 	/* NOTREACHED */
66 }
67 
68 void
69 #if __STDC__
70 warn(const char *fmt, ...)
71 #else
72 warn(fmt, va_alist)
73 	char *fmt;
74         va_dcl
75 #endif
76 {
77 	va_list ap;
78 #if __STDC__
79 	va_start(ap, fmt);
80 #else
81 	va_start(ap);
82 #endif
83 	(void)fprintf(stderr, "stty: ");
84 	(void)vfprintf(stderr, fmt, ap);
85 	va_end(ap);
86 	(void)fprintf(stderr, "\n");
87 }
88