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