Debian Codegraph @

tty_subs.c

raw symbols SCIP
1 /* $OpenBSD: tty_subs.c,v 1.17 2016/08/26 04:22:13 guenther Exp $ */
2 /* $NetBSD: tty_subs.c,v 1.5 1995/03/21 09:07:52 cgd Exp $ */
3
4 /*-
5 * Copyright (c) 2012, 2014, 2016
6 * mirabilos <[email protected]>
7 * Copyright (c) 1992 Keith Muller.
8 * Copyright (c) 1992, 1993
9 * The Regents of the University of California. All rights reserved.
10 *
11 * This code is derived from software contributed to Berkeley by
12 * Keith Muller of the University of California, San Diego.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * 2. Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in the
21 * documentation and/or other materials provided with the distribution.
22 * 3. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <errno.h>
42 #include <fcntl.h>
43 #include <stdarg.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #if HAVE_STRINGS_H
48 #include <strings.h>
49 #endif
50 #include <unistd.h>
51
52 #include "pax.h"
53 #include "extern.h"
54
55 __RCSID("$MirOS: src/bin/pax/tty_subs.c,v 1.13 2018/12/12 18:08:48 tg Exp $");
56
57 /*
58 * routines that deal with I/O to and from the user
59 */
60
61 /* device for interactive I/O */
62 static const char devtty[] = "/dev/tty";
63 /* file descriptor for accessing it */
64 static int ttyfd;
65
66 /*
67 * tty_init()
68 * try to open the controlling terminal (if any) for this process. if the
69 * open fails, future ops that require user input will get an EOF
70 */
71
72 int
73 tty_init(void)
74 {
75 if ((ttyfd = binopen2(BO_CLEXEC, devtty, O_RDWR)) == -1 && iflag) {
76 syswarn(1, errno, "Fatal error, cannot open %s", devtty);
77 return (-1);
78 }
79
80 return (0);
81 }
82
83 /*
84 * tty_prnt()
85 * print a message using the specified format to the controlling tty
86 * if there is no controlling terminal, just return.
87 */
88
89 void
90 tty_prnt(const char *fmt, ...)
91 {
92 va_list ap;
93 char *cp;
94 int len;
95
96 va_start(ap, fmt);
97 if (ttyfd != -1) {
98 len = vasprintf(&cp, fmt, ap);
99 if (len != -1) {
100 dwrite(ttyfd, cp, len);
101 free(cp);
102 }
103 }
104 va_end(ap);
105 }
106
107 /*
108 * tty_rd()
109 * read a string from the controlling terminal if it is open
110 * Return:
111 * pointer caller must free if data was read, NULL otherwise
112 */
113
114 char *
115 tty_rd(void)
116 {
117 return (ttyfd == -1 ? NULL : fdgetline(ttyfd));
118 }
119
120 /*
121 * paxwarn()
122 * write a warning message to stderr. if "set" the exit value of pax
123 * will be non-zero.
124 */
125
126 void
127 paxwarn(int set, const char *fmt, ...)
128 {
129 va_list ap;
130
131 va_start(ap, fmt);
132 if (set)
133 exit_val = 1;
134 /*
135 * when vflag we better ship out an extra \n to get this message on a
136 * line by itself
137 */
138 if (vfpart) {
139 (void)fflush(listf);
140 (void)fputc('\n', stderr);
141 vfpart = 0;
142 }
143 (void)fprintf(stderr, "%s: ", argv0);
144 (void)vfprintf(stderr, fmt, ap);
145 va_end(ap);
146 (void)fputc('\n', stderr);
147 }
148
149 /*
150 * syswarn()
151 * write a warning message to stderr. if "set" the exit value of pax
152 * will be non-zero.
153 */
154
155 void
156 syswarn(int set, int errnum, const char *fmt, ...)
157 {
158 va_list ap;
159
160 va_start(ap, fmt);
161 if (set)
162 exit_val = 1;
163 /*
164 * when vflag we better ship out an extra \n to get this message on a
165 * line by itself
166 */
167 if (vfpart) {
168 (void)fflush(listf);
169 (void)fputc('\n', stderr);
170 vfpart = 0;
171 }
172 (void)fprintf(stderr, "%s: ", argv0);
173 (void)vfprintf(stderr, fmt, ap);
174 va_end(ap);
175
176 /*
177 * format and print the errno
178 */
179 if (errnum > 0)
180 (void)fprintf(stderr, ": %s", strerror(errnum));
181 (void)fputc('\n', stderr);
182 }
183
184 /*
185 * fdgetline()
186 * read a line from a file descriptor, similar to fgetln(3).
187 * caller must free(3) the result string.
188 */
189
190 char fdgetline_err;
191
192 char *
193 fdgetline(int fd)
194 {
195 size_t n = 0;
196 char *rv = NULL;
197 size_t z = 32;
198 ssize_t rdr;
199 char *np;
200 int term;
201
202 term = zeroflag ? '\0' : '\n'; /* path termination character */
203
204 goto fdgetline_alloc;
205
206 do {
207 if (n == z) {
208 z <<= 1;
209 if (z < n) {
210 /* overflow */
211 break;
212 }
213 fdgetline_alloc:
214 if ((np = realloc(rv, z)) == NULL) {
215 /* allocation error */
216 break;
217 }
218 rv = np;
219 }
220
221 rdr = read(fd, rv + n, 1);
222 if (rdr == 0 && n == 0) {
223 /* EOF reached, but nothing ever read */
224 free(rv);
225 rv = NULL;
226 goto fdgetline_eod;
227 }
228 if (rdr == 0 || (rdr == 1 && rv[n] == term)) {
229 /* EOF or EOL */
230 rv[n++] = 0;
231 if ((np = realloc(rv, n)) != NULL)
232 rv = np;
233 fdgetline_eod:
234 fdgetline_err = 0;
235 return (rv);
236 }
237 ++n;
238 } while (rdr == 1);
239
240 /* fall through do-while if rdr > 1 (read too much) or < 0 (error) */
241 /* get here via break on memory allocation errors */
242
243 free(rv);
244 fdgetline_err = 1;
245 return (NULL);
246 }