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