1 /* $OpenBSD: tty_subs.c,v 1.5 1997/07/25 18:58:39 mickey Exp $ */
2 /* $NetBSD: tty_subs.c,v 1.5 1995/03/21 09:07:52 cgd Exp $ */
5 * Copyright (c) 1992 Keith Muller.
6 * Copyright (c) 1992, 1993
7 * The Regents of the University of California. All rights reserved.
9 * This code is derived from software contributed to Berkeley by
10 * Keith Muller of the University of California, San Diego.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 static char sccsid[] = "@(#)tty_subs.c 8.2 (Berkeley) 4/18/94";
45 static char rcsid[] = "$OpenBSD: tty_subs.c,v 1.5 1997/07/25 18:58:39 mickey Exp $";
49 #include <sys/types.h>
52 #include <sys/param.h>
68 * routines that deal with I/O to and from the user
71 #define DEVTTY "/dev/tty" /* device for interactive i/o */
72 static FILE *ttyoutf = NULL; /* output pointing at control tty */
73 static FILE *ttyinf = NULL; /* input pointing at control tty */
77 * try to open the controlling termina (if any) for this process. if the
78 * open fails, future ops that require user input will get an EOF
91 if ((ttyfd = open(DEVTTY, O_RDWR)) >= 0) {
92 if ((ttyoutf = fdopen(ttyfd, "w")) != NULL) {
93 if ((ttyinf = fdopen(ttyfd, "r")) != NULL)
95 (void)fclose(ttyoutf);
101 paxwarn(1, "Fatal error, cannot open %s", DEVTTY);
109 * print a message using the specified format to the controlling tty
110 * if there is no controlling terminal, just return.
115 tty_prnt(char *fmt, ...)
118 tty_prnt(fmt, va_alist)
131 (void)vfprintf(ttyoutf, fmt, ap);
133 (void)fflush(ttyoutf);
138 * read a string from the controlling terminal if it is open into the
141 * 0 if data was read, -1 otherwise.
146 tty_read(char *str, int len)
156 if ((--len <= 0) || (ttyinf == NULL) || (fgets(str,len,ttyinf) == NULL))
161 * strip off that trailing newline
163 if ((pt = strchr(str, '\n')) != NULL)
170 * write a warning message to stderr. if "set" the exit value of pax
176 paxwarn(int set, char *fmt, ...)
179 paxwarn(set, fmt, va_alist)
194 * when vflag we better ship out an extra \n to get this message on a
197 if (vflag && vfpart) {
198 (void)fputc('\n', stderr);
201 (void)fprintf(stderr, "%s: ", argv0);
202 (void)vfprintf(stderr, fmt, ap);
204 (void)fputc('\n', stderr);
209 * write a warning message to stderr. if "set" the exit value of pax
215 syswarn(int set, int errnum, char *fmt, ...)
218 syswarn(set, errnum, fmt, va_alist)
234 * when vflag we better ship out an extra \n to get this message on a
237 if (vflag && vfpart) {
238 (void)fputc('\n', stderr);
241 (void)fprintf(stderr, "%s: ", argv0);
242 (void)vfprintf(stderr, fmt, ap);
246 * format and print the errno
249 (void)fprintf(stderr, " <%s>", strerror(errnum));
250 (void)fputc('\n', stderr);