]>
git.saurik.com Git - apple/file_cmds.git/blob - pax/tty_subs.c
5a78a71252b4d9bdedaf2306cb18b5dade0226e1
1 /* $OpenBSD: tty_subs.c,v 1.12 2003/06/02 23:32:09 millert 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. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 static const char sccsid
[] = "@(#)tty_subs.c 8.2 (Berkeley) 4/18/94";
41 static const char rcsid
[] = "$OpenBSD: tty_subs.c,v 1.12 2003/06/02 23:32:09 millert Exp $";
45 #include <sys/types.h>
48 #include <sys/param.h>
60 * routines that deal with I/O to and from the user
63 #define DEVTTY "/dev/tty" /* device for interactive i/o */
64 static FILE *ttyoutf
= NULL
; /* output pointing at control tty */
65 static FILE *ttyinf
= NULL
; /* input pointing at control tty */
69 * try to open the controlling terminal (if any) for this process. if the
70 * open fails, future ops that require user input will get an EOF
78 if ((ttyfd
= open(DEVTTY
, O_RDWR
)) >= 0) {
79 if ((ttyoutf
= fdopen(ttyfd
, "w")) != NULL
) {
80 if ((ttyinf
= fdopen(ttyfd
, "r")) != NULL
)
82 (void)fclose(ttyoutf
);
88 paxwarn(1, "Fatal error, cannot open %s", DEVTTY
);
96 * print a message using the specified format to the controlling tty
97 * if there is no controlling terminal, just return.
101 tty_prnt(const char *fmt
, ...)
107 (void)vfprintf(ttyoutf
, fmt
, ap
);
109 (void)fflush(ttyoutf
);
114 * read a string from the controlling terminal if it is open into the
117 * 0 if data was read, -1 otherwise.
121 tty_read(char *str
, int len
)
125 if ((--len
<= 0) || (ttyinf
== NULL
) || (fgets(str
,len
,ttyinf
) == NULL
))
130 * strip off that trailing newline
132 if ((pt
= strchr(str
, '\n')) != NULL
)
139 * write a warning message to stderr. if "set" the exit value of pax
144 paxwarn(int set
, const char *fmt
, ...)
149 if (set
&& (pax_invalid_action
==0))
152 * when vflag we better ship out an extra \n to get this message on a
155 if (vflag
&& vfpart
) {
157 (void)fputc('\n', stderr
);
160 (void)fprintf(stderr
, "%s: ", argv0
);
161 (void)vfprintf(stderr
, fmt
, ap
);
163 (void)fputc('\n', stderr
);
168 * write a warning message to stderr. if "set" the exit value of pax
173 syswarn(int set
, int errnum
, const char *fmt
, ...)
181 * when vflag we better ship out an extra \n to get this message on a
184 if (vflag
&& vfpart
) {
186 (void)fputc('\n', stderr
);
189 (void)fprintf(stderr
, "%s: ", argv0
);
190 (void)vfprintf(stderr
, fmt
, ap
);
194 * format and print the errno
197 (void)fprintf(stderr
, " <%s>", strerror(errnum
));
198 (void)fputc('\n', stderr
);