xref: /freebsd/bin/pax/tty_subs.c (revision e043f37205ffbde5627ff299ad25cd532f2956f0)
14b88c807SRodney W. Grimes /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
44b88c807SRodney W. Grimes  * Copyright (c) 1992 Keith Muller.
54b88c807SRodney W. Grimes  * Copyright (c) 1992, 1993
64b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
74b88c807SRodney W. Grimes  *
84b88c807SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
94b88c807SRodney W. Grimes  * Keith Muller of the University of California, San Diego.
104b88c807SRodney W. Grimes  *
114b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
124b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
134b88c807SRodney W. Grimes  * are met:
144b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
154b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
164b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
174b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
184b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
19fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
204b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
214b88c807SRodney W. Grimes  *    without specific prior written permission.
224b88c807SRodney W. Grimes  *
234b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
244b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
254b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
264b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
274b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
284b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
294b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
304b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
314b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
324b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
334b88c807SRodney W. Grimes  * SUCH DAMAGE.
344b88c807SRodney W. Grimes  */
354b88c807SRodney W. Grimes 
364b88c807SRodney W. Grimes #include <sys/types.h>
374b88c807SRodney W. Grimes #include <sys/stat.h>
384b88c807SRodney W. Grimes #include <fcntl.h>
394b88c807SRodney W. Grimes #include <stdio.h>
404b88c807SRodney W. Grimes #include <unistd.h>
414b88c807SRodney W. Grimes #include <string.h>
424b88c807SRodney W. Grimes #include "pax.h"
434b88c807SRodney W. Grimes #include "extern.h"
444b88c807SRodney W. Grimes #include <stdarg.h>
454b88c807SRodney W. Grimes 
464b88c807SRodney W. Grimes /*
474b88c807SRodney W. Grimes  * routines that deal with I/O to and from the user
484b88c807SRodney W. Grimes  */
494b88c807SRodney W. Grimes 
504b88c807SRodney W. Grimes #define DEVTTY	  "/dev/tty"      /* device for interactive i/o */
514b88c807SRodney W. Grimes static FILE *ttyoutf = NULL;		/* output pointing at control tty */
524b88c807SRodney W. Grimes static FILE *ttyinf = NULL;		/* input pointing at control tty */
534b88c807SRodney W. Grimes 
544b88c807SRodney W. Grimes /*
554b88c807SRodney W. Grimes  * tty_init()
5646be34b9SKris Kennaway  *	try to open the controlling terminal (if any) for this process. if the
574b88c807SRodney W. Grimes  *	open fails, future ops that require user input will get an EOF
584b88c807SRodney W. Grimes  */
594b88c807SRodney W. Grimes 
604b88c807SRodney W. Grimes int
tty_init(void)614b88c807SRodney W. Grimes tty_init(void)
624b88c807SRodney W. Grimes {
634b88c807SRodney W. Grimes 	int ttyfd;
644b88c807SRodney W. Grimes 
654b88c807SRodney W. Grimes 	if ((ttyfd = open(DEVTTY, O_RDWR)) >= 0) {
664b88c807SRodney W. Grimes 		if ((ttyoutf = fdopen(ttyfd, "w")) != NULL) {
674b88c807SRodney W. Grimes 			if ((ttyinf = fdopen(ttyfd, "r")) != NULL)
684b88c807SRodney W. Grimes 				return(0);
694b88c807SRodney W. Grimes 			(void)fclose(ttyoutf);
704b88c807SRodney W. Grimes 		}
714b88c807SRodney W. Grimes 		(void)close(ttyfd);
724b88c807SRodney W. Grimes 	}
734b88c807SRodney W. Grimes 
744b88c807SRodney W. Grimes 	if (iflag) {
75778766feSKris Kennaway 		paxwarn(1, "Fatal error, cannot open %s", DEVTTY);
764b88c807SRodney W. Grimes 		return(-1);
774b88c807SRodney W. Grimes 	}
784b88c807SRodney W. Grimes 	return(0);
794b88c807SRodney W. Grimes }
804b88c807SRodney W. Grimes 
814b88c807SRodney W. Grimes /*
824b88c807SRodney W. Grimes  * tty_prnt()
834b88c807SRodney W. Grimes  *	print a message using the specified format to the controlling tty
844b88c807SRodney W. Grimes  *	if there is no controlling terminal, just return.
854b88c807SRodney W. Grimes  */
864b88c807SRodney W. Grimes 
874b88c807SRodney W. Grimes void
tty_prnt(const char * fmt,...)88a2e73040SKris Kennaway tty_prnt(const char *fmt, ...)
894b88c807SRodney W. Grimes {
904b88c807SRodney W. Grimes 	va_list ap;
914b88c807SRodney W. Grimes 	if (ttyoutf == NULL)
924b88c807SRodney W. Grimes 		return;
93e79f14faSTim J. Robbins 	va_start(ap, fmt);
944b88c807SRodney W. Grimes 	(void)vfprintf(ttyoutf, fmt, ap);
954b88c807SRodney W. Grimes 	va_end(ap);
964b88c807SRodney W. Grimes 	(void)fflush(ttyoutf);
974b88c807SRodney W. Grimes }
984b88c807SRodney W. Grimes 
994b88c807SRodney W. Grimes /*
1004b88c807SRodney W. Grimes  * tty_read()
1014b88c807SRodney W. Grimes  *	read a string from the controlling terminal if it is open into the
1024b88c807SRodney W. Grimes  *	supplied buffer
1034b88c807SRodney W. Grimes  * Return:
1044b88c807SRodney W. Grimes  *	0 if data was read, -1 otherwise.
1054b88c807SRodney W. Grimes  */
1064b88c807SRodney W. Grimes 
1074b88c807SRodney W. Grimes int
tty_read(char * str,int len)1084b88c807SRodney W. Grimes tty_read(char *str, int len)
1094b88c807SRodney W. Grimes {
110f789b261SWarner Losh 	char *pt;
1114b88c807SRodney W. Grimes 
1124b88c807SRodney W. Grimes 	if ((--len <= 0) || (ttyinf == NULL) || (fgets(str,len,ttyinf) == NULL))
1134b88c807SRodney W. Grimes 		return(-1);
1144b88c807SRodney W. Grimes 	*(str + len) = '\0';
1154b88c807SRodney W. Grimes 
1164b88c807SRodney W. Grimes 	/*
1174b88c807SRodney W. Grimes 	 * strip off that trailing newline
1184b88c807SRodney W. Grimes 	 */
1194b88c807SRodney W. Grimes 	if ((pt = strchr(str, '\n')) != NULL)
1204b88c807SRodney W. Grimes 		*pt = '\0';
1214b88c807SRodney W. Grimes 	return(0);
1224b88c807SRodney W. Grimes }
1234b88c807SRodney W. Grimes 
1244b88c807SRodney W. Grimes /*
125778766feSKris Kennaway  * paxwarn()
126778766feSKris Kennaway  *	write a warning message to stderr. if "set" the exit value of pax
1274b88c807SRodney W. Grimes  *	will be non-zero.
1284b88c807SRodney W. Grimes  */
1294b88c807SRodney W. Grimes 
1304b88c807SRodney W. Grimes void
paxwarn(int set,const char * fmt,...)131778766feSKris Kennaway paxwarn(int set, const char *fmt, ...)
1324b88c807SRodney W. Grimes {
1334b88c807SRodney W. Grimes 	va_list ap;
1344b88c807SRodney W. Grimes 	va_start(ap, fmt);
1354b88c807SRodney W. Grimes 	if (set)
1364b88c807SRodney W. Grimes 		exit_val = 1;
1374b88c807SRodney W. Grimes 	/*
1384b88c807SRodney W. Grimes 	 * when vflag we better ship out an extra \n to get this message on a
1394b88c807SRodney W. Grimes 	 * line by itself
1404b88c807SRodney W. Grimes 	 */
1414b88c807SRodney W. Grimes 	if (vflag && vfpart) {
142b1787decSKris Kennaway 		(void)fflush(listf);
1434b88c807SRodney W. Grimes 		(void)fputc('\n', stderr);
1444b88c807SRodney W. Grimes 		vfpart = 0;
1454b88c807SRodney W. Grimes 	}
1464b88c807SRodney W. Grimes 	(void)fprintf(stderr, "%s: ", argv0);
1474b88c807SRodney W. Grimes 	(void)vfprintf(stderr, fmt, ap);
1484b88c807SRodney W. Grimes 	va_end(ap);
1494b88c807SRodney W. Grimes 	(void)fputc('\n', stderr);
1504b88c807SRodney W. Grimes }
1514b88c807SRodney W. Grimes 
1524b88c807SRodney W. Grimes /*
153778766feSKris Kennaway  * syswarn()
154778766feSKris Kennaway  *	write a warning message to stderr. if "set" the exit value of pax
1554b88c807SRodney W. Grimes  *	will be non-zero.
1564b88c807SRodney W. Grimes  */
1574b88c807SRodney W. Grimes 
1584b88c807SRodney W. Grimes void
syswarn(int set,int errnum,const char * fmt,...)159778766feSKris Kennaway syswarn(int set, int errnum, const char *fmt, ...)
1604b88c807SRodney W. Grimes {
1614b88c807SRodney W. Grimes 	va_list ap;
1624b88c807SRodney W. Grimes 	va_start(ap, fmt);
1634b88c807SRodney W. Grimes 	if (set)
1644b88c807SRodney W. Grimes 		exit_val = 1;
1654b88c807SRodney W. Grimes 	/*
1664b88c807SRodney W. Grimes 	 * when vflag we better ship out an extra \n to get this message on a
1674b88c807SRodney W. Grimes 	 * line by itself
1684b88c807SRodney W. Grimes 	 */
1694b88c807SRodney W. Grimes 	if (vflag && vfpart) {
170b1787decSKris Kennaway 		(void)fflush(listf);
1714b88c807SRodney W. Grimes 		(void)fputc('\n', stderr);
1724b88c807SRodney W. Grimes 		vfpart = 0;
1734b88c807SRodney W. Grimes 	}
1744b88c807SRodney W. Grimes 	(void)fprintf(stderr, "%s: ", argv0);
1754b88c807SRodney W. Grimes 	(void)vfprintf(stderr, fmt, ap);
1764b88c807SRodney W. Grimes 	va_end(ap);
1774b88c807SRodney W. Grimes 
1784b88c807SRodney W. Grimes 	/*
1794b88c807SRodney W. Grimes 	 * format and print the errno
1804b88c807SRodney W. Grimes 	 */
1814b88c807SRodney W. Grimes 	if (errnum > 0)
182778766feSKris Kennaway 		(void)fprintf(stderr, " <%s>", strerror(errnum));
1834b88c807SRodney W. Grimes 	(void)fputc('\n', stderr);
1844b88c807SRodney W. Grimes }
185