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