]> git.saurik.com Git - apple/file_cmds.git/blame - pax/tty_subs.c
file_cmds-116.11.tar.gz
[apple/file_cmds.git] / pax / tty_subs.c
CommitLineData
44a7a5ab
A
1/* $OpenBSD: tty_subs.c,v 1.5 1997/07/25 18:58:39 mickey 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. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the University of
23 * California, Berkeley and its contributors.
24 * 4. Neither the name of the University nor the names of its contributors
25 * may be used to endorse or promote products derived from this software
26 * without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * SUCH DAMAGE.
39 */
40
41#ifndef lint
42#if 0
43static char sccsid[] = "@(#)tty_subs.c 8.2 (Berkeley) 4/18/94";
44#else
45static char rcsid[] __attribute__((__unused__)) = "$OpenBSD: tty_subs.c,v 1.5 1997/07/25 18:58:39 mickey Exp $";
46#endif
47#endif /* not lint */
48
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 <errno.h>
56#include <unistd.h>
57#include <stdlib.h>
58#include <string.h>
59#include "pax.h"
60#include "extern.h"
61#ifdef __STDC__
62#include <stdarg.h>
63#else
64#include <varargs.h>
65#endif
66
67/*
68 * routines that deal with I/O to and from the user
69 */
70
71#define DEVTTY "/dev/tty" /* device for interactive i/o */
72static FILE *ttyoutf = NULL; /* output pointing at control tty */
73static FILE *ttyinf = NULL; /* input pointing at control tty */
74
75/*
76 * tty_init()
77 * try to open the controlling termina (if any) for this process. if the
78 * open fails, future ops that require user input will get an EOF
79 */
80
81#ifdef __STDC__
82int
83tty_init(void)
84#else
85int
86tty_init()
87#endif
88{
89 int ttyfd;
90
91 if ((ttyfd = open(DEVTTY, O_RDWR)) >= 0) {
92 if ((ttyoutf = fdopen(ttyfd, "w")) != NULL) {
93 if ((ttyinf = fdopen(ttyfd, "r")) != NULL)
94 return(0);
95 (void)fclose(ttyoutf);
96 }
97 (void)close(ttyfd);
98 }
99
100 if (iflag) {
101 paxwarn(1, "Fatal error, cannot open %s", DEVTTY);
102 return(-1);
103 }
104 return(0);
105}
106
107/*
108 * tty_prnt()
109 * print a message using the specified format to the controlling tty
110 * if there is no controlling terminal, just return.
111 */
112
113#ifdef __STDC__
114void
115tty_prnt(char *fmt, ...)
116#else
117void
118tty_prnt(fmt, va_alist)
119 char *fmt;
120 va_dcl
121#endif
122{
123 va_list ap;
124# ifdef __STDC__
125 va_start(ap, fmt);
126# else
127 va_start(ap);
128# endif
129 if (ttyoutf == NULL)
130 return;
131 (void)vfprintf(ttyoutf, fmt, ap);
132 va_end(ap);
133 (void)fflush(ttyoutf);
134}
135
136/*
137 * tty_read()
138 * read a string from the controlling terminal if it is open into the
139 * supplied buffer
140 * Return:
141 * 0 if data was read, -1 otherwise.
142 */
143
144#ifdef __STDC__
145int
146tty_read(char *str, int len)
147#else
148int
149tty_read(str, len)
150 char *str;
151 int len;
152#endif
153{
154 register char *pt;
155
156 if ((--len <= 0) || (ttyinf == NULL) || (fgets(str,len,ttyinf) == NULL))
157 return(-1);
158 *(str + len) = '\0';
159
160 /*
161 * strip off that trailing newline
162 */
163 if ((pt = strchr(str, '\n')) != NULL)
164 *pt = '\0';
165 return(0);
166}
167
168/*
169 * paxwarn()
170 * write a warning message to stderr. if "set" the exit value of pax
171 * will be non-zero.
172 */
173
174#ifdef __STDC__
175void
176paxwarn(int set, char *fmt, ...)
177#else
178void
179paxwarn(set, fmt, va_alist)
180 int set;
181 char *fmt;
182 va_dcl
183#endif
184{
185 va_list ap;
186# ifdef __STDC__
187 va_start(ap, fmt);
188# else
189 va_start(ap);
190# endif
191 if (set)
192 exit_val = 1;
193 /*
194 * when vflag we better ship out an extra \n to get this message on a
195 * line by itself
196 */
197 if (vflag && vfpart) {
198 (void)fputc('\n', stderr);
199 vfpart = 0;
200 }
201 (void)fprintf(stderr, "%s: ", argv0);
202 (void)vfprintf(stderr, fmt, ap);
203 va_end(ap);
204 (void)fputc('\n', stderr);
205}
206
207/*
208 * syswarn()
209 * write a warning message to stderr. if "set" the exit value of pax
210 * will be non-zero.
211 */
212
213#ifdef __STDC__
214void
215syswarn(int set, int errnum, char *fmt, ...)
216#else
217void
218syswarn(set, errnum, fmt, va_alist)
219 int set;
220 int errnum;
221 char *fmt;
222 va_dcl
223#endif
224{
225 va_list ap;
226# ifdef __STDC__
227 va_start(ap, fmt);
228# else
229 va_start(ap);
230# endif
231 if (set)
232 exit_val = 1;
233 /*
234 * when vflag we better ship out an extra \n to get this message on a
235 * line by itself
236 */
237 if (vflag && vfpart) {
238 (void)fputc('\n', stderr);
239 vfpart = 0;
240 }
241 (void)fprintf(stderr, "%s: ", argv0);
242 (void)vfprintf(stderr, fmt, ap);
243 va_end(ap);
244
245 /*
246 * format and print the errno
247 */
248 if (errnum > 0)
249 (void)fprintf(stderr, " <%s>", strerror(errnum));
250 (void)fputc('\n', stderr);
251}