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