"Das U-Boot" Source Tree
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

cmd: printf: add helper functions from busybox

Import the following helper functions from Busybox-1.33.1 which are
required by printf.c:

process_escape_sequence from libbb/process_escape_sequence.c,
skip_whitespace from libbb/skip_whitespace.c,
overlapping_strcpy from libbb/safe_strncpy.c

src-url: https://git.busybox.net/busybox/
commit bcc5b0e6caca6c7602a6a41f "Bump version to 1.33.1"
version: 1.33.1

Signed-off-by: Roland Gaudig <roland.gaudig@weidmueller.com>

authored by

Roland Gaudig and committed by
Tom Rini
6244cda4 9571f1ac

+121
+121
cmd/printf.c
··· 79 79 80 80 typedef void FAST_FUNC (*converter)(const char *arg, void *result); 81 81 82 + #define WANT_HEX_ESCAPES 0 83 + 84 + /* Usual "this only works for ascii compatible encodings" disclaimer. */ 85 + #undef _tolower 86 + #define _tolower(X) ((X)|((char) 0x20)) 87 + 88 + char FAST_FUNC bb_process_escape_sequence(const char **ptr) 89 + { 90 + const char *q; 91 + unsigned num_digits; 92 + unsigned n; 93 + unsigned base; 94 + 95 + num_digits = n = 0; 96 + base = 8; 97 + q = *ptr; 98 + 99 + if (WANT_HEX_ESCAPES && *q == 'x') { 100 + ++q; 101 + base = 16; 102 + ++num_digits; 103 + } 104 + 105 + /* bash requires leading 0 in octal escapes: 106 + * \02 works, \2 does not (prints \ and 2). 107 + * We treat \2 as a valid octal escape sequence. */ 108 + do { 109 + unsigned r; 110 + unsigned d = (unsigned char)(*q) - '0'; 111 + #if WANT_HEX_ESCAPES 112 + if (d >= 10) { 113 + d = (unsigned char)_tolower(*q) - 'a'; 114 + //d += 10; 115 + /* The above would map 'A'-'F' and 'a'-'f' to 10-15, 116 + * however, some chars like '@' would map to 9 < base. 117 + * Do not allow that, map invalid chars to N > base: 118 + */ 119 + if ((int)d >= 0) 120 + d += 10; 121 + } 122 + #endif 123 + if (d >= base) { 124 + if (WANT_HEX_ESCAPES && base == 16) { 125 + --num_digits; 126 + if (num_digits == 0) { 127 + /* \x<bad_char>: return '\', 128 + * leave ptr pointing to x */ 129 + return '\\'; 130 + } 131 + } 132 + break; 133 + } 134 + 135 + r = n * base + d; 136 + if (r > UCHAR_MAX) { 137 + break; 138 + } 139 + 140 + n = r; 141 + ++q; 142 + } while (++num_digits < 3); 143 + 144 + if (num_digits == 0) { 145 + /* Not octal or hex escape sequence. 146 + * Is it one-letter one? */ 147 + 148 + /* bash builtin "echo -e '\ec'" interprets \e as ESC, 149 + * but coreutils "/bin/echo -e '\ec'" does not. 150 + * Manpages tend to support coreutils way. 151 + * Update: coreutils added support for \e on 28 Oct 2009. */ 152 + static const char charmap[] ALIGN1 = { 153 + 'a', 'b', 'e', 'f', 'n', 'r', 't', 'v', '\\', '\0', 154 + '\a', '\b', 27, '\f', '\n', '\r', '\t', '\v', '\\', '\\', 155 + }; 156 + const char *p = charmap; 157 + do { 158 + if (*p == *q) { 159 + q++; 160 + break; 161 + } 162 + } while (*++p != '\0'); 163 + /* p points to found escape char or NUL, 164 + * advance it and find what it translates to. 165 + * Note that \NUL and unrecognized sequence \z return '\' 166 + * and leave ptr pointing to NUL or z. */ 167 + n = p[sizeof(charmap) / 2]; 168 + } 169 + 170 + *ptr = q; 171 + 172 + return (char) n; 173 + } 174 + 175 + char* FAST_FUNC skip_whitespace(const char *s) 176 + { 177 + /* In POSIX/C locale (the only locale we care about: do we REALLY want 178 + * to allow Unicode whitespace in, say, .conf files? nuts!) 179 + * isspace is only these chars: "\t\n\v\f\r" and space. 180 + * "\t\n\v\f\r" happen to have ASCII codes 9,10,11,12,13. 181 + * Use that. 182 + */ 183 + while (*s == ' ' || (unsigned char)(*s - 9) <= (13 - 9)) 184 + s++; 185 + 186 + return (char *) s; 187 + } 188 + 189 + /* Like strcpy but can copy overlapping strings. */ 190 + void FAST_FUNC overlapping_strcpy(char *dst, const char *src) 191 + { 192 + /* Cheap optimization for dst == src case - 193 + * better to have it here than in many callers. 194 + */ 195 + if (dst != src) { 196 + while ((*dst = *src) != '\0') { 197 + dst++; 198 + src++; 199 + } 200 + } 201 + } 202 + 82 203 static int multiconvert(const char *arg, void *result, converter convert) 83 204 { 84 205 if (*arg == '"' || *arg == '\'') {