diff options
Diffstat (limited to 'scripts/dtc/util.c')
-rw-r--r-- | scripts/dtc/util.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/scripts/dtc/util.c b/scripts/dtc/util.c index fb124eea4919..3550f86bd6df 100644 --- a/scripts/dtc/util.c +++ b/scripts/dtc/util.c | |||
@@ -46,6 +46,36 @@ char *xstrdup(const char *s) | |||
46 | return d; | 46 | return d; |
47 | } | 47 | } |
48 | 48 | ||
49 | /* based in part from (3) vsnprintf */ | ||
50 | int xasprintf(char **strp, const char *fmt, ...) | ||
51 | { | ||
52 | int n, size = 128; /* start with 128 bytes */ | ||
53 | char *p; | ||
54 | va_list ap; | ||
55 | |||
56 | /* initial pointer is NULL making the fist realloc to be malloc */ | ||
57 | p = NULL; | ||
58 | while (1) { | ||
59 | p = xrealloc(p, size); | ||
60 | |||
61 | /* Try to print in the allocated space. */ | ||
62 | va_start(ap, fmt); | ||
63 | n = vsnprintf(p, size, fmt, ap); | ||
64 | va_end(ap); | ||
65 | |||
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 | } | ||
75 | *strp = p; | ||
76 | return strlen(p); | ||
77 | } | ||
78 | |||
49 | char *join_path(const char *path, const char *name) | 79 | char *join_path(const char *path, const char *name) |
50 | { | 80 | { |
51 | int lenp = strlen(path); | 81 | int lenp = strlen(path); |