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