xref: /netbsd-src/bin/pax/tty_subs.c (revision 326b2259b73e878289ebd80cd9d20bc5aee35e99)
1 /*	$NetBSD: tty_subs.c,v 1.14 2003/08/07 09:05:23 agc Exp $	*/
2 
3 /*-
4  * Copyright (c) 1992, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Keith Muller of the University of California, San Diego.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*-
36  * Copyright (c) 1992 Keith Muller.
37  *
38  * This code is derived from software contributed to Berkeley by
39  * Keith Muller of the University of California, San Diego.
40  *
41  * Redistribution and use in source and binary forms, with or without
42  * modification, are permitted provided that the following conditions
43  * are met:
44  * 1. Redistributions of source code must retain the above copyright
45  *    notice, this list of conditions and the following disclaimer.
46  * 2. Redistributions in binary form must reproduce the above copyright
47  *    notice, this list of conditions and the following disclaimer in the
48  *    documentation and/or other materials provided with the distribution.
49  * 3. All advertising materials mentioning features or use of this software
50  *    must display the following acknowledgement:
51  *	This product includes software developed by the University of
52  *	California, Berkeley and its contributors.
53  * 4. Neither the name of the University nor the names of its contributors
54  *    may be used to endorse or promote products derived from this software
55  *    without specific prior written permission.
56  *
57  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  */
69 
70 #include <sys/cdefs.h>
71 #if defined(__RCSID) && !defined(lint)
72 #if 0
73 static char sccsid[] = "@(#)tty_subs.c	8.2 (Berkeley) 4/18/94";
74 #else
75 __RCSID("$NetBSD: tty_subs.c,v 1.14 2003/08/07 09:05:23 agc Exp $");
76 #endif
77 #endif /* not lint */
78 
79 #include <sys/types.h>
80 #include <sys/time.h>
81 #include <sys/stat.h>
82 #include <sys/param.h>
83 #include <fcntl.h>
84 #include <stdio.h>
85 #include <ctype.h>
86 #include <errno.h>
87 #include <unistd.h>
88 #include <stdlib.h>
89 #include <string.h>
90 #include "pax.h"
91 #include "extern.h"
92 #include <stdarg.h>
93 
94 /*
95  * routines that deal with I/O to and from the user
96  */
97 
98 #define DEVTTY		"/dev/tty"	/* device for interactive i/o */
99 static FILE *ttyoutf = NULL;		/* output pointing at control tty */
100 static FILE *ttyinf = NULL;		/* input pointing at control tty */
101 
102 /*
103  * tty_init()
104  *	try to open the controlling termina (if any) for this process. if the
105  *	open fails, future ops that require user input will get an EOF
106  */
107 
108 int
109 tty_init(void)
110 {
111 	int ttyfd;
112 
113 	if ((ttyfd = open(DEVTTY, O_RDWR)) >= 0) {
114 		if ((ttyoutf = fdopen(ttyfd, "w")) != NULL) {
115 			if ((ttyinf = fdopen(ttyfd, "r")) != NULL)
116 				return(0);
117 			(void)fclose(ttyoutf);
118 		}
119 		(void)close(ttyfd);
120 	}
121 
122 	if (iflag) {
123 		tty_warn(1, "Fatal error, cannot open %s", DEVTTY);
124 		return(-1);
125 	}
126 	return(0);
127 }
128 
129 /*
130  * tty_prnt()
131  *	print a message using the specified format to the controlling tty
132  *	if there is no controlling terminal, just return.
133  */
134 
135 void
136 tty_prnt(const char *fmt, ...)
137 {
138 	va_list ap;
139 	if (ttyoutf == NULL)
140 		return;
141 	va_start(ap, fmt);
142 	(void)vfprintf(ttyoutf, fmt, ap);
143 	va_end(ap);
144 	(void)fflush(ttyoutf);
145 }
146 
147 /*
148  * tty_read()
149  *	read a string from the controlling terminal if it is open into the
150  *	supplied buffer
151  * Return:
152  *	0 if data was read, -1 otherwise.
153  */
154 
155 int
156 tty_read(char *str, int len)
157 {
158 	char *pt;
159 
160 	if ((--len <= 0) || (ttyinf == NULL) || (fgets(str,len,ttyinf) == NULL))
161 		return(-1);
162 	*(str + len) = '\0';
163 
164 	/*
165 	 * strip off that trailing newline
166 	 */
167 	if ((pt = strchr(str, '\n')) != NULL)
168 		*pt = '\0';
169 	return(0);
170 }
171 
172 /*
173  * tty_warn()
174  *	write a warning message to stderr. if "set" the exit value of pax
175  *	will be non-zero.
176  */
177 
178 void
179 tty_warn(int set, const char *fmt, ...)
180 {
181 	va_list ap;
182 	va_start(ap, fmt);
183 	if (set)
184 		exit_val = 1;
185 	/*
186 	 * when vflag we better ship out an extra \n to get this message on a
187 	 * line by itself
188 	 */
189 	if (vflag && vfpart) {
190 		(void)fputc('\n', stderr);
191 		vfpart = 0;
192 	}
193 	(void)fprintf(stderr, "%s: ", argv0);
194 	(void)vfprintf(stderr, fmt, ap);
195 	va_end(ap);
196 	(void)fputc('\n', stderr);
197 }
198 
199 /*
200  * syswarn()
201  *	write a warning message to stderr. if "set" the exit value of pax
202  *	will be non-zero.
203  */
204 
205 void
206 syswarn(int set, int errnum, const char *fmt, ...)
207 {
208 	va_list ap;
209 	va_start(ap, fmt);
210 	if (set)
211 		exit_val = 1;
212 	/*
213 	 * when vflag we better ship out an extra \n to get this message on a
214 	 * line by itself
215 	 */
216 	if (vflag && vfpart) {
217 		(void)fputc('\n', stdout);
218 		vfpart = 0;
219 	}
220 	(void)fprintf(stderr, "%s: ", argv0);
221 	(void)vfprintf(stderr, fmt, ap);
222 	va_end(ap);
223 
224 	/*
225 	 * format and print the errno
226 	 */
227 	if (errnum > 0)
228 		(void)fprintf(stderr, " (%s)", strerror(errnum));
229 	(void)fputc('\n', stderr);
230 }
231