X-Git-Url: https://git.openpandora.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Fvsprintf.c;h=797428afd1114f3dc1ad3c239917b9f04ceccd55;hb=a6a888b3c20cf559c8a2e6e4d86c570dda2ef0f5;hp=e4e9031dd9c38709b82fd7de2da931ada0cec248;hpb=160b13f17c4f92805c4ad03c3336dded15c693e9;p=pandora-kernel.git diff --git a/lib/vsprintf.c b/lib/vsprintf.c index e4e9031dd9c3..797428afd111 100644 --- a/lib/vsprintf.c +++ b/lib/vsprintf.c @@ -23,6 +23,7 @@ #include #include +#include /* for PAGE_SIZE */ #include /** @@ -186,49 +187,49 @@ static char * number(char * buf, char * end, unsigned long long num, int base, i size -= precision; if (!(type&(ZEROPAD+LEFT))) { while(size-->0) { - if (buf <= end) + if (buf < end) *buf = ' '; ++buf; } } if (sign) { - if (buf <= end) + if (buf < end) *buf = sign; ++buf; } if (type & SPECIAL) { if (base==8) { - if (buf <= end) + if (buf < end) *buf = '0'; ++buf; } else if (base==16) { - if (buf <= end) + if (buf < end) *buf = '0'; ++buf; - if (buf <= end) + if (buf < end) *buf = digits[33]; ++buf; } } if (!(type & LEFT)) { while (size-- > 0) { - if (buf <= end) + if (buf < end) *buf = c; ++buf; } } while (i < precision--) { - if (buf <= end) + if (buf < end) *buf = '0'; ++buf; } while (i-- > 0) { - if (buf <= end) + if (buf < end) *buf = tmp[i]; ++buf; } while (size-- > 0) { - if (buf <= end) + if (buf < end) *buf = ' '; ++buf; } @@ -271,7 +272,8 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) /* 'z' changed to 'Z' --davidm 1/25/99 */ /* 't' added for ptrdiff_t */ - /* Reject out-of-range values early */ + /* Reject out-of-range values early. Large positive sizes are + used for unknown buffer sizes. */ if (unlikely((int) size < 0)) { /* There can be only one.. */ static int warn = 1; @@ -281,16 +283,17 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) } str = buf; - end = buf + size - 1; + end = buf + size; - if (end < buf - 1) { - end = ((void *) -1); - size = end - buf + 1; + /* Make sure end is always >= buf */ + if (end < buf) { + end = ((void *)-1); + size = end - buf; } for (; *fmt ; ++fmt) { if (*fmt != '%') { - if (str <= end) + if (str < end) *str = *fmt; ++str; continue; @@ -356,17 +359,17 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) case 'c': if (!(flags & LEFT)) { while (--field_width > 0) { - if (str <= end) + if (str < end) *str = ' '; ++str; } } c = (unsigned char) va_arg(args, int); - if (str <= end) + if (str < end) *str = c; ++str; while (--field_width > 0) { - if (str <= end) + if (str < end) *str = ' '; ++str; } @@ -381,18 +384,18 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) if (!(flags & LEFT)) { while (len < field_width--) { - if (str <= end) + if (str < end) *str = ' '; ++str; } } for (i = 0; i < len; ++i) { - if (str <= end) + if (str < end) *str = *s; ++str; ++s; } while (len < field_width--) { - if (str <= end) + if (str < end) *str = ' '; ++str; } @@ -425,7 +428,7 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) continue; case '%': - if (str <= end) + if (str < end) *str = '%'; ++str; continue; @@ -448,11 +451,11 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) break; default: - if (str <= end) + if (str < end) *str = '%'; ++str; if (*fmt) { - if (str <= end) + if (str < end) *str = *fmt; ++str; } else { @@ -482,14 +485,13 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) str = number(str, end, num, base, field_width, precision, flags); } - if (str <= end) - *str = '\0'; - else if (size > 0) - /* don't write out a null byte if the buf size is zero */ - *end = '\0'; - /* the trailing null byte doesn't count towards the total - * ++str; - */ + if (size > 0) { + if (str < end) + *str = '\0'; + else + *end = '\0'; + } + /* the trailing null byte doesn't count towards the total */ return str-buf; } @@ -847,3 +849,26 @@ int sscanf(const char * buf, const char * fmt, ...) } EXPORT_SYMBOL(sscanf); + + +/* Simplified asprintf. */ +char *kasprintf(gfp_t gfp, const char *fmt, ...) +{ + va_list ap; + unsigned int len; + char *p; + + va_start(ap, fmt); + len = vsnprintf(NULL, 0, fmt, ap); + va_end(ap); + + p = kmalloc(len+1, gfp); + if (!p) + return NULL; + va_start(ap, fmt); + vsnprintf(p, len+1, fmt, ap); + va_end(ap); + return p; +} + +EXPORT_SYMBOL(kasprintf);