GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: bin/pax/tty_subs.c Lines: 26 50 52.0 %
Date: 2016-12-06 Branches: 8 28 28.6 %

Line Branch Exec Source
1
/*	$OpenBSD: tty_subs.c,v 1.16 2014/11/23 05:28:12 guenther 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. Neither the name of the University nor the names of its contributors
21
 *    may be used to endorse or promote products derived from this software
22
 *    without specific prior written permission.
23
 *
24
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34
 * SUCH DAMAGE.
35
 */
36
37
#include <sys/types.h>
38
#include <sys/time.h>
39
#include <sys/stat.h>
40
#include <fcntl.h>
41
#include <stdio.h>
42
#include <errno.h>
43
#include <unistd.h>
44
#include <stdlib.h>
45
#include <string.h>
46
#include "pax.h"
47
#include "extern.h"
48
#include <stdarg.h>
49
50
/*
51
 * routines that deal with I/O to and from the user
52
 */
53
54
#define DEVTTY		"/dev/tty"	/* device for interactive i/o */
55
static FILE *ttyoutf = NULL;		/* output pointing at control tty */
56
static FILE *ttyinf = NULL;		/* input pointing at control tty */
57
58
/*
59
 * tty_init()
60
 *	try to open the controlling terminal (if any) for this process. if the
61
 *	open fails, future ops that require user input will get an EOF
62
 */
63
64
int
65
tty_init(void)
66
32
{
67
	int ttyfd;
68
69
32
	if ((ttyfd = open(DEVTTY, O_RDWR | O_CLOEXEC)) >= 0) {
70
32
		if ((ttyoutf = fdopen(ttyfd, "w")) != NULL) {
71
32
			if ((ttyinf = fdopen(ttyfd, "r")) != NULL)
72
32
				return(0);
73
			(void)fclose(ttyoutf);
74
		}
75
		(void)close(ttyfd);
76
	}
77
78
	if (iflag) {
79
		paxwarn(1, "Fatal error, cannot open %s", DEVTTY);
80
		return(-1);
81
	}
82
	return(0);
83
}
84
85
/*
86
 * tty_prnt()
87
 *	print a message using the specified format to the controlling tty
88
 *	if there is no controlling terminal, just return.
89
 */
90
91
void
92
tty_prnt(const char *fmt, ...)
93
{
94
	va_list ap;
95
96
	va_start(ap, fmt);
97
	if (ttyoutf == NULL) {
98
		va_end(ap);
99
		return;
100
	}
101
	(void)vfprintf(ttyoutf, fmt, ap);
102
	va_end(ap);
103
	(void)fflush(ttyoutf);
104
}
105
106
/*
107
 * tty_read()
108
 *	read a string from the controlling terminal if it is open into the
109
 *	supplied buffer
110
 * Return:
111
 *	0 if data was read, -1 otherwise.
112
 */
113
114
int
115
tty_read(char *str, int len)
116
{
117
	if (ttyinf == NULL || fgets(str, len, ttyinf) == NULL)
118
		return(-1);
119
120
	/*
121
	 * strip off that trailing newline
122
	 */
123
	str[strcspn(str, "\n")] = '\0';
124
	return(0);
125
}
126
127
/*
128
 * paxwarn()
129
 *	write a warning message to stderr. if "set" the exit value of pax
130
 *	will be non-zero.
131
 */
132
133
void
134
paxwarn(int set, const char *fmt, ...)
135
2
{
136
	va_list ap;
137
138
2
	va_start(ap, fmt);
139
2
	if (set)
140
		exit_val = 1;
141
	/*
142
	 * when vflag we better ship out an extra \n to get this message on a
143
	 * line by itself
144
	 */
145

2
	if (vflag && vfpart) {
146
		(void)fflush(listf);
147
		(void)fputc('\n', stderr);
148
		vfpart = 0;
149
	}
150
2
	(void)fprintf(stderr, "%s: ", argv0);
151
2
	(void)vfprintf(stderr, fmt, ap);
152
2
	va_end(ap);
153
2
	(void)fputc('\n', stderr);
154
2
}
155
156
/*
157
 * syswarn()
158
 *	write a warning message to stderr. if "set" the exit value of pax
159
 *	will be non-zero.
160
 */
161
162
void
163
syswarn(int set, int errnum, const char *fmt, ...)
164
2
{
165
	va_list ap;
166
167
2
	va_start(ap, fmt);
168
2
	if (set)
169
2
		exit_val = 1;
170
	/*
171
	 * when vflag we better ship out an extra \n to get this message on a
172
	 * line by itself
173
	 */
174

2
	if (vflag && vfpart) {
175
		(void)fflush(listf);
176
		(void)fputc('\n', stderr);
177
		vfpart = 0;
178
	}
179
2
	(void)fprintf(stderr, "%s: ", argv0);
180
2
	(void)vfprintf(stderr, fmt, ap);
181
2
	va_end(ap);
182
183
	/*
184
	 * format and print the errno
185
	 */
186
2
	if (errnum > 0)
187
2
		(void)fprintf(stderr, ": %s", strerror(errnum));
188
2
	(void)fputc('\n', stderr);
189
2
}