1 /*        $NetBSD: tty_subs.c,v 1.19 2007/04/23 18:40:22 christos Exp $         */
2 
3 /*-
4  * Copyright (c) 1992 Keith Muller.
5  * Copyright (c) 1992, 1993
6  *        The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Keith Muller of the University of California, San Diego.
10  *
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.
22  *
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.
34  */
35 
36 #if HAVE_NBTOOL_CONFIG_H
37 #include "nbtool_config.h"
38 #endif
39 
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 */
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 <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>
63 
64 /*
65  * routines that deal with I/O to and from the user
66  */
67 
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 */
71 
72 /*
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.
76  */
77 
78 int
tty_init(void)79 tty_init(void)
80 {
81           int ttyfd;
82 
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);
88                     }
89                     (void)close(ttyfd);
90           }
91 
92           if (iflag) {
93                     tty_warn(1, "Fatal error, cannot open %s", DEVTTY);
94                     return -1;
95           }
96           return 0;
97 }
98 
99 /*
100  * tty_prnt()
101  *        print a message using the specified format to the controlling tty
102  *        if there is no controlling terminal, just return.
103  */
104 
105 void
tty_prnt(const char * fmt,...)106 tty_prnt(const char *fmt, ...)
107 {
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);
115 }
116 
117 /*
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.
123  */
124 
125 int
tty_read(char * str,int len)126 tty_read(char *str, int len)
127 {
128           char *pt;
129 
130           if ((--len <= 0) || (ttyinf == NULL) || (fgets(str,len,ttyinf) == NULL))
131                     return -1;
132           *(str + len) = '\0';
133 
134           /*
135            * strip off that trailing newline
136            */
137           if ((pt = strchr(str, '\n')) != NULL)
138                     *pt = '\0';
139           return 0;
140 }
141 
142 /*
143  * tty_warn()
144  *        write a warning message to stderr. if "set" the exit value of pax
145  *        will be non-zero.
146  */
147 
148 void
tty_warn(int set,const char * fmt,...)149 tty_warn(int set, const char *fmt, ...)
150 {
151           va_list ap;
152           va_start(ap, fmt);
153           if (set)
154                     exit_val = 1;
155           /*
156            * when vflag we better ship out an extra \n to get this message on a
157            * line by itself
158            */
159           if ((Vflag || vflag) && vfpart) {
160                     (void)fputc('\n', stderr);
161                     vfpart = 0;
162           }
163           (void)fprintf(stderr, "%s: ", argv0);
164           (void)vfprintf(stderr, fmt, ap);
165           va_end(ap);
166           (void)fputc('\n', stderr);
167 }
168 
169 /*
170  * syswarn()
171  *        write a warning message to stderr. if "set" the exit value of pax
172  *        will be non-zero.
173  */
174 
175 void
syswarn(int set,int errnum,const char * fmt,...)176 syswarn(int set, int errnum, const char *fmt, ...)
177 {
178           va_list ap;
179           va_start(ap, fmt);
180           if (set)
181                     exit_val = 1;
182           /*
183            * when vflag we better ship out an extra \n to get this message on a
184            * line by itself
185            */
186           if ((Vflag || vflag) && vfpart) {
187                     (void)fputc('\n', stdout);
188                     vfpart = 0;
189           }
190           (void)fprintf(stderr, "%s: ", argv0);
191           (void)vfprintf(stderr, fmt, ap);
192           va_end(ap);
193 
194           /*
195            * format and print the errno
196            */
197           if (errnum > 0)
198                     (void)fprintf(stderr, " (%s)", strerror(errnum));
199           (void)fputc('\n', stderr);
200 }
201