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