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