date and time created 91/05/02 13:34:52 by bostic
authorKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Fri, 3 May 1991 04:34:52 +0000 (20:34 -0800)
committerKeith Bostic <bostic@ucbvax.Berkeley.EDU>
Fri, 3 May 1991 04:34:52 +0000 (20:34 -0800)
SCCS-vsn: bin/stty/util.c 5.1

usr/src/bin/stty/util.c [new file with mode: 0644]

diff --git a/usr/src/bin/stty/util.c b/usr/src/bin/stty/util.c
new file mode 100644 (file)
index 0000000..a2d8303
--- /dev/null
@@ -0,0 +1,87 @@
+/*-
+ * Copyright (c) 1991 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * %sccs.include.redist.c%
+ */
+
+#ifndef lint
+static char sccsid[] = "@(#)util.c     5.1 (Berkeley) %G%";
+#endif /* not lint */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/ioctl.h>
+#include <termios.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "stty.h"
+#include "extern.h"
+
+/*
+ * Gross, but since we're changing the control descriptor from 1 to 0, most
+ * users will be probably be doing "stty > /dev/sometty" by accident.  If 1
+ * and 2 are both ttys, but not the same, assume that 1 was incorrectly
+ * redirected.
+ */
+void
+checkredirect()
+{
+       struct stat sb1, sb2;
+
+       if (isatty(STDOUT_FILENO) && isatty(STDERR_FILENO) &&
+           !fstat(STDOUT_FILENO, &sb1) && !fstat(STDERR_FILENO, &sb2) &&
+           (sb1.st_rdev != sb2.st_rdev))
+warn("stdout appears redirected, but stdin is the control descriptor");
+}
+
+#if __STDC__
+#include <stdarg.h>
+#else
+#include <varargs.h>
+#endif
+
+void
+#if __STDC__
+err(const char *fmt, ...)
+#else
+err(fmt, va_alist)
+       char *fmt;
+        va_dcl
+#endif
+{
+       va_list ap;
+#if __STDC__
+       va_start(ap, fmt);
+#else
+       va_start(ap);
+#endif
+       (void)fprintf(stderr, "stty: ");
+       (void)vfprintf(stderr, fmt, ap);
+       va_end(ap);
+       (void)fprintf(stderr, "\n");
+       exit(1);
+       /* NOTREACHED */
+}
+
+void
+#if __STDC__
+warn(const char *fmt, ...)
+#else
+warn(fmt, va_alist)
+       char *fmt;
+        va_dcl
+#endif
+{
+       va_list ap;
+#if __STDC__
+       va_start(ap, fmt);
+#else
+       va_start(ap);
+#endif
+       (void)fprintf(stderr, "stty: ");
+       (void)vfprintf(stderr, fmt, ap);
+       va_end(ap);
+       (void)fprintf(stderr, "\n");
+}