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