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