diff options
Diffstat (limited to 'scripts/dtc/util.c')
| -rw-r--r-- | scripts/dtc/util.c | 60 |
1 files changed, 39 insertions, 21 deletions
diff --git a/scripts/dtc/util.c b/scripts/dtc/util.c index a69b7a13463d..9c6fb5f286ae 100644 --- a/scripts/dtc/util.c +++ b/scripts/dtc/util.c | |||
| @@ -46,36 +46,54 @@ char *xstrdup(const char *s) | |||
| 46 | return d; | 46 | return d; |
| 47 | } | 47 | } |
| 48 | 48 | ||
| 49 | /* based in part from (3) vsnprintf */ | 49 | int xavsprintf_append(char **strp, const char *fmt, va_list ap) |
| 50 | int xasprintf(char **strp, const char *fmt, ...) | ||
| 51 | { | 50 | { |
| 52 | int n, size = 128; /* start with 128 bytes */ | 51 | int n, size = 0; /* start with 128 bytes */ |
| 53 | char *p; | 52 | char *p; |
| 54 | va_list ap; | 53 | va_list ap_copy; |
| 55 | 54 | ||
| 56 | /* initial pointer is NULL making the fist realloc to be malloc */ | 55 | p = *strp; |
| 57 | p = NULL; | 56 | if (p) |
| 58 | while (1) { | 57 | size = strlen(p); |
| 59 | p = xrealloc(p, size); | ||
| 60 | 58 | ||
| 61 | /* Try to print in the allocated space. */ | 59 | va_copy(ap_copy, ap); |
| 62 | va_start(ap, fmt); | 60 | n = vsnprintf(NULL, 0, fmt, ap_copy) + 1; |
| 63 | n = vsnprintf(p, size, fmt, ap); | 61 | va_end(ap_copy); |
| 64 | va_end(ap); | 62 | |
| 63 | p = xrealloc(p, size + n); | ||
| 64 | |||
| 65 | n = vsnprintf(p + size, n, fmt, ap); | ||
| 65 | 66 | ||
| 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; | 67 | *strp = p; |
| 76 | return strlen(p); | 68 | return strlen(p); |
| 77 | } | 69 | } |
| 78 | 70 | ||
| 71 | int xasprintf_append(char **strp, const char *fmt, ...) | ||
| 72 | { | ||
| 73 | int n; | ||
| 74 | va_list ap; | ||
| 75 | |||
| 76 | va_start(ap, fmt); | ||
| 77 | n = xavsprintf_append(strp, fmt, ap); | ||
| 78 | va_end(ap); | ||
| 79 | |||
| 80 | return n; | ||
| 81 | } | ||
| 82 | |||
| 83 | int xasprintf(char **strp, const char *fmt, ...) | ||
| 84 | { | ||
| 85 | int n; | ||
| 86 | va_list ap; | ||
| 87 | |||
| 88 | *strp = NULL; | ||
| 89 | |||
| 90 | va_start(ap, fmt); | ||
| 91 | n = xavsprintf_append(strp, fmt, ap); | ||
| 92 | va_end(ap); | ||
| 93 | |||
| 94 | return n; | ||
| 95 | } | ||
| 96 | |||
| 79 | char *join_path(const char *path, const char *name) | 97 | char *join_path(const char *path, const char *name) |
| 80 | { | 98 | { |
| 81 | int lenp = strlen(path); | 99 | int lenp = strlen(path); |
