.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | 3 | * Copyright 2011 The Chromium Authors, All Rights Reserved. |
---|
3 | 4 | * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc. |
---|
4 | 5 | * |
---|
5 | 6 | * util_is_printable_string contributed by |
---|
6 | 7 | * Pantelis Antoniou <pantelis.antoniou AT gmail.com> |
---|
7 | | - * |
---|
8 | | - * This program is free software; you can redistribute it and/or |
---|
9 | | - * modify it under the terms of the GNU General Public License as |
---|
10 | | - * published by the Free Software Foundation; either version 2 of the |
---|
11 | | - * License, or (at your option) any later version. |
---|
12 | | - * |
---|
13 | | - * This program is distributed in the hope that it will be useful, |
---|
14 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | | - * General Public License for more details. |
---|
17 | | - * |
---|
18 | | - * You should have received a copy of the GNU General Public License |
---|
19 | | - * along with this program; if not, write to the Free Software |
---|
20 | | - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
---|
21 | | - * USA |
---|
22 | 8 | */ |
---|
23 | 9 | |
---|
24 | 10 | #include <ctype.h> |
---|
.. | .. |
---|
27 | 13 | #include <stdarg.h> |
---|
28 | 14 | #include <string.h> |
---|
29 | 15 | #include <assert.h> |
---|
| 16 | +#include <inttypes.h> |
---|
30 | 17 | |
---|
31 | 18 | #include <errno.h> |
---|
32 | 19 | #include <fcntl.h> |
---|
.. | .. |
---|
46 | 33 | return d; |
---|
47 | 34 | } |
---|
48 | 35 | |
---|
49 | | -/* based in part from (3) vsnprintf */ |
---|
50 | | -int xasprintf(char **strp, const char *fmt, ...) |
---|
| 36 | +int xavsprintf_append(char **strp, const char *fmt, va_list ap) |
---|
51 | 37 | { |
---|
52 | | - int n, size = 128; /* start with 128 bytes */ |
---|
| 38 | + int n, size = 0; /* start with 128 bytes */ |
---|
53 | 39 | char *p; |
---|
54 | | - va_list ap; |
---|
| 40 | + va_list ap_copy; |
---|
55 | 41 | |
---|
56 | | - /* initial pointer is NULL making the fist realloc to be malloc */ |
---|
57 | | - p = NULL; |
---|
58 | | - while (1) { |
---|
59 | | - p = xrealloc(p, size); |
---|
| 42 | + p = *strp; |
---|
| 43 | + if (p) |
---|
| 44 | + size = strlen(p); |
---|
60 | 45 | |
---|
61 | | - /* Try to print in the allocated space. */ |
---|
62 | | - va_start(ap, fmt); |
---|
63 | | - n = vsnprintf(p, size, fmt, ap); |
---|
64 | | - va_end(ap); |
---|
| 46 | + va_copy(ap_copy, ap); |
---|
| 47 | + n = vsnprintf(NULL, 0, fmt, ap_copy) + 1; |
---|
| 48 | + va_end(ap_copy); |
---|
65 | 49 | |
---|
66 | | - /* If that worked, return the string. */ |
---|
67 | | - if (n > -1 && n < size) |
---|
68 | | - break; |
---|
69 | | - /* Else try again with more space. */ |
---|
70 | | - if (n > -1) /* glibc 2.1 */ |
---|
71 | | - size = n + 1; /* precisely what is needed */ |
---|
72 | | - else /* glibc 2.0 */ |
---|
73 | | - size *= 2; /* twice the old size */ |
---|
74 | | - } |
---|
| 50 | + p = xrealloc(p, size + n); |
---|
| 51 | + |
---|
| 52 | + n = vsnprintf(p + size, n, fmt, ap); |
---|
| 53 | + |
---|
75 | 54 | *strp = p; |
---|
76 | 55 | return strlen(p); |
---|
| 56 | +} |
---|
| 57 | + |
---|
| 58 | +int xasprintf_append(char **strp, const char *fmt, ...) |
---|
| 59 | +{ |
---|
| 60 | + int n; |
---|
| 61 | + va_list ap; |
---|
| 62 | + |
---|
| 63 | + va_start(ap, fmt); |
---|
| 64 | + n = xavsprintf_append(strp, fmt, ap); |
---|
| 65 | + va_end(ap); |
---|
| 66 | + |
---|
| 67 | + return n; |
---|
| 68 | +} |
---|
| 69 | + |
---|
| 70 | +int xasprintf(char **strp, const char *fmt, ...) |
---|
| 71 | +{ |
---|
| 72 | + int n; |
---|
| 73 | + va_list ap; |
---|
| 74 | + |
---|
| 75 | + *strp = NULL; |
---|
| 76 | + |
---|
| 77 | + va_start(ap, fmt); |
---|
| 78 | + n = xavsprintf_append(strp, fmt, ap); |
---|
| 79 | + va_end(ap); |
---|
| 80 | + |
---|
| 81 | + return n; |
---|
77 | 82 | } |
---|
78 | 83 | |
---|
79 | 84 | char *join_path(const char *path, const char *name) |
---|
.. | .. |
---|
227 | 232 | return val; |
---|
228 | 233 | } |
---|
229 | 234 | |
---|
230 | | -int utilfdt_read_err_len(const char *filename, char **buffp, off_t *len) |
---|
| 235 | +int utilfdt_read_err(const char *filename, char **buffp, size_t *len) |
---|
231 | 236 | { |
---|
232 | 237 | int fd = 0; /* assume stdin */ |
---|
233 | 238 | char *buf = NULL; |
---|
234 | | - off_t bufsize = 1024, offset = 0; |
---|
| 239 | + size_t bufsize = 1024, offset = 0; |
---|
235 | 240 | int ret = 0; |
---|
236 | 241 | |
---|
237 | 242 | *buffp = NULL; |
---|
.. | .. |
---|
264 | 269 | free(buf); |
---|
265 | 270 | else |
---|
266 | 271 | *buffp = buf; |
---|
267 | | - *len = bufsize; |
---|
| 272 | + if (len) |
---|
| 273 | + *len = bufsize; |
---|
268 | 274 | return ret; |
---|
269 | 275 | } |
---|
270 | 276 | |
---|
271 | | -int utilfdt_read_err(const char *filename, char **buffp) |
---|
272 | | -{ |
---|
273 | | - off_t len; |
---|
274 | | - return utilfdt_read_err_len(filename, buffp, &len); |
---|
275 | | -} |
---|
276 | | - |
---|
277 | | -char *utilfdt_read_len(const char *filename, off_t *len) |
---|
| 277 | +char *utilfdt_read(const char *filename, size_t *len) |
---|
278 | 278 | { |
---|
279 | 279 | char *buff; |
---|
280 | | - int ret = utilfdt_read_err_len(filename, &buff, len); |
---|
| 280 | + int ret = utilfdt_read_err(filename, &buff, len); |
---|
281 | 281 | |
---|
282 | 282 | if (ret) { |
---|
283 | 283 | fprintf(stderr, "Couldn't open blob from '%s': %s\n", filename, |
---|
.. | .. |
---|
286 | 286 | } |
---|
287 | 287 | /* Successful read */ |
---|
288 | 288 | return buff; |
---|
289 | | -} |
---|
290 | | - |
---|
291 | | -char *utilfdt_read(const char *filename) |
---|
292 | | -{ |
---|
293 | | - off_t len; |
---|
294 | | - return utilfdt_read_len(filename, &len); |
---|
295 | 289 | } |
---|
296 | 290 | |
---|
297 | 291 | int utilfdt_write_err(const char *filename, const void *blob) |
---|
.. | .. |
---|
400 | 394 | |
---|
401 | 395 | printf(" = <"); |
---|
402 | 396 | for (i = 0, len /= 4; i < len; i++) |
---|
403 | | - printf("0x%08x%s", fdt32_to_cpu(cell[i]), |
---|
| 397 | + printf("0x%08" PRIx32 "%s", fdt32_to_cpu(cell[i]), |
---|
404 | 398 | i < (len - 1) ? " " : ""); |
---|
405 | 399 | printf(">"); |
---|
406 | 400 | } else { |
---|