xref: /original-bsd/bin/pax/tty_subs.c (revision ffad4576)
1 /*-
2  * Copyright (c) 1992 Keith Muller.
3  * Copyright (c) 1992 The Regents of the University of California.
4  * 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  * %sccs.include.redist.c%
10  */
11 
12 #ifndef lint
13 static char sccsid[] = "@(#)tty_subs.c	1.1 (Berkeley) 12/13/92";
14 #endif /* not lint */
15 
16 #include <sys/types.h>
17 #include <sys/time.h>
18 #include <sys/stat.h>
19 #include <sys/param.h>
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include "pax.h"
28 #include "extern.h"
29 #if __STDC__
30 #include <stdarg.h>
31 #else
32 #include <varargs.h>
33 #endif
34 
35 /*
36  * routines that deal with I/O to and from the user
37  */
38 
39 #define DEVTTY          "/dev/tty"      /* device for interactive i/o */
40 static FILE *ttyoutf = NULL;		/* output pointing at control tty */
41 static FILE *ttyinf = NULL;		/* input pointing at control tty */
42 extern char *sys_errlist[];		/* errno printable strings */
43 
44 /*
45  * tty_init()
46  *	try to open the controlling termina (if any) for this process. if the
47  *	open fails, future ops that require user input will get an EOF
48  */
49 
50 #if __STDC__
51 int
52 tty_init(void)
53 #else
54 int
55 tty_init()
56 #endif
57 {
58 	int ttyfd;
59 
60         if ((ttyfd = open(DEVTTY, O_RDWR)) >= 0) {
61 		if ((ttyoutf = fdopen(ttyfd, "w")) != NULL) {
62 			if ((ttyinf = fdopen(ttyfd, "r")) != NULL)
63 				return(0);
64 			(void)fclose(ttyoutf);
65 		}
66 		(void)close(ttyfd);
67 	}
68 
69 	if (iflag) {
70 		warn(1, "Fatal error, cannot open %s", DEVTTY);
71 		return(-1);
72 	}
73 	return(0);
74 }
75 
76 /*
77  * tty_prnt()
78  *	print a message using the specified format to the controlling tty
79  *	if there is no controlling terminal, just return.
80  */
81 
82 #if __STDC__
83 void
84 tty_prnt(char *fmt, ...)
85 #else
86 void
87 tty_prnt(fmt, va_alist)
88 	char *fmt;
89 	va_dcl
90 #endif
91 {
92 	va_list ap;
93 #	if __STDC__
94 	va_start(ap, fmt);
95 #	else
96 	va_start(ap);
97 #	endif
98 	if (ttyoutf == NULL)
99 		return;
100 	(void)vfprintf(ttyoutf, fmt, ap);
101 	va_end(ap);
102 	(void)fflush(ttyoutf);
103 }
104 
105 /*
106  * tty_read()
107  *	read a string from the controlling terminal if it is open into the
108  *	supplied buffer
109  * Return:
110  *	0 if data was read, -1 otherwise.
111  */
112 
113 #if __STDC__
114 int
115 tty_read(char *str, int len)
116 #else
117 int
118 tty_read(str, len)
119 	char *str;
120 	int len;
121 #endif
122 {
123 	register char *pt;
124 
125 	if ((--len <= 0) || (ttyinf == NULL) || (fgets(str,len,ttyinf) == NULL))
126 		return(-1);
127 	*(str + len) = '\0';
128 
129 	/*
130 	 * strip off that trailing newline
131 	 */
132 	if ((pt = strchr(str, '\n')) != NULL)
133 		*pt = '\0';
134 	return(0);
135 }
136 
137 /*
138  * warn()
139  *	write a warning message to stderr. if "set" the exit value of pax
140  *	will be non-zero.
141  */
142 
143 #if __STDC__
144 void
145 warn(int set, char *fmt, ...)
146 #else
147 void
148 warn(set, fmt, va_alist)
149 	int set;
150 	char *fmt;
151 	va_dcl
152 #endif
153 {
154 	va_list ap;
155 #	if __STDC__
156 	va_start(ap, fmt);
157 #	else
158 	va_start(ap);
159 #	endif
160 	if (set)
161 		exit_val = 1;
162 	/*
163 	 * when vflag we better ship out an extra \n to get this message on a
164 	 * line by itself
165 	 */
166 	if (vflag && vfpart) {
167 		(void)fputc('\n', stderr);
168 		vfpart = 0;
169 	}
170 	(void)fputs("pax: ", stderr);
171 	(void)vfprintf(stderr, fmt, ap);
172 	va_end(ap);
173 	(void)fputc('\n', stderr);
174 }
175 
176 /*
177  * syswarn()
178  *	write a warning message to stderr. if "set" the exit value of pax
179  *	will be non-zero.
180  */
181 
182 #if __STDC__
183 void
184 syswarn(int set, int errnum, char *fmt, ...)
185 #else
186 void
187 syswarn(set, errnum, fmt, va_alist)
188 	int set;
189 	int errnum;
190 	char *fmt;
191 	va_dcl
192 #endif
193 {
194 	va_list ap;
195 #	if __STDC__
196 	va_start(ap, fmt);
197 #	else
198 	va_start(ap);
199 #	endif
200 	if (set)
201 		exit_val = 1;
202 	/*
203 	 * when vflag we better ship out an extra \n to get this message on a
204 	 * line by itself
205 	 */
206 	if (vflag && vfpart) {
207 		(void)fputc('\n', stderr);
208 		vfpart = 0;
209 	}
210 	(void)fputs("pax: ", stderr);
211 	(void)vfprintf(stderr, fmt, ap);
212 	va_end(ap);
213 
214 	/*
215 	 * format and print the errno
216 	 */
217 	if (errnum > 0)
218 		(void)fprintf(stderr, " <%s>", sys_errlist[errnum]);
219 	(void)fputc('\n', stderr);
220 }
221