xref: /netbsd-src/usr.bin/vndcompress/utils.c (revision 946379e7b37692fc43f68eb0d1c10daa0a7f3b6c)
1 /*	$NetBSD: utils.c,v 1.4 2014/01/22 06:15:31 riastradh Exp $	*/
2 
3 /*-
4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Taylor R. Campbell.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: utils.c,v 1.4 2014/01/22 06:15:31 riastradh Exp $");
34 
35 #include <sys/types.h>
36 
37 #include <assert.h>
38 #include <err.h>
39 #include <errno.h>
40 #include <inttypes.h>
41 #include <limits.h>
42 #include <signal.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <unistd.h>
47 
48 #include "common.h"
49 
50 /* XXX Seems to be missing from <stdio.h>...  */
51 int	snprintf_ss(char *restrict, size_t, const char *restrict, ...)
52 	    __printflike(3, 4);
53 int	vsnprintf_ss(char *restrict, size_t, const char *restrict, va_list)
54 	    __printflike(3, 0);
55 
56 #include "utils.h"
57 
58 /*
59  * Read, returning partial data only at end of file.
60  */
61 ssize_t
62 read_block(int fd, void *buffer, size_t n)
63 {
64 	char *p = buffer, *const end __unused = (p + n);
65 	size_t total_read = 0;
66 
67 	while (n > 0) {
68 		const ssize_t n_read = read(fd, p, n);
69 		if (n_read == -1)
70 			return -1;
71 		assert(n_read >= 0);
72 		if (n_read == 0)
73 			break;
74 
75 		assert((size_t)n_read <= n);
76 		n -= (size_t)n_read;
77 
78 		assert(p <= end);
79 		assert(n_read <= (end - p));
80 		p += (size_t)n_read;
81 
82 		assert((size_t)n_read <= (SIZE_MAX - total_read));
83 		total_read += (size_t)n_read;
84 	}
85 
86 	return total_read;
87 }
88 
89 /*
90  * Read from a specified position, returning partial data only at end
91  * of file.
92  */
93 ssize_t
94 pread_block(int fd, void *buffer, size_t n, off_t fdpos)
95 {
96 	char *p = buffer, *const end __unused = (p + n);
97 	size_t total_read = 0;
98 
99 	assert(0 <= fdpos);
100 	assert(n <= (OFF_MAX - (uintmax_t)fdpos));
101 
102 	while (n > 0) {
103 		assert(total_read <= n);
104 		const ssize_t n_read = pread(fd, p, n, (fdpos + total_read));
105 		if (n_read == -1)
106 			return -1;
107 		assert(n_read >= 0);
108 		if (n_read == 0)
109 			break;
110 
111 		assert((size_t)n_read <= n);
112 		n -= (size_t)n_read;
113 
114 		assert(p <= end);
115 		assert(n_read <= (end - p));
116 		p += (size_t)n_read;
117 
118 		assert((size_t)n_read <= (SIZE_MAX - total_read));
119 		total_read += (size_t)n_read;
120 	}
121 
122 	return total_read;
123 }
124 
125 /*
126  * Signal-safe err/warn utilities.  The errno varieties are limited to
127  * having no format arguments for reasons of laziness.
128  */
129 
130 void
131 err_ss(int exit_value, const char *msg)
132 {
133 	warn_ss(msg);
134 	_Exit(exit_value);
135 }
136 
137 void
138 errx_ss(int exit_value, const char *format, ...)
139 {
140 	va_list va;
141 
142 	va_start(va, format);
143 	vwarnx_ss(format, va);
144 	va_end(va);
145 	_Exit(exit_value);
146 }
147 
148 void
149 warn_ss(const char *msg)
150 {
151 	int error = errno;
152 
153 	warnx_ss("%s: %s", msg, strerror(error));
154 
155 	errno = error;
156 }
157 
158 void
159 warnx_ss(const char *format, ...)
160 {
161 	va_list va;
162 
163 	va_start(va, format);
164 	vwarnx_ss(format, va);
165 	va_end(va);
166 }
167 
168 void
169 vwarnx_ss(const char *format, va_list va)
170 {
171 	char buf[128];
172 
173 	(void)strlcpy(buf, getprogname(), sizeof(buf));
174 	(void)strlcat(buf, ": ", sizeof(buf));
175 
176 	const int n = vsnprintf_ss(&buf[strlen(buf)], (sizeof(buf) -
177 		strlen(buf)), format, va);
178 	if (n <= 0) {
179 		const char fallback[] =
180 		    "vndcompress: Help!  I'm trapped in a signal handler!\n";
181 		(void)write(STDERR_FILENO, fallback, __arraycount(fallback));
182 	} else {
183 		(void)strlcat(buf, "\n", sizeof(buf));
184 		(void)write(STDERR_FILENO, buf, strlen(buf));
185 	}
186 }
187 
188 void
189 block_signals(sigset_t *old_sigmask)
190 {
191 	sigset_t block;
192 
193 	(void)sigfillset(&block);
194 	(void)sigprocmask(SIG_BLOCK, &block, old_sigmask);
195 }
196 
197 void
198 restore_sigmask(const sigset_t *sigmask)
199 {
200 
201 	(void)sigprocmask(SIG_SETMASK, sigmask, NULL);
202 }
203