xref: /dragonfly/bin/pax/tty_subs.c (revision af79c6e5)
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  * @(#)tty_subs.c	8.2 (Berkeley) 4/18/94
38  * $FreeBSD: src/bin/pax/tty_subs.c,v 1.11.2.1 2001/08/01 05:03:12 obrien Exp $
39  * $DragonFly: src/bin/pax/tty_subs.c,v 1.3 2003/09/28 14:39:14 hmp Exp $
40  */
41 
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <fcntl.h>
45 #include <stdio.h>
46 #include <unistd.h>
47 #include <stdlib.h>
48 #include <string.h>
49 #include "pax.h"
50 #include "extern.h"
51 #ifdef __STDC__
52 #include <stdarg.h>
53 #else
54 #include <varargs.h>
55 #endif
56 
57 /*
58  * routines that deal with I/O to and from the user
59  */
60 
61 #define DEVTTY	  "/dev/tty"      /* device for interactive i/o */
62 static FILE *ttyoutf = NULL;		/* output pointing at control tty */
63 static FILE *ttyinf = NULL;		/* input pointing at control tty */
64 
65 /*
66  * tty_init()
67  *	try to open the controlling terminal (if any) for this process. if the
68  *	open fails, future ops that require user input will get an EOF
69  */
70 
71 int
72 tty_init(void)
73 {
74 	int ttyfd;
75 
76 	if ((ttyfd = open(DEVTTY, O_RDWR)) >= 0) {
77 		if ((ttyoutf = fdopen(ttyfd, "w")) != NULL) {
78 			if ((ttyinf = fdopen(ttyfd, "r")) != NULL)
79 				return(0);
80 			(void)fclose(ttyoutf);
81 		}
82 		(void)close(ttyfd);
83 	}
84 
85 	if (iflag) {
86 		paxwarn(1, "Fatal error, cannot open %s", DEVTTY);
87 		return(-1);
88 	}
89 	return(0);
90 }
91 
92 /*
93  * tty_prnt()
94  *	print a message using the specified format to the controlling tty
95  *	if there is no controlling terminal, just return.
96  */
97 
98 void
99 tty_prnt(const char *fmt, ...)
100 {
101 	va_list ap;
102 #	ifdef __STDC__
103 	va_start(ap, fmt);
104 #	else
105 	va_start(ap);
106 #	endif
107 	if (ttyoutf == NULL)
108 		return;
109 	(void)vfprintf(ttyoutf, fmt, ap);
110 	va_end(ap);
111 	(void)fflush(ttyoutf);
112 }
113 
114 /*
115  * tty_read()
116  *	read a string from the controlling terminal if it is open into the
117  *	supplied buffer
118  * Return:
119  *	0 if data was read, -1 otherwise.
120  */
121 
122 int
123 tty_read(char *str, int len)
124 {
125 	register char *pt;
126 
127 	if ((--len <= 0) || (ttyinf == NULL) || (fgets(str,len,ttyinf) == NULL))
128 		return(-1);
129 	*(str + len) = '\0';
130 
131 	/*
132 	 * strip off that trailing newline
133 	 */
134 	if ((pt = strchr(str, '\n')) != NULL)
135 		*pt = '\0';
136 	return(0);
137 }
138 
139 /*
140  * paxwarn()
141  *	write a warning message to stderr. if "set" the exit value of pax
142  *	will be non-zero.
143  */
144 
145 void
146 paxwarn(int set, const char *fmt, ...)
147 {
148 	va_list ap;
149 #	ifdef __STDC__
150 	va_start(ap, fmt);
151 #	else
152 	va_start(ap);
153 #	endif
154 	if (set)
155 		exit_val = 1;
156 	/*
157 	 * when vflag we better ship out an extra \n to get this message on a
158 	 * line by itself
159 	 */
160 	if (vflag && vfpart) {
161 		(void)fflush(listf);
162 		(void)fputc('\n', stderr);
163 		vfpart = 0;
164 	}
165 	(void)fprintf(stderr, "%s: ", argv0);
166 	(void)vfprintf(stderr, fmt, ap);
167 	va_end(ap);
168 	(void)fputc('\n', stderr);
169 }
170 
171 /*
172  * syswarn()
173  *	write a warning message to stderr. if "set" the exit value of pax
174  *	will be non-zero.
175  */
176 
177 void
178 syswarn(int set, int errnum, const char *fmt, ...)
179 {
180 	va_list ap;
181 #	ifdef __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)fflush(listf);
194 		(void)fputc('\n', stderr);
195 		vfpart = 0;
196 	}
197 	(void)fprintf(stderr, "%s: ", argv0);
198 	(void)vfprintf(stderr, fmt, ap);
199 	va_end(ap);
200 
201 	/*
202 	 * format and print the errno
203 	 */
204 	if (errnum > 0)
205 		(void)fprintf(stderr, " <%s>", strerror(errnum));
206 	(void)fputc('\n', stderr);
207 }
208