tty_subs.c revision 1556
1185029Spjd/*- 2185029Spjd * Copyright (c) 1992 Keith Muller. 3185029Spjd * Copyright (c) 1992, 1993 4185029Spjd * The Regents of the University of California. All rights reserved. 5185029Spjd * 6185029Spjd * This code is derived from software contributed to Berkeley by 7185029Spjd * Keith Muller of the University of California, San Diego. 8185029Spjd * 9185029Spjd * Redistribution and use in source and binary forms, with or without 10185029Spjd * modification, are permitted provided that the following conditions 11185029Spjd * are met: 12185029Spjd * 1. Redistributions of source code must retain the above copyright 13185029Spjd * notice, this list of conditions and the following disclaimer. 14185029Spjd * 2. Redistributions in binary form must reproduce the above copyright 15185029Spjd * notice, this list of conditions and the following disclaimer in the 16185029Spjd * documentation and/or other materials provided with the distribution. 17185029Spjd * 3. All advertising materials mentioning features or use of this software 18185029Spjd * must display the following acknowledgement: 19185029Spjd * This product includes software developed by the University of 20185029Spjd * California, Berkeley and its contributors. 21185029Spjd * 4. Neither the name of the University nor the names of its contributors 22209962Smm * may be used to endorse or promote products derived from this software 23185029Spjd * without specific prior written permission. 24185029Spjd * 25185029Spjd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26185029Spjd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27185029Spjd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28185029Spjd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29185029Spjd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30185029Spjd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31185029Spjd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32192800Strasz * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33192800Strasz * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34192800Strasz * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35192800Strasz * SUCH DAMAGE. 36192800Strasz */ 37192800Strasz 38192800Strasz#ifndef lint 39192800Straszstatic char sccsid[] = "@(#)tty_subs.c 8.2 (Berkeley) 4/18/94"; 40219089Spjd#endif /* not lint */ 41192800Strasz 42185029Spjd#include <sys/types.h> 43185029Spjd#include <sys/time.h> 44185029Spjd#include <sys/stat.h> 45185029Spjd#include <sys/param.h> 46185029Spjd#include <fcntl.h> 47192800Strasz#include <stdio.h> 48185029Spjd#include <ctype.h> 49185029Spjd#include <errno.h> 50185029Spjd#include <unistd.h> 51185029Spjd#include <stdlib.h> 52185029Spjd#include <string.h> 53185029Spjd#include "pax.h" 54185029Spjd#include "extern.h" 55185029Spjd#if __STDC__ 56185029Spjd#include <stdarg.h> 57185029Spjd#else 58185029Spjd#include <varargs.h> 59185029Spjd#endif 60219089Spjd 61185029Spjd/* 62192800Strasz * routines that deal with I/O to and from the user 63185029Spjd */ 64185029Spjd 65185029Spjd#define DEVTTY "/dev/tty" /* device for interactive i/o */ 66185029Spjdstatic FILE *ttyoutf = NULL; /* output pointing at control tty */ 67185029Spjdstatic FILE *ttyinf = NULL; /* input pointing at control tty */ 68185029Spjd 69185029Spjd/* 70185029Spjd * tty_init() 71185029Spjd * try to open the controlling termina (if any) for this process. if the 72185029Spjd * open fails, future ops that require user input will get an EOF 73185029Spjd */ 74185029Spjd 75185029Spjd#if __STDC__ 76185029Spjdint 77185029Spjdtty_init(void) 78185029Spjd#else 79185029Spjdint 80185029Spjdtty_init() 81185029Spjd#endif 82185029Spjd{ 83185029Spjd int ttyfd; 84185029Spjd 85185029Spjd if ((ttyfd = open(DEVTTY, O_RDWR)) >= 0) { 86185029Spjd if ((ttyoutf = fdopen(ttyfd, "w")) != NULL) { 87185029Spjd if ((ttyinf = fdopen(ttyfd, "r")) != NULL) 88185029Spjd return(0); 89185029Spjd (void)fclose(ttyoutf); 90185029Spjd } 91185029Spjd (void)close(ttyfd); 92185029Spjd } 93185029Spjd 94185029Spjd if (iflag) { 95185029Spjd warn(1, "Fatal error, cannot open %s", DEVTTY); 96185029Spjd return(-1); 97185029Spjd } 98185029Spjd return(0); 99185029Spjd} 100185029Spjd 101185029Spjd/* 102185029Spjd * tty_prnt() 103185029Spjd * print a message using the specified format to the controlling tty 104185029Spjd * if there is no controlling terminal, just return. 105185029Spjd */ 106185029Spjd 107185029Spjd#if __STDC__ 108185029Spjdvoid 109185029Spjdtty_prnt(char *fmt, ...) 110185029Spjd#else 111185029Spjdvoid 112185029Spjdtty_prnt(fmt, va_alist) 113185029Spjd char *fmt; 114185029Spjd va_dcl 115185029Spjd#endif 116185029Spjd{ 117185029Spjd va_list ap; 118185029Spjd# if __STDC__ 119185029Spjd va_start(ap, fmt); 120185029Spjd# else 121185029Spjd va_start(ap); 122185029Spjd# endif 123185029Spjd if (ttyoutf == NULL) 124185029Spjd return; 125185029Spjd (void)vfprintf(ttyoutf, fmt, ap); 126185029Spjd va_end(ap); 127185029Spjd (void)fflush(ttyoutf); 128185029Spjd} 129185029Spjd 130185029Spjd/* 131185029Spjd * tty_read() 132185029Spjd * read a string from the controlling terminal if it is open into the 133185029Spjd * supplied buffer 134185029Spjd * Return: 135185029Spjd * 0 if data was read, -1 otherwise. 136185029Spjd */ 137185029Spjd 138185029Spjd#if __STDC__ 139185029Spjdint 140185029Spjdtty_read(char *str, int len) 141185029Spjd#else 142185029Spjdint 143185029Spjdtty_read(str, len) 144185029Spjd char *str; 145185029Spjd int len; 146185029Spjd#endif 147185029Spjd{ 148185029Spjd register char *pt; 149185029Spjd 150185029Spjd if ((--len <= 0) || (ttyinf == NULL) || (fgets(str,len,ttyinf) == NULL)) 151185029Spjd return(-1); 152185029Spjd *(str + len) = '\0'; 153185029Spjd 154185029Spjd /* 155185029Spjd * strip off that trailing newline 156185029Spjd */ 157185029Spjd if ((pt = strchr(str, '\n')) != NULL) 158185029Spjd *pt = '\0'; 159185029Spjd return(0); 160185029Spjd} 161185029Spjd 162185029Spjd/* 163185029Spjd * warn() 164185029Spjd * write a warning message to stderr. if "set" the exit value of pax 165185029Spjd * will be non-zero. 166185029Spjd */ 167185029Spjd 168185029Spjd#if __STDC__ 169209962Smmvoid 170209962Smmwarn(int set, char *fmt, ...) 171209962Smm#else 172209962Smmvoid 173185029Spjdwarn(set, fmt, va_alist) 174185029Spjd int set; 175185029Spjd char *fmt; 176185029Spjd va_dcl 177185029Spjd#endif 178185029Spjd{ 179185029Spjd va_list ap; 180185029Spjd# if __STDC__ 181185029Spjd va_start(ap, fmt); 182185029Spjd# else 183185029Spjd va_start(ap); 184185029Spjd# endif 185185029Spjd if (set) 186185029Spjd exit_val = 1; 187185029Spjd /* 188185029Spjd * when vflag we better ship out an extra \n to get this message on a 189185029Spjd * line by itself 190185029Spjd */ 191185029Spjd if (vflag && vfpart) { 192185029Spjd (void)fputc('\n', stderr); 193185029Spjd vfpart = 0; 194185029Spjd } 195185029Spjd (void)fprintf(stderr, "%s: ", argv0); 196185029Spjd (void)vfprintf(stderr, fmt, ap); 197185029Spjd va_end(ap); 198185029Spjd (void)fputc('\n', stderr); 199185029Spjd} 200185029Spjd 201185029Spjd/* 202185029Spjd * syswarn() 203185029Spjd * write a warning message to stderr. if "set" the exit value of pax 204185029Spjd * will be non-zero. 205185029Spjd */ 206185029Spjd 207185029Spjd#if __STDC__ 208185029Spjdvoid 209185029Spjdsyswarn(int set, int errnum, char *fmt, ...) 210185029Spjd#else 211185029Spjdvoid 212185029Spjdsyswarn(set, errnum, fmt, va_alist) 213185029Spjd int set; 214185029Spjd int errnum; 215185029Spjd char *fmt; 216185029Spjd va_dcl 217185029Spjd#endif 218185029Spjd{ 219185029Spjd va_list ap; 220185029Spjd# if __STDC__ 221185029Spjd va_start(ap, fmt); 222185029Spjd# else 223185029Spjd va_start(ap); 224185029Spjd# endif 225185029Spjd if (set) 226185029Spjd exit_val = 1; 227185029Spjd /* 228185029Spjd * when vflag we better ship out an extra \n to get this message on a 229185029Spjd * line by itself 230185029Spjd */ 231185029Spjd if (vflag && vfpart) { 232185029Spjd (void)fputc('\n', stderr); 233185029Spjd vfpart = 0; 234185029Spjd } 235185029Spjd (void)fprintf(stderr, "%s: ", argv0); 236185029Spjd (void)vfprintf(stderr, fmt, ap); 237185029Spjd va_end(ap); 238185029Spjd 239185029Spjd /* 240185029Spjd * format and print the errno 241185029Spjd */ 242185029Spjd if (errnum > 0) 243185029Spjd (void)fprintf(stderr, " <%s>", sys_errlist[errnum]); 244185029Spjd (void)fputc('\n', stderr); 245185029Spjd} 246185029Spjd