tty_subs.c revision 31666
1/*- 2 * Copyright (c) 1992 Keith Muller. 3 * Copyright (c) 1992, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * This code is derived from software contributed to Berkeley by 7 * Keith Muller of the University of California, San Diego. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * $Id: tty_subs.c,v 1.7 1997/08/29 16:12:30 sos Exp $ 38 */ 39 40#ifndef lint 41static char const sccsid[] = "@(#)tty_subs.c 8.2 (Berkeley) 4/18/94"; 42#endif /* not lint */ 43 44#include <sys/types.h> 45#include <sys/stat.h> 46#include <fcntl.h> 47#include <stdio.h> 48#include <unistd.h> 49#include <stdlib.h> 50#include <string.h> 51#include "pax.h" 52#include "extern.h" 53#if __STDC__ 54#include <stdarg.h> 55#else 56#include <varargs.h> 57#endif 58 59/* 60 * routines that deal with I/O to and from the user 61 */ 62 63#define DEVTTY "/dev/tty" /* device for interactive i/o */ 64static FILE *ttyoutf = NULL; /* output pointing at control tty */ 65static FILE *ttyinf = NULL; /* input pointing at control tty */ 66 67/* 68 * tty_init() 69 * try to open the controlling termina (if any) for this process. if the 70 * open fails, future ops that require user input will get an EOF 71 */ 72 73#if __STDC__ 74int 75tty_init(void) 76#else 77int 78tty_init() 79#endif 80{ 81 int ttyfd; 82 83 if ((ttyfd = open(DEVTTY, O_RDWR)) >= 0) { 84 if ((ttyoutf = fdopen(ttyfd, "w")) != NULL) { 85 if ((ttyinf = fdopen(ttyfd, "r")) != NULL) 86 return(0); 87 (void)fclose(ttyoutf); 88 } 89 (void)close(ttyfd); 90 } 91 92 if (iflag) { 93 pax_warn(1, "Fatal error, cannot open %s", DEVTTY); 94 return(-1); 95 } 96 return(0); 97} 98 99/* 100 * tty_prnt() 101 * print a message using the specified format to the controlling tty 102 * if there is no controlling terminal, just return. 103 */ 104 105#if __STDC__ 106void 107tty_prnt(char *fmt, ...) 108#else 109void 110tty_prnt(fmt, va_alist) 111 char *fmt; 112 va_dcl 113#endif 114{ 115 va_list ap; 116# if __STDC__ 117 va_start(ap, fmt); 118# else 119 va_start(ap); 120# endif 121 if (ttyoutf == NULL) 122 return; 123 (void)vfprintf(ttyoutf, fmt, ap); 124 va_end(ap); 125 (void)fflush(ttyoutf); 126} 127 128/* 129 * tty_read() 130 * read a string from the controlling terminal if it is open into the 131 * supplied buffer 132 * Return: 133 * 0 if data was read, -1 otherwise. 134 */ 135 136#if __STDC__ 137int 138tty_read(char *str, int len) 139#else 140int 141tty_read(str, len) 142 char *str; 143 int len; 144#endif 145{ 146 register char *pt; 147 148 if ((--len <= 0) || (ttyinf == NULL) || (fgets(str,len,ttyinf) == NULL)) 149 return(-1); 150 *(str + len) = '\0'; 151 152 /* 153 * strip off that trailing newline 154 */ 155 if ((pt = strchr(str, '\n')) != NULL) 156 *pt = '\0'; 157 return(0); 158} 159 160/* 161 * pax_warn() 162 * write a pax_warning message to stderr. if "set" the exit value of pax 163 * will be non-zero. 164 */ 165 166#if __STDC__ 167void 168pax_warn(int set, char *fmt, ...) 169#else 170void 171pax_warn(set, fmt, va_alist) 172 int set; 173 char *fmt; 174 va_dcl 175#endif 176{ 177 va_list ap; 178# if __STDC__ 179 va_start(ap, fmt); 180# else 181 va_start(ap); 182# endif 183 if (set) 184 exit_val = 1; 185 /* 186 * when vflag we better ship out an extra \n to get this message on a 187 * line by itself 188 */ 189 if (vflag && vfpart) { 190 (void)fputc('\n', stderr); 191 vfpart = 0; 192 } 193 (void)fprintf(stderr, "%s: ", argv0); 194 (void)vfprintf(stderr, fmt, ap); 195 va_end(ap); 196 (void)fputc('\n', stderr); 197} 198 199/* 200 * sys_warn() 201 * write a pax_warning message to stderr. if "set" the exit value of pax 202 * will be non-zero. 203 */ 204 205#if __STDC__ 206void 207sys_warn(int set, int errnum, char *fmt, ...) 208#else 209void 210sys_warn(set, errnum, fmt, va_alist) 211 int set; 212 int errnum; 213 char *fmt; 214 va_dcl 215#endif 216{ 217 va_list ap; 218# if __STDC__ 219 va_start(ap, fmt); 220# else 221 va_start(ap); 222# endif 223 if (set) 224 exit_val = 1; 225 /* 226 * when vflag we better ship out an extra \n to get this message on a 227 * line by itself 228 */ 229 if (vflag && vfpart) { 230 (void)fputc('\n', stderr); 231 vfpart = 0; 232 } 233 (void)fprintf(stderr, "%s: ", argv0); 234 (void)vfprintf(stderr, fmt, ap); 235 va_end(ap); 236 237 /* 238 * format and print the errno 239 */ 240 if (errnum > 0) 241 (void)fprintf(stderr, " <%s>", sys_errlist[errnum]); 242 (void)fputc('\n', stderr); 243} 244