/*-1* SPDX-License-Identifier: BSD-3-Clause2*3* Copyright (c) 1992 Keith Muller.4* Copyright (c) 1992, 19935* The Regents of the University of California. All rights reserved.6*7* This code is derived from software contributed to Berkeley by8* Keith Muller of the University of California, San Diego.9*10* Redistribution and use in source and binary forms, with or without11* modification, are permitted provided that the following conditions12* are met:13* 1. Redistributions of source code must retain the above copyright14* notice, this list of conditions and the following disclaimer.15* 2. Redistributions in binary form must reproduce the above copyright16* notice, this list of conditions and the following disclaimer in the17* documentation and/or other materials provided with the distribution.18* 3. Neither the name of the University nor the names of its contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*/3435#include <sys/types.h>36#include <sys/stat.h>37#include <fcntl.h>38#include <stdio.h>39#include <unistd.h>40#include <string.h>41#include "pax.h"42#include "extern.h"43#include <stdarg.h>4445/*46* routines that deal with I/O to and from the user47*/4849#define DEVTTY "/dev/tty" /* device for interactive i/o */50static FILE *ttyoutf = NULL; /* output pointing at control tty */51static FILE *ttyinf = NULL; /* input pointing at control tty */5253/*54* tty_init()55* try to open the controlling terminal (if any) for this process. if the56* open fails, future ops that require user input will get an EOF57*/5859int60tty_init(void)61{62int ttyfd;6364if ((ttyfd = open(DEVTTY, O_RDWR)) >= 0) {65if ((ttyoutf = fdopen(ttyfd, "w")) != NULL) {66if ((ttyinf = fdopen(ttyfd, "r")) != NULL)67return(0);68(void)fclose(ttyoutf);69}70(void)close(ttyfd);71}7273if (iflag) {74paxwarn(1, "Fatal error, cannot open %s", DEVTTY);75return(-1);76}77return(0);78}7980/*81* tty_prnt()82* print a message using the specified format to the controlling tty83* if there is no controlling terminal, just return.84*/8586void87tty_prnt(const char *fmt, ...)88{89va_list ap;90if (ttyoutf == NULL)91return;92va_start(ap, fmt);93(void)vfprintf(ttyoutf, fmt, ap);94va_end(ap);95(void)fflush(ttyoutf);96}9798/*99* tty_read()100* read a string from the controlling terminal if it is open into the101* supplied buffer102* Return:103* 0 if data was read, -1 otherwise.104*/105106int107tty_read(char *str, int len)108{109char *pt;110111if ((--len <= 0) || (ttyinf == NULL) || (fgets(str,len,ttyinf) == NULL))112return(-1);113*(str + len) = '\0';114115/*116* strip off that trailing newline117*/118if ((pt = strchr(str, '\n')) != NULL)119*pt = '\0';120return(0);121}122123/*124* paxwarn()125* write a warning message to stderr. if "set" the exit value of pax126* will be non-zero.127*/128129void130paxwarn(int set, const char *fmt, ...)131{132va_list ap;133va_start(ap, fmt);134if (set)135exit_val = 1;136/*137* when vflag we better ship out an extra \n to get this message on a138* line by itself139*/140if (vflag && vfpart) {141(void)fflush(listf);142(void)fputc('\n', stderr);143vfpart = 0;144}145(void)fprintf(stderr, "%s: ", argv0);146(void)vfprintf(stderr, fmt, ap);147va_end(ap);148(void)fputc('\n', stderr);149}150151/*152* syswarn()153* write a warning message to stderr. if "set" the exit value of pax154* will be non-zero.155*/156157void158syswarn(int set, int errnum, const char *fmt, ...)159{160va_list ap;161va_start(ap, fmt);162if (set)163exit_val = 1;164/*165* when vflag we better ship out an extra \n to get this message on a166* line by itself167*/168if (vflag && vfpart) {169(void)fflush(listf);170(void)fputc('\n', stderr);171vfpart = 0;172}173(void)fprintf(stderr, "%s: ", argv0);174(void)vfprintf(stderr, fmt, ap);175va_end(ap);176177/*178* format and print the errno179*/180if (errnum > 0)181(void)fprintf(stderr, " <%s>", strerror(errnum));182(void)fputc('\n', stderr);183}184185186