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