hc
2023-12-08 01573e231f18eb2d99162747186f59511f56b64d
kernel/scripts/dtc/util.c
....@@ -1,24 +1,10 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * Copyright 2011 The Chromium Authors, All Rights Reserved.
34 * Copyright 2008 Jon Loeliger, Freescale Semiconductor, Inc.
45 *
56 * util_is_printable_string contributed by
67 * 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
228 */
239
2410 #include <ctype.h>
....@@ -27,6 +13,7 @@
2713 #include <stdarg.h>
2814 #include <string.h>
2915 #include <assert.h>
16
+#include <inttypes.h>
3017
3118 #include <errno.h>
3219 #include <fcntl.h>
....@@ -46,34 +33,52 @@
4633 return d;
4734 }
4835
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)
5137 {
52
- int n, size = 128; /* start with 128 bytes */
38
+ int n, size = 0; /* start with 128 bytes */
5339 char *p;
54
- va_list ap;
40
+ va_list ap_copy;
5541
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);
6045
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);
6549
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
+
7554 *strp = p;
7655 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;
7782 }
7883
7984 char *join_path(const char *path, const char *name)
....@@ -227,11 +232,11 @@
227232 return val;
228233 }
229234
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)
231236 {
232237 int fd = 0; /* assume stdin */
233238 char *buf = NULL;
234
- off_t bufsize = 1024, offset = 0;
239
+ size_t bufsize = 1024, offset = 0;
235240 int ret = 0;
236241
237242 *buffp = NULL;
....@@ -264,20 +269,15 @@
264269 free(buf);
265270 else
266271 *buffp = buf;
267
- *len = bufsize;
272
+ if (len)
273
+ *len = bufsize;
268274 return ret;
269275 }
270276
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)
278278 {
279279 char *buff;
280
- int ret = utilfdt_read_err_len(filename, &buff, len);
280
+ int ret = utilfdt_read_err(filename, &buff, len);
281281
282282 if (ret) {
283283 fprintf(stderr, "Couldn't open blob from '%s': %s\n", filename,
....@@ -286,12 +286,6 @@
286286 }
287287 /* Successful read */
288288 return buff;
289
-}
290
-
291
-char *utilfdt_read(const char *filename)
292
-{
293
- off_t len;
294
- return utilfdt_read_len(filename, &len);
295289 }
296290
297291 int utilfdt_write_err(const char *filename, const void *blob)
....@@ -400,7 +394,7 @@
400394
401395 printf(" = <");
402396 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]),
404398 i < (len - 1) ? " " : "");
405399 printf(">");
406400 } else {