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