1 /* $NetBSD: tty_subs.c,v 1.5 1995/03/21 09:07:52 cgd 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 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)tty_subs.c 8.2 (Berkeley) 4/18/94"; 43 #else 44 static char rcsid[] = "$NetBSD: tty_subs.c,v 1.5 1995/03/21 09:07:52 cgd Exp $"; 45 #endif 46 #endif /* not lint */ 47 48 #include <sys/types.h> 49 #include <sys/time.h> 50 #include <sys/stat.h> 51 #include <sys/param.h> 52 #include <fcntl.h> 53 #include <stdio.h> 54 #include <ctype.h> 55 #include <errno.h> 56 #include <unistd.h> 57 #include <stdlib.h> 58 #include <string.h> 59 #include "pax.h" 60 #include "extern.h" 61 #if __STDC__ 62 #include <stdarg.h> 63 #else 64 #include <varargs.h> 65 #endif 66 67 /* 68 * routines that deal with I/O to and from the user 69 */ 70 71 #define DEVTTY "/dev/tty" /* device for interactive i/o */ 72 static FILE *ttyoutf = NULL; /* output pointing at control tty */ 73 static FILE *ttyinf = NULL; /* input pointing at control tty */ 74 75 /* 76 * tty_init() 77 * try to open the controlling termina (if any) for this process. if the 78 * open fails, future ops that require user input will get an EOF 79 */ 80 81 #if __STDC__ 82 int 83 tty_init(void) 84 #else 85 int 86 tty_init() 87 #endif 88 { 89 int ttyfd; 90 91 if ((ttyfd = open(DEVTTY, O_RDWR)) >= 0) { 92 if ((ttyoutf = fdopen(ttyfd, "w")) != NULL) { 93 if ((ttyinf = fdopen(ttyfd, "r")) != NULL) 94 return(0); 95 (void)fclose(ttyoutf); 96 } 97 (void)close(ttyfd); 98 } 99 100 if (iflag) { 101 warn(1, "Fatal error, cannot open %s", DEVTTY); 102 return(-1); 103 } 104 return(0); 105 } 106 107 /* 108 * tty_prnt() 109 * print a message using the specified format to the controlling tty 110 * if there is no controlling terminal, just return. 111 */ 112 113 #if __STDC__ 114 void 115 tty_prnt(char *fmt, ...) 116 #else 117 void 118 tty_prnt(fmt, va_alist) 119 char *fmt; 120 va_dcl 121 #endif 122 { 123 va_list ap; 124 # if __STDC__ 125 va_start(ap, fmt); 126 # else 127 va_start(ap); 128 # endif 129 if (ttyoutf == NULL) 130 return; 131 (void)vfprintf(ttyoutf, fmt, ap); 132 va_end(ap); 133 (void)fflush(ttyoutf); 134 } 135 136 /* 137 * tty_read() 138 * read a string from the controlling terminal if it is open into the 139 * supplied buffer 140 * Return: 141 * 0 if data was read, -1 otherwise. 142 */ 143 144 #if __STDC__ 145 int 146 tty_read(char *str, int len) 147 #else 148 int 149 tty_read(str, len) 150 char *str; 151 int len; 152 #endif 153 { 154 register char *pt; 155 156 if ((--len <= 0) || (ttyinf == NULL) || (fgets(str,len,ttyinf) == NULL)) 157 return(-1); 158 *(str + len) = '\0'; 159 160 /* 161 * strip off that trailing newline 162 */ 163 if ((pt = strchr(str, '\n')) != NULL) 164 *pt = '\0'; 165 return(0); 166 } 167 168 /* 169 * warn() 170 * write a warning message to stderr. if "set" the exit value of pax 171 * will be non-zero. 172 */ 173 174 #if __STDC__ 175 void 176 warn(int set, char *fmt, ...) 177 #else 178 void 179 warn(set, fmt, va_alist) 180 int set; 181 char *fmt; 182 va_dcl 183 #endif 184 { 185 va_list ap; 186 # if __STDC__ 187 va_start(ap, fmt); 188 # else 189 va_start(ap); 190 # endif 191 if (set) 192 exit_val = 1; 193 /* 194 * when vflag we better ship out an extra \n to get this message on a 195 * line by itself 196 */ 197 if (vflag && vfpart) { 198 (void)fputc('\n', stderr); 199 vfpart = 0; 200 } 201 (void)fprintf(stderr, "%s: ", argv0); 202 (void)vfprintf(stderr, fmt, ap); 203 va_end(ap); 204 (void)fputc('\n', stderr); 205 } 206 207 /* 208 * syswarn() 209 * write a warning message to stderr. if "set" the exit value of pax 210 * will be non-zero. 211 */ 212 213 #if __STDC__ 214 void 215 syswarn(int set, int errnum, char *fmt, ...) 216 #else 217 void 218 syswarn(set, errnum, fmt, va_alist) 219 int set; 220 int errnum; 221 char *fmt; 222 va_dcl 223 #endif 224 { 225 va_list ap; 226 # if __STDC__ 227 va_start(ap, fmt); 228 # else 229 va_start(ap); 230 # endif 231 if (set) 232 exit_val = 1; 233 /* 234 * when vflag we better ship out an extra \n to get this message on a 235 * line by itself 236 */ 237 if (vflag && vfpart) { 238 (void)fputc('\n', stderr); 239 vfpart = 0; 240 } 241 (void)fprintf(stderr, "%s: ", argv0); 242 (void)vfprintf(stderr, fmt, ap); 243 va_end(ap); 244 245 /* 246 * format and print the errno 247 */ 248 if (errnum > 0) 249 (void)fprintf(stderr, " <%s>", strerror(errnum)); 250 (void)fputc('\n', stderr); 251 } 252