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