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 #ifndef lint 37c9a8d1f4SPhilippe Charnier #if 0 38c9a8d1f4SPhilippe Charnier static char sccsid[] = "@(#)tty_subs.c 8.2 (Berkeley) 4/18/94"; 39c9a8d1f4SPhilippe Charnier #endif 404b88c807SRodney W. Grimes #endif /* not lint */ 412749b141SDavid E. O'Brien #include <sys/cdefs.h> 422749b141SDavid E. O'Brien __FBSDID("$FreeBSD$"); 434b88c807SRodney W. Grimes 444b88c807SRodney W. Grimes #include <sys/types.h> 454b88c807SRodney W. Grimes #include <sys/stat.h> 464b88c807SRodney W. Grimes #include <fcntl.h> 474b88c807SRodney W. Grimes #include <stdio.h> 484b88c807SRodney W. Grimes #include <unistd.h> 494b88c807SRodney W. Grimes #include <string.h> 504b88c807SRodney W. Grimes #include "pax.h" 514b88c807SRodney W. Grimes #include "extern.h" 524b88c807SRodney W. Grimes #include <stdarg.h> 534b88c807SRodney W. Grimes 544b88c807SRodney W. Grimes /* 554b88c807SRodney W. Grimes * routines that deal with I/O to and from the user 564b88c807SRodney W. Grimes */ 574b88c807SRodney W. Grimes 584b88c807SRodney W. Grimes #define DEVTTY "/dev/tty" /* device for interactive i/o */ 594b88c807SRodney W. Grimes static FILE *ttyoutf = NULL; /* output pointing at control tty */ 604b88c807SRodney W. Grimes static FILE *ttyinf = NULL; /* input pointing at control tty */ 614b88c807SRodney W. Grimes 624b88c807SRodney W. Grimes /* 634b88c807SRodney W. Grimes * tty_init() 6446be34b9SKris Kennaway * try to open the controlling terminal (if any) for this process. if the 654b88c807SRodney W. Grimes * open fails, future ops that require user input will get an EOF 664b88c807SRodney W. Grimes */ 674b88c807SRodney W. Grimes 684b88c807SRodney W. Grimes int 694b88c807SRodney W. Grimes tty_init(void) 704b88c807SRodney W. Grimes { 714b88c807SRodney W. Grimes int ttyfd; 724b88c807SRodney W. Grimes 734b88c807SRodney W. Grimes if ((ttyfd = open(DEVTTY, O_RDWR)) >= 0) { 744b88c807SRodney W. Grimes if ((ttyoutf = fdopen(ttyfd, "w")) != NULL) { 754b88c807SRodney W. Grimes if ((ttyinf = fdopen(ttyfd, "r")) != NULL) 764b88c807SRodney W. Grimes return(0); 774b88c807SRodney W. Grimes (void)fclose(ttyoutf); 784b88c807SRodney W. Grimes } 794b88c807SRodney W. Grimes (void)close(ttyfd); 804b88c807SRodney W. Grimes } 814b88c807SRodney W. Grimes 824b88c807SRodney W. Grimes if (iflag) { 83778766feSKris Kennaway paxwarn(1, "Fatal error, cannot open %s", DEVTTY); 844b88c807SRodney W. Grimes return(-1); 854b88c807SRodney W. Grimes } 864b88c807SRodney W. Grimes return(0); 874b88c807SRodney W. Grimes } 884b88c807SRodney W. Grimes 894b88c807SRodney W. Grimes /* 904b88c807SRodney W. Grimes * tty_prnt() 914b88c807SRodney W. Grimes * print a message using the specified format to the controlling tty 924b88c807SRodney W. Grimes * if there is no controlling terminal, just return. 934b88c807SRodney W. Grimes */ 944b88c807SRodney W. Grimes 954b88c807SRodney W. Grimes void 96a2e73040SKris Kennaway tty_prnt(const char *fmt, ...) 974b88c807SRodney W. Grimes { 984b88c807SRodney W. Grimes va_list ap; 994b88c807SRodney W. Grimes if (ttyoutf == NULL) 1004b88c807SRodney W. Grimes return; 101e79f14faSTim J. Robbins va_start(ap, fmt); 1024b88c807SRodney W. Grimes (void)vfprintf(ttyoutf, fmt, ap); 1034b88c807SRodney W. Grimes va_end(ap); 1044b88c807SRodney W. Grimes (void)fflush(ttyoutf); 1054b88c807SRodney W. Grimes } 1064b88c807SRodney W. Grimes 1074b88c807SRodney W. Grimes /* 1084b88c807SRodney W. Grimes * tty_read() 1094b88c807SRodney W. Grimes * read a string from the controlling terminal if it is open into the 1104b88c807SRodney W. Grimes * supplied buffer 1114b88c807SRodney W. Grimes * Return: 1124b88c807SRodney W. Grimes * 0 if data was read, -1 otherwise. 1134b88c807SRodney W. Grimes */ 1144b88c807SRodney W. Grimes 1154b88c807SRodney W. Grimes int 1164b88c807SRodney W. Grimes tty_read(char *str, int len) 1174b88c807SRodney W. Grimes { 118f789b261SWarner Losh char *pt; 1194b88c807SRodney W. Grimes 1204b88c807SRodney W. Grimes if ((--len <= 0) || (ttyinf == NULL) || (fgets(str,len,ttyinf) == NULL)) 1214b88c807SRodney W. Grimes return(-1); 1224b88c807SRodney W. Grimes *(str + len) = '\0'; 1234b88c807SRodney W. Grimes 1244b88c807SRodney W. Grimes /* 1254b88c807SRodney W. Grimes * strip off that trailing newline 1264b88c807SRodney W. Grimes */ 1274b88c807SRodney W. Grimes if ((pt = strchr(str, '\n')) != NULL) 1284b88c807SRodney W. Grimes *pt = '\0'; 1294b88c807SRodney W. Grimes return(0); 1304b88c807SRodney W. Grimes } 1314b88c807SRodney W. Grimes 1324b88c807SRodney W. Grimes /* 133778766feSKris Kennaway * paxwarn() 134778766feSKris Kennaway * write a warning message to stderr. if "set" the exit value of pax 1354b88c807SRodney W. Grimes * will be non-zero. 1364b88c807SRodney W. Grimes */ 1374b88c807SRodney W. Grimes 1384b88c807SRodney W. Grimes void 139778766feSKris Kennaway paxwarn(int set, const char *fmt, ...) 1404b88c807SRodney W. Grimes { 1414b88c807SRodney W. Grimes va_list ap; 1424b88c807SRodney W. Grimes va_start(ap, fmt); 1434b88c807SRodney W. Grimes if (set) 1444b88c807SRodney W. Grimes exit_val = 1; 1454b88c807SRodney W. Grimes /* 1464b88c807SRodney W. Grimes * when vflag we better ship out an extra \n to get this message on a 1474b88c807SRodney W. Grimes * line by itself 1484b88c807SRodney W. Grimes */ 1494b88c807SRodney W. Grimes if (vflag && vfpart) { 150b1787decSKris Kennaway (void)fflush(listf); 1514b88c807SRodney W. Grimes (void)fputc('\n', stderr); 1524b88c807SRodney W. Grimes vfpart = 0; 1534b88c807SRodney W. Grimes } 1544b88c807SRodney W. Grimes (void)fprintf(stderr, "%s: ", argv0); 1554b88c807SRodney W. Grimes (void)vfprintf(stderr, fmt, ap); 1564b88c807SRodney W. Grimes va_end(ap); 1574b88c807SRodney W. Grimes (void)fputc('\n', stderr); 1584b88c807SRodney W. Grimes } 1594b88c807SRodney W. Grimes 1604b88c807SRodney W. Grimes /* 161778766feSKris Kennaway * syswarn() 162778766feSKris Kennaway * write a warning message to stderr. if "set" the exit value of pax 1634b88c807SRodney W. Grimes * will be non-zero. 1644b88c807SRodney W. Grimes */ 1654b88c807SRodney W. Grimes 1664b88c807SRodney W. Grimes void 167778766feSKris Kennaway syswarn(int set, int errnum, const char *fmt, ...) 1684b88c807SRodney W. Grimes { 1694b88c807SRodney W. Grimes va_list ap; 1704b88c807SRodney W. Grimes va_start(ap, fmt); 1714b88c807SRodney W. Grimes if (set) 1724b88c807SRodney W. Grimes exit_val = 1; 1734b88c807SRodney W. Grimes /* 1744b88c807SRodney W. Grimes * when vflag we better ship out an extra \n to get this message on a 1754b88c807SRodney W. Grimes * line by itself 1764b88c807SRodney W. Grimes */ 1774b88c807SRodney W. Grimes if (vflag && vfpart) { 178b1787decSKris Kennaway (void)fflush(listf); 1794b88c807SRodney W. Grimes (void)fputc('\n', stderr); 1804b88c807SRodney W. Grimes vfpart = 0; 1814b88c807SRodney W. Grimes } 1824b88c807SRodney W. Grimes (void)fprintf(stderr, "%s: ", argv0); 1834b88c807SRodney W. Grimes (void)vfprintf(stderr, fmt, ap); 1844b88c807SRodney W. Grimes va_end(ap); 1854b88c807SRodney W. Grimes 1864b88c807SRodney W. Grimes /* 1874b88c807SRodney W. Grimes * format and print the errno 1884b88c807SRodney W. Grimes */ 1894b88c807SRodney W. Grimes if (errnum > 0) 190778766feSKris Kennaway (void)fprintf(stderr, " <%s>", strerror(errnum)); 1914b88c807SRodney W. Grimes (void)fputc('\n', stderr); 1924b88c807SRodney W. Grimes } 193