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