tty_subs.c revision 1.2
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/*static char sccsid[] = "from: @(#)tty_subs.c	8.2 (Berkeley) 4/18/94";*/
40static char rcsid[] = "$Id: tty_subs.c,v 1.2 1994/06/13 16:34:32 jtc Exp $";
41#endif /* not lint */
42
43#include <sys/types.h>
44#include <sys/time.h>
45#include <sys/stat.h>
46#include <sys/param.h>
47#include <fcntl.h>
48#include <stdio.h>
49#include <ctype.h>
50#include <errno.h>
51#include <unistd.h>
52#include <stdlib.h>
53#include <string.h>
54#include "pax.h"
55#include "extern.h"
56#if __STDC__
57#include <stdarg.h>
58#else
59#include <varargs.h>
60#endif
61
62/*
63 * routines that deal with I/O to and from the user
64 */
65
66#define DEVTTY          "/dev/tty"      /* device for interactive i/o */
67static FILE *ttyoutf = NULL;		/* output pointing at control tty */
68static FILE *ttyinf = NULL;		/* input pointing at control tty */
69
70/*
71 * tty_init()
72 *	try to open the controlling termina (if any) for this process. if the
73 *	open fails, future ops that require user input will get an EOF
74 */
75
76#if __STDC__
77int
78tty_init(void)
79#else
80int
81tty_init()
82#endif
83{
84	int ttyfd;
85
86        if ((ttyfd = open(DEVTTY, O_RDWR)) >= 0) {
87		if ((ttyoutf = fdopen(ttyfd, "w")) != NULL) {
88			if ((ttyinf = fdopen(ttyfd, "r")) != NULL)
89				return(0);
90			(void)fclose(ttyoutf);
91		}
92		(void)close(ttyfd);
93	}
94
95	if (iflag) {
96		warn(1, "Fatal error, cannot open %s", DEVTTY);
97		return(-1);
98	}
99	return(0);
100}
101
102/*
103 * tty_prnt()
104 *	print a message using the specified format to the controlling tty
105 *	if there is no controlling terminal, just return.
106 */
107
108#if __STDC__
109void
110tty_prnt(char *fmt, ...)
111#else
112void
113tty_prnt(fmt, va_alist)
114	char *fmt;
115	va_dcl
116#endif
117{
118	va_list ap;
119#	if __STDC__
120	va_start(ap, fmt);
121#	else
122	va_start(ap);
123#	endif
124	if (ttyoutf == NULL)
125		return;
126	(void)vfprintf(ttyoutf, fmt, ap);
127	va_end(ap);
128	(void)fflush(ttyoutf);
129}
130
131/*
132 * tty_read()
133 *	read a string from the controlling terminal if it is open into the
134 *	supplied buffer
135 * Return:
136 *	0 if data was read, -1 otherwise.
137 */
138
139#if __STDC__
140int
141tty_read(char *str, int len)
142#else
143int
144tty_read(str, len)
145	char *str;
146	int len;
147#endif
148{
149	register char *pt;
150
151	if ((--len <= 0) || (ttyinf == NULL) || (fgets(str,len,ttyinf) == NULL))
152		return(-1);
153	*(str + len) = '\0';
154
155	/*
156	 * strip off that trailing newline
157	 */
158	if ((pt = strchr(str, '\n')) != NULL)
159		*pt = '\0';
160	return(0);
161}
162
163/*
164 * warn()
165 *	write a warning message to stderr. if "set" the exit value of pax
166 *	will be non-zero.
167 */
168
169#if __STDC__
170void
171warn(int set, char *fmt, ...)
172#else
173void
174warn(set, fmt, va_alist)
175	int set;
176	char *fmt;
177	va_dcl
178#endif
179{
180	va_list ap;
181#	if __STDC__
182	va_start(ap, fmt);
183#	else
184	va_start(ap);
185#	endif
186	if (set)
187		exit_val = 1;
188	/*
189	 * when vflag we better ship out an extra \n to get this message on a
190	 * line by itself
191	 */
192	if (vflag && vfpart) {
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#if __STDC__
209void
210syswarn(int set, int errnum, char *fmt, ...)
211#else
212void
213syswarn(set, errnum, fmt, va_alist)
214	int set;
215	int errnum;
216	char *fmt;
217	va_dcl
218#endif
219{
220	va_list ap;
221#	if __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)fputc('\n', stderr);
234		vfpart = 0;
235	}
236	(void)fprintf(stderr, "%s: ", argv0);
237	(void)vfprintf(stderr, fmt, ap);
238	va_end(ap);
239
240	/*
241	 * format and print the errno
242	 */
243	if (errnum > 0)
244		(void)fprintf(stderr, " <%s>", sys_errlist[errnum]);
245	(void)fputc('\n', stderr);
246}
247