Home | History | Annotate | Line # | Download | only in pax
tty_subs.c revision 1.4
      1  1.1  jtc /*-
      2  1.1  jtc  * Copyright (c) 1992 Keith Muller.
      3  1.1  jtc  * Copyright (c) 1992, 1993
      4  1.1  jtc  *	The Regents of the University of California.  All rights reserved.
      5  1.1  jtc  *
      6  1.1  jtc  * This code is derived from software contributed to Berkeley by
      7  1.1  jtc  * Keith Muller of the University of California, San Diego.
      8  1.1  jtc  *
      9  1.1  jtc  * Redistribution and use in source and binary forms, with or without
     10  1.1  jtc  * modification, are permitted provided that the following conditions
     11  1.1  jtc  * are met:
     12  1.1  jtc  * 1. Redistributions of source code must retain the above copyright
     13  1.1  jtc  *    notice, this list of conditions and the following disclaimer.
     14  1.1  jtc  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  jtc  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  jtc  *    documentation and/or other materials provided with the distribution.
     17  1.1  jtc  * 3. All advertising materials mentioning features or use of this software
     18  1.1  jtc  *    must display the following acknowledgement:
     19  1.1  jtc  *	This product includes software developed by the University of
     20  1.1  jtc  *	California, Berkeley and its contributors.
     21  1.1  jtc  * 4. Neither the name of the University nor the names of its contributors
     22  1.1  jtc  *    may be used to endorse or promote products derived from this software
     23  1.1  jtc  *    without specific prior written permission.
     24  1.1  jtc  *
     25  1.1  jtc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  1.1  jtc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  1.1  jtc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  1.1  jtc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  1.1  jtc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  1.1  jtc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  1.1  jtc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  1.1  jtc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  1.1  jtc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.1  jtc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.1  jtc  * SUCH DAMAGE.
     36  1.1  jtc  */
     37  1.1  jtc 
     38  1.1  jtc #ifndef lint
     39  1.2  jtc /*static char sccsid[] = "from: @(#)tty_subs.c	8.2 (Berkeley) 4/18/94";*/
     40  1.4  jtc static char *rcsid = "$Id: tty_subs.c,v 1.4 1994/10/11 06:06:44 jtc Exp $";
     41  1.1  jtc #endif /* not lint */
     42  1.1  jtc 
     43  1.1  jtc #include <sys/types.h>
     44  1.1  jtc #include <sys/time.h>
     45  1.1  jtc #include <sys/stat.h>
     46  1.1  jtc #include <sys/param.h>
     47  1.1  jtc #include <fcntl.h>
     48  1.1  jtc #include <stdio.h>
     49  1.1  jtc #include <ctype.h>
     50  1.1  jtc #include <errno.h>
     51  1.1  jtc #include <unistd.h>
     52  1.1  jtc #include <stdlib.h>
     53  1.1  jtc #include <string.h>
     54  1.1  jtc #include "pax.h"
     55  1.1  jtc #include "extern.h"
     56  1.1  jtc #if __STDC__
     57  1.1  jtc #include <stdarg.h>
     58  1.1  jtc #else
     59  1.1  jtc #include <varargs.h>
     60  1.1  jtc #endif
     61  1.1  jtc 
     62  1.1  jtc /*
     63  1.1  jtc  * routines that deal with I/O to and from the user
     64  1.1  jtc  */
     65  1.1  jtc 
     66  1.1  jtc #define DEVTTY          "/dev/tty"      /* device for interactive i/o */
     67  1.1  jtc static FILE *ttyoutf = NULL;		/* output pointing at control tty */
     68  1.1  jtc static FILE *ttyinf = NULL;		/* input pointing at control tty */
     69  1.1  jtc 
     70  1.1  jtc /*
     71  1.1  jtc  * tty_init()
     72  1.1  jtc  *	try to open the controlling termina (if any) for this process. if the
     73  1.1  jtc  *	open fails, future ops that require user input will get an EOF
     74  1.1  jtc  */
     75  1.1  jtc 
     76  1.1  jtc #if __STDC__
     77  1.1  jtc int
     78  1.1  jtc tty_init(void)
     79  1.1  jtc #else
     80  1.1  jtc int
     81  1.1  jtc tty_init()
     82  1.1  jtc #endif
     83  1.1  jtc {
     84  1.1  jtc 	int ttyfd;
     85  1.1  jtc 
     86  1.1  jtc         if ((ttyfd = open(DEVTTY, O_RDWR)) >= 0) {
     87  1.1  jtc 		if ((ttyoutf = fdopen(ttyfd, "w")) != NULL) {
     88  1.1  jtc 			if ((ttyinf = fdopen(ttyfd, "r")) != NULL)
     89  1.1  jtc 				return(0);
     90  1.1  jtc 			(void)fclose(ttyoutf);
     91  1.1  jtc 		}
     92  1.1  jtc 		(void)close(ttyfd);
     93  1.1  jtc 	}
     94  1.1  jtc 
     95  1.1  jtc 	if (iflag) {
     96  1.1  jtc 		warn(1, "Fatal error, cannot open %s", DEVTTY);
     97  1.1  jtc 		return(-1);
     98  1.1  jtc 	}
     99  1.1  jtc 	return(0);
    100  1.1  jtc }
    101  1.1  jtc 
    102  1.1  jtc /*
    103  1.1  jtc  * tty_prnt()
    104  1.1  jtc  *	print a message using the specified format to the controlling tty
    105  1.1  jtc  *	if there is no controlling terminal, just return.
    106  1.1  jtc  */
    107  1.1  jtc 
    108  1.1  jtc #if __STDC__
    109  1.1  jtc void
    110  1.1  jtc tty_prnt(char *fmt, ...)
    111  1.1  jtc #else
    112  1.1  jtc void
    113  1.1  jtc tty_prnt(fmt, va_alist)
    114  1.1  jtc 	char *fmt;
    115  1.1  jtc 	va_dcl
    116  1.1  jtc #endif
    117  1.1  jtc {
    118  1.1  jtc 	va_list ap;
    119  1.1  jtc #	if __STDC__
    120  1.1  jtc 	va_start(ap, fmt);
    121  1.1  jtc #	else
    122  1.1  jtc 	va_start(ap);
    123  1.1  jtc #	endif
    124  1.1  jtc 	if (ttyoutf == NULL)
    125  1.1  jtc 		return;
    126  1.1  jtc 	(void)vfprintf(ttyoutf, fmt, ap);
    127  1.1  jtc 	va_end(ap);
    128  1.1  jtc 	(void)fflush(ttyoutf);
    129  1.1  jtc }
    130  1.1  jtc 
    131  1.1  jtc /*
    132  1.1  jtc  * tty_read()
    133  1.1  jtc  *	read a string from the controlling terminal if it is open into the
    134  1.1  jtc  *	supplied buffer
    135  1.1  jtc  * Return:
    136  1.1  jtc  *	0 if data was read, -1 otherwise.
    137  1.1  jtc  */
    138  1.1  jtc 
    139  1.1  jtc #if __STDC__
    140  1.1  jtc int
    141  1.1  jtc tty_read(char *str, int len)
    142  1.1  jtc #else
    143  1.1  jtc int
    144  1.1  jtc tty_read(str, len)
    145  1.1  jtc 	char *str;
    146  1.1  jtc 	int len;
    147  1.1  jtc #endif
    148  1.1  jtc {
    149  1.1  jtc 	register char *pt;
    150  1.1  jtc 
    151  1.1  jtc 	if ((--len <= 0) || (ttyinf == NULL) || (fgets(str,len,ttyinf) == NULL))
    152  1.1  jtc 		return(-1);
    153  1.1  jtc 	*(str + len) = '\0';
    154  1.1  jtc 
    155  1.1  jtc 	/*
    156  1.1  jtc 	 * strip off that trailing newline
    157  1.1  jtc 	 */
    158  1.1  jtc 	if ((pt = strchr(str, '\n')) != NULL)
    159  1.1  jtc 		*pt = '\0';
    160  1.1  jtc 	return(0);
    161  1.1  jtc }
    162  1.1  jtc 
    163  1.1  jtc /*
    164  1.1  jtc  * warn()
    165  1.1  jtc  *	write a warning message to stderr. if "set" the exit value of pax
    166  1.1  jtc  *	will be non-zero.
    167  1.1  jtc  */
    168  1.1  jtc 
    169  1.1  jtc #if __STDC__
    170  1.1  jtc void
    171  1.1  jtc warn(int set, char *fmt, ...)
    172  1.1  jtc #else
    173  1.1  jtc void
    174  1.1  jtc warn(set, fmt, va_alist)
    175  1.1  jtc 	int set;
    176  1.1  jtc 	char *fmt;
    177  1.1  jtc 	va_dcl
    178  1.1  jtc #endif
    179  1.1  jtc {
    180  1.1  jtc 	va_list ap;
    181  1.1  jtc #	if __STDC__
    182  1.1  jtc 	va_start(ap, fmt);
    183  1.1  jtc #	else
    184  1.1  jtc 	va_start(ap);
    185  1.1  jtc #	endif
    186  1.1  jtc 	if (set)
    187  1.1  jtc 		exit_val = 1;
    188  1.1  jtc 	/*
    189  1.1  jtc 	 * when vflag we better ship out an extra \n to get this message on a
    190  1.1  jtc 	 * line by itself
    191  1.1  jtc 	 */
    192  1.1  jtc 	if (vflag && vfpart) {
    193  1.1  jtc 		(void)fputc('\n', stderr);
    194  1.1  jtc 		vfpart = 0;
    195  1.1  jtc 	}
    196  1.1  jtc 	(void)fprintf(stderr, "%s: ", argv0);
    197  1.1  jtc 	(void)vfprintf(stderr, fmt, ap);
    198  1.1  jtc 	va_end(ap);
    199  1.1  jtc 	(void)fputc('\n', stderr);
    200  1.1  jtc }
    201  1.1  jtc 
    202  1.1  jtc /*
    203  1.1  jtc  * syswarn()
    204  1.1  jtc  *	write a warning message to stderr. if "set" the exit value of pax
    205  1.1  jtc  *	will be non-zero.
    206  1.1  jtc  */
    207  1.1  jtc 
    208  1.1  jtc #if __STDC__
    209  1.1  jtc void
    210  1.1  jtc syswarn(int set, int errnum, char *fmt, ...)
    211  1.1  jtc #else
    212  1.1  jtc void
    213  1.1  jtc syswarn(set, errnum, fmt, va_alist)
    214  1.1  jtc 	int set;
    215  1.1  jtc 	int errnum;
    216  1.1  jtc 	char *fmt;
    217  1.1  jtc 	va_dcl
    218  1.1  jtc #endif
    219  1.1  jtc {
    220  1.1  jtc 	va_list ap;
    221  1.1  jtc #	if __STDC__
    222  1.1  jtc 	va_start(ap, fmt);
    223  1.1  jtc #	else
    224  1.1  jtc 	va_start(ap);
    225  1.1  jtc #	endif
    226  1.1  jtc 	if (set)
    227  1.1  jtc 		exit_val = 1;
    228  1.1  jtc 	/*
    229  1.1  jtc 	 * when vflag we better ship out an extra \n to get this message on a
    230  1.1  jtc 	 * line by itself
    231  1.1  jtc 	 */
    232  1.1  jtc 	if (vflag && vfpart) {
    233  1.1  jtc 		(void)fputc('\n', stderr);
    234  1.1  jtc 		vfpart = 0;
    235  1.1  jtc 	}
    236  1.1  jtc 	(void)fprintf(stderr, "%s: ", argv0);
    237  1.1  jtc 	(void)vfprintf(stderr, fmt, ap);
    238  1.1  jtc 	va_end(ap);
    239  1.1  jtc 
    240  1.1  jtc 	/*
    241  1.1  jtc 	 * format and print the errno
    242  1.1  jtc 	 */
    243  1.1  jtc 	if (errnum > 0)
    244  1.4  jtc 		(void)fprintf(stderr, " <%s>", strerror(errnum));
    245  1.1  jtc 	(void)fputc('\n', stderr);
    246  1.1  jtc }
    247