| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: tprof_top.c,v 1.7 2022/12/16 08:02:04 ryo Exp $ */ | | 1 | /* $NetBSD: tprof_top.c,v 1.8 2022/12/23 19:37:06 christos Exp $ */ |
2 | | | 2 | |
3 | /*- | | 3 | /*- |
4 | * Copyright (c) 2022 Ryo Shimizu <ryo@nerv.org> | | 4 | * Copyright (c) 2022 Ryo Shimizu <ryo@nerv.org> |
5 | * All rights reserved. | | 5 | * All rights reserved. |
6 | * | | 6 | * |
7 | * Redistribution and use in source and binary forms, with or without | | 7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions | | 8 | * modification, are permitted provided that the following conditions |
9 | * are met: | | 9 | * are met: |
10 | * 1. Redistributions of source code must retain the above copyright | | 10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. | | 11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright | | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the | | 13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. | | 14 | * documentation and/or other materials provided with the distribution. |
| @@ -18,27 +18,27 @@ | | | @@ -18,27 +18,27 @@ |
18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | 18 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, |
20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | | 20 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | | 21 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | | 22 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
23 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | | 23 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
24 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | | 24 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
25 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | | 25 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
26 | * POSSIBILITY OF SUCH DAMAGE. | | 26 | * POSSIBILITY OF SUCH DAMAGE. |
27 | */ | | 27 | */ |
28 | | | 28 | |
29 | #include <sys/cdefs.h> | | 29 | #include <sys/cdefs.h> |
30 | #ifndef lint | | 30 | #ifndef lint |
31 | __RCSID("$NetBSD: tprof_top.c,v 1.7 2022/12/16 08:02:04 ryo Exp $"); | | 31 | __RCSID("$NetBSD: tprof_top.c,v 1.8 2022/12/23 19:37:06 christos Exp $"); |
32 | #endif /* not lint */ | | 32 | #endif /* not lint */ |
33 | | | 33 | |
34 | #include <sys/param.h> | | 34 | #include <sys/param.h> |
35 | #include <sys/types.h> | | 35 | #include <sys/types.h> |
36 | #include <sys/ioctl.h> | | 36 | #include <sys/ioctl.h> |
37 | #include <sys/rbtree.h> | | 37 | #include <sys/rbtree.h> |
38 | #include <sys/select.h> | | 38 | #include <sys/select.h> |
39 | #include <sys/time.h> | | 39 | #include <sys/time.h> |
40 | | | 40 | |
41 | #include <assert.h> | | 41 | #include <assert.h> |
42 | #include <err.h> | | 42 | #include <err.h> |
43 | #include <errno.h> | | 43 | #include <errno.h> |
44 | #include <fcntl.h> | | 44 | #include <fcntl.h> |
| @@ -184,41 +184,45 @@ lim_newline(int *lim) | | | @@ -184,41 +184,45 @@ lim_newline(int *lim) |
184 | *lim = win.ws_col; | | 184 | *lim = win.ws_col; |
185 | } | | 185 | } |
186 | | | 186 | |
187 | static int | | 187 | static int |
188 | lim_printf(int *lim, const char *fmt, ...) | | 188 | lim_printf(int *lim, const char *fmt, ...) |
189 | { | | 189 | { |
190 | va_list ap; | | 190 | va_list ap; |
191 | size_t written; | | 191 | size_t written; |
192 | char *p; | | 192 | char *p; |
193 | | | 193 | |
194 | if (*lim <= 0) | | 194 | if (*lim <= 0) |
195 | return 0; | | 195 | return 0; |
196 | | | 196 | |
197 | p = alloca(*lim + 1); | | 197 | p = malloc(*lim + 1); |
| | | 198 | if (p == NULL) |
| | | 199 | return -1; |
198 | | | 200 | |
199 | va_start(ap, fmt); | | 201 | va_start(ap, fmt); |
200 | vsnprintf(p, *lim + 1, fmt, ap); | | 202 | vsnprintf(p, *lim + 1, fmt, ap); |
201 | va_end(ap); | | 203 | va_end(ap); |
202 | | | 204 | |
203 | written = strlen(p); | | 205 | written = strlen(p); |
204 | if (written == 0) { | | 206 | if (written == 0) { |
| | | 207 | free(p); |
205 | *lim = 0; | | 208 | *lim = 0; |
206 | return 0; | | 209 | return 0; |
207 | } | | 210 | } |
208 | | | 211 | |
209 | fwrite(p, written, 1, stdout); | | 212 | fwrite(p, written, 1, stdout); |
210 | *lim -= written; | | 213 | *lim -= written; |
211 | | | 214 | |
| | | 215 | free(p); |
212 | return written; | | 216 | return written; |
213 | } | | 217 | } |
214 | | | 218 | |
215 | static void | | 219 | static void |
216 | sigwinch_handler(int signo) | | 220 | sigwinch_handler(int signo) |
217 | { | | 221 | { |
218 | char *p; | | 222 | char *p; |
219 | | | 223 | |
220 | win.ws_col = tigetnum("lines"); | | 224 | win.ws_col = tigetnum("lines"); |
221 | win.ws_row = tigetnum("cols"); | | 225 | win.ws_row = tigetnum("cols"); |
222 | | | 226 | |
223 | nontty = ioctl(STDOUT_FILENO, TIOCGWINSZ, &win); | | 227 | nontty = ioctl(STDOUT_FILENO, TIOCGWINSZ, &win); |
224 | if (nontty != 0) { | | 228 | if (nontty != 0) { |