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