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