tty_subs.c revision 22988
11897Swollman/*- 21897Swollman * Copyright (c) 1992 Keith Muller. 31897Swollman * Copyright (c) 1992, 1993 41897Swollman * The Regents of the University of California. All rights reserved. 51897Swollman * 61897Swollman * This code is derived from software contributed to Berkeley by 71897Swollman * Keith Muller of the University of California, San Diego. 8100441Scharnier * 91897Swollman * Redistribution and use in source and binary forms, with or without 101897Swollman * modification, are permitted provided that the following conditions 111897Swollman * are met: 12100441Scharnier * 1. Redistributions of source code must retain the above copyright 131897Swollman * notice, this list of conditions and the following disclaimer. 141897Swollman * 2. Redistributions in binary form must reproduce the above copyright 151897Swollman * notice, this list of conditions and the following disclaimer in the 16100441Scharnier * documentation and/or other materials provided with the distribution. 171897Swollman * 3. All advertising materials mentioning features or use of this software 181897Swollman * must display the following acknowledgement: 191897Swollman * This product includes software developed by the University of 20100441Scharnier * California, Berkeley and its contributors. 211897Swollman * 4. Neither the name of the University nor the names of its contributors 221897Swollman * may be used to endorse or promote products derived from this software 231897Swollman * without specific prior written permission. 24100441Scharnier * 251897Swollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 261897Swollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 271897Swollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 281897Swollman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2912798Swpaul * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30100441Scharnier * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 311897Swollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32146833Sstefanf * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3312798Swpaul * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 341897Swollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3599979Salfred * SUCH DAMAGE. 361897Swollman * 37100441Scharnier * $Id$ 38100441Scharnier */ 39100441Scharnier 401897Swollman#ifndef lint 41100441Scharnierstatic char const sccsid[] = "@(#)tty_subs.c 8.2 (Berkeley) 4/18/94"; 42100441Scharnier#endif /* not lint */ 431897Swollman 4412798Swpaul#include <sys/types.h> 4567191Sbrian#include <sys/time.h> 4667191Sbrian#include <sys/stat.h> 4712798Swpaul#include <sys/param.h> 481897Swollman#include <fcntl.h> 491897Swollman#include <stdio.h> 5012798Swpaul#include <errno.h> 51152398Sdwmalone#include <unistd.h> 521897Swollman#include <stdlib.h> 531897Swollman#include <string.h> 541897Swollman#include "pax.h" 551897Swollman#include "extern.h" 561897Swollman#if __STDC__ 571897Swollman#include <stdarg.h> 581897Swollman#else 591897Swollman#include <varargs.h> 601897Swollman#endif 6192921Simp 62152398Sdwmalone/* 63152398Sdwmalone * routines that deal with I/O to and from the user 64152398Sdwmalone */ 6592921Simp 6692921Simp#define DEVTTY "/dev/tty" /* device for interactive i/o */ 6792921Simpstatic FILE *ttyoutf = NULL; /* output pointing at control tty */ 6892921Simpstatic FILE *ttyinf = NULL; /* input pointing at control tty */ 69152398Sdwmalone 7012798Swpaul/* 711897Swollman * tty_init() 72100441Scharnier * try to open the controlling termina (if any) for this process. if the 731897Swollman * open fails, future ops that require user input will get an EOF 741897Swollman */ 75152398Sdwmalone 761897Swollman#if __STDC__ 771897Swollmanint 781897Swollmantty_init(void) 791897Swollman#else 801897Swollmanint 811897Swollmantty_init() 821897Swollman#endif 831897Swollman{ 84100441Scharnier int ttyfd; 851897Swollman 861897Swollman if ((ttyfd = open(DEVTTY, O_RDWR)) >= 0) { 87152398Sdwmalone if ((ttyoutf = fdopen(ttyfd, "w")) != NULL) { 881897Swollman if ((ttyinf = fdopen(ttyfd, "r")) != NULL) 891897Swollman return(0); 901897Swollman (void)fclose(ttyoutf); 911897Swollman } 921897Swollman (void)close(ttyfd); 931897Swollman } 941897Swollman 951897Swollman if (iflag) { 96100441Scharnier warn(1, "Fatal error, cannot open %s", DEVTTY); 971897Swollman return(-1); 981897Swollman } 99152398Sdwmalone return(0); 1001897Swollman} 1011897Swollman 1021897Swollman/* 1031897Swollman * tty_prnt() 1041897Swollman * print a message using the specified format to the controlling tty 1051897Swollman * if there is no controlling terminal, just return. 1061897Swollman */ 1071897Swollman 1081897Swollman#if __STDC__ 109100441Scharniervoid 1101897Swollmantty_prnt(char *fmt, ...) 1111897Swollman#else 112152398Sdwmalonevoid 1131897Swollmantty_prnt(fmt, va_alist) 1141897Swollman char *fmt; 1151897Swollman va_dcl 1161897Swollman#endif 1171897Swollman{ 1181897Swollman va_list ap; 1191897Swollman# if __STDC__ 1201897Swollman va_start(ap, fmt); 1211897Swollman# else 1221897Swollman va_start(ap); 1231897Swollman# endif 124100441Scharnier if (ttyoutf == NULL) 1251897Swollman return; 1261897Swollman (void)vfprintf(ttyoutf, fmt, ap); 127152398Sdwmalone va_end(ap); 1281897Swollman (void)fflush(ttyoutf); 1291897Swollman} 1301897Swollman 1311897Swollman/* 1321897Swollman * tty_read() 1331897Swollman * read a string from the controlling terminal if it is open into the 134100441Scharnier * supplied buffer 1351897Swollman * Return: 1361897Swollman * 0 if data was read, -1 otherwise. 137152398Sdwmalone */ 1381897Swollman 1391897Swollman#if __STDC__ 1401897Swollmanint 1411897Swollmantty_read(char *str, int len) 1421897Swollman#else 1431897Swollmanint 1441897Swollmantty_read(str, len) 1451897Swollman char *str; 1461897Swollman int len; 1471897Swollman#endif 148100441Scharnier{ 1491897Swollman register char *pt; 1501897Swollman 151152398Sdwmalone if ((--len <= 0) || (ttyinf == NULL) || (fgets(str,len,ttyinf) == NULL)) 1521897Swollman return(-1); 1531897Swollman *(str + len) = '\0'; 15412798Swpaul 15512798Swpaul /* 15612798Swpaul * strip off that trailing newline 1571897Swollman */ 1581897Swollman if ((pt = strchr(str, '\n')) != NULL) 1591897Swollman *pt = '\0'; 1601897Swollman return(0); 1611897Swollman} 1621897Swollman 1631897Swollman/* 1641897Swollman * warn() 1651897Swollman * write a warning message to stderr. if "set" the exit value of pax 1661897Swollman * will be non-zero. 1671897Swollman */ 16812798Swpaul 16912798Swpaul#if __STDC__ 17012798Swpaulvoid 17112798Swpaulwarn(int set, char *fmt, ...) 17212798Swpaul#else 17312798Swpaulvoid 1741897Swollmanwarn(set, fmt, va_alist) 1751897Swollman int set; 1761897Swollman char *fmt; 1771897Swollman va_dcl 1781897Swollman#endif 1791897Swollman{ 1801897Swollman va_list ap; 181100441Scharnier# if __STDC__ 1821897Swollman va_start(ap, fmt); 1831897Swollman# else 1841897Swollman va_start(ap); 1851897Swollman# endif 1861897Swollman if (set) 1871897Swollman exit_val = 1; 1881897Swollman /* 1891897Swollman * when vflag we better ship out an extra \n to get this message on a 1901897Swollman * line by itself 1911897Swollman */ 1921897Swollman if (vflag && vfpart) { 1931897Swollman (void)fputc('\n', stderr); 1941897Swollman vfpart = 0; 19512798Swpaul } 19612798Swpaul (void)fprintf(stderr, "%s: ", argv0); 19712798Swpaul (void)vfprintf(stderr, fmt, ap); 19812798Swpaul va_end(ap); 19912798Swpaul (void)fputc('\n', stderr); 20012798Swpaul} 2011897Swollman 2021897Swollman/* 2031897Swollman * syswarn() 2041897Swollman * write a warning message to stderr. if "set" the exit value of pax 2051897Swollman * will be non-zero. 2061897Swollman */ 2071897Swollman 2081897Swollman#if __STDC__ 2091897Swollmanvoid 2101897Swollmansyswarn(int set, int errnum, char *fmt, ...) 211100441Scharnier#else 2121897Swollmanvoid 2131897Swollmansyswarn(set, errnum, fmt, va_alist) 2141897Swollman int set; 2151897Swollman int errnum; 2161897Swollman char *fmt; 2171897Swollman va_dcl 2181897Swollman#endif 2191897Swollman{ 2201897Swollman va_list ap; 2211897Swollman# if __STDC__ 2221897Swollman va_start(ap, fmt); 2231897Swollman# else 2241897Swollman va_start(ap); 2251897Swollman# endif 2261897Swollman if (set) 2271897Swollman exit_val = 1; 2281897Swollman /* 2291897Swollman * when vflag we better ship out an extra \n to get this message on a 2301897Swollman * line by itself 2311897Swollman */ 2321897Swollman if (vflag && vfpart) { 2331897Swollman (void)fputc('\n', stderr); 2341897Swollman vfpart = 0; 2351897Swollman } 2361897Swollman (void)fprintf(stderr, "%s: ", argv0); 2371897Swollman (void)vfprintf(stderr, fmt, ap); 2381897Swollman va_end(ap); 2391897Swollman 2401897Swollman /* 2411897Swollman * format and print the errno 2421897Swollman */ 2431897Swollman if (errnum > 0) 2441897Swollman (void)fprintf(stderr, " <%s>", sys_errlist[errnum]); 2451897Swollman (void)fputc('\n', stderr); 2461897Swollman} 2471897Swollman