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