aboutsummaryrefslogtreecommitdiff
path: root/contrib/dtc/util.c
diff options
context:
space:
mode:
authorOleksandr Tymoshenko <gonzo@FreeBSD.org>2017-03-10 17:36:05 +0000
committerOleksandr Tymoshenko <gonzo@FreeBSD.org>2017-03-10 17:36:05 +0000
commitb718842baa9b4c088ab5d2f79c72b65ccf38ecd2 (patch)
tree5171c9e7690e43a60a3223a1e9a0a0142a73085c /contrib/dtc/util.c
parent09dced1093fd9571bc9c52ce0dfb03596779a5ae (diff)
parentf059bd1ebfc4cf2e96c4639ad7fa6cf3a3198a2f (diff)
downloadsrc-b718842baa9b4c088ab5d2f79c72b65ccf38ecd2.tar.gz
src-b718842baa9b4c088ab5d2f79c72b65ccf38ecd2.zip
Merge from vendor branch importing dtc 1.4.3
Major new feature in this import is FDT overlay support
Notes
Notes: svn path=/head/; revision=315009
Diffstat (limited to 'contrib/dtc/util.c')
-rw-r--r--contrib/dtc/util.c49
1 files changed, 37 insertions, 12 deletions
diff --git a/contrib/dtc/util.c b/contrib/dtc/util.c
index 330b594b70ba..3550f86bd6df 100644
--- a/contrib/dtc/util.c
+++ b/contrib/dtc/util.c
@@ -39,11 +39,41 @@
char *xstrdup(const char *s)
{
int len = strlen(s) + 1;
- char *dup = xmalloc(len);
+ char *d = xmalloc(len);
- memcpy(dup, s, len);
+ memcpy(d, s, len);
- return dup;
+ return d;
+}
+
+/* based in part from (3) vsnprintf */
+int xasprintf(char **strp, const char *fmt, ...)
+{
+ int n, size = 128; /* start with 128 bytes */
+ char *p;
+ va_list ap;
+
+ /* initial pointer is NULL making the fist realloc to be malloc */
+ p = NULL;
+ while (1) {
+ p = xrealloc(p, size);
+
+ /* Try to print in the allocated space. */
+ va_start(ap, fmt);
+ n = vsnprintf(p, size, fmt, ap);
+ va_end(ap);
+
+ /* If that worked, return the string. */
+ if (n > -1 && n < size)
+ break;
+ /* Else try again with more space. */
+ if (n > -1) /* glibc 2.1 */
+ size = n + 1; /* precisely what is needed */
+ else /* glibc 2.0 */
+ size *= 2; /* twice the old size */
+ }
+ *strp = p;
+ return strlen(p);
}
char *join_path(const char *path, const char *name)
@@ -152,7 +182,6 @@ char get_escape_char(const char *s, int *i)
int j = *i + 1;
char val;
- assert(c);
switch (c) {
case 'a':
val = '\a';
@@ -219,10 +248,6 @@ int utilfdt_read_err_len(const char *filename, char **buffp, off_t *len)
if (offset == bufsize) {
bufsize *= 2;
buf = xrealloc(buf, bufsize);
- if (!buf) {
- ret = ENOMEM;
- break;
- }
}
ret = read(fd, &buf[offset], bufsize - offset);
@@ -353,7 +378,6 @@ int utilfdt_decode_type(const char *fmt, int *type, int *size)
void utilfdt_print_data(const char *data, int len)
{
int i;
- const char *p = data;
const char *s;
/* no data, don't print */
@@ -375,11 +399,12 @@ void utilfdt_print_data(const char *data, int len)
const uint32_t *cell = (const uint32_t *)data;
printf(" = <");
- for (i = 0; i < len; i += 4)
- printf("0x%08x%s", fdt32_to_cpu(cell[i / 4]),
- i < (len - 4) ? " " : "");
+ for (i = 0, len /= 4; i < len; i++)
+ printf("0x%08x%s", fdt32_to_cpu(cell[i]),
+ i < (len - 1) ? " " : "");
printf(">");
} else {
+ const unsigned char *p = (const unsigned char *)data;
printf(" = [");
for (i = 0; i < len; i++)
printf("%02x%s", *p++, i < len - 1 ? " " : "");